0 votes
1 view
in SQL by (37.2k points)

Can anyone tell me how to create foreign key in SQL?

1 Answer

0 votes
by (90.2k points)

Suppose if you want to create a foreign key Customer_ID that is referencing to the ID of the Customers table here is the syntax for that:

Create TABLE orders_table (

ID                              INT                  NOT NULL,

DATE                        DATETIME,

Customer_ID          INT references CUSTOMERS (ID),

PRIMARY KEY (ID)

);

If you want to add a foreign key to an already existing table here is the syntax for that:

ALTER TABLE ORDERS_TABLE

ADD FOREIGN KEY (Customer_ID) REFERENCES CUSTOMERS (ID);

You can learn SQL by enrolling in this SQL course by Intellipaat.

Related questions

Welcome to Intellipaat Community. Get your technical queries answered by top developers !


Categories

...