0
foreign key syntax
what's the syntax to use foreign key in sql?
2 Answers
0
A FOREIGN KEY in one table points to a PRIMARY KEY in another table.
CUSTOMERS table:
CREATE TABLE CUSTOMERS(
ID INT NOT NULL,
NAME VARCHAR (20) NOT NULL,
AGE INT NOT NULL,
ADDRESS CHAR (25) ,
SALARY DECIMAL (18, 2),
PRIMARY KEY (ID)
);
ORDERS table:
CREATE TABLE ORDERS (
ID INT NOT NULL,
DATE DATETIME,
CUSTOMER_ID INT references CUSTOMERS(ID),
AMOUNT double,
PRIMARY KEY (ID)
);
ALTER TABLE ORDERS
ADD FOREIGN KEY (Customer_ID) REFERENCES CUSTOMERS (ID);
0
thankss ^^ my professor doesn't explain it in detail.. that's why i still confused. thanks a lot for your answer. it's really helpful ^^