Thursday, 31 March 2011

SQL foreign key

A foreign key is a field (or fields) that points to the primary key of another table. The purpose of the foreign key is to ensure referential integrity of the data. In other words, only values that are supposed to appear in the database are permitted.
 
Table CUSTOMER
column namecharacteristic
SIDPrimary Key
Last_Name 
First_Name 

Table ORDERS
column namecharacteristic
Order_IDPrimary Key
Order_Date 
Customer_SIDForeign Key
Amount 


Script :
CREATE TABLE ORDERS
(Order_ID integer primary key,
Order_Date datetime,
Customer_SID integer references CUSTOMER(SID),
Amount double);


if you alter the reference:

ALTER TABLE ORDERS
ADD FOREIGN KEY (customer_sid) REFERENCES CUSTOMER(SID);

No comments:

Post a Comment