oracle数据库学习相关笔记-相关约束

1、主键约束的三种方式java

1.在字段后加primary key  约束 id  varchar(32) primary key
2.在表建立好以后添加外键约束 alter table  student  add  constraints pk_student_id PRIMARY key(id);
3.在建立表的语句的最后面使用 constraints pk_表名_字段名 primary key(字段名)python

4.删除主键约束alter  table  student drop constraints  pk_student_id;sql

验证流程:学习

1-1.建立一张表,在id后加上主键约束 primary key测试

create table student(
	id  varchar(32) primary key,
    name  varchar(32) not null
)

1-2.插入两条数据code

insert into student(id,name) values('1','张三');
insert into student(id,name) values('1','李四');

1-3.报错以下blog

1-4.删除表,而后从新建立ci

-- 删除表
drop table student;
-- 从新建立
create table student(
	id  varchar(32),
    name  varchar(32) not null,
	constraints pk_student_id PRIMARY key(id)
)

1-5.插入原来的两条数据table

1-6.报错以下class

1-7.删除表,而后从新建立

​drop table student;

​create table student(
    id  varchar(32),
    name  varchar(32) not null
)

1-8.修改表的外键约束

alter table  student  add  constraints pk_student_id PRIMARY key(id);

1-9.插入原来的两条数据,报一样的错误

2、检查约束的三种方式

1.直接在建立表的字段后使用 check(条件) 例如      sage number(3) check(sage<150 and sage>0),
2.在建立表的语句的最后面使用 constraints ck_表名_字段名 check(条件)
3.在建立表后使用 alter table 表名 add  constraints ck_表名_字段名 check(条件);
4.删除检查约束 alter table student drop  constraints 检查约束名;

3、非空约束的三种方式

1.直接在建立表的字段后使用 not null 关键字 
2.在建立表的语句的最后面使用 constraints ck_表名_字段名 check(字段名 is not null)
3.在建立表后使用 alter table 表名 add  constraints ck_表名_字段名 check(字段名 is not null);
4.删除非空约束 alter table student drop  constraints 非空约束名;

4、惟一约束的三种方式

1.直接在建立表的字段后使用 unique
2.在建立表的语句后面使用 constraints un_表名_字段名 unique(字段名);
3.在建立表后使用 alter table 表名 add  constraints un_表名_字段名 unique(字段名);
4.删除约束:alter table 表名 drop  constraints 惟一约束名;

5、外键约束

--二维表建立 外键约束学习:
   --建立学生表
   create table student(
         sno number(10) primary key,
         sname varchar2(100) not null,
         sage number(3) check(sage>0 and sage<150),
         ssex char(4) check(ssex='男' or ssex='女'),
         sfav varchar2(500),
         sqq varchar2(30) unique,
         cid number(10) --references clazz(cno)
         --constraints fk_student_cid foreign key(cid) references clazz(cno)--外键
      )
      --添加外键
      alter  table student add constraints fk_student_cid foreign key(cid) references clazz(cno) on delete set null
      alter  table student drop constraints fk_student_cid 
      drop table student
      --添加测试数据
      insert into student values(1,'张三001',18,'男','唱歌','657889900',1);
      insert into student values(2,'张三002',18,'男','唱歌','657889901',1);
      insert into student values(3,'李四001',18,'男','唱歌','657889903',2);
      insert into student values(4,'李四002',18,'男','唱歌','657889904',2);
   --建立班级表
      create table clazz(
       cno number(10) primary key,
       cname varchar2(100) not null,
       cdesc varchar2(300)
      
      )
      --添加测试数据
       insert into clazz values(1,'java','6666');
       insert into clazz values(2,'python','33333');
  --查询学生及其班级信息
      select * from student  s
      inner join clazz c
      on s.cno=c.cno
  --问题:居然能够在学生表中插入一个不存在班级
      insert into student values(5,'李四003',18,'男','唱歌','657889905',3);
  --使用外键:
      --做用:当在子表中插入的数据在父表中不存在,则会自动报错。
      --概念:当一张表的某个字段的值须要依赖另一张表的某个字段的值,则使用外键约束。
             --其中主动依赖的表称为子表,被依赖的表称为父表。外键加在子表中。
      --使用: 
             --在子表中的字段后直接使用   references 父表名(字段) 例如: cid number(10) references clazz(cno)
             --在建立表语句的最后面使用  constraints fk_子表名_字段名 foreign key(字段名) references 父表名(字段名)
             --在建立表后使用:alter table 表名 add constraints fk_子表名_字段名 foreign key(字段名) references 父表名(字段名)
            --删除外键:alter table 表名 drop constraints 外键约束名
      --外键选取:
            --通常选取父表的主键做为子表的外键。
      --外键的缺点:
            --没法直接删除父表数据,除非级联删除
            --级联删除:在添加外键约束时,使用关键字 on delete cascade
                     --使用:当删除父表数据时,自动删除子表相关全部数据。
                     --缺点:没法保留子表历史数据。
                     --使用关键字 on delete set null
                           --删除父表数据时,将子表中的依赖字段的值设置为null。
                           --注意:子表依赖字段不能添加非空约束。
           --删除班级1的信息
           select * from student
           delete from clazz where cno=1
相关文章
相关标签/搜索