Primary key PRIMARY KEY in SQL Server

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 TAB LE ten_bang 
(
cot1 kieu_du_lieu [ NULL | NOT NULL ] [ PRIMARY KEY ],
cot2 kieu_du_lieu [ NULL | NOT NULL ],
.
);

or

 CREATE TABLE ten_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 nh anvien 
( 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 nhanvi en 
( 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 vn ien 
( 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 T ABLE 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 TAB LE 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 IN DEX ten_rang_buoc ON ten_bang 
DISABLE;

For example, disable the primary key with the ALTER INDEX command

 ALTER INDEX 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

 ALT ER INDEX ten_rang_buoc ON ten_bang 
REBU ILD;

For example, activate the primary key with the ALTER INDEX command

 ALTE R 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

4 ★ | 2 Vote

May be interested

  • The difference between web server and app serverThe difference between web server and app server
    you have probably seen that the terms web server and app server are often used interchangeably as if they are related to the same thing and also facilitate the website to function properly. but in reality, they are not the same.
  • Network basics: Part 3 - DNS ServerNetwork basics: Part 3 - DNS Server
    a dns server is a server that contains a database of public ip addresses and hostnames associated with them. in most cases, the dns server is used to resolve or translate those common names into ip addresses as required.
  • How to set up your own Git server on LinuxHow to set up your own Git server on Linux
    while you can count on globally renowned git hosting services like github, in some cases it is better to host a personal git server for enhanced privacy, customizability, and security.
  • Use IIS to set up FTP Server on WindowsUse IIS to set up FTP Server on Windows
    set up an ftp server (file transfer protocol server) to share and convert large files with unlimited traffic.
  • How to change DNS server on the most popular routersHow to change DNS server on the most popular routers
    changing the dns server settings on your router is not difficult, but every manufacturer uses their own custom interface, which means the process can be very different depending on which router you are owned.
  • Set up Domain and receive Email with Microsoft BPOSSet up Domain and receive Email with Microsoft BPOS
    in this article, we will show you the easiest way to set up and run bpos: configure bpos as the primary mail server for a domain.
  • What is VPS? VPS used to do? What is VPS different from Server?What is VPS? VPS used to do? What is VPS different from Server?
    what is vps? vps used to do? what is vps different from server ?. when you intend to learn about network data or open the website, you will definitely be introduced to many different server and server services. but server hosting has a lot of tricks
  • Primary font, practice writing beautiful fontPrimary font, practice writing beautiful font
    primary font, practice writing beautiful font. the handwriting strokes of the students are always the most beautiful handwriting of each of us because then we are honed and molded the handwriting carefully from teachers and teachers. if you loved the handwriting, but couldn't write as well as a kid.
  • 7 great ideas using Raspberry Pi as a server7 great ideas using Raspberry Pi as a server
    raspberry pi is a great solution for many computer projects, from learning programming to remote control a car to building a basic stop-motion animation studio. but do you know that raspberry pi can also be used as a server? here are some ideas for using raspberry pi as a server.
  • New points in SQL Server 2017New points in SQL Server 2017
    the sql server 2017 version is primarily connected to linux, bringing the power of sql to linux. in short, you can install sql server 2017 on linux, using sql server 2017 on linux-based docker containers. sql server 2017 also allows you to choose development languages, develop it on-premise or cloud-based.