Oracle syntax example for adding table constraints

Source: Internet
Author: User

Transferred from:http://jingyan.baidu.com/article/f54ae2fccda68d1e93b84942.html

Example:

--Class table

CREATE TABLE   number VARCHAR2 (not NULL);

--Add a primary key to the Tclass table

ALTER TABLE ADD CONSTRAINT  PRIMARYKEY(cl_id);

---student table

CREATE TABLESTUDENT (st_idVARCHAR2( -),--Single-line comment primary keySt_codeVARCHAR2( -),--UNIQUE ConstraintSt_nameVARCHAR2( -),--non-null constraintSt_age Number, St_birthday DATE,--Default ConstraintsSt_sexVARCHAR2(4),--Add a Check constraintcl_id Number            --Add a foreign key (the primary key of the primary table is referenced));

--Add primary key to student table

ALTER TABLE ADD CONSTRAINT PRIMARY KEY (st_id);

--Give Name list non-empty constraint

ALTER TABLE  CONSTRAINTnotNULL;

--Add a unique constraint to St_code

  

ALTER TABLE ADD CONSTRAINT UNIQUE (St_code);

  

--st_sex can only be male or female

ALTER TABLE ADD CONSTRAINT CHECK (St_sex=' male 'OR st_sex=' female ' );

--st_birthday Default to Sysdate

ALTER TABLE DEFAULT Sysdate;

---adding foreign keys

ALTER TABLE ADD CONSTRAINT FOREIGN KEY REFERENCES Tclass (cl_id);

Oracle syntax example for adding table constraints

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.