SQL UNIQUE Constraint Instance Tutorial

Source: Internet
Author: User

Unique constraints of the SQL
Unique identification of unique constraints per record in a database table.

Both the uniqueness and primary constraints provide the guarantee of a single column or set of columns.

A PRIMARY KEY constraint automatically has a unique constraint that defines it.

Please note that you can have many unique restrictions on each table, but only one primary KEY constraint per seat.


Unique SQL constraints Create a table
When the following SQL creates a "p_id" column with a unique constraint, "person" is creating a table:

CREATE TABLE Persons
(
p_id int not null,
LastName varchar (255) is not null 
 , FirstName varchar (255), Address
varchar (255), City
varchar (255),
UNIQUE (p_id
)

SQL Server/oracle/ms Access:

CREATE TABLE Persons
(
p_id int not NULL UNIQUE,
LastName varchar (255) Not null,< c17>
FirstName varchar (255), Address
varchar (255), City
varchar (255)
)
   
   

To make naming a unique constraint and to identify multiple columns on the unique constraints, use the following SQL syntax:

Mysql/sql Server/oracle/ms Access:

 CREATE TABLE Persons   (p_id int not null,   LastName varchar (255) NOT NULL,   FirstName varcha R (255), address varchar (255), City varchar (255), CONSTRAINT Uc_personid UNIQUE (p_id,lastname)) 
 
      
      
Database Unique constraints Change table
When you create a "p_id" column for a unique constraint, the table is already established, use the following sql:

 alter TAB LE Persons ADD Unique (p_id) 
   
 
        
        

To make naming a unique constraint, and determined that multiple columns are on a unique constraint, use the following SQL syntax:

 

 ALTER TABLE Persons ADD CONSTRAINT uc_personid Unique (p_id,lastna Me) 
   
 
          
          

Drop uniqueness a unique constraint
abort a unique about Bundle, use the following sql:

 

 ALTER TABLE Persons DROP INDEX uc_personid 
   
 

SQL server/oracle/ms Access:

 ALTER TABLE Persons DROP CONSTRAINT uc_personid< /pre> 

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.