Foreign Key Constraint
Foreign Key Constraint
571
rough
elated thro common column
tatbles
a r e
Items
and it
it nd should be declared Itemno. The column Itemno is
t h e able
t ht i a lntegritp i.e.,
e,n
both the i foreign key in child table Orders
tables should be created ary
ity to enforce
CREATE TABLE I t e
as
follows
no char(5) NOT NULL PRIMARY KEY,
CREATE TABLE O r d e r s
child table.
);
O r d e r n o
Number (6, e) OT NULL PRIMARY KEY,
Ttemno char(5) REFERENCES Items (Itemno), primary key of parent table.
thaRENCES clause.
REFERENCI
To enforce a foreign
key
constraint at column
declared foreign key
through
level, the syntax to
he
tollowedi s
SINTAX
olumnna datatype (size) REFERENCES tablename
eolumnname) ] [ON DELETE CASCADE] [ON UPDATE CASCADE 1?
[(co.
he first columnname is the name of related column in child table and the columnname
Herethe REFERENCES clause is the name of related column in the parent table. The
appearing after RE)
Ppafter
ilename after EFERENCES
REF clause is the name of parent table or primary table to which the
c u r r e n t table is related.
olumn
in
related columnname in parent table, then, by default MySQL will reference
you skip the of parent table.
the primary key
CASCADE also, while defining foreign key constraint then in
Tfyou write ON DELETE
a row o r a tuple
is deleted in parent table, all its related tuples or rows in the child table
deleted.
will automatically be
constraint then in
ON UPDATE CASCADE also, while defining foreign key
Ifyou write
related tuples or rows in the child table
case a row o r a is updated in parent table, all its
tuple
will automatically be updated.
as
FOREIGN KEY table constraint,
constraint c a n also be applied through
Ihe foreign key
shown below: NOT NULL PRIMARY KEY,
OrderID SMALLINT
CREATE TABLE Orders ( NOT NULL,
ModelID SMALLINT
ModelDescrip VARCHAR (40),
SMALLINT NOT NULL,
SupplierID REFERENCES
Models (ModelID)
FOREIGN KEY
(ModelID)
CASCADE,
CASCADE ON UPDATE (SupplierID)
DELETE Suppliers
HodeliID and SupplieriD are ON
KEY (SupplierID)
REFERENCES
Fiqune
13.FOREIGN KEY/REFERENCE Constraint.
Important Foreign Key and Storgge Engne
MySQL is capable of creating databases in many
default storage engine different storage
is ISAM but
ironically it engines that offer different features. The
does not support
foreign key constraint are being foreign keys. So, if
database tables to InnoDB ignored or not implemented, you need
you find that your
storage engine.
to
change the storage
engine of your
To do so you simply need to write following
command for your connecting tables
ALTER TABLE
«tablename>
ALTER TABLE tablel ENGINEENGINE innodb;
innodb =
Once done, you can create and implememt foreign keys for your
tables.
13.3.1B Applying Table Constraints
When a constraint is to be
applied on a group of columns of
constraint. The table constraints ble
in the end of the table, it is called
combination of icode and descp ofappear
table items to
table definition. For
instance, it you
be
unique, you may write it as folloWS
AMANDS
REATE
ABLE
It ems
1Cod
har (5)
NOT NULL, 573
har (20)
des NOT NULL,
ROL integer
QOH integer,
CHECK (ROL <QOH),
UNIQUE (icode, descp) ) these are a
table
constraints
h e
aboveS t a t e m e
mmtersa s
CREATE ABLE members tname, it can beprimary
lastnan key for the table
done
firstname char
as follows:
(15) NOT NULL,
lastname char (15) NOT
city char (20) NULL
PRIMARY KEY (tirstname, lastname)) this is a table constraint
.areign key constraint for a
cod. For example, it a groupgroup of
o define
Itemno char(5),
Names to Constraints
131C Assigning
Rv default, MysQL assigns a unique name to each constraint defined by you. MySQL names
constraints as
SYS_Cn
where n is an integer that unique. For instance, SYS_CO03217,
makes the constraint name
SYNTAX
TABLE statements:
two CREATE
the following
forexample, consider
CREATE TABLE Items
CONSTRAINT p_Itemkey PRIMARY KEY,
Itemno char(5)
;
CREATE TABLE Orders
Orderno Itemno)
(Orderno,
FOREIGN KEY
fkey Orders
ONSTRAINT Group Itemt)
(Order#,
REFERENCESOrdMaster
ON -
574
st"e
icture
Type Number(4)
:
in created table
Ensure the follouing specification User_lD should
be unique
ID should be declared as Primary Key
First_Name & Last_Name must not remain
Salary must be greater than 5000. blant
Solution. CREATE TABLE Employee
PRIMARY KEY,
ID Number (4) NOT NULL
NULL,
First_Name Varchar(30) NOT
Last_Name Varchar (30) NOT NULL,
User_ID Varchar (10) UNIQUE,
CHECK (Salary> 5000) )
Salary Number (9,2)
affected
MySQL responds as: Query OK, 0 rows