How to establish a composite primary key in the database table (ie, multiple fields as the primary key)

zhaozj2021-02-11  193

The primary key is an important property of the database table, and the establishment of the primary key can avoid exactly the same record in the table, that is, the record value of the primary key is in a table is unique.

There are two ways to establish a primary key: one is established in the GUI environment provided by the database, and the other is to perform the establishment through the SQL statement, which is described below.

1. Established (as an example of SQL7) in the GUI environment provided by the database.

After entering the table information, press the CTRL button to select multiple lines, then click the primary key button above.

2. Establish it through the SQL statement. It is divided into two, one is to write directly in the form of the form, and the other is to change the table structure after the construction of the table.

Write directly in the table statement:

Create Table Name (Field Name 1 INT Not Null, Field Name 2 NVARCHAR (13) Not Null Primary Key (Field Name 1, Field Name 2), Field Name 3 .........................

After the table, change the table structure:

Create Table Name (Field Name 1 Int Not Null, Field Name 2 NVARCHAR (13) Not Null Field Name 3 ............ 字 名 N ............) Go

ALTER TABLE Name with nocheck add constraint [PK_ Table Name] Primary Key Nonclustered ([Field Name 1], [Field Name 2]) GO

转载请注明原文地址:https://www.9cbs.com/read-4389.html

New Post(0)