Oracle about defining constraints/modifying table structures/modifying constraints

Source: Internet
Author: User

There are 5 types of---Constraints: PRIMARY key foreign key unique non-NULL check 5 class constraints

Column-level constraints and table-level constraints in Oracle

Column-level constraints: Columns are then added to the constraint when the table is created

For example:

CREATE TABLE Table11 (Stuno number (2) constraint TABLE_PK primary key,tname varchar2 (5) unique--unique constraint)

The above two fields are the constraints written directly above the columns: Optional [constraint TABLE_PK] primary key,

Table-level constraints:

For example:

--table-level constraint CREATE TABLE Table22 (Idno number (2), Tname varchar2 (5) unique,--UNIQUE constraint price number (5,2) check (price>100),-- Check constraint Stuno number (2),    --below the table-level constraint is the constraint TB_2_PK primary key (Idno) under all the shoes columns, constraint TB_2_FK foreign key ( Stuno) references Table11 (Stuno))

Add a constraint called table-level constraint below all fields

----------------Modifying a table

ALTER TABLE TABLE22 drop [column name,,,]--delete column ALTER TABLE TABLE22 add column name type [default value]--add column ALTER TABLE table22 modify column name Type [default]--Modify Column

--------Adding table-level constraints

ALTER TABLE TABLENAMEADD constraint TABLE_PK primary key (ID);--Add primary key ALTER TABLE Tablenamedrop table_pk;--Delete primary key

------To modify a constraint on a column

ALTER TABLE tablename Modify column name constraint Notnull not null;--add non-null constraint ALTER TABLE tablename modify column name constraint Check2 CHEC K (column name >1200);--check constraint

-----To remove a constraint for a column

ALTER TABLE tname  DROP constraint constraint name;--delete ALTER TABLE Tname  modify column name by constraint name null;--Delete non-null constraint

  

  

  

  

Oracle about defining constraints/modifying table structures/modifying 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.