473,508 Members | 2,195 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

check constraint questions

Dear all,

Is it possible to define a check constraint on a field that the field value
must exist in another table non-unique field? how to write the create table
DDL?

i.e
TABLE A
TYPE, REGION
Apple, A
Apple, B

TABLE B
TYPE,COST
Apple, $1,000
Orange, $2,000

Could a constraint be created in TABLE B on the TYPE field that the TYPE
must exist in TABLE A??

Thanks in advance!

Henry
Nov 12 '05 #1
2 8222
you can do that with primary key/foreign key concept, defining a child
and parent relationship. table A will be the parent and table B will be
the child.

if the type in table A does not exist a corresponding type in table B
cannot be created.
------------------------------------------------
-- DDL Statements for table "DB2INST1"."A"
------------------------------------------------

CREATE TABLE "DB2INST1"."A" (
"TYPE_ID" INTEGER NOT NULL ,
"TYPE_NAME" VARCHAR(20) )
IN "USERSPACE1" ;

-- DDL Statements for primary key on Table "DB2INST1"."A"

ALTER TABLE "DB2INST1"."A"
ADD PRIMARY KEY
("TYPE_ID");
------------------------------------------------
-- DDL Statements for table "DB2INST1"."B"
------------------------------------------------

CREATE TABLE "DB2INST1"."B" (
"TYPE_ID" INTEGER NOT NULL ,
"COST" INTEGER )
IN "USERSPACE1" ;

-- DDL Statements for foreign keys on Table "DB2INST1"."B"

ALTER TABLE "DB2INST1"."B"
ADD CONSTRAINT "TYPE_FK" FOREIGN KEY
("TYPE_ID")
REFERENCES "DB2INST1"."A"
("TYPE_ID")
ON DELETE NO ACTION
ON UPDATE NO ACTION
ENFORCED
ENABLE QUERY OPTIMIZATION;
#######################

$ db2 "insert into a(type_id, type_name) values (10001, 'Apples')"
DB20000I The SQL command completed successfully.
$ db2 "insert into a(type_id, type_name) values (10002, 'Oranges')"
DB20000I The SQL command completed successfully.
$ db2 "insert into b(type_id, cost) values (10001, 300)"
DB20000I The SQL command completed successfully.
$ db2 "insert into b(type_id, cost) values (10002, 200)"
DB20000I The SQL command completed successfully.
$ db2 "insert into b(type_id, cost) values (10004, 700)"
DB21034E The command was processed as an SQL statement because it was
not a valid Command Line Processor command. During SQL processing it
returned: SQL0530N The insert or update value of the FOREIGN KEY
"DB2INST1.B.TYPE_FK" is not equal to any value of the parent key of the
parent table. SQLSTATE=23503

the last SQL statement and it's result, is what you wanted.... right?
regards,
dotyet

Nov 12 '05 #2
Dotyet,

Thanks very much!, you have spotted the key problem for me.

Regards,

Henry
"dotyet" <do****@yahoo.com> wrote in message
news:11**********************@f14g2000cwb.googlegr oups.com...
you can do that with primary key/foreign key concept, defining a child
and parent relationship. table A will be the parent and table B will be
the child.

if the type in table A does not exist a corresponding type in table B
cannot be created.
------------------------------------------------
-- DDL Statements for table "DB2INST1"."A"
------------------------------------------------

CREATE TABLE "DB2INST1"."A" (
"TYPE_ID" INTEGER NOT NULL ,
"TYPE_NAME" VARCHAR(20) )
IN "USERSPACE1" ;

-- DDL Statements for primary key on Table "DB2INST1"."A"

ALTER TABLE "DB2INST1"."A"
ADD PRIMARY KEY
("TYPE_ID");
------------------------------------------------
-- DDL Statements for table "DB2INST1"."B"
------------------------------------------------

CREATE TABLE "DB2INST1"."B" (
"TYPE_ID" INTEGER NOT NULL ,
"COST" INTEGER )
IN "USERSPACE1" ;

-- DDL Statements for foreign keys on Table "DB2INST1"."B"

ALTER TABLE "DB2INST1"."B"
ADD CONSTRAINT "TYPE_FK" FOREIGN KEY
("TYPE_ID")
REFERENCES "DB2INST1"."A"
("TYPE_ID")
ON DELETE NO ACTION
ON UPDATE NO ACTION
ENFORCED
ENABLE QUERY OPTIMIZATION;
#######################

$ db2 "insert into a(type_id, type_name) values (10001, 'Apples')"
DB20000I The SQL command completed successfully.
$ db2 "insert into a(type_id, type_name) values (10002, 'Oranges')"
DB20000I The SQL command completed successfully.
$ db2 "insert into b(type_id, cost) values (10001, 300)"
DB20000I The SQL command completed successfully.
$ db2 "insert into b(type_id, cost) values (10002, 200)"
DB20000I The SQL command completed successfully.
$ db2 "insert into b(type_id, cost) values (10004, 700)"
DB21034E The command was processed as an SQL statement because it was
not a valid Command Line Processor command. During SQL processing it
returned: SQL0530N The insert or update value of the FOREIGN KEY
"DB2INST1.B.TYPE_FK" is not equal to any value of the parent key of the
parent table. SQLSTATE=23503

the last SQL statement and it's result, is what you wanted.... right?
regards,
dotyet

Nov 12 '05 #3

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

5
2806
by: Doug Baroter | last post by:
Hi, DDL: -- create table #task (taskID int identity(1,1) primary key, taskName varchar(25) unique, taskCompleteDate dateTime, taskComplete bit default(0)); /* Business Rules: a) if...
2
7155
by: Doug Baroter | last post by:
Hi, DDLs and DMLs: create table #job (jobID int identity(1,1) primary key, jobName varchar(25) unique not null, jobEndDate dateTime, jobComplete bit default(0), check (( is null and = 0) OR (...
3
9045
by: RAD | last post by:
I am working with an evaluation copy of SQL Server 2000 for the first time; my DB experience lies with MS Access. I have a simple table in SQL Server (tblCompany) that has a field called...
0
1819
by: Fabre Lambeau | last post by:
I've got a problem when adding a CONSTRAINT CHECK on a table by calling a function. It just seems not to work... Here is the table (simplified to only the relevant fields for this case): ...
9
3391
by: Edmund Dengler | last post by:
Greetings! Just trying some tests out, and wanted to know about some optimizations. If I do a CHECK constraint on a table, is this used to optimize a SELECT or does Postgresql rely mostly on...
3
6297
by: ferg | last post by:
I have a Customer table. The table has two different CHECK constraints. Then there is the Customer details dialog, which provides the user with an UI for changing users. I have some UPDATE sql,...
1
5892
by: huyuhui | last post by:
The following is a question of LOAD utility. Question: How does the DB2 enforce table check constraints for data added to table with the LOAD utility? A. With the BUILD phase of LOAD B. With the...
1
1425
by: jecaruthersjr | last post by:
I am attempting to write a Check Constraint on an MSSQL2k server that will only allow a string to be INSERTED or UPDATED if it is formatted correctly. The correct format being, that the first...
1
6523
by: Spectre1337 | last post by:
Hello, it seems like the check constraint validation of MS SQL Server Management Studio express is horribly, horribly broken. Either that or I'm using it wrong. I hope it's the latter. I'm...
0
7227
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
7331
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
7391
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
1
7054
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
7501
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
1
5056
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...
0
4713
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...
0
3188
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
0
424
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.