day 03
********************
五个约束:限制
primary key foreign key/references not null unique check
主键 外键 非空 唯一 检查
主键约束:primary key
在一个表中,能够唯一定位一条数据的列
主键列:非空且唯一
一张表只能有一个主键
数据库会在具有唯一性的列上自动添加唯一性约束
create table 表名(
id number constraint 约束名 primary key, //id number(3) primary key,这样就行
列名 数据类型
)
*:constraint :约束名 可以不写
//主键默认唯一
外键约束:foreign key *:references
外键:在子表中,某个列引用了母表中主键列上的值,子表中这个列就是外键列
*:一张表可以有多个外键 //tid number(3) references teacher(id) -----------注意有s
引用teacher母表id,使tid成为外键列
非空:not null //name varchar2(20) not null,
唯一:unique //name char(6) not null unique,
检查:check(用length或者between and) //qq varchar2(20) check(length(qq)>=8),
//salary number(7,2) check(salary between 10000 and 20000),
********************************* 再把检查的值传进去
建表添加约束的例子:
create table teacher(
id number(3) primary key,
name varchar2(20) not null unique,
salary number(8,2) check(salary<=200000)
);
create table class(
id number(3) primary key,
name char(6) not null unique,
qq varchar2(20) check(length(qq)>=8),
tid number(3) references teacher(id) //此class表的tid字段为外键,连接teacher母表中id字段
);
create table school(
id number(3) primary key,
name varchar2(20) not null unique,
phone varchar2(20) check(length(phone)>=7) //注意括号里在传进去
);
create table student(
id number(3) primary key,
name varchar2(20) not null,
birthday date,
salary number(7,2) check(salary between 10000 and 20000), //再传进去
email varchar2(50) unique,
sid number(3) references school(id),
cid number(3) references class(id)
);
如何添加约束:
alter table text add constraint aa primary key(name);
如何查看已经存在的约束:
select * from user_constraints;当前用户所有的约束
select constraint_name,constraint_type from user_constraints
where table_name = upper('text');
如何删除约束:
alter table text drop constraint aa;
*******************************************
*:数据库中表的约束越多,表越健壮
*:约束越多,效率越低
*:通常一张表常用的约束 主键约束 外键约束
子查询 = 嵌套查询 = 某些查询的条件是通过查询得出来的 //子查询不如连表查询好写
select (select name from school where id = sid),name from student;
//括号里是又查询的条件
select name from student where sid=(select id from student where name like '哪咤');
sid=1;
连表查询:----
内连接:通过关系能够关联出部分数据
inner join on = join on //join可以去掉
两步: //第一行,是查询内容,第二行on后是查询条件
select student.name,school.name from student //inner join on=join on
inner join school on student.sid = school.id; //on后面的条件,是两张表,字表外键字段和母表字段连接的条件,这样才能时间连接(外键)
外连接:不仅包括有关联关系的数据,还包括没有关联关系的数据
outer join on
左外联接:以左表为主,左表中的每一行数据,无论是否满足条件最终都会显示(左边可能多一块)
但是关联表的所有信息都以空展示 //左表信息为主,右表只有与左边啊有关联的数据才
from 左表 left join 右表 on 关联关系 能展示
*:左表关联上右表的数据+关联不上的左表的部分数据
select school.name,student.name from school
left join student on school.id = student.sid;
//但是如果再加一行的话,就不是只是看from 之后一定是左表,join后面是什么表,要看左连接还是外连接
//right join school on school.id = student.sid; //上两个表是左表,sch是右表,但是,以right为主
************************************
一张表里的连接查询:
select a.ename,b.ename from emp a
join emp b on a.mgr=b.empno;
************************************
右外连接:右表为主,右表中的每一行数据,无论是否满足条件最终都会显示(右表显示为主,右边可能多一块)
但是关联表的所有信息都以空展示 //右表信息为主,左表与右边有关系的数据才会展示
from 左表 right join 右表 on 关联关系
*:右表关联上左表的数据+关联不上的右表的部分数据
select student.name,school.name from student
right join school on school.id = student.sid;
//但是如果再加一行的话,就不是只是看from 之后一定是左表,join后面是什么表,要看左连接还是外连接
//left join student on school.id = student.sid; //上两个表是左边,stu是右表,但是,left为主
select class.name,school.name,student.name from student
left join class on class.id = student.cid
right join school on school.id = student.sid;
======================================================
全外连接:两张表的数据都展示
full join on
from 左表 full join 右表 on 对应关系
*:左表关联上右表的数据+关联不上的左表的部分数据+关联不上的右表的部分数据
select student.name,school.name from student
full join school on school.id = student.sid;
交叉连接:两张表的数据一一对应 交叉形成最后结果
cross join
select student.name,book.bname from student
cross join book;
select student.name,book.bname from student,book;
*:什么时候使用多表连接?什么时候使用子查询?
1.如果需要查询的数据在多个表中,一定要用多表连接 //查询数据在多个表中,要用多表连接
2.不需要表A中的列,但又有该表A的条件,可以用子查询 //用到A的条件,不用A的列
3.子查询(嵌套查询)中如果使用了in some any all这几个关键字,效率表较低 //子查询中用到in/some/any/all 可以使用多表查询
可以转换成多表关联的方式