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、二维表创建约束学习:(主键约束,非空约束,检查约束,唯一约束)

 --删除表:drop table 表名;
drop table student; select * from student;
--创建简单的学生表
create table student(
sno number(15), --primary key, --添加主键约束_1
sname varchar2(50),-- not null,--添加非空约束_1
sex varchar2(4),--check(sex='男' or sex='女'),--添加检查约束
sage number(3),-- check(sage>0 and sag<=200),--添加检查约束_1
sbirth date,
phone number(20) -- 添加唯一约束 unique
--constraints pk_student_sno primary key(sno) --添加主键约束_2
--constraints ck_student_sname check(sname is not null) --添加非空约束_2
--constraints ck_student_sage check(sage>0 and sage<=200)--添加检查约束_2
--constraints un_student_phone unique(phone)--唯一约束 );
----------------------------------创建表后在去创建约束---------------------------------------
--添加主键约束
alter table student add constraints pk_student_sno primary key(sno);--添加主键约束_3
alter table student drop constraints pk_student_sno;--删除主键约束
--添加非空约束
alter table student add constraints ck_student_sname check(sname is not null) --添加非空约束_3
alter table student drop constraints ck_student_sname;--删除非空约束
--添加检查约束
alter table student add constraints ck_student_sage check(sage>0 and sag<=200)--添加检查约束_3
alter table student drop constraints ck_student_sage; --删除检查约束
--添加唯一约束
alter table student add constraints un_student_phone unique(phone)--唯一约束 --添加测试数据
insert into student values(2018112001,'小喜庆','女',18,'7-10月-18',10086); ----------------------------------没有添加约束存在的一些问题---------------------------------------
---Oracle表约束【没有添加约束存在的一些问题】
--问题1:字段(学号)可重复添加
insert into student values(2018112001,'迪丽热巴','女',27,to_date('1992-06-03','yyyy-mm-dd'),10010);
--解决1:使用主键 primary key,特点:非空唯一
添加主键方法:1:直接在创建表的字段后使用关键字 primary key
2:在创建表的语句的最后面使用 constraints pk_表名_字段名 primary key(字段名)
3:在创建表后使用 alter table 表名 add constraints pk_表名_字段名 primary key(字段名);
4:删除主键 alter table student drop constraints 主键的约束名; --问题2:必填的字段(sname)可以为空
insert into student values(2018112001,'','女',27,to_date('1992-06-03','yyyy-mm-dd'),10010);
--解决2:使用非空约束 not null
添加非空约束: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 非空约束名; --问题3:违背了自然定律
insert into student values(2018112001,'小喜庆','女',218,'7-10月-18',10086);
--解决3:使用检查约束
添加非空约束:1:直接在创建表的字段后使用 check(条件) 例如 sage number(3) check(sage>0 and sag<=200),
2:在创建表的语句的最后面使用 constraints ck_表名_字段名 check(条件)
3:在创建表后使用 alter table 表名 add constraints ck_表名_字段名 check(条件);
4:删除检查约束 alter table student drop constraints 检查约束名;
--问题4:手机号可以重复
insert into student values(2018112001,'小喜庆','女',218,'7-10月-18',10086);
--问题4:使用唯一约束
添加非空约束:1:直接在创建表的字段后使用 unique
2:在创建表的语句后面使用 constraints un_表名_字段名 unique(字段名);
3:在创建表后使用 alter table 表名 add constraints un_表名_字段名 unique(字段名);
4:删除约束:alter table 表名 drop constraints 唯一约束名;
--创建表并同时添加约束
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、二维表创建约束学习:外键约束

 --学生信息表
create table student (
sid number(10) primary key,
sname varchar2(50) not null,
ssex char(4) check(ssex='男' or ssex='女'),
sage number(3) check(sage>=0 and sage<=200),
sqq number(20) unique,
cno number(10) references class(cno)
);
--添加数据
insert into student values(1,'迪丽热巴','女',27,13245668,1);
insert into student values(2,'游戏解说柚子','女',24,11545668,1);
insert into student values(3,'杰西','女',22,135668,2);
insert into student values(4,'杰克','男',21,1323268,2); --班级表
create table class (
cno number(10) primary key,
cname varchar2(50) not null,
cdesc varchar2(50)
);
insert into class values(1,'计算机1班','明星班');
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。
      --注意:子表依赖字段不能添加非空约束。

最新文章

  1. October 25th Week 44th Tuesday 2016
  2. W3C对DOM2.0定义的标准事件
  3. Delphi编程建议遵守的规范1---缩进、各种语句的用法
  4. 如何做好APP测试?
  5. google打不开啦,咋办?
  6. Sublime text 快捷键总结
  7. google maps js v3 api教程(3) -- 创建infowindow
  8. 纯Python包发布setup脚本编写示例
  9. Asp.net中用户自定义控件 ascx的使用
  10. Selenium: 空指针error
  11. webpack 2.x 配置
  12. jmeter响应断言
  13. IO流之字节流知识总结
  14. 20175208 《Java程序设计》第八周学习总结
  15. FileInputStream文件字节输入流程序
  16. python之sys.stdout、sys.stdin
  17. MySql 5.7安装(随机密码,修改默认密码)两个坑
  18. python 生成器 迭代器 yiled
  19. iOS仿UC浏览器顶部频道滚动效果
  20. 如何使用OpenGL中的扩展

热门文章

  1. SQL server中获取语句执行时间
  2. COALESCE关键字的使用
  3. linux之反向代理,反向代理实例,负载均衡实例
  4. python之pip使用技巧
  5. Stream系列(十五)File方法使用
  6. Spring+SpringMvc+Hibernate整合记录
  7. LeetCode之链表总结
  8. python调用hanlp进行命名实体识别
  9. bootstrap-table服务端分页操作
  10. redis5.0 数据结构与命令