oracle--约束(主键、非空、检查)
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 | 问题1:学号重复了,数据还可以插入成功 使用主键约束:学号是唯一标识一条数据的,所以必须唯一且不能为空 ---(1)、在确定为主键的字段后添加 primary key关键字 ---(2)、在创建表的后面使用:constraints pk_表名_字段名 primary key(字段名) --(3)、在创建表后使用 alter table 表名 add constraints pk_表名_字段名 primary key(字段名); --删除主键:alter table 表名 drop constraints pk_表名_字段名 问题2:姓名可以为空。 使用非空约束 ---(1)、创建表的时候在字段后面添加not null ---(2)、在创建表字段后使用 constraints ck_表名_字段名 check(字段名 is not null) 了解 --(3)、在创建表后使用alter table 表名 modify 字段名 类型 not null; ---(4)、修改字段可以存储空值:alter table 表名 modify 字段名 类型 null; 问题3:性别不但可以为空,还可以为其他不知道的字符 使用检查约束 ---(1)、创建表的时候在字段后使用 default 值 check(条件), ---------但是会允许空值的出现,并且默认值只有在字段不声明的情况下生效 ---(2)、在创建表所有字段后使用:constraints ck_表名_字段名 check(条件) ---(3)、在创建表后使用:alter table 表名 add constraints ck_表名_字段名 check(条件) 问题4:年龄可以超过200 --使用检查约束条件 问题5:qq号一致 使用唯一约束 --(1)、在字段后直接使用unique关键字 --(2)、在所有字段后使用:constraints uk_表名_字段名 unique(字段名) --(3)、 alter table 表名 add constraints uk_表名_字段名 unique(字段名) --删除唯一约束:alter table 表名 drop constraints uk_表名_字段名 |
使用外键约束
--(1)、在字段后使用 references 参照表表名(参照字段)
--(2)、在所有字段后使用 constraints fk_表名_字段名 foreign key(字段名) references 参照表名(参照字段名)
--(3)、在创建表后使用alter table 表名 add constraints fk_表名_字段名 foreign key(字段名) references 参照表名(参照字段名)
--删除外键 alter table 表名 drop constraints fk_表名_字段名
在删除父表数据的时候需要先删除子表数据?
解决方式1:先解除主外键关联,然后删除数据,再然后添加主外键关联
解决方式2:在创建外键的时候使用级联操作。
--在创建外键时 on delete cascade
--在创建外键时 on delete set null
怎么选取外键?
一般将主表的主键作为子表的外键
外键的值能为 not null? 不建议在外键后使用非空约束
1、主键约束
三种方式主键约束方式
1 2 3 4 5 6 7 8 | create table student( sno number(10) primary key, sname varchar2(100), sage number(3), ssex char(4), sbirth date, sqq varchar2(30) ); |
1 2 3 4 5 6 7 8 9 | create table student( sno number(10), sname varchar2(100), sage number(3), ssex char(4), sbirth date, sqq varchar2(30), constraint pk_student_sno primary key(sno) ---添加主键约束 ); |
1 2 3 4 5 6 7 8 | create table student( sno number(10), sname varchar2(100), sage number(3), ssex char(4), sbirth date, sqq varchar2(30) );alter table student add constraint pk_student_sno primary key(sno);alter table student drop constraint pk_student_sno; |
1 2 | select * from student for update; drop table student; |
非空约束
三种方式
1 2 3 4 5 6 7 8 | create table student( sno number(10) primary key, sname varchar2(100) not null, sage number(3), ssex char(4), sbirth date, sqq varchar2(30) ); |
1 2 3 4 5 6 7 8 9 | create table student( sno number(10) primary key, sname varchar2(100), sage number(3), ssex char(4), sbirth date, sqq varchar2(30), constraints ck_student_sname check(sname is not null) ); |
1 2 3 4 5 6 7 8 9 | create table student( sno number(10) primary key, sname varchar2(100), sage number(3), ssex char(4), sbirth date, sqq varchar2(30) ); |
alter table student add constraint ch_student_sname check(sname is not null);
1 | alter table student drop constraint ch_student_sname |
检查约束
1 2 3 4 5 6 7 8 | create table student( sno number(10) primary key, sname varchar2(100) not null, sage number(3) check(sage< 150 and sage>0), ssex char(4) check(ssex ='男' or ssex = '女'), sbirth date, sqq varchar2(30) ); |
1 2 3 4 5 6 7 8 9 | create table student( sno number(10) primary key, sname varchar2(100) not null, sage number(3), ssex char(4) check(ssex ='男' or ssex = '女'), sbirth date, sqq varchar2(30), constraint ch_student_sage check(sage< 150 and sage>0) ); |
1 2 3 4 5 6 7 8 | create table student( sno number(10) primary key, sname varchar2(100) not null, sage number(3), ssex char(4) check(ssex ='男' or ssex = '女'), sbirth date, sqq varchar2(30) ); |
alter table student add constraint ch_student_sage check(sage<150 and sage>0);
1 | alter table student drop constraint ch_student_sage; |
唯一约束
1 2 3 4 5 6 7 8 | create table student( sno number(10) primary key, sname varchar2(100) not null, sage number(3) check(sage>1 and sage < 200) , ssex char(4) check(ssex ='男' or ssex = '女'), sbirth date, sqq varchar2(30) unique ); |
插入俩次报错
1 | insert into student values(6,'关晓彤003',18,'女',to_date('1998-05-26','yyyy-mm-dd'),'989'); |
1 2 3 4 5 6 7 8 9 | create table student( sno number(10) primary key, sname varchar2(100) not null, sage number(3) check(sage>1 and sage < 200) , ssex char(4) check(ssex ='男' or ssex = '女'), sbirth date, sqq varchar2(30), constraint ch_student_sqq unique(sqq) ); |
1 | 第三种 |
1 2 3 4 5 6 7 8 | create table student( sno number(10) primary key, sname varchar2(100) not null, sage number(3) check(sage>1 and sage < 200) , ssex char(4) check(ssex ='男' or ssex = '女'), sbirth date, sqq varchar2(30) ); |
1 | alter table student add constraint un_student_sqq unique(sqq); |
外键约束
选取--一般选取 父表的主键做为字表的外键
缺点--无法删除班级3的信息
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 | create table student( sno number(10) primary key, sname varchar2(100) not null, sage number(3) check(sage>1 and sage < 200) , ssex char(4) check(ssex ='男' or ssex = '女'), sbirth date, sqq varchar2(30) unique, cno number(10) references clazz(cno) --可以起不同的名,是映射关系 ); create table clazz ( cno number(10) primary key, cname varchar(100) not null, cdesc varchar(200) ); insert into student values(11,'关晓彤003',18,'女',to_date('1998-05-26','yyyy-mm-dd'),'11',1); insert into student values(12,'关晓彤003',18,'女',to_date('1998-05-26','yyyy-mm-dd'),'22',1); insert into student values(13,'关晓彤003',18,'女',to_date('1998-05-26','yyyy-mm-dd'),'333',2); insert into student values(14,'关晓彤003',18,'女',to_date('1998-05-26','yyyy-mm-dd'),'444',2); insert into clazz values(1,'java','java'); insert into clazz values(2,'pathon','pathon'); select * from student s inner join clazz c on s.cno = c.cno; insert into student values(15,'关晓彤003',18,'女',to_date('1998-05-26','yyyy-mm-dd'),'55',3); |
1 2 3 4 5 6 7 8 9 10 | create table student( sno number(10) primary key, sname varchar2(100) not null, sage number(3) check(sage>1 and sage < 200) , ssex char(4) check(ssex ='男' or ssex = '女'), sbirth date, sqq varchar2(30) unique, cno number(10),--可以起不同的名,是映射关系 constraint fk_student_cno foreign key(cno) references clazz(cno) ); |
1 2 3 4 5 6 7 8 9 | create table student( sno number(10) primary key, sname varchar2(100) not null, sage number(3) check(sage>1 and sage < 200) , ssex char(4) check(ssex ='男' or ssex = '女'), sbirth date, sqq varchar2(30) unique, cno number(10)--可以起不同的名,是映射关系 ); |
alter table student add constraint fk_student_cno foreign key(cno) references clazz(cno);
alter table student drop constraint fk_student_cno;
1 | on delete cascade 删除父表时候,字表一起删除 |
1 | delete set null 删除父表时候,字表设置为null |
1 2 | alter table student add constraint fk_student_cno foreign key(cno) references clazz(cno) on delete cascade; alter table student add constraint fk_student_cno foreign key(cno) references clazz(cno) on delete set null; |
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】凌霞软件回馈社区,博客园 & 1Panel & Halo 联合会员上线
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】博客园社区专享云产品让利特惠,阿里云新客6.5折上折
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步