How to create a table foreign key constraint in Oracle?

To create a table foreign key constraint in Oracle database, you can use the following syntax:

ALTER TABLE child_table
ADD CONSTRAINT fk_constraint_name
FOREIGN KEY (child_column)
REFERENCES parent_table(parent_column);

The child_table is the subtable where the foreign key constraint needs to be added, child_column is the column in the subtable where the foreign key constraint needs to be added, fk_constraint_name is the name of the foreign key constraint, parent_table is the parent table, and parent_column is the column in the parent table.

For example, to add a foreign key constraint in the table named orders, linking the column customer_id to the column id in the customers table, you can use the following SQL statement:

ALTER TABLE orders
ADD CONSTRAINT fk_orders_customers
FOREIGN KEY (customer_id)
REFERENCES customers(id);

This will create a foreign key constraint named fk_orders_customers that links the customer_id column in the orders table with the id column in the customers table.

Leave a Reply 0

Your email address will not be published. Required fields are marked *


广告
Closing in 10 seconds
bannerAds