use the following picture to answer the question below Assuming Order table already exist, add a Foreign Key Constraint (CUSTID references Customer table).
QUESTION 7
use the following picture to answer the question below
-
Assuming Order table already exist, add a Foreign Key Constraint (CUSTID references Customer table).


To add a Foreign Key Constraint to an already existing table, the below query syntax is used.
ALTER TABLE TABLE_NAME ADD FOREIGN KEY (COLUMN_NAME) REFERENCES REFE_TABLE_NAME(REF_COLUMN_NAME);
- In the above syntax, keywords are represented in bold letters.
- "TABLE_NAME" is a table name for which the foreign key constraint is going to be added to one of its column.
- "COLUMN_NAME" is a column name to which the foreign key constraint is going to be added.
- "REFE_TABLE_NAME" is a table name that "TABLE_NAME" refers to.
- "REF_COLUMN_NAME" is the primary key field of "REFE_TABLE_NAME".
To add a Foreign Key Constraint to an already existing table "ORDERS", the below steps needs to be followed,
Consider the below queries are used to create the tables "ORDERS", and "CUSTOMER".
create table CUSTOMER(CUSTID int,LNAME char(20),FNAME char(30),CITY char(20), STATE char(20), PHONE int, PRIMARY KEY(CUSTID));
create table ORDERS(ORDERID int, INVID int, ORDERDATE date, CUSTID int,ORDER_PRICE double,QUANTITY int, PRIMARY KEY(INVID));
Note:
For a table name "ORDER", when creating the table if a table name is given as "ORDER" then it is considered as keyword. So I have used the table name as "ORDERS".
Now, the below query is used to add a Foreign Key Constraint to an already existing table "ORDERS".
ALTER TABLE ORDERS ADD FOREIGN KEY(CUSTID) REFERENCES CUSTOMER(CUSTID);
Trending now
This is a popular solution!
Step by step
Solved in 2 steps with 1 images









