Oracle database Add, query, delete unique constraint

Source: Internet
Author: User

A unique constraint is called a unique constraint, and you can set the field values entered in the table to be unique, similar to the primary key, except that the unique constraint can have more than one, and the primary key can only have a field that is not a primary key but is guaranteed to be unique, it is possible to suggest a unique constraint

(1) Adding a unique constraint

"1" adds a unique constraint while creating a table

Add a unique constraint when creating a table, after creating the table

Grammar:

CONSTRAINT Constraint name UNIQUE (field name)

Example:

create table orderinfo  (              orderid varchar2 (Ten),              CUSTOMID VARCHAR2 (,      )        productid varchar2 (Ten),              orderdate varchar2 (Ten),              orderquality varchar2 (Ten),              senddate varchar2 (Ten),              [constraint unq_orderid] unique (ORDERID)           ); 

"2" adds a unique constraint using alter

Grammar:

ALTER table name ADD CONSTRAINT constraint name unique (field name)

Example:

ALTER TABLE ORDERINFO ADD CONSTRAINT unq_orderid UNIQUE (ORDERID);

(2) View UNIQUE constraints

Syntax:

Select cu.* from User_cons_columns cu, user_constraints au where cu.constraint_name = Au.constraint_name and Au.constrai Nt_type = ' U ' and au.table_name = ' table name ';

Example:

Select cu.* from User_cons_columns cu, user_constraints au where cu.constraint_name = Au.constraint_name and Au.constrai Nt_type = ' U ' and au.table_name = ' ORDERINFO ';


(3) Delete Unique constraint

Syntax:

ALTER TABLE table_name DROP CONSTRAINT constraint_name;

Example:

--delete the unique constraint for the Order Information table

ALTER TABLE ORDERINFO DROP CONSTRAINT Unq_orderid;



This article is from the "Loly_zhang" blog, make sure to keep this source http://lolyzhang.blog.51cto.com/10029387/1889005

Oracle database Add, query, delete unique constraint

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.