Oracle primary key, non-null, check, unique, default, FOREIGN KEY constraint

Source: Internet
Author: User

--First Add PRIMARY KEY constraint
ALTER TABLE student
Add constraint Pk_student_sno primary key (SNO)

--Delete Constraint
ALTER TABLE student
Drop constraint Pk_student_sno

--not NULL
ALTER TABLE student
Modify (sname varchar2 (+) NOT NULL)

--check CHECK Constraints
ALTER TABLE student
Add constraint ck_student_sex check (sex = ' male ' or sex = ' female ')

--Default constraints
ALTER TABLE student
Modify (Address varchar2 () default ' Hunan Software Evaluation Center ')

--Unique constraint
ALTER TABLE student
Add constraint Uq_student_cardid unique (cardid)


--FOREIGN KEY constraint
ALTER TABLE Score
Add constraint Fk_student_score foreign key (SNO) references student (SNO)

Or

--Method One: Write the constraint directly behind the field
CREATE TABLE Student
(
Sno Int PRIMARY key,--primary key
Sname varchar2 () NOT null,--non-empty
Sex VARCHAR2 (2) Check (sex in (' Male ', ' female ')),--check (sex = ' male ' or sex= ' female '),
Address varchar2 default ' Hunan Changsha ',--defaults constraint
Cardid varchar2 () unique NOT null-unique
)


--Method Two: Write the constraint after writing all the fields.
CREATE TABLE Test
(
Sno Int,
Sname varchar2 (20),
Constraint Pk_test primary KEY (SNO)
)

--FOREIGN KEY constraint
--(when creating a foreign key constraint while creating a table in Oracle, it is not necessary to add foreign key if the constraint is written directly behind a single field)
--(when creating a foreign key constraint while creating a table in Oracle, if the constraint is written directly after all fields, you need to add foreign key)
CREATE TABLE Score
(
Sno int references student (SNO),
CNO int,
Grade float,
Constraint Pk_score primary KEY (SNO,CNO),
Constraint Fk_student_score foreign KEY (CNO) References course (CNO)
)

Oracle primary key, non-null, check, unique, default, FOREIGN KEY 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.