DATABASE & SQL/PLSQL

adplus-dvertising
PRIMARY KEY Constraint
Previous Home Next

A Primary Key constraints is defined on a column whose values uniquely identify rows in a table. These columns are referred to as the primary key columns. A primary key column can not contain NULL values since it is used to uniquely identify rows in a table. the primary key ensure entity integrity.

You can define a PRIMARY KEY constraints at the time of a table creation, or you can add it later by altering the table. While defining a PRIMARY KEY constraints, you need to specify a name for the constraints. If a name is not specified, SQL server automatically assigns a name to the constraints.

CREATE TABLE table_name
(column_name data_type CONSTRAINT constraints_name PRIMARY KEY),..

Example:

CREATE TABLE employee (emp_name char (4) CONSTRAINT pfemp_name PRIMARY KEY)

The above command creates a table employee with emp_name as the primary key. The name of the constraints is pfemp_name.

output

Example:

Primary key also created after creating a table by following command:

ALTER TABLE  employee
ADD CONSTRAINT  Pfemp_name PRIMARY KEY  (emp_id)

output

Previous Home Next