Generic selectors
Exact matches only
Search in title
Search in content
Post Type Selectors

Primary and Foreign key

PRIMARY KEY AND FOREIGN KEY


Primary 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.
For example:

CREATE TABLE Student( RollNumber INT, NAME VARCHAR (20), 
PRIMARY KEY (RollNumber) );
How to delete a primary key?
ALTER TABLE Student DROP PRIMARY KEY ;
Foreign 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.
For example:
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;

Leave a Comment