SQLCookbook中文版:第4章 插入,更新和删除; 第5章 元数据查询

第4章 插入,更新和删除

4.1 插入新记录

使用insert子句

4.2 插入默认值

insert into D(id) values(default)

4.3 插入空值

使用null

4.4 将查询结果插入表

在insert 语句后跟上select语句

insert into dept_10(deptno,dname,loc)
select deptno,dname,loc from dept where deptno='10'

4.5 复制表定义

Oracle,MySQL,PG

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

查询 1=0 会返回空行.

4.6 一次向多个表插入记录

(略)

4.7 仅允许用户操作指定的列

基于表创建包含部分列的视图, 并将视图授权给用户

4.8 在表中编辑记录: update

update emp set sal=sal*1.1 where deptno='20'

4.9 有条件的更新

udpate emp set sal=sal*1.2 where empno in (select empno from emp_bonus)

4.10 使用其他表中的值更新

PG:

update emp set sal=ns.sal,comm=ns.sal/2 from ns where ns.deptno=emp.deptno

以上使用 ns表中的 sal值更新 emp 表中的两个字段的值.

4.11 合并记录: 执行相应的插入,更新,删除操作

(略)

4.12 从表中删除所有记录

delete from emp

4.13 删除指定的记录

delete from emp where deptno='10'

4.14 删除单个记录

delete from emp where empno='7782'

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

如果某员工的部门不存在,删除该员工

delect from emp where deptno not in (select deptno from dept)

4.16 删除重复的记录:某列重复只保留一条记录

按定义重复的列分组,然后只保留该组内的一条记录

数据准备:

DROP TABLE IF EXISTS dupes;
create table dupes (id integer, name varchar(20));
insert into dupes values (1,'Jimmy');
insert into dupes values (2,'Tommy');
insert into dupes values (3,'See You');
insert into dupes values (4,'Hello Kitty');
insert into dupes values (5,'Hello Kitty');
insert into dupes values (6,'Hello Kitty');
insert into dupes values (7,'Hello Kitty');

按name分组, 每组保留一个:

delete from dupes where id not in (
    select max(id) from dupes group by name
)

数据:

id name
1 Jimmy
2 Tommy
3 See You
7 Hello Kitty

4.17 删除从其他表引用的记录

数据准备:

create table dept_accidents(deptno decimal(2,0), accidents_name varchar(20));
insert into dept_accidents values(10,'broken foot');
insert into dept_accidents values(10,'flesh wound');
insert into dept_accidents values(20,'fire');
insert into dept_accidents values(20,'fire');
insert into dept_accidents values(20,'flood');
insert into dept_accidents values(30,'fire');

下面要删除发生了3次或3次以上事故的dept对应的所有员工

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

重点是查出deptno:

select deptno from dept_accidents group by deptno having count(deptno)>=3

group by 后面不能跟where子句?

第5章 元数据查询

5.1 列出模式中的表

PG,MySQL:

select table_name from information_schema.tables where table_schema='sqlcookbook';

实际上使用 show tables; 也OK啊!

5.2 列出表中的列

列名称,数据类型,列在表中的位置

PG,MySQL

select column_name,data_type,ordinal_position from information_schema.columns 
where table_schema='sqlcookbook' and table_name='emp'

输出:

column_name data_type ordinal_position
empno decimal 1
ename varchar 2
job varchar 3
mgr decimal 4
hiredate date 5
sal decimal 6
comm decimal 7
deptno decimal 8

desc emp; 的输出:

Field Type Null Key Default Extra
empno decimal(4,0) NO  NULL
ename varchar(10) YES  NULL
job varchar(9) YES  NULL
mgr decimal(4,0) YES  NULL
hiredate date YES  NULL
sal decimal(7,2) YES  NULL
comm decimal(7,2) YES  NULL
deptno decimal(2,0) YES  NULL

5.3 列出表的索引列

MySQL

show index from emp

5.4 列出表约束

PG,MySQL

select a.table_name,a.constraint_name,a.constraint_type,b.column_name
from information_schema.table_constraints a,information_schema.key_column_usage b
where a.table_name='emp'
and a.table_schema='sqlcookbook'
and a.table_name=b.table_name
and a.table_schema=b.table_schema
and a.constraint_name=b.constraint_name

5.5 列出没有相应索引的外键

(略)

5.6 使用SQL来生成SQL:动态SQL

(略)

5.7 在Oracle中来描述数据字典的视图

(略)

正文完
 
评论(没有评论)