Primary Key Constraints

  • A primary key is used to uniquely identify each row in a table.
  • primary key mus contain value
  • primary key column cannot contain NULL values.
  • A primary key can consist of one or more fields on a table. When multiple fields are used as a primary key, they are called a composite key.
  • Primary keys can be specified during alter table or create table

MySQL :

CREATE TABLE articles
(cat_id integer,
title varchar(30),
description varchar(30),
PRIMARY KEY (cat_id));

Oracle :

CREATE TABLE articles
(cat_id integer PRIMARY KEY,
title varchar(30),
description varchar(30));

SQL Server :

CREATE TABLE articles
(cat_id integer PRIMARY KEY,
title varchar(30),
description varchar(30));

Below are examples for specifying a primary key by altering a table:

MySQL/MSSQL/Oracle:

ALTER TABLE articles ADD PRIMARY KEY (cat_id);

Bookmark This Page

Link Partners