Showing posts with label constraint. Show all posts
Showing posts with label constraint. Show all posts

Wednesday, March 21, 2012

Referential Integrity with empty string and ZERO

Hi,
How could I define referential integrity using FK constraint which allow me to have empty string/ZERO number instead of NULL value ?

Thank you

If you place a foreign key constraint on a column, zero is only a valid value if there is a zero in foreign key table. The same applies to empty strings.

If the foreign key value of a particular row is unknown (or it doesnt have one), that's where the use of null comes in. Of course a NOT NULL constraint used in conjunction with the FK constraint can prevent the use of nulls (forcing a valid FK value).

To the best of my knowledge, it's not possible to change this behavior.

Referential Integrity with empty string and ZERO

Hi,
How could I define referential integrity using FK constraint which allow me to have empty string/ZERO number instead of NULL value ?

Thank you

If you place a foreign key constraint on a column, zero is only a valid value if there is a zero in foreign key table. The same applies to empty strings.

If the foreign key value of a particular row is unknown (or it doesnt have one), that's where the use of null comes in. Of course a NOT NULL constraint used in conjunction with the FK constraint can prevent the use of nulls (forcing a valid FK value).

To the best of my knowledge, it's not possible to change this behavior.

Referential constraint between two tables in two databases

Is it possible to define a referential constraint between two tables in two different databases (on two servers)? Or are there beter best practices methods/products to achieve this result.

i think not possible

Thank u

Baba

Please remember to click "Mark as Answer" on this post if it helped you.

|||

You should be able to set up the reference as a linked server (http://www.databasejournal.com/features/mssql/article.php/3085211,http://msdn2.microsoft.com/en-us/library/ms188279.aspx -- this is a sql server 2005 article, but earlier versions support linked servers as well)

I doubt that you can enforce the constraint declaritively, but I am quite sure you can do it using a trigger. Let us know if you need help with the triggers.

|||

And what can be used when databases are sitted on the same server?

|||

Then it's even easier because you don't need the linked server, you can simply reference the table on the other database by indluding the DB name, eg,

database2.dbo.sometable

Or (and this is the way I do it), create a view in database1 pointing to the table in database2

use database1
go
create view TableXyz as
select * from databse2.dbo.TableXyz
go
grant insert, update, select, delete on TableXyz to ...
go

Then, from within database1, all references to TableXyz really point to database2.dbo.TableXyz

I think the declaritive RI may work in this scenario but I'm not positive, but in any case a trigger will work

sql

Monday, March 12, 2012

reference to either of two tables

I must link from one table to either of two tables X & Y. I can store
this link in columns:
REF_X_ID NULLABLE
REF_Y_ID NULLABLE
plus a constraint to ensure only one REF_*_ID is set for a row
OR
in columns:
REF_ID NOT NULL
WHICH_TABLE
i.e. store ID for both tables in only one column, and add an other
column that identifies which table REF_ID refers.
Which would be smarter idea considering table design, adding rows and
quering the table?On 23 Apr 2005 14:31:30 -0700, bbla32@.op.pl wrote:

>I must link from one table to either of two tables X & Y. I can store
>this link in columns:
>REF_X_ID NULLABLE
>REF_Y_ID NULLABLE
>plus a constraint to ensure only one REF_*_ID is set for a row
>OR
>in columns:
>REF_ID NOT NULL
>WHICH_TABLE
>i.e. store ID for both tables in only one column, and add an other
>column that identifies which table REF_ID refers.
>Which would be smarter idea considering table design, adding rows and
>quering the table?
Hi bbla32,
Since you can't use a FOREIGN KEY constraint on the latter and you can
use it on the former, I'd definitely favor the first version.
Best, Hugo
--
(Remove _NO_ and _SPAM_ to get my e-mail address)