Oracle数据库第三日:字段约束与连表查询

约束:限制

  主键约束:primary key 

      主键:在一张表中能够唯一定位一条数据的列成为主键列(非空且唯一);

  外键约束:forgin key

      外键:在字表中一个列引用了母表中的主键列,那么这个列在子表中就被称为外键列(一张表可以有多个外键);

  非空约束:not null 

      被约束的列添加数据时不可为空;

  唯一约束:unique

      被约束的列数据唯一不可重复;

  检查约束:check

  查看当前用户下有哪些约束:
  select table_name,constraint_name,constraint_type from user_constraints;
  建表以后,删除约束:
  alter table test2 drop constraint zj;

  先删除子表约束再删除母表;

  约束越多,表越健壮,效率越低;

连表查询

  子查询:嵌套查询     

      1.葫芦娃是哪个学校的?  

      select sid from student where name = '葫芦娃';
      2.还有谁是这个学校的?
      select name from student where sid = 2;

      select name from student where sid = (select sid from student where name = '葫芦娃') and name <> '葫芦娃';

  自查询(了解,效率极低)

     select student.name,school.name from student,school
     where student.sid = school.id;

  内连接 inner join on = join on
       两张表有关联关系的数据
       select student.name,school.name from student
       join school on school.id = student.sid;

  外连接 outer join on 

      不仅包含有关联关系的数据,也包含没有关联关系的数据;

  左外连接 left join on 右外连接 right join on  ; 

      以左表为主,左表的所有数据都展示,对应不上的右表的数据以空展示
      *:左表关联上右表的数据 + 关联不上的左表的数据
      *:from 左表 left join 右表 on 关联关系

全外连接 full join on (两张表的数据都展示)

      *:左表关联上右表的数据 + 关联不上的左表的数据 + 关联不上的右表的数据

交叉链接 cross join

      两张表的数据一一对应,交叉形成最后的结果

 

 

 

 

 

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值