Primary key PRIMARY KEY in SQL Server
Learn how to create, delete, disable or enable primary keys in SQL Server with syntax and examples.
Learn how to create, delete, disable or enable primary keys in SQL Server with syntax and examples.
What is the primary key in SQL Server?
In SQL Server (Transact-SQL), the primary key is a field or a combination of fields and is defined as a single record. No field in the primary key contains a NULL value. A table has only one primary key. The primary key can be defined with the CREATE TABLE command or the ALTER TABLE command.
Create the primary key - with the CREATE TABLE command
The main key generation syntax is with the CREATE TABLE command
CREATE TABLE ten_bang
(
cot1 kieu_du_lieu [ NULL | NOT NULL ] [ PRIMARY KEY ],
cot2 kieu_du_lieu [ NULL | NOT NULL ],
.
);
or
CREATE TABLEten_bang
(
cot1 kieu_du_lieu [ NULL | NOT NULL ],
cot2 kieu_du_lieu [ NULL | NOT NULL ],
…
CONSTRAINT ten_rang_buoc PRIMARY KEY (cot1, cot2, … cot_n)
);
The primary key generation example is with the CREATE TABLE command
CREATE TABLE nhanvien
( nhanvien_id INT PRIMARY KEY,
ho VARCHAR(50) NOT NULL,
ten VARCHAR(50), NOT NULL,
luong MONEY
);
In this example, the primary key for the nhanvien table is made up of a field called nhanvien_id. In addition, the primary key can be created as follows:
CREATE TABLE nhanvien
( nhanvien_id INT,
ho VARCHAR(50) NOT NULL,
ten VARCHAR(50), NOT NULL,
luong MONEY
CONSTRAINT nhanvien_pk PRIMARY KEY (nhanvien_id)
);
Now is the example of creating a primary key with more than 1 field in SQL Server.
CREATE TABLE vnien
( ho VARCHAR(50) NOT NULL,
ten VARCHAR(50), NOT NULL,
luong MONEY
CONSTRAINT nhanvien_pk PRIMARY KEY (ho, ten)
);
In this example, we created the primary key made up of two columns, ho
and ten
. These two fields will uniquely identify the record in the table.
Create a primary key - with the ALTER TABLE command
Primary keys can only be created with the ALTER TABLE command in columns defined as NOT NULL. If the column allows NULL values, the primary key cannot be added without deleting or recreating the table.
The syntax for creating a primary key is with the ALTER TABLE command
ALTER TABLE ten_bang
ADD CONSTRAINT t
en_rang_buoc PRIMARY KEY (cot1, cot2, . cot_n);
Example of creating a primary key with the ALTER TABLE command
ALTER TABLE nhanvien
ADD CON
STRAINT nhanvien_pk PRIMARY KEY (nhanvien_id);
In this example, we create the primary key for the available table, including the nhanvien_id field . Note that the nhanvien_id field must be defined as NOT NULL before, otherwise it will have to be deleted and re-created the table and define this field as NOT NULL.
You can create a primary key with more than 1 field as in the following example.
ALTER TABLE staff
ADD CONSTRAINT n
hanvien_pk PRIMARY KEY (cough, ten);
The primary key for the table includes 2 fields, namely first and last name. Both must be defined as NOT NULL.
Delete the primary key in SQL Server
The syntax for deleting the primary key is with the ALTER TABLE command
ALTER TABLE ten_bang
DROP CONS
TRAINT ten_rang_buoc;
Example of deleting the primary key with the ALTER TABLE command
ALTER TABLE staff
DROP CONSTRAINT
nhanvien_pk;
The above example deletes the primary key in the table.
Disable the primary key in SQL Server
The syntax to disable the primary key with the ALTER INDEX command
ALTER INDEX ten_rang_buoc ON ten_bang
DISABLE;
For example, disable the primary key with the ALTER INDEX command
ALTERINDEX nhanvien_pk ON nhanvien
DISABLE
;
The example above disables the primary key in the table.
Activate the primary key in SQL Server
The syntax for activating the primary key is with the ALTER INDEX command
ALTER INDEX ten_rang_buoc ON ten_bang
REBU
ILD;
For example, activate the primary key with the ALTER INDEX command
ALTER INDEX nhanvien_pk ON nhanvien
REBUI
LD;
The above example re-activates the primary key of the table.
Previous article: CREATE TABLE command in SQL Server
The following article: Alter Table command in SQL Server
You should read it
- How to use Primary font
- How to create Primary Zones in Windows Server 2012
- CREATE TABLE command in SQL Server
- How to Help Achieve Universal Primary Education
- Cuong Tieu Ngu wallpaper for the phone
- Facebook cover photo Cuong Tieu Ngu
- Use eBox as Windows Primary Domain Controller
- MS Access - Lesson 7: Lock
- How to protect DNS server against hackers
- How to install and configure MySQL server on Pi
- Primary font, practice writing beautiful font
- How to Use an iMac as an External Monitor
Maybe you are interested
5 quietest keyboards 4 ways to return to the previous version of Windows 10 without losing data Is the air on the plane safe? Invite to download the space theme wallpapers on the NASA XS inspired by NASA How to change the color of the message frame on iPhone / iPad How to transform the message interface on iPhone