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