473,668 Members | 2,482 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 8236
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.TYP E_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.c om> wrote in message
news:11******** **************@ f14g2000cwb.goo glegroups.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.TYP E_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
2817
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 taskCompleteDate is NULL (like default) then, taskComplete may not be set to 1 (attempt to update it to 1 would fail);
2
7180
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 ( is not null and = 1))); Q1 with check constraint: sample dagta
3
9061
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 "Ticker." When new company stock tickers (i.e., MSFT for Microsoft) are entered into the field, I'd like them in all caps--whether the user types msft, Msft, MsFt, etc. In Access, this was easy--simply set the Format to ">" in table design view. In...
0
1835
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): CREATE TABLE public.tb_contacts ( contact_id serial NOT NULL, actor_id varchar(50) NOT NULL, contacttype_id varchar(6) NOT NULL,
9
3421
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 normal index search? For example, I want to create some tables to manage different data in a kind of <object, relationship, object2> manner, but where object2 could be an IP address, text, a number, etc. So I thought of doing the following:
3
6317
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, which is called once the user clicks the OK button on this dialog. try { int rows = cmd.ExecuteNonQuery(); } catch(SqlException se)
1
5946
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 SET INTEGRITY statement C. With the DELETE phase of the LOAD D. With the UPDATE CONSTRAINTS statement Answer is A
1
1433
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 character is uppercase , and any trailing characters in the word are lowercase, unless there is a space. If there is a space, the next character after the space should be uppercase , and any trailing characters in the word will be lowercase. ...
1
6530
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 trying to add a check constraint to an empty table. The problem is that just about no form of expression syntax seems to get accepted by MSSQLSMS. On my a table "Projects" I've created a ntext column called "status". The following check constraint...
0
8459
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, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
8889
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, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
8790
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 tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
8572
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 Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
8652
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 protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
1
6206
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 presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
4372
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
2
2017
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
2
1779
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 can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.