SQL Server Forums
Profile | Register | Active Topics | Members | Search | Forum FAQ
 
Register Now and get your question answered!
Username:
Password:
Save Password
Forgot your Password?

 All Forums
 SQL Server 2000 Forums
 SQL Server Development (2000)
 Enforcing One-To-(finite #) relationship
 New Topic  Reply to Topic
 Printer Friendly
Author Previous Topic Topic Next Topic  

CactusJuice
Starting Member

USA
46 Posts

Posted - 11/11/2002 :  19:19:38  Show Profile  Visit CactusJuice's Homepage  Reply with Quote
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

USA
1157 Posts

Posted - 11/11/2002 :  19:25:29  Show Profile  Reply with Quote
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

USA
46 Posts

Posted - 11/12/2002 :  11:55:02  Show Profile  Visit CactusJuice's Homepage  Reply with Quote
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

United Kingdom
12543 Posts

Posted - 11/12/2002 :  12:04:16  Show Profile  Visit nr's Homepage  Reply with Quote
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 - 02/03/2006 :  06:18:54  Show Profile  Reply with Quote
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

United Kingdom
12543 Posts

Posted - 02/03/2006 :  07:44:44  Show Profile  Visit nr's Homepage  Reply with Quote
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)

USA
7020 Posts

Posted - 02/03/2006 :  12:00:08  Show Profile  Reply with Quote
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

Edited by - Michael Valentine Jones on 02/03/2006 12:02:47
Go to Top of Page

jsmith8858
Dr. Cross Join

USA
7423 Posts

Posted - 02/03/2006 :  12:08:51  Show Profile  Visit jsmith8858's Homepage  Reply with Quote
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 - 02/03/2006 :  13:31:26  Show Profile  Reply with Quote
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

USA
7423 Posts

Posted - 02/03/2006 :  14:52:38  Show Profile  Visit jsmith8858's Homepage  Reply with Quote
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

Australia
1591 Posts

Posted - 02/03/2006 :  16:41:04  Show Profile  Reply with Quote
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
  Previous Topic Topic Next Topic  
 New Topic  Reply to Topic
 Printer Friendly
Jump To:
SQL Server Forums © 2000-2009 SQLTeam Publishing, LLC Go To Top Of Page
This page was generated in 0.12 seconds. Powered By: Snitz Forums 2000