SQL PRIMARY Key constraint instance and PRIMARY key syntax

Source: Internet
Author: User
Tags table definition sql primary key


Define a table-level PRIMARY KEY constraint named Pk_ks when creating an exam basic table (school number, course number, score), the main code is a
CJ Int,
Results int Privary Key

)
Attribute group (School number, course number)
CREATE TABLE Pk_ks (
Constraint Pk_ks primary KEY (XH,KCH)
)
KCH int,
ID int,
Best Answer CREATE TABLE KSJCB (XH int,
CourseID int,

The PRIMARY KEY constraint uniquely identifies each record in the Database tutorial table.

The primary key must contain a unique value.

Primary key columns cannot contain NULL values.

Each table should have a primary key, and each table can have only one primary key.


Okay, now let's see. Create and modify PRIMARY KEY constraints

You can create a single PRIMARY KEY constraint as part of a table definition when you create a table. If the table already exists and there is no PRIMARY key constraint, you can add a PRIMARY key constraint. A table can have only one PRIMARY KEY constraint.

If a PRIMARY KEY constraint already exists, you can modify or delete it. For example, you can make the table's PRIMARY key constraint reference other columns, change the column's order, index name, aggregation options, or fill factor for the PRIMARY key constraint. However, you cannot change the length of the column defined using the PRIMARY KEY constraint.

Attention:
To modify a PRIMARY key constraint, you must first delete the existing PRIMARY key constraint, and then re-create the constraint with the new definition.


When you add a PRIMARY key constraint to an existing column in a table, the SQL Server 2005 database engine checks the data and metadata of the existing columns to ensure that the primary key complies with the following rules:

column is not allowed to have null values.
The specified PRIMARY KEY constraint column is implicitly converted to not NULL when the table is created.

Cannot have duplicate values.
If you add a PRIMARY KEY constraint for a column that has duplicate values or allow null values, the database engine returns an error without adding a constraint.

You cannot add a PRIMARY KEY constraint that violates the above rules.

The database engine automatically creates a unique index to enforce the uniqueness requirements for PRIMARY KEY constraints. If a clustered index does not exist in the table or does not explicitly specify a nonclustered index, a unique clustered index is created to enforce the PRIMARY KEY constraint.

You cannot delete the PRIMARY KEY constraint if the following conditions are true:

If the FOREIGN key constraint in another table references a PRIMARY key constraint, the FOREIGN key constraint must be deleted first.

Table contains PRIMARY XML indexes that apply to itself

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.