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).
![CUSTOMER Table
CUSTID | LNAME | FNAME | CITY
| STATE | PHONE
HARRIS
EDWARDS MITCH
107
PAULA
OSSEO
WI
7155559035
232
WASHBURN WI
7155556975
133
GARCIA
MARIA
RADISSON
WI
7155558332
154
MILLER
LEE
SILVER
WI
7155559002
179
CHANG
ALISA
SISBAY
WI
7155550087
CUSTID
ORDER Table
ORDERID|INVID|ORDERDATE | CUSTID| ORDER_PRICE|QUANTITY
1057
11668 5/29/2001
107
259.99
1
1057
11776 5/29/2001
107
59.90
2
11777 5/29/2001
11780 5/31/2001
1058
232
29.95
59.95
1
1059
133
1
1060
11775 5/31/2001
154
59.90
2
1061
11779 6/01/2001
179
59.95
1
INVID
INVENTORY Table
INVID| ITEMID | ITEMSIZE | CCOLOR | CURR PRICE | QOH
11668
786
SIENNA
259.99
16
11669
786
FOREST
259.99
12
11775
KHAKI
KHAKI
29.95
29.95
894
S
150
11776
894
147
11777
894
L
KHAKI
29.95
59.95
120
11778
897
S
TEAL
152
117
11779
897
M
TEAL
59.95
11780
897
L
TEAL
59.95
125
ITEMID
ITEM Table
ITEMDI ITEMDESC
| CATEGORY
894
WOMEN'S HIKING SHORTS
WOMEN'S CLOTHING
WOMEN'S CLOTHING
MEN'S CLOTHING
897
WOMEN'S FLEECE PULLOVER
786
MEN'S EXPEDITION PARKA](/v2/_next/image?url=https%3A%2F%2Fcontent.bartleby.com%2Fqna-images%2Fquestion%2F4a5d1eb1-3f81-4d1a-b6f5-b63bc1325c48%2F815255ee-aa13-4979-b239-ee76301eb434%2Fe2ol7be_processed.png&w=3840&q=75)
![](/static/compass_v2/shared-icons/check-mark.png)
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
![Blurred answer](/static/compass_v2/solution-images/blurred-answer.jpg)
![Database System Concepts](https://www.bartleby.com/isbn_cover_images/9780078022159/9780078022159_smallCoverImage.jpg)
![Starting Out with Python (4th Edition)](https://www.bartleby.com/isbn_cover_images/9780134444321/9780134444321_smallCoverImage.gif)
![Digital Fundamentals (11th Edition)](https://www.bartleby.com/isbn_cover_images/9780132737968/9780132737968_smallCoverImage.gif)
![Database System Concepts](https://www.bartleby.com/isbn_cover_images/9780078022159/9780078022159_smallCoverImage.jpg)
![Starting Out with Python (4th Edition)](https://www.bartleby.com/isbn_cover_images/9780134444321/9780134444321_smallCoverImage.gif)
![Digital Fundamentals (11th Edition)](https://www.bartleby.com/isbn_cover_images/9780132737968/9780132737968_smallCoverImage.gif)
![C How to Program (8th Edition)](https://www.bartleby.com/isbn_cover_images/9780133976892/9780133976892_smallCoverImage.gif)
![Database Systems: Design, Implementation, & Manag…](https://www.bartleby.com/isbn_cover_images/9781337627900/9781337627900_smallCoverImage.gif)
![Programmable Logic Controllers](https://www.bartleby.com/isbn_cover_images/9780073373843/9780073373843_smallCoverImage.gif)