Back

Explore Courses Blog Tutorials Interview Questions
0 votes
2 views
in SQL by (55.6k points)

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

1 Answer

0 votes
by (119k 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

0 votes
1 answer
0 votes
1 answer
0 votes
1 answer
asked Oct 20, 2020 in SQL by dev_sk2311 (45k points)

Browse Categories

...