搬山行者

无志愁压头,有志能搬山

业余程序员的学习笔记~


Sql经典实例 第四章

目录

复制数据到另外一个表

insert into dept_east(deptno, dname, loc)
  select deptno, dname, loc 
    from dept
    where loc in ('New York', 'BOSTON')

复制表定义

create table dept_2 
  as select * from dept where 1 = 0

使用另外一个表的数据更新记录

update emp 
  set sal = ns.sal,
      e.ecomm = ns.sql / 2
      from new_sal as ns
      where ns.deptno = emp.deptno

删除违反参照完整性的记录

delete from emp
  where not exists (
    where dept.deptno = emp.deptno
  )

删除被其它表参照的记录

delete from emp
  where deptno in (
    select deptno from dept_accidents
      group by deptno
      having count(*) >= 3
  )