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
 General SQL Server Forums
 New to SQL Server Programming
 Declaring foreign key/relationship constraints

Author  Topic 

stamford
Starting Member

47 Posts

Posted - 2011-06-08 : 20:04:22


Greetings, would appreciate some help with this one please.
If I create two tables as below and there is a 1:n relationship between the tables so that the primary key from TABLE one (random_int) is posted in TABLE two as a foreign key to represent the relationship. There is a conditional constraint in as much that the sum of an instance of TABLE one and an instance of TABLE two i.e. random_int and another_random_int as a row in the relationship table cannot total more than 150. How would I declare a check constraint such as this please?

Also the second part of my question is that in TABLE two I have declared two separate named constraints for the column another_small_int i.e. another_small_int_value_1 & another_small_int_value_2. It looks sorta OK like this but is it good practice to keep the constraints separate like this or is it best to combine them as one constraint, and if so, how?

CREATE TABLE one (
random_int SMALLINT NOT NULL,
random_varchar VARCHAR (5),
PRIMARY KEY (random_int),
CONSTRAINT random_int_range
CHECK (random_int BETWEEN 1 AND 99),
CONSTRAINT random_varchar_range
CHECK (random_varchar IN (‘five’, ‘six’, ‘seven’))

CREATE TABLE two (
random_date DATE NOT NULL,
another_small_int SMALLINT,
PRIMARY KEY (random_date),
FOREIGN KEY (random_int) REFERENCES one,
CONSTRAINT random_date_year
CHECK (random_date YEAR (date) = 2010),
CONSTRAINT another_small_int_value_1
CHECK (another_small_int < 100)),
CONSTRAINT another_small_int_value_2
CHECK (another_small_int DECIMAL (5, 2))


Many thanks!

jcelko
Esteemed SQL Purist

547 Posts

Posted - 2011-06-08 : 23:08:59
Single constraints so you will get single errors messages that pin-point the problem


--CELKO--
Books in Celko Series for Morgan-Kaufmann Publishing
Analytics and OLAP in SQL
Data and Databases: Concepts in Practice
Data, Measurements and Standards in SQL
SQL for Smarties
SQL Programming Style
SQL Puzzles and Answers
Thinking in Sets
Trees and Hierarchies in SQL
Go to Top of Page

stamford
Starting Member

47 Posts

Posted - 2011-06-09 : 15:07:31


OK but can I write a separate constraint in TABLE two for the foreign key random_int even though the key will appear in a row in a relationship entity/table?
Go to Top of Page

stamford
Starting Member

47 Posts

Posted - 2011-06-09 : 18:45:44


So to shorten and re-phrase my question, say for example two tables, X and Y, both contribute primary keys, a and b, to make up a relationship entity/table Z.

How and where would you declare a check constraint to ensure that only specific values of X.a could exist in a row with specific values of Y.b in relationship Z?

Say X.a had a value of 'black' and Y.b had a value of 'white'. How and where would you declare a check constraint to keep them apart, or to keep them together? Would you declare a check constraint in either X or Y, or would you declare a new domain which included a check constraint?
Go to Top of Page
   

- Advertisement -