Oracle 5.0 Reference Manual page 1063

Table of Contents

Advertisement

Examples of Foreign Key Clauses
Here is a simple example that relates
CREATE TABLE parent (
id INT NOT NULL,
PRIMARY KEY (id)
) ENGINE=INNODB;
CREATE TABLE child (
id INT,
parent_id INT,
INDEX par_ind (parent_id),
FOREIGN KEY (parent_id)
REFERENCES parent(id)
ON DELETE CASCADE
) ENGINE=INNODB;
A more complex example in which a
foreign key references a two-column index in the
index in the
CREATE TABLE product (
category INT NOT NULL, id INT NOT NULL,
price DECIMAL,
PRIMARY KEY(category, id)
)
ENGINE=INNODB;
CREATE TABLE customer (
id INT NOT NULL,
PRIMARY KEY (id)
)
ENGINE=INNODB;
CREATE TABLE product_order (
no INT NOT NULL AUTO_INCREMENT,
product_category INT NOT NULL,
product_id INT NOT NULL,
customer_id INT NOT NULL,
PRIMARY KEY(no),
INDEX (product_category, product_id),
INDEX (customer_id),
FOREIGN KEY (product_category, product_id)
REFERENCES product(category, id)
ON UPDATE CASCADE ON DELETE RESTRICT,
FOREIGN KEY (customer_id)
REFERENCES customer(id)
)
ENGINE=INNODB;
Adding foreign keys
You can add a new foreign key constraint to an existing table by using
relating to foreign keys for this statement is shown here:
ALTER TABLE
ADD [CONSTRAINT [symbol]] FOREIGN KEY
[index_name] (index_col_name, ...)
REFERENCES
[ON DELETE reference_option]
[ON UPDATE reference_option]
The foreign key can be self referential (referring to the same table). When you add a foreign key
constraint to a table using
Dropping Foreign Keys
You can also use
CREATE TABLE
table:
customer
tbl_name
tbl_name
(index_col_name,...)
TABLE, remember to create the required indexes first.
ALTER
to drop foreign keys, using the syntax shown here:
ALTER TABLE
Syntax
and
tables through a single-column foreign key:
parent
child
table has foreign keys for two other tables. One
product_order
table. The other references a single-column
product
1043
TABLE. The syntax
ALTER

Advertisement

Table of Contents
loading

This manual is also suitable for:

Mysql 5.0

Table of Contents