How to set primary key in sql server

before the start

restrictions and limitations

  • A table can contain only one PRIMARY KEY constraint.

  • All columns defined in PRIMARY KEY constraints must be defined as NOT NULL. If Nullability is not specified, the Nullability of all columns that join the PRIMARY KEY constraint will be set to NOT NULL .

Creating a primary key automatically creates a corresponding unique clustered index, clustered index, or nonclustered index (if so specified)

If you want to redefine a primary key, you must first delete any relationship with the existing primary key before you can create a new primary key. At this point, a message will appear warning you that existing relationships will be automatically deleted as part of this process.

If the primary key consists of multiple columns, duplicate values ​​will be allowed for one of the columns, but various combinations of values ​​for all columns in the primary key must be unique.

In SQL Server, the primary key is a column that uniquely identifies each row of records in the data table. If a table does not have a primary key, the records in the table cannot be distinguished. In addition, the primary key also helps to improve query performance. Using the primary key speeds up the access speed of records in the table. Therefore, when using SQL Server for database design, the design of the primary key is particularly important.

First, after creating the data table in the database, you can use the following T-SQL code statement

1. Use T-SQL command. After executing the command, the primary key will be set successfully.

ALTER TABLE 表名
ADD CONSTRAINT PK_主键名称 PRIMARY KEY (列名)

--其中,表名是要设置主键的表名,主键名称是主键约束的名称(可以自己定义),列名是要设置为主键的列名

ALTER TABLE sys_menu
ADD CONSTRAINT PK_menu_id PRIMARY KEY (menu_id)



ALTER TABLE student
ADD CONSTRAINT PK_student PRIMARY KEY (StuID) --将表中的StuID设为主键


ALTER TABLE student
DROP CONSTRAINT PK_student			  --将表中的主键约束PK_studnet删除





In addition, in SQL Server, you can also use the following code to define the primary key of the table

CREATE TABLE table_name

(

column_name datatype PRIMARY KEY

)

CREATE TABLE student                                           --表名为student
(
	  StuID int NOT NULL PRIMARY KEY,                           --学生学号
	  StuName varchar(15) NOT NULL,                             --学生姓名
	  Sex char(2) NULL,                                         --性别
	  Major varchar(20) NULL,                                   --所选专业
)


primary key(column name 1, column name 2)

In addition, in SQL Server, you can also use the following code to define a joint primary key, that is, a primary key with multiple fields. For example, to define the columns customer_id and order_id as a joint primary key at the same time, you can use the following T-SQL code



CREATE TABLE orders

(

customer_id INT,

order_id INT,

PRIMARY KEY(customer_id, order_id)

)

alter table table name add constraint pk_name primary key (column name 1, column name 2)

Therefore, in SQL Server, it is very easy to define the primary key of a table. Whether it is a single primary key or a combined primary key, it only takes a few lines of code to complete the definition.

Set the primary key and make it non-null

 First make sure that the column you want to set the primary key is not empty. If your column is empty, the following code can help you set it to empty and add a primary key.

/*Table_Member是数据表的名字,StudentID 是要设为主键的列,bigint 是主键列的数据类型*/
ALTER TABLE Table_Member ALTER COLUMN StudentID bigint NOT NULL;
 
/*Table_Member是数据表的名字,StudentID 是要设为主键的列*/
ALTER TABLE Table_Member WITH NOCHECK
ADD CONSTRAINT PK_Table_Member PRIMARY KEY (StudentID)

Change the character type of the primary key column in the established table

Similarly, this operation must be divided into two steps. In the design concept of SQL server, the data type of the primary key column cannot be changed. If you want to change the character type of the column that is not the primary key, jump directly to the second step.

ALTER TABLE [表名] DROP CONSTRAINT [主键约束名]

ALTER TABLE [表名] ALTER COLUMN [列名] [数据类型]

Guess you like

Origin blog.csdn.net/zgscwxd/article/details/132462165