" /> Password <br /><input type="password" name="passwd" id="passwd" class="inputbox" size="18" alt="password" />
Home SQL Primary Key
PDF Note

PRIMARY KEY - This restriction allows you to uniquely identify each record in the table.

The primary key must contain unique values.

The primary key can not contain NULL values.

SQL PRIMARY KEY constraints in CREATE TABLE

The following SQL creates a PRIMARY KEY constraint on column "P_Id", when creating a table "Persons":

MySQL:

1
2
3
4
5
6
7
8
CREATE TABLE Persons (
P_Id int NOT NULL,
LastName varchar(255) NOT NULL,
FirstName varchar(255),
Address varchar(255),
City varchar(255),
PRIMARY KEY (P_Id)
)

SQL Server / Oracle / MS Access:

1
2
3
4
5
6
7
CREATE TABLE Persons (
P_Id int NOT NULL PRIMARY KEY,
LastName varchar(255) NOT NULL,
FirstName varchar(255),
Address varchar(255),
City varchar(255)
)

MySQL / SQL Server / Oracle / MS Access:

1
2
3
4
5
6
7
8
CREATE TABLE Persons (
P_Id int NOT NULL,
LastName varchar(255) NOT NULL,
FirstName varchar(255),
Address varchar(255),
City varchar(255),
CONSTRAINT pk_PersonID PRIMARY KEY (P_Id,LastName)
)

SQL PRIMARY KEY Constraints with ALTER TABLE

MySQL / SQL Server / Oracle / MS Access:

1
2
ALTER TABLE Persons
ADD PRIMARY KEY (P_Id)

MySQL / SQL Server / Oracle / MS Access:

1
2
ALTER TABLE Persons
ADD CONSTRAINT pk_PersonID PRIMARY KEY (P_Id,LastName)

- Note:

Removal of PRIMARY KEY

To remove a PRIMARY KEY constraint, use the following SQL:

MySQL:

1
2
ALTER TABLE Persons
DROP PRIMARY KEY

SQL Server / Oracle / MS Access:

1
2
ALTER TABLE Persons
DROP CONSTRAINT pk_PersonID

Comments

 
-1 #1 Milton Junior 2011-03-28 22:16 Boy, was I starting to need things were arranged this way, it was worth it!
I really enjoyed!
Quote
 
 
-4 #2 nguyenthephong 2011-04-25 04:04 correct key is wisdom, wisdom that I deleted skeptical ears are clear, so that no baos nbhuw after applying only home delivery!
Msg 156, Level 15, State 1, Line 1
Incorrect syntax near the keyword 'primary'.
Quote
 
 
0 #3 km 2011-12-20 13:58 Quote
 

Authorization

Nice Ajax Poll

Which one of my extensions is the best?

Statistics

Advertisement