Database - Primary Key
PRIMARY KEY
The PRIMARY KEY constraint
uniquely identifies each record
in a database table.
Primary keys must
contain unique values.
A primary
key column
cannot contain NULL values.
Each table should have a primary key, and each table can
have only ONE primary key.
COLUMN LEVEL
CONSTRAINT
To add a primary key for a column.
To add a primary
key while creating a table
create table emp (empno int constraint pk1_emp primary
key,
ename varchar(20),desg char(3),salary money,deptno int)
TABLE LEVEL
CONSTRAINT
To add a primary key for a table.
COMPOSITE PRIMARY
KEY
If we add primary key for more than one
column it is
called
as composite primary key.
create table emp2(empno int ,ename varchar(20),desg char(3),
salary money,deptno int,constraint pk_emp2 primary
key(empno,deptno))
Adding primary
key for an existing table
To add an primary key for an existing table,first me must alter
the
column to not null(because primary
does not allow null
values).
Alter table emps add constraint Pk_emps primary
key(empno)
Dropping a
primary key
alter table emps drop constraint pk_emps
Comments
Post a Comment