--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