1A primary key allows each record in a table to be uniquely identified. There can only be one
2primary key per table, and you can assign this constraint to any single or combination of columns.
3However, this means each value within this column(s) must be unique.
4Typically in a table, the primary key is an ID column, and is usually paired with the AUTO_
5INCREMENT keyword. This means the value increases automatically as new records are created.
6CREATE TABLE users (
7id int NOT NULL AUTO_INCREMENT,
8first_name varchar(255),
9last_name varchar(255) NOT NULL,
10address varchar(255),
11email varchar(255),
12PRIMARY KEY (id)
13);
1ALTER TABLE <Table_Name>
2DROP CONSTRAINT <constraint_name>
3
4ALTER TABLE <Table_Name>
5ADD CONSTRAINT <constraint_name> PRIMARY KEY (<Column1>,<Column2>)
1Primary Key :
2It is unique column in every table in a database
3It can ONLY accept;
4 - nonduplicate values
5 - cannot be NULL
6
7
8
9Foreign Key:
10It is a column that comes from a different table and
11using Foreign key tables are related each other
12It is the primary key of another table
13It can be duplicate or null for another table
14
15
16
17
18Unique Key:
19Only unique value and also can contain NULL
1Primary Key :
2It is unique column in every table in a database
3It can ONLY accept;
4 - nonduplicate values
5 - cannot be NULL
1CREATE TABLE Persons (
2 Rollno int NOT NULL,
3 FirstName varchar(255),
4 LastName varchar(255),
5 Age int,
6 PRIMARY KEY (ID)
7);
8
1
2
3
4
5 CONSTRAINT pk_purchase_orders PRIMARY KEY(po_nr)
6Code language: SQL (Structured Query Language) (sql)