Primary key constraints of the primary key database

Source: Internet
Author: User

Primary KEY constraints of the rimary key database

Database primary key constraints
The primary key constraint uniquely identifies each record in a database table.

The primary key must contain unique values.

The primary key column cannot contain null values.

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

Create Table with database primary key constraints
When the following SQL statement creates the "P_Id" column on a primary key, "person" creates a table:

MySQL:

Create table Persons
(
P_Id int not null,
LastName varchar (255) not null,
FirstName varchar (255 ),
Address varchar (255 ),
City varchar (255 ),
Primary key (P_Id)
)
SQL Server/Oracle/MS Access:

Create table Persons
(
P_Id int not null primary key,
LastName varchar (255) not null,
FirstName varchar (255 ),
Address varchar (255 ),
City varchar (255)
)
 
Use the following SQL Syntax: CREATE TABLE Persons
(
P_Id int not null,
LastName varchar (255) not null,
FirstName varchar (255 ),
Address varchar (255 ),
City varchar (255 ),
CONSTRAINT pk_PersonID primary key (P_Id, LastName)
)
When you want to create a "P_Id" column for the primary key constraint of a database to change the table, the table has been created. Use the following SQL:

SQL Server/Oracle/MS Access:

Alter table Persons
Add primary key (P_Id)
 
To make the primary key constraint of the name and determine a primary key constraint for multiple columns, use the following SQL Syntax:

SQL Server/Oracle/MS Access:

Alter table Persons
Add constraint pk_PersonID primary key (P_Id, LastName)
 
Note: If you use the alter table statement to add a primary key, the primary key column (Sunday) must have been declared as not containing a null value (the first time the TABLE was created ). One of the key constraints for dropping is to discard a primary key constraint and use the following SQL: MySQL: ALTER TABLE Persons
DROP PRIMARY KEY
SQL Server/Oracle/MS Access:

Alter table Persons
Drop constraint pk_PersonID

Related Article

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.