Oracle database Add, query, delete primary KEY constraints

Source: Internet
Author: User

A PRIMARY KEY constraint has only one in each table, but a PRIMARY key constraint can consist of multiple columns

You can create a PRIMARY KEY constraint when you create a table, or you can add a primary key constraint by using the ALTER statement

1: Add a PRIMARY KEY constraint

"1"

Syntax :

CREATE Table table name (field name primary key, field name ...);

Example:

CREATE TABLE Student (ID number primary key,name varchar), age number (3), email varchar2 (50),);


"2"

Syntax :

CREATE Table table name (field name, field name ...), [CONSTRAINT constraint name] primary key (field name, field name)--one column only needs to write one, multiple columns specify primary key at the same time, separated by ', ' number;

Example:

CREATE TABLE Student (ID number, name varchar), age number (3), email varchar2, primary key (Id,name )--one column only needs to write one, multiple columns simultaneously specify the primary key, separated by the ', ' number;

"3"

Syntax :

ALTER table name ADD CONSTRAINT constraint name PRIMARY KEY (field name);

Example:

ALTER TABLE STUDENT ADD CONSTRAINT pk_id PRIMARY KEY (ID);


2: Querying PRIMARY KEY constraints

Syntax:

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

Example:

Select cu.* from User_cons_columns cu, user_constraints au where cu.constraint_name = Au.constraint_name and Au.constraint _type = ' P ' and au.table_name = ' STUDENT ';


3: Delete primary KEY constraint

Grammar:

ALTER TABLE table_name DROP CONSTRAINT constraint_name;

Example:

To add a primary key when creating a table, first query out the primary key name, then delete

ALTER TABLE STUDENT DROP CONSTRAINT pk_stu_id;



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

Oracle database Add, query, delete primary KEY constraints

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.