Back
Next
SQL Tutorial will guide us now, that an SQL Primary Key is used to uniquely identify each
row in our table. It can either be part of the actual
record itself , or it can be an artificial field one
that has nothing to do with the actual record.
An SQL Primary Key can consist of one or more fields on a
table. When multiple fields are used as an SQL primary key,
they are called a composite SQL primary key.
Primary keys can be specified either when the table
is created (using SQL CREATE TABLE) or by changing the
existing table structure (using SQL ALTER TABLE).
Below are examples for specifying a SQL Primary Key when creating a table:
Example of SQL PRIMARY Key Constraint with MySQL Create Table.
CREATE TABLE Employees
(empID integer,
FirstName varchar(30),
LastName varchar(30),
PRIMARY KEY (empID));
Example of SQL PRIMARY Key Constraints with Oracle Create Table.
CREATE TABLE Employees
(empID integer PRIMARY KEY,
FirstName varchar(30),
LastName varchar(30));
Example of SQL PRIMARY Key Constraints wtih SQL Server Create Table.
CREATE TABLE Employees
(empID integer PRIMARY KEY,
FirstName varchar(30),
LastName varchar(30));
Below are examples for specifying an SQL Primary Key Constraint by using SQL
ALTER Table Command:
SQL PRIMARY Key Constraint Example with MySQL Alter Table.
ALTER TABLE Employees ADD PRIMARY KEY (empID);
SQL PRIMARY Key Constraint Example with Oracle Alter Table.
ALTER TABLE Employees ADD PRIMARY KEY (empID);
SQL PRIMARY Key Constraint Example with SQL Server Alter Table.
ALTER TABLE Employees ADD PRIMARY KEY (empID);
Note: Before using the SQL ALTER TABLE command to add an SQL Primary Key
Constraint, we'll need to
make sure that the field is defined as 'NOT NULL' in database, other wise
NULL cannot be an accepted value for that field we must make that field or
column as 'NOT NULL'
Back
Next
|