In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-02-23 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Database >
Share
Shulou(Shulou.com)06/01 Report--
This article mainly introduces the method of setting primary key in sql database, which has certain reference value and can be used for reference by friends who need it. I hope you will learn a lot after reading this article. Next, let the editor take you to learn about it.
Setting method: 1, use the SQL Server Management Studio tool to connect to the database; 2, expand the "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, select "set primary key", that is, complete the setting of the primary key, click Save.
The primary key (primary key) is one or more fields in a table whose values are used to uniquely identify a record in the table. In a relationship between two tables, the primary keyword is used to reference a specific record from another table in one table. The primary keyword is a unique keyword 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 primary key
Open [SQL Server Management Studio] management tool and connect to the database
Define a primary key using the SSMS tool
Expand Database, then expand the corresponding database, expand tables in the database, right-click the data table for which you want to define the primary key, and select Design.
Enter the table designer interface, right-click the appropriate row (press and hold the Ctrl key to select if you need to define a compound primary key that contains multiple columns), and then select set Primary key to complete the primary key setting.
Click the Save button, or press Ctrl+ F5 to save. Expand the data table, expand keys, and you can see the defined primary key.
If not, press the Refresh key or F5 key to refresh.
Define the primary key in SQL mode
Method 1: define the primary key when creating the data table
Click New query in the SSMS toolbar to open the SQL Editor window
Enter the create SQL code
USE schoolDB-- Open the database schoolDBGOIF EXISTS (SELECT * FROM sysobjects WHERE name='student') DROP TABLE student-- check to see if student already exists, if so Delete GOCREATE TABLE student-- table name student (StuID int NOT NULL PRIMARY KEY,-- student number StuName varchar (15) NOT NULL,-- student name Sex char (2) NULL -- gender Major varchar (20) NULL,-- selected major)
Click the analyze button, or press and hold Ctrl+F5 to parse the SQL code to ensure that the SQL statement syntax is correct.
Click the execute button, or press and hold F5 to execute the SQL code.
View the keys in the data table.
Method 2: modify the data table to define the primary key
Click New query in the SSMS toolbar to open the SQL Editor window
Enter the create SQL code
USE schoolDB-- Open database schoolDBGOALTER TABLE studentADD CONSTRAINT PK_student PRIMARY KEY (StuID)-- set StuID in the table as the primary key
Click the analyze button, or press and hold Ctrl+F5 to parse the SQL code to ensure that the SQL statement syntax is correct.
Click the execute button, or press and hold F5 to execute the SQL code.
View the keys in the data table.
Delete primary key
Use the SSMS tool to delete the primary key
Method 1: delete the primary key in object Explorer
Expand the data table where you need to delete the primary key, and then expand key.
Right-click the primary key that you want to delete and select Delete.
In the delete object interface, click "OK" to complete the primary key deletion.
Method 2: delete the primary key in the table designer
Right-click the data table where you want to delete the primary key, and select Design.
Enter the table designer interface, right-click the row that contains the primary key, and select Delete Primary key.
Click the save key, or press Ctrl+ F5 to save the data table information after deleting the primary key.
Use SQL to delete the primary key
Click New query in the SSMS toolbar to open the SQL Editor window
Enter the create SQL code
USE schoolDB-- Open database schoolDBGOALTER TABLE studentDROP CONSTRAINT PK_student-- delete the primary key constraint PK_studnet in the table
Click the analyze button, or press and hold Ctrl+F5 to parse the SQL code to ensure that the SQL statement syntax is correct.
Click the execute button, or press and hold F5 to execute the SQL code.
Primary key has been deleted.
Thank you for reading this article carefully. I hope it is helpful for everyone to share the method of setting the primary key in sql database. At the same time, I also hope that you can support us, pay attention to the industry information channel, and find out if you encounter problems. Detailed solutions are waiting for you to learn!
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 0
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.