--首先添加主鍵約束
alter table student
add constraint PK_student_sno primary key(sno)
--刪除約束
alter table student
drop constraint PK_student_sno
--not null
alter table student
modify (sname varchar2(30) not null)
--check 檢查約束
alter table student
add constraint CK_student_sex check(sex = '男' or sex = '女')
--默認約束
alter table student
modify (address varchar2(20) default '湖南軟件評測中心')
--唯一約束
alter table student
add constraint UQ_student_cardid unique(cardid)
--外鍵約束
alter table score
add constraint FK_student_score foreign key(sno) references student(sno)
或者
--方式一:直接將約束寫在字段的后面
create table student
(
sno int primary key,--主鍵
sname varchar2(20) not null,--非空
sex varchar2(2) check(sex in ('男','女')),--check(sex ='男'or sex='女'),
address varchar2(20) default '湖南長沙',--默認約束
cardid varchar2(20) unique not null --唯一
)
--方式二:將所有字段寫好后在來寫約束
create table test
(
sno int ,
sname varchar2(20),
constraint PK_TEST primary key(sno)
)
-- 外鍵約束
--(oracle里面創建表的同時創建外鍵約束的時候如果是將約束直接寫在單個的字段后面是不需要加foreign key)
--(oracle里面創建表的同時創建外鍵約束的時候如果是將約束直接寫在所有的字段后面是需要加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)
)