PRIMARY KEY AND FOREIGN KEY
A primary key is a field in a table which uniquely identifies each row in a table.
Primary keys must contain unique values.
A primary key column cannot have NULL values.
A table can have only one primary key, which may consist of single or multiple fields.
How to create a primary key?
Use attribute primary key to make key primary key.
CREATE TABLE Student( RollNumber INT, NAME VARCHAR (20),
PRIMARY KEY (RollNumber) );
How to delete a primary key?
ALTER TABLE Student DROP PRIMARY KEY ;
A foreign key is a key used to link two tables together. This is sometimes also called as a referencing key.
Foreign key matches a primary key in another table.
How to create a foreign key?
Use reference keyword to make a key foreign key.
CREATE TABLE College( CollegeCode INT NOT NULL,
Student_ID INT references Student(RollNumber),
PRIMARY KEY (CollegeCode) );
How to delete a foreign key?
ALTER TABLE Student DROP FOREIGN KEY;