Please start any new threads on our new site at https://forums.sqlteam.com. We've got lots of great SQL Server experts to answer whatever question you can come up with.

 All Forums
 SQL Server 2000 Forums
 SQL Server Development (2000)
 Enforcing One-To-(finite #) relationship

Author  Topic 

CactusJuice
Starting Member

46 Posts

Posted - 2002-11-11 : 19:19:38
I cannot find any examples on how to create a one-to-finite relationship. Specifically I need to limit records in a foreign table to a manx of 5. I know how to do a one-to-one by using a UNIQUE constriant. But how do I create a one-to-five relationship? There can be from zero to five records but never more than five.

I'm guessing I need to use a CONSTRAINT but how? Any help or links to this type of topic would be appreciated. Thanks.

JustinBigelow
SQL Gigolo

1157 Posts

Posted - 2002-11-11 : 19:25:29
I'd suggest making your insert into a stored procedure. At the beginning of the procedure start a transaction. Then do a count of records with the same foreign key. If the count is five then rollback the transaction, if it is less than five then perform the insert and commit the transaction.

hth,
Justin

Have you hugged your SQL Server today?
Go to Top of Page

CactusJuice
Starting Member

46 Posts

Posted - 2002-11-12 : 11:55:02
OK, but isn't there anyway to build this relational integrity into the architecture of of the database? I guess I've never come across this before and just surprised that a sproc is the only way. Thanks.

Go to Top of Page

nr
SQLTeam MVY

12543 Posts

Posted - 2002-11-12 : 12:04:16
You could use a trigger to enforce it.

This isn't really a relation between tables.
You are saying there can't be more than 5 duplicate values for this field in the table.

1-1
1-many
are types of relationships - there's nothing special about the number 5 that affects the architecture.

==========================================
Cursors are useful if you don't know sql.
DTS can be used in a similar way.
Beer is not cold and it isn't fizzy.
Go to Top of Page

tomsi
Starting Member

3 Posts

Posted - 2006-02-03 : 06:18:54
But how about a zero to one relationship? With that I mean that the foreign key column is unique value or null (and that two nulls are defined as not unique). Is trigger way to go with this as well?

Tomsi
Go to Top of Page

nr
SQLTeam MVY

12543 Posts

Posted - 2006-02-03 : 07:44:44
A unique index will allow one null in the table.
A foreign key will allow a null in the field.
Apart from that you would probably have to use a trigger or enforce it in the interface.

==========================================
Cursors are useful if you don't know sql.
DTS can be used in a similar way.
Beer is not cold and it isn't fizzy.
Go to Top of Page

Michael Valentine Jones
Yak DBA Kernel (pronounced Colonel)

7020 Posts

Posted - 2006-02-03 : 12:00:08
quote:
Originally posted by CactusJuice

OK, but isn't there anyway to build this relational integrity into the architecture of of the database? I guess I've never come across this before and just surprised that a sproc is the only way. Thanks.


You could add an additional tinyint column, let's call it FK_Count.
Add a constraint on FK_Count to allow only values one to five. Then add a unique constraint to the table on the combination of the FK column and FK_Count.

This will contrain your table to a max of 5 rows with any individual foreign key, but you will have to have the logic for dealing with FK_Count in code. Not difficult, but you will have to do it.

If the foreign key is nullable, you probably cannot use this approach.



CODO ERGO SUM
Go to Top of Page

jsmith8858
Dr. Cross Join

7423 Posts

Posted - 2006-02-03 : 12:08:51
quote:
Originally posted by tomsi

But how about a zero to one relationship? With that I mean that the foreign key column is unique value or null (and that two nulls are defined as not unique). Is trigger way to go with this as well?

Tomsi



Isn't this essentially a 1:1 relationship, both tables have the same PK?

i.e.,

Table1: (ID primary key)
Table2: (ID primary key, ALSO FK to table1 (ID))

Go to Top of Page

tomsi
Starting Member

3 Posts

Posted - 2006-02-03 : 13:31:26
quote:
Isn't this essentially a 1:1 relationship, both tables have the same PK?

i.e.,

Table1: (ID primary key)
Table2: (ID primary key, ALSO FK to table1 (ID))



No, I don't think so. You cannot set a PK column to NULL. Some of the point is that the row can exist, but without a need to point to another row.
Go to Top of Page

jsmith8858
Dr. Cross Join

7423 Posts

Posted - 2006-02-03 : 14:52:38
quote:
Originally posted by tomsi

quote:
Isn't this essentially a 1:1 relationship, both tables have the same PK?

i.e.,

Table1: (ID primary key)
Table2: (ID primary key, ALSO FK to table1 (ID))



No, I don't think so. You cannot set a PK column to NULL. Some of the point is that the row can exist, but without a need to point to another row.



I don't get what you are looking for ... can you offer an example? You don't need to put use NULLs anywhere in the model I gave you -- if there is no matching row for Table1 in Table2, then there is no need to store a NULL anywhere ... there just isn't a matching row in Table2.
Go to Top of Page

byrmol
Shed Building SQL Farmer

1591 Posts

Posted - 2006-02-03 : 16:41:04
http://weblogs.sqlteam.com/davidm/archive/2003/11/18/539.aspx

DavidM

Intelligent Design is NOT science.

A front-end is something that tries to violate a back-end.
Go to Top of Page
   

- Advertisement -