Oracle【二维表管理:约束】

1、简单的表创建和字段类型
最简单的方式去创建表(没有添加主键之类的约束条件)
【Oracle的字段类型】
number:数值类型
--整数类型:number(a) 总长度a
--小数类型:number(a,b) 总长度a,小数长度b,小数可缺省
varchar2:字符类型
--字符类型 varchar2(ln) ln表示字符的最大长度,实际存储内存的长度<=ln
--特点:多态分配存储空间,节省空间
char:字符类型
--字符类型char(ln) 不管字符数据长度多大,直接在内存开辟ln大小的空间存储数据
--特点:存储效率高于varchar2
date:日期类型
创建表最基本语法:

create table 表名(
     字段名 类型,
     字段名 类型,
     字段名 类型,
     ........
     字段名 类型  --最后一个字段不要加逗号
    );

创建一张学生表并添加n条测试数据:

--创建一张学生表
create table student(
  sno number(10),
  sname varchar2(50),
  sage number(3),
  ssex char(4),
  sbirth date,
  sjob varchar2(100)
);
--添加测试数据
insert into student values(1,'小喜庆','女',18,'7-10月-18','学生');
insert into student values(2,'迪丽热巴','女',27,to_date('1992-06-03','yyyy-mm-dd'),'演员');
--删除表:drop table 表名;
drop table student;



2、二维表创建约束学习:(主键约束,非空约束,检查约束,唯一约束)

 1 --删除表:drop table 表名;
 2 drop table student;
 3 
 4 select * from student;
 5 --创建简单的学生表
 6 create table student(
 7   sno number(15), --primary key, --添加主键约束_1
 8   sname varchar2(50),-- not null,--添加非空约束_1
 9   sex varchar2(4),--check(sex='男' or sex='女'),--添加检查约束
10   sage number(3),-- check(sage>0 and sag<=200),--添加检查约束_1
11   sbirth date,
12   phone number(20) -- 添加唯一约束 unique
13   --constraints pk_student_sno primary key(sno) --添加主键约束_2
14   --constraints ck_student_sname check(sname is not null) --添加非空约束_2
15   --constraints ck_student_sage check(sage>0 and sage<=200)--添加检查约束_2
16   --constraints un_student_phone unique(phone)--唯一约束
17   
18 );
19 ----------------------------------创建表后在去创建约束---------------------------------------
20 --添加主键约束
21 alter table student add constraints pk_student_sno primary key(sno);--添加主键约束_3
22 alter table student drop  constraints pk_student_sno;--删除主键约束
23 --添加非空约束
24 alter table student add  constraints ck_student_sname check(sname is not null) --添加非空约束_3
25 alter table student drop  constraints ck_student_sname;--删除非空约束
26 --添加检查约束
27 alter table student add constraints ck_student_sage check(sage>0 and sag<=200)--添加检查约束_3
28 alter table student drop  constraints ck_student_sage; --删除检查约束
29 --添加唯一约束
30 alter table student add constraints un_student_phone unique(phone)--唯一约束
31 
32 
33 
34 --添加测试数据
35 insert into student values(2018112001,'小喜庆','',18,'7-10月-18',10086);
36 
37 ----------------------------------没有添加约束存在的一些问题---------------------------------------
38 ---Oracle表约束【没有添加约束存在的一些问题】
39 --问题1:字段(学号)可重复添加  
40 insert into student values(2018112001,'迪丽热巴','',27,to_date('1992-06-03','yyyy-mm-dd'),10010);
41 --解决1:使用主键 primary key,特点:非空唯一
42 添加主键方法:1:直接在创建表的字段后使用关键字 primary key
43              2:在创建表的语句的最后面使用 constraints pk_表名_字段名 primary key(字段名)
44              3:在创建表后使用 alter table 表名 add  constraints pk_表名_字段名 primary key(字段名);
45              4:删除主键 alter table student drop  constraints 主键的约束名;
46              
47 --问题2:必填的字段(sname)可以为空
48 insert into student values(2018112001,'','',27,to_date('1992-06-03','yyyy-mm-dd'),10010);
49 --解决2:使用非空约束 not null
50 添加非空约束:1:直接在创建表的字段后使用关键字 not null
51              2:在创建表的语句的最后面使用 constraints ck_表名_字段名 check(字段名 is not null)
52              3:在创建表后使用 alter table 表名 add  constraints ck_表名_字段名 check(字段名 is not null);
53              4:删除非空约束 alter table student drop  constraints 非空约束名;
54 
55 --问题3:违背了自然定律
56 insert into student values(2018112001,'小喜庆','',218,'7-10月-18',10086);
57 --解决3:使用检查约束
58 添加非空约束:1:直接在创建表的字段后使用 check(条件) 例如      sage number(3) check(sage>0 and sag<=200),
59              2:在创建表的语句的最后面使用 constraints ck_表名_字段名 check(条件)
60              3:在创建表后使用 alter table 表名 add  constraints ck_表名_字段名 check(条件);
61              4:删除检查约束 alter table student drop  constraints 检查约束名;
62 --问题4:手机号可以重复
63 insert into student values(2018112001,'小喜庆','',218,'7-10月-18',10086);
64 --问题4:使用唯一约束
65 添加非空约束:1:直接在创建表的字段后使用 unique
66              2:在创建表的语句后面使用 constraints un_表名_字段名 unique(字段名);
67              3:在创建表后使用 alter table 表名 add  constraints un_表名_字段名 unique(字段名);
68              4:删除约束:alter table 表名 drop  constraints 唯一约束名;
View Code
--创建表并同时添加约束
create table student(
  sno number(15), 
  sname varchar2(50),
  sex varchar2(4),
  sage number(3),
  sbirth date,
  phone number(20),
  constraints pk_student_sno primary key(sno), --添加主键约束
  constraints ck_student_sname check(sname is not null), --添加非空约束
  constraints ck_student_sage check(sage>0 and sage<=200),--添加检查约束
  constraints un_student_phone unique(phone)--唯一约束
  
);

--添加测试数据
insert into student values(2018112001,'小喜庆','女',18,'7-10月-18',10086);
insert into student values(2018112002,'迪丽热巴','女',27,to_date('1992-06-03','yyyy-mm-dd'),10010);

3、二维表创建约束学习:外键约束

 1 --学生信息表
 2 create table student (
 3       sid number(10) primary key,
 4       sname varchar2(50) not null,
 5       ssex char(4) check(ssex='' or ssex=''),
 6       sage number(3) check(sage>=0 and sage<=200),
 7       sqq number(20) unique,
 8       cno number(10) references class(cno)
 9 );
10 --添加数据
11 insert into student values(1,'迪丽热巴','',27,13245668,1);
12 insert into student values(2,'游戏解说柚子','',24,11545668,1);
13 insert into student values(3,'杰西','',22,135668,2);
14 insert into student values(4,'杰克','',21,1323268,2);
15 
16 --班级表
17 create table class (
18       cno number(10) primary key,
19       cname varchar2(50) not null,
20       cdesc varchar2(50)
21 );
22 insert into class values(1,'计算机1班','明星班');
23 insert into class values(2,'计算机2班','游戏解说班');


测试两张表是否关联成功

-- 查询姓名,班级编号,和班级名称
select s.sname,c.cno,c.cname
from student s
inner join class c
on s.cno=c.cno; 

--问题:可以在学生表中插入一个不存在的班级
insert into student values(5,'杰克','男',21,1268,3);

解决:使用外键:作用:当在子表中插入数据,在父表中不存在,自动报错
概念:当一张表的某个字段的值需要依赖另外一张表的某个字段的值,则使用外键约束。
   其中主动依赖的表称为子表,被依赖的表称为父表。外键加在子表中。
使用方法:
  在子表中的字段后直接使用 references 父表名(字段) 例如: cno number(10) references class(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。
      --注意:子表依赖字段不能添加非空约束。

原文地址:https://www.cnblogs.com/cao-yin/p/9751065.html