Home > Database > SQL > body text

How to set primary key in sql database?

青灯夜游
Release: 2020-07-28 10:54:29
Original
41337 people have browsed it

Setting method: 1. Use the SQL Server Management Studio tool to connect to the database; 2. Expand "Table" in the database, right-click the data table that needs to be set, and select "Design"; 3. In the table designer interface, right-click the corresponding row and select "Set Primary Key" to complete the setting of the primary key and click Save.

How to set primary key in sql database?

The primary key is one or more fields in the table, and its value is used to uniquely identify a record in the table. In a relationship between two tables, the primary key is used in one table to reference a specific record from the other table. The primary key is a unique key that is part of the table definition. The primary key of a table can be composed of multiple keywords, and the columns of the primary key cannot contain null values.

Define the primary key

Open the [SQL Server Management Studio] management tool and connect to the database

Use the SSMS tool to define the primary key

  1. Expand "Database", then expand the corresponding database, then expand "Table" in the database, right-click the data table where the primary key needs to be defined, and select "Design" .
    How to set primary key in sql database?
  2. Enter the table designer interface, right-click the corresponding row (if you need to define a composite primary key containing multiple columns, hold down the Ctrl key to select), and then select "Set Primary Key", This completes the setting of the primary key.
    How to set primary key in sql database?
  3. Click the save button, or press the Ctrl F5 key to save. Expand the data table and expand "Key" to see the defined primary key.
    How to set primary key in sql database?
  4. If not, please press the refresh key or F5 key to refresh.
    How to set primary key in sql database?

Use SQL to define the primary key

Method 1: Define the primary key when creating the data table

  1. Click "New Query" in the SSMS toolbar to open the "SQL Editor" window
    How to set primary key in sql database?
  2. Enter the creation SQL code
USE schoolDB                                                 --打开数据库schoolDB
GO
IF EXISTS(SELECT * FROM sysobjects WHERE name='student') 
DROP TABLE student                --检查student是否已经存在,如果存在,则删除
GO
CREATE TABLE student                                           --表名为student
(
	  StuID int NOT NULL PRIMARY KEY,                                           --学生学号
	  StuName varchar(15) NOT NULL,                                 --学生姓名
	  Sex char(2) NULL,                                             --性别
	  Major varchar(20) NULL,                                      --所选专业
)
Copy after login
  1. Click the "Analyze" button, or hold down Ctrl F5 to syntax analyze the SQL code to ensure that the SQL statement syntax is correct.
    How to set primary key in sql database?
  2. Click the "Execute" button, or hold down F5 to execute the SQL code.
    How to set primary key in sql database?
  3. View the keys in the data table.
    How to set primary key in sql database?

Method 2: Modify the data table definition primary key

  1. Click "New Query" in the SSMS toolbar, Open the "SQL Editor" window
    How to set primary key in sql database?
  2. Enter the creation SQL code
USE schoolDB                                  --打开数据库schoolDB
GO
ALTER TABLE student
ADD CONSTRAINT PK_student PRIMARY KEY (StuID) --将表中的StuID设为主键
Copy after login
  1. Click the "Analyze" button, or hold down Ctrl F5 to edit the SQL The code is grammatically analyzed to ensure that the SQL statement syntax is correct.
    How to set primary key in sql database?
  2. Click the "Execute" button, or hold down F5 to execute the SQL code.
    How to set primary key in sql database?
  3. View the keys in the data table.
    How to set primary key in sql database?

Delete primary key

Delete primary key using SSMS tool

Method 1: Delete the primary key in the Object Explorer

  1. 展开需要删除主键的数据表,然后再展开“键”。
    How to set primary key in sql database?
  2. 右击需要删除的主键,选择“删除”。
    How to set primary key in sql database?
  3. 在删除对象界面,点击“确定”,即可完成主键删除。
    How to set primary key in sql database?

方式二:在表设计器中删除主键

  1. 右击需要删除主键的数据表,选择“设计”。
    How to set primary key in sql database?
  2. 进入表设计器界面,右击包含主键的行,选择“删除主键”。
    How to set primary key in sql database?
  3. 点击保存键,或者按Ctrl+F5键进行保存删除主键后的数据表信息。
    How to set primary key in sql database?

使用SQL方式删除主键

  1. 在SSMS工具栏中单击“新建查询”,打开“SQL编辑器”窗口
    How to set primary key in sql database?

  2. 输入创建SQL代码

USE schoolDB                          --打开数据库schoolDB
GO
ALTER TABLE student
DROP CONSTRAINT PK_student			  --将表中的主键约束PK_studnet删除
Copy after login
  1. 点击“分析”按钮,或按住Ctrl+F5,对SQL代码进行语法分析,确保SQL语句语法正确。
    How to set primary key in sql database?
  2. 点击“执行”按钮,或按住F5,执行SQL代码。
    How to set primary key in sql database?
  3. 主键已被删除。
    How to set primary key in sql database?

相关教程推荐:《数据库视频教程》、《PHP教程

The above is the detailed content of How to set primary key in sql database?. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!