Differentiate Primary key and foreign key?

Answered by Kaushik Chandra

A primary key is basically a combination of fields that indicate a unique row. It is a NOT NULL constraint which means the primary key cannot be NULL.


Example:

CREATE TABLE Orders(

   OID INT NOT NULL,

   ONAME VARCHAR (20) NOT NULL,

   MenuItem CHAR (25),

   PRIMARY KEY (OID)

);

A foreign key is utilized to connect two diverse tables together. This could be accomplished by relating a foreign key in one table with a primary key in another table.

Example:

CREATE TABLE Orders (

 OID INT NOT NULL,

 ONum INT NOT NULL,

 PID INT,

 PRIMARY KEY (OID),

 FOREIGN KEY (PID) REFERENCES Persons(PID)

);



Your Answer

Interviews

Parent Categories