SQL PRIMARY KEY instance tutorial

Source: Internet
Author: User
Tags sql primary key

SQL PRIMARY KEY instance tutorial

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 ))

Change table with database primary key constraints
To create a column named "P_Id" for the primary key constraint, the table has been created. Use the following SQL statement:

 

Alter table PersonsADD CONSTRAINT pk_PersonID primary key (P_Id, LastName)
 
Look at mysql

MySQL:

Alter table PersonsDROP PRIMARY KEY

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.