class_xi_xii_prm_frm

  CLASS XI-XII

(SQL PRIMARY KEY AND FOREIGN KEY RELATIONSHIP METHODS)


HOW TO USE PRIMARY KEY AND FOREIGN KEY
Look at the following two tables:
"Persons" table:
PersonID
LastName
FirstName
Age
1
Hansen
Ola
30
2
Svendson
Tove
23
3
Pettersen
Kari
20

"Orders" table:
OrderID
OrderNumber
PersonID
1
77895
3
2
44678
3
3
22456
2
4
24562
1
MySQL:
CREATE TABLE Orders (
    OrderID int
NOT NULL,
    OrderNumber int
NOT NULL,
    PersonID int,
   
PRIMARY KEY (OrderID),
   
FOREIGN KEY (PersonID) REFERENCES Persons(PersonID)
);
SQL Server / Oracle / MS Access(CBSE BOARD EXAMINATION COMMAND):
CREATE TABLE Orders (
    OrderID int
NOT NULL PRIMARY KEY,
    OrderNumber int
NOT NULL,
    PersonID int
FOREIGN KEY REFERENCES Persons(PersonID)
);



To allow naming of a FOREIGN KEY constraint, and for defining a FOREIGN KEY constraint on multiple columns, use the following SQL syntax:
MySQL / SQL Server / Oracle / MS Access:
CREATE TABLE Orders (
    OrderID int
NOT NULL,
    OrderNumber int
NOT NULL,
    PersonID int,
   
PRIMARY KEY (OrderID),
   
CONSTRAINT FK_PersonOrder FOREIGN KEY (PersonID)
   
REFERENCES Persons(PersonID)
);

Followers

Popular Posts

Recent Posts