5.Oracle5

1. 子查询:

  1. select 子查询

    -- select 子查询
    select t1.ename, (select dname from dept t2 where t2.deptno = t1.deptno)
    from emp t1
    -- 限制: 不能对应多行
    select dname, (select ename from emp t2 where t1.deptno = t2.deptno)
    from dept t1;
    
  2. from子查询:

​ 根据题意,结果中有跨表的列时用from

  1. where子查询

​ 根据题意,结果中不需要跨表

2. 约束–DDL

  • default : 默认值

  • not null: 非空

    create table student(
    stuno number(5), -- stuno number(5) not null,
    stuname varchar2(20) default '路人甲'
    );
    
    insert into student values(1, null); -- 
    insert into student (stuno) values(2);
    insert into student (stuname) values('路人乙')
    select * from student;
    alter table student modify stuno not null; -- 注意:已有数据不能违反这个约束
    delete from student where stuname='路人乙';
    
    
  • 唯一约束: unique 默认创建唯一索引(提高查询的速度)

    • create table 表名(字段 类型 unique); 默认创建了唯一约束的名字

    • alter table 表名 add constraint 约束名字 约束的类型 (字段1,字段2)

      ---- 唯一 unique 有名字 。默认创建索引(提高查询效率)
      alter table student add constraint Uni_Student_stuno unique (stuno);
      
      truncate table student;
      insert into student values(1, null);
      insert into student values(1, null);
      drop table student;
      
      create table student(
      stuno number(5) not null unique, -- 随机的系统名
      stuname varchar2(20) default '路人甲'
      );
      insert into student values(1, null);
      
  • 主键约束: primary key: === not null + unique ; 创建主键约束,会默认创建索引

    • 关系型数据库ORDBMS: 关系(主外关系)
    
    drop table student;
    create table student(
    stuno number(5) , 
    stuname varchar2(20) default '路人甲'
    );
    
    alter table student add constraint PK_STUDENT_stuno primary key (stuno);
    insert into student values(1, null);
    insert into student values(null, null);
    
  • 外键约束: 外键要关联主键(唯一)

    • 外键的数据是 关联主键中的数据

    • 外键的数据可以为空

    • 在关系n:1

      -- foreign key : 外键: 外键要关联主键(唯一约束)
      -- 外键的内容要在主键中有: 
      select * from emp;
      select * from dept;
      
      create table clazz(
             clazzno number primary key,
             clazzname varchar2(30)
      );
      
      insert into clazz values(1, '2115');
      insert into clazz values(2, '2116');
      insert into clazz values(3, '2117');
      select * from clazz;
      alter table student add clazzno number(4);
      alter table student add constraint FK_Student_clazzno Foreign key (clazzno)
            references clazz(clazzno); -- 背下来
      
      insert into student values(56, 'jerry', 4); -- 违反FK
      insert into student values(56, 'jerry', null); -- 
        select * from student;
        
        commit;
      
  • 2
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值