CONSTRAINT [constraint-name] PRIMARY KEY(attribute-list)
|
CREATE TABLE test1
(
ssn char(9),
salary decimal(10,2),
CONSTRAINT Test1PrimKey PRIMARY KEY(ssn)
);
|
insert into test1 values ('111223333', 4000.00);
insert into test1 values ('111223333', 5000.00); // fails !!
|
The second insert command will fail (because 111223333 is already in the database !!!)
CREATE TABLE test2
(
essn CHAR(9),
pno INTEGER,
hours dec(5,2),
CONSTRAINT Test2PrimKey PRIMARY KEY (essn, pno)
);
|
insert into test2 values ('111223333', 44, 4.5);
insert into test2 values ('111223333', 23, 3.5);
insert into test2 values ('111223333', 44, 6.5);
|
|
|
CONSTRAINT [constraint-name] UNIQUE(attribute-list)
|
CREATE TABLE test3
(
dnumber INTEGER,
dname CHAR(20),
CONSTRAINT Test3PrimKey PRIMARY KEY(dnumber), /* Primary key */
CONSTRAINT Test3SecondKey UNIQUE(dname) /* Another key */
);
|
specifies that no two tuples in the "test3" relation may have the same value for dname
Example:
|
|
CONSTRAINT [constraint-name]
FOREIGN KEY (attribute-list)
REFRENCES relation(attribute-list)
|
CREATE TABLE test4
(
ssn CHAR(9), /* <------- primary key */
salary dec(9,2),
CONSTRAINT Test4PrimKey PRIMARY KEY(ssn)
);
CREATE TABLE test5
(
essn CHAR(9),
pno INTEGER,
CONSTRAINT Test5ForeignKey
FOREIGN KEY (essn) /* (essn) is used as foreign key */
REFERENCES test4(ssn) /* It references "ssn" in relation "test4" */
);
|
because there is no tuple in test4 with SSN='111223333'
|