1 默认值约束(默认值):对表中的某个列提前设置好默认值,当执行插入操作的时候,如果该列没有插入列值,则系统会自动的插入之前设置的默认值。
  1)每个列只能插入一个默认值。
  2)创建表的时候设置默认值。
    create table worker10(
      id number(4) primary key,
      name varchar2(50),
      hiredate date default sysdate,
      age number(3) default 23
    )
    insert into worker10
    values(1,'徐达','22-12月-12',42)
    insert into worker10(id,name)
    values(2,'常遇春')
  3)修改表的时候设置默认值
    使用modify
    格式:
      alter table 表名
      modify 列名 数据类型 default 数值
      alter table worker10
      modify age number(3) default 30

  4) 删除默认值约束
    alter table 表名
    modify 列名 数据类型 default null
    alter table worker10
    modify age number(3) default null
    案例:删除worker10在hiredate列上的默认值
      alter table worker10
      modify hiredate date default null

 

2 关联查询(一):所需要查询的数据来源于多张表,使用表的关联查询(表连接查询)来连接多张表,查询对应的每张表中的数据。
  1)格式:
    select 别名1.*/列名,别名2.*/列名
    from 表1 别名1,表2 别名2
    where 关联条件
    --反例:
    案例:查询emp表中员工的编号,姓名以及所属部门的编号,名称
      select e.empno,e.ename,d.deptno,d.dname
      from emp e,dept d --48
      select count(*) from emp --12
      select count(*) from dept --4
  2) 笛卡尔积:在多表连接查询中,出现两张表之间数据的任意组合的现象。在实际的企业中,一定不要出现笛卡尔积。在多表的连接查询中,必须要加入关联条件,避免笛卡尔积的出现。
  3) 关联条件:根据两张表之间的关联关系,设计关联条件,比如emp表中deptno(员工所属部门的编号)跟dept表中deptno(部门编号)对应emp表和dept表之间的关联条件:emp.deptno = dept.deptno
    案例:查询emp表中员工的编号,姓名,职位以及所在部门的编号,名称
      select e.empno,e.ename,e.job,d.deptno,d.dname
      from emp e,dept d
      where e.deptno = d.deptno

    案例:查询emp表中员工的编号,姓名,职位,入职时间,工资以及所在部门的名称和地址
      select e.empno,e.ename,e.job,e.hiredate,e.sal,d.dname,d.loc
      from emp e,dept d
      where e.deptno = d.deptno

    案例:查询emp表中员工姓名,工资,奖金,上级领导编号以及部门的编号,名称和地址
      select e.ename,e.sal,e.comm,e.mgr,d.*
      from emp e,dept d
      where e.deptno = d.deptno

    案例:查询emp表中员工的编号,姓名,工资以及所在部门编号,地址,最后根据员工的所属部门的编号进行升序排列,如果部门编号一致,根据员工的编号进行降序排列。
      select e.empno,e.ename,e.sal,d.deptno,d.loc
      from emp e,dept d
      where e.deptno = d.deptno
      order by d.deptno asc,empno desc

    案例:查询emp表中员工的名字中不包含'K'的员工的编号,姓名,职位,工资以及所属部门的编号,名称,最后根据员工的编号进行升序排列
      select e.empno,e.ename,e.job,e.sal,d.deptno,d.dname
      from emp e,dept d
      where e.deptno = d.deptno and ename not like '%K%'
      order by e.empno asc

  4) 等值连接,关联条件使用"="进行连接的。
    案例:查询emp表中工资在1000~3000之间的员工的编号,姓名,职位,工资以及所在部门的编号,名称,最后根据员工的编号进行降序排列
      select e.empno,e.ename,e.job,e.sal,d.deptno,d.dname
      from emp e,dept d
      where e.deptno = d.deptno ande.sal between 1000 and 3000
      order by e.empno desc
  5) 非等值连接:关联条件不是“=”进行连接。emp表中员工的工资sal应该在salgrade(工资等级表)中最低工资losal和最高工资hisal之间,emp表和salgrade表之间的关联条件:emp.sal between s.losal and s.hisal

    案例:查询emp表中员工的编号,姓名,职位,工资以及工资的等级
      select e.empno,e.ename,e.job,e.sal,s.grade
      from emp e,salgrade s
      where e.sal between s.losal and s.hisal

    案例:查询salgrade表中工资的等级,该等级最低工资和最高工资以及该等级下员工的编号,姓名,职位,工资,入职时间,最后根据工资等级进行降序排列,如果等级一致根据员工的工资进行升序排列。
      select s.*,e.empno,e.ename,e.job,e.sal,e.hiredate
      from salgrade s,emp e
      where e.sal between s.losal and s.hisal
      order by s.grade desc,e.sal asc
    案例:查询10,30部门的员工的编号,姓名,职位,入职时间,工资以及该工资的等级,最后根据入职时间进行降序排列
      select e.empno,e.ename,e.job,e.hiredate,e.sal,s.grade
      from emp e,salgrade s
      where e.sal between s.losal and s.hisal and e.deptno in(10,30)
      order by e.hiredate desc

  6)自连接:表中列之间具有关联关系,可以把一张表当成两张表,然后再进行关联查询。在emp表中mgr是当前员工上级领导(经理)的编号,把emp表中看成两张表,一张是员工表,另一张是领导表。员工表和领导表之间的关联关系:员工表中上级领导编号mgr等于领导表中员工的编号empno,关联条件:员工表.mgr = 领导表.empno

    案例:查询emp表中员工的编号,姓名,职位,以及该员工上级领导的编号,姓名,职位
      select e.empno,e.ename,e.job,m.empno,m.ename,m.job
      from emp e,emp m --e,员工表;m,领导表
      where e.mgr = m.empno
    案例:查询emp表中员工的编号,姓名,职位,工资以及上级领导的编号,姓名,工资,职位,最后根据员工的编号进行降序排列。
      select e.empno,e.ename,e.job,e.sal,e.mgr,m.ename,m.job,m.sal
      from emp e,emp m --e,员工表;m,领导表
      where e.mgr=m.empno
      order by e.empno desc

    案例:查询emp表中职位不是ANALYST,CLERK员工的姓名,职位,入职时间以及领导编号,姓名,职位,入职时间,最后根据员工的入职时间进行降序排列。
      select e.ename,e.job,e.hiredate,m.empno,m.ename,m.job,m.hiredate
      from emp e,emp m --e,员工表;m,领导表
      where e.mgr = m.empno and e.job not in('ANALYST','CLERK')
      order by e.hiredate desc

    案例:查询emp表中员工的姓名,工资,入职时间以及所在部门的编号,上级领导姓名,工资,并且要求员工工资必须在1000~3000之间,最后根据工资进行升序排列。
      select e.ename,e.sal,e.hiredate,e.deptno,m.ename,m.sal
      from emp e,emp m
      where e.mgr = m.empno and e.sal between 1000 and 3000
      order by e.sal asc

  7) 内连接:进行多表连接查询中,只查询有关联关系的数据,而不查询没有关联关系的数据,称为内连接。比如emp表和dept表进行关联查询的时候,dept表含有40号部门
    案例:查询emp表中员工的编号,姓名,职位,工资以及所在部门的编号,名称
      select e.empno,e.ename,e.job,e.sal,d.deptno,d.dname
      from emp e,dept d
      where e.deptno = d.deptno

    案例:查询emp表中员工的编号,姓名,职位,所在部门编号,名称以及该员工的工资和工资的等级。
      select e.empno,e.ename,e.job,d.deptno,d.dname,e.sal,s.grade
      from emp e,dept d,salgrade s
      where e.deptno = d.deptno and e.sal between s.losal and s.hisal

    案例:查询emp表中工资高于1000,并且职位不是MANAGER员工的编号,姓名,职位,工资,入职时间,员工所在部门的编号,名称,地址,以及该工资的等级和该等级最低工资,最后根据部门编号进行降序排列,如果部门编号一致,根据员工的入职时间进行升序排列。
      select e.empno,e.ename,e.job,e.sal,e.hiredate,d.*,s.grade,s.losal
      from emp e,dept d,salgrade s
      where e.deptno = d.deptno and e.sal between s.losal and s.hisal and e.sal>1000 and job <> 'MANAGER'
      order by d.deptno desc,e.hiredate asc
    案例:查询emp表中名字中不包含'S'的员工的姓名,职位,工资,上级领导编号,姓名,以及该员工所在部门的编号,名称,最后根据员工的工资进行升序排列
      select e.ename,e.job,e.sal,e.mgr,m.ename,d.deptno,d.dname
      from emp e,emp m,dept d
      where e.mgr = m.empno and e.deptno = d.deptno and e.ename not like '%S%'
      order by e.sal asc

 

3 非空约束:指定某一个列的列值不为空,非空约束属于检查约束的一种。
  1)创建表的时候,指定非空约束。
    create table worker11(
      id number(4) primary key,
      name varchar2(50) unique not null,
      age number(3) not null
    )
    insert into worker11 values(1,'乔峰',31)
    --反例
    insert into worker11(id,name)
    values(2,'西门吹雪')
    insert into worker11(id,age)
    values(3,100)

  2)修改表的时候设置非空约束
    --使用modify,而且可以同时设置多个非空约束
    格式:
      alter table 表名
      modify(列名1 not null)
      modify(列名2 not null)
      modify(列名3 not null)
      ...
      modify(列名n not null)

      create table worker12(
        id number(4) primary key,
        name varchar2(50),
        age number(3),
        address varchar2(50),
        email varchar2(50)
      )
      alter table worker12
      modify(name not null)
      modify(age not null)
      modify(address not null)
      modify(email not null)

  3) 删除非空约束(了解):把非空约束所修饰的列设置为null
    --使用modify
    alter table 表名
    modify(列名1 null)
    modify(列名2 null)
    modify(列名3 null)
    ...
    modify(列名n null)
    案例:删除worker12中name,age,address,email列上的非空约束
      alter table worker12
      modify(name null)
      modify(age null)
      modify(address null)
      modify(email null)

 

4 索引:索引是建立在表中列上的数据库对象,用于提高数据的查询速度。
  1)索引是一种用来提高查询效率的机制。
  2)索引一旦创建以后就由Oracle自动进行维护,编写SQL语句的时候不需要指定使用的是哪一个索引。
  3)主键所修饰的列,默认就有索引。
    create table worker13(
      id number(4) primary key,
      name varchar2(50)
    )
  4)被唯一约束修饰的列,系统默认设置索引。
    create table worker14(
      id number(4) primary key,
      name varchar2(50) unique,
      age number(3)
    )
  5)创建索引的格式:
    create index 索引名称 on 表名(列名)
    create index index_bname on book(bname)
    案例:在book表中author和pub列上添加索引index_author和index_pub
      create index index_author on book(author)
      create index index_pub on book(pub)
  6)删除索引的格式:
    drop index 索引名称
    drop index index_bname
    案例:删除book中author和pub列上的索引
      drop index index_author
      drop index index_pub