# SQL Cookbook
## 查找空值
查找空值切記不能用`=`操作, 會返回`Empty Set`
~~~
mysql> select * from emp where comm is null;
~~~
## 空值轉換為實際值返回
> 改變輸入的形式, 但并不改變表中的數據
coalesce()函數有1個或者多個參數, comm非空時返回comm, null時返回0, 也可以用`case語句判斷實現`
~~~
mysql> select coalesce(comm, 0) from emp;
~~~
## 從一個表中查找與其他表不匹配的記錄
`外連接`
~~~
mysql> select d.* from dept d left outer join emp e
-> on (d.deptno = e.deptno) where e.deptno is null;
~~~
## 插入更新刪除
## 插入技巧
## 從一個表向另外的表中復制行
解決方案: 在insert語句后面緊跟一個用來產生索要插入行的查詢
~~~
mysql> create table dept_east(
-> deptno int,
-> dname varchar(30),
-> loc varchar(30));
mysql> insert into dept_east(deptno, dname, loc)
-> select deptno, dname, loc from dept
-> where loc in('new york', 'boston');
~~~
## 賦值表定義
> 只復制已有表的定義, 不復制其中的記錄, 創建表的時候, 使用`一個不返回任何行的子查詢, where的條件時鐘為false`
~~~
mysql> create table dept_2
-> as
-> select * from dept where 1 = 0;
~~~
## 修改技巧
~~~
mysql> update emp set sal = sal * 1.10 where deptno = 20;
~~~
## 刪除技巧
### 刪除所有記錄
~~~
mysql> delete from dept_2;
~~~