10.二维表管理(创建&约束&字段)

简单二维表的创建

使用:create table 表名(字段名 类型,字段名 类型,….);

数据类型:

  • number类型
    • 数值类型
    • 整数类型
      number(a):总长度为a
    • 浮点数类型
      number(a,b):总长度为a,小数位长度为b,小数位可以不写。
  • 字符类型
    • varchar2(ln)
      ln表示字符的最大长度,实际存储内存长度是根据字符大小来分配,但是最大不能超过ln
      特点:动态分配存储空间,节省空间
    • char(ln)
      不管字符数据长度是多大,直接开辟ln大小的空间存储数据
      特点:存储效率高于varchar2
  • date类型
create table student(  sno number(10),  sname varchar2(100),  sage number(3),  ssex char(4),  sfav varchar2(500),  sbirth date)--添加测试数据insert into student values(1,'柳岩',18,'女','拍电影''01-1月-1985');insert into student values(2,'古力娜扎',20,'女','拍电影'to_date('1990-01-01','yyyy-mm-dd'));select * from student

二维表的约束

主键

特点:非空唯一

使用:

  1. 直接在创建表的字段后使用 primary key
  2. 在创建表的语句最后面使用 constraint pk_表名_字段名 primary key(字段名)
  3. 在创建表后使用 alter table 表名 add constraint 主键的约束名(一般写成pk_表名_字段名) primary key(字段名)
  4. 删除主键约束 alter table 表名 drop constraint 主键的约束名

非空约束

使用:

  1. 直接在创建表的字段后使用 not null
  2. 在创建表的语句最后面使用 constraint ck_表名_字段名 check(字段名 is not null)
  3. 在创建表后使用 alter table 表名 add constraint 非空约束名(一般写成ck_表名_字段名) check(字段名 is not null)
  4. 删除非空约束 alter table 表名 drop constraint 非空约束名

检查约束

使用:

  1. 直接在创建表的字段后使用 check(条件)
  2. 在创建表的语句最后面使用 constraint ck_表名_字段名 check(字段名 is not null)
  3. 在创建表后使用 alter table 表名 add constraint 检查约束名(一般写成ck_表名_字段名) check(条件)
  4. 删除检查约束 alter table 表名 drop constraint 检查约束名

唯一约束

使用:

  1. 直接在创建表的字段后使用 unique
  2. 在创建表的语句最后面使用 constraint un_表名_字段名 unique(字段名)
  3. 在创建表后使用 alter table 表名 add constraint 唯一约束名(一般写成un_表名_字段名) unique(字段名)
  4. 删除唯一约束 alter table 表名 drop constraint 唯一约束名
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='女')    sfav varchar2(500),    sbirth date,    sqq varchar2(30)--unique    --为字段sno添加主键约束    --,constraint pk_student_sno primary key(sno)    --为字段sname添加非空约束    --,constraint ck_student_sname check(sname is not null)    --为字段sage添加检查约束    --,constraint ck_student_sage check(sage<150 and sage>0)    --为字段ssex添加检查约束    --,constraint ck_student_ssex check(ssex='男' or ssex='女')    --为字段sqq添加唯一约束    --,constraint un_student_sqq unique(sqq));--添加主键约束alter table student add constraint pk_student_sno primary key(sno);--删除主键约束alter table student drop constraint pk_student_sno;--添加非空约束alter table student add constraint ck_student_sname check(sname is not null);--删除非空约束alter table student drop constraint ck_student_sname;--添加检查约束限制年龄为正常范围alter table student add constraint ck_student_sage check(sage<150 and sage>0);--删除限制年龄为正常范围的检查约束alter table student drop constraint ck_student_sage;--添加检查约束校验性别alter table student add constraint ck_student_ssex check(ssex='男' or ssex='女');--删除校验性别的检查约束alter table student drop constraint ck_student_ssex;--添加唯一约束alter table student add constraint un_student_sqq unique(sqq);--删除唯一约束alter table student drop constraint un_student_sqq;select * from student;drop table student;

外键约束

作用:当在子表中插入的数据在父表中不存在的时候,会自动报错

概念:当一张表的某个字段的值需要依赖另外一张表的某个字段的值时,使用外键约束,其中主动依赖的表称为子表,被依赖的表称为父表,外键加在子表中

使用:

  1. 在子表创建的时候直接在字段后使用 references 父表名(字段名)
  2. 在子表创建的时候在语句最后使用 constraint 外键约束名(一般为fk_字表名_字段名) foreign key(字段名) references 父表名(字段名)
  3. 在子表创建后使用 alter table 子表名 add constraint 外键约束名(一般为fk_字表名_字段名) foreign key(字段名) references 父表名(字段名)
  4. 删除外键约束 alter table 子表名 drop constraint 外键约束名

外键选取:一般选取父表的主键作为子表的外键

外键的缺点:无法直接删除父表数据
解决方案:

  1. 级联删除:在添加外键约束时,使用关键字 on delete cascade
    当删除父表数据时,自动删除子表相关的所有数据
    缺点:无法保留子表的历史数据
  2. 在添加外键约束时,使用关键字 on delete set null
    删除父表数据时,将子表中的依赖字段的值设置为null
    注意:子表依赖字段不能添加非空约束
--创建学生表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='女'),  sfav varchar2(500),  sqq varchar2(30) unique,  cno number(10) --references clazz(cno)  --添加外键约束  --,constraint fk_student_cno foreign key(cno) references clazz(cno));--添加外键约束alter table student add constraint fk_student_cno foreign key(cno) references clazz(cno);--删除外键约束alter table student drop constraint fk_student_cno;--添加测试数据insert into student values(1,'张三',18,'男','唱歌','657889900',1);insert into student values(2,'李四',18,'男','跳舞','657889901',1);insert into student values(3,'王麻子',18,'男','BBox','657889902',2);insert into student values(4,'廖壳子',18,'男','篮球','657889903',2);drop table student;--创建班级表create table clazz(  cno number(10) primary key,  cname varchar2(100) not null,  cdesc varchar2(300));--添加测试数据insert into clazz values(1,'java高级班','666');insert into clazz values(2,'python高级班','233');--查询学生及其班级信息select * from student sinner join clazz con s.cno=c.cno

二维表的维护

添加新的字段

alter table 表名 add 字段名 类型

alter table student add sphone number(11);--在学生表中添加新字段sphone

修改原有字段

  1. 修改字段类型
    alter table 表名 modify 字段名 新的类型

    alter table student modify sphone varchar2(11);
  2. 修改字段名
    alter table 表名 rename column 字段名 to 新的字段名;

    alter table student rename column sphone to phone;
  3. 删除字段
    alter table 表名 drop column 字段名

    alter table student drop column phone;

修改表名

alter table 原有表名 rename to 新的表名

alter table student rename to student2;alter table student2 rename to student;

删除表

drop table 表名

drop table student;
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值