Copyright © 2019 Linyer. All Rights Reserved
本教程使用 Oracle 示例 数据库
选择 departments 表中所有列
select * from departments;
选择 departments 表中 department_id,location_id 两列
SELECT department_id, location_id
FROM departments;
SQL语句不区分大小写
可以为一行也可为多行
所有员工薪水加 300
SELECT last_name, salary, salary + 300
FROM employees;
SELECT last_name, salary, 12*salary+100
FROM employees;
SELECT last_name, salary, 12*(salary+100)
FROM employees;
SELECT last_name AS name, commission_pct comm
FROM employees;
SELECT last_name "Name", salary*12 "Annual Salary"
FROM employees;
SQL语句中只有这里用双引号,其余都用单引号
||
表示SELECT last_name||job_id AS "Employees"
FROM employees;
SELECT last_name ||' is a '||job_id
AS "Employee Details"
FROM employees;
SELECT last_name ||': 1 Month salary = '||salary Monthly
FROM employees;
SELECT department_id
FROM employees;
DISTINCT
即distinct
关键字可消除重复行SELECT distinct department_id
FROM employees;
查询 department_id = 90 的员工
SELECT employee_id, last_name, job_id, department_id
FROM employees
WHERE department_id = 90 ;
查询 last_name 为 Whalen 的员工
SELECT last_name, job_id, department_id
FROM employees
WHERE last_name = 'Whalen';
名字大写后,不返回结果
SELECT last_name, job_id, department_id
FROM employees
WHERE last_name = 'WHALEN';
在 WHERE 子句中 不 能使用别名
查询薪水小于等于 3000 的员工的 last_name 和 salary
SELECT last_name, salary
FROM employees
WHERE salary <= 3000;
查询薪水位于 2500 和 3500 的员工
SELECT last_name, salary
FROM employees
WHERE salary between 2500 and 3500;
查询 manager_id 为 100,101,201 的员工
SELECT employee_id, last_name, salary, manager_id
FROM employees
WHERE manager_id IN (100, 101, 201);
查询 last_name 为 Hartstein 和 Vargas 的员工
SELECT employee_id, manager_id, department_id
FROM employees
WHERE last_name IN ('Hartstein', 'Vargas');
查询 first_name 首字母为 S 的员工
SELECT first_name
FROM employees
WHERE first_name LIKE 'S%';
SELECT last_name, hire_date
FROM employees
WHERE hire_date LIKE '%05';
SELECT last_name
FROM employees
WHERE last_name LIKE '_o%';
ESCAPE
即escape
条件来转码查询 job_id 包含 SA_ 的员工
SELECT employee_id, last_name, job_id
FROM employees
WHERE job_id LIKE '%SA\_%' escape '\';
查询空值
SELECT last_name, manager_id
FROM employees
WHERE manager_id IS NULL;
SELECT last_name, job_id, commission_pct
FROM employees
WHERE commission_pct IS NULL;
查询薪水大于等于 10000 并且 job_id 中包含 MAN 的员工
SELECT employee_id, last_name, job_id, salary
FROM employees
WHERE salary >=10000
AND job_id LIKE '%MAN%';
查询薪水大于等于 10000 或者 job_id 中包含 MAN 的员工
SELECT employee_id, last_name, job_id, salary
FROM employees
WHERE salary >= 10000
OR job_id LIKE '%MAN%';
查询 job_id 不为 IT_PROG,ST_CLERK,SA_REP 的员工
SELECT last_name, job_id
FROM employees
WHERE job_id
NOT IN ('IT_PROG', 'ST_CLERK', 'SA_REP');
... WHERE job_id NOT IN ('AC_ACCOUNT', 'AD_VP')
... WHERE salary NOT BETWEEN 10000 AND 15000
... WHERE last_name NOT LIKE '%A%'
... WHERE commission_pct IS NOT NULL
括号可改变优先级
先 AND 后 OR
SELECT last_name, job_id, salary
FROM employees
WHERE job_id = 'SA_REP'
OR job_id = 'AD_PRES'
AND salary > 15000;
先 OR 后 AND
SELECT last_name, job_id, salary
FROM employees
WHERE (job_id = 'SA_REP'
OR job_id = 'AD_PRES')
AND salary > 15000;
ORDER BY
子句可对行进行排序
按 hire_date 升序排列
SELECT last_name, job_id, department_id, hire_date
FROM employees
ORDER BY hire_date ;
SELECT last_name, job_id, department_id, hire_date
FROM employees
ORDER BY hire_date DESC ;
空值 在 升序 中 最后 显示,在 降序 中 最先 显示
SELECT employee_id, last_name, salary*12 annsal
FROM employees
ORDER BY annsal;
先按部门排序,再按每个部门薪水降序排序
SELECT last_name, department_id, salary
FROM employees
ORDER BY department_id, salary DESC;
即使该列 不 在SELECT列表中,仍可按该列排序
LOWER(‘SQL Course’)–>sql course
UPPER(‘SQL Course’)–>SQL COURSE
INITCAP(‘SQL Course’)–>Sql Course
lower
将所有字母转换为小写upper
将所有字母转换为小写inicap
将所有字母转换为首字母大写,其余小写查询员工的 小写 last_name
SELECT employee_id, last_name, department_id
FROM employees
WHERE lower(last_name) = 'higgins';
SELECT employee_id, upper(last_name), department_id
FROM employees
WHERE inicap(last_name) = 'Higgins';
CONCAT(‘Hello’, ‘World’)–>HelloWorld
SUBSTR(‘HelloWorld’,1,5)–>Hello
LENGTH(‘HelloWorld’)–>10
INSTR(‘HelloWorld’, ‘W’)–>6
LPAD(salary,10,’#’)–>#####24000
RPAD(salary, 10, ‘#’)–>24000#####
TRIM(‘H’ FROM ‘HelloWorld’)–>elloWorld
concat
将俩字符串连一起substr
将指定起点终点之间字符串截出length
以数字显示字符串长度instr
以数字显示指定字符位于字符串中的位置lpad
按右对齐,在原字符串左边填充字符,并指定填充字符串加原字符串总长度rpad
按左对齐,在原字符串右边填充字符,并指定填充字符串加原字符串总长度trim
借去字符串中的头或尾的字符,或都截去SELECT employee_id, concat(first_name, last_name) NAME,
job_id, length(last_name),
instr(last_name, 'a') "Contains 'a'?"
FROM employees
WHERE substr(job_id, 4) = 'REP';
SELECT employee_id, concat(first_name, last_name) NAME,
LENGTH (last_name), instr(last_name, 'a') "Contains 'a'?"
FROM employees
WHERE substr(last_name, -1, 1) = 'n';
ROUND(45.926, 2)–>45.93
TRUNC(45.926, 2)–>45.92
MOD(1600, 300)–>100
round
将值四舍五入到指定小数位trunc
将值阶断到指定小数位(不舍入)mod
返回除法运算余数SELECT sysdate
FROM DUAL;
日期+数字结果为日期相当于向日期中添加天数
日期-数字结果为日期相当于向日期中减去天数
日期-日期结果为天数相当于从一个日期减另一个日期
日期+数字/24结果为日期相当于向日期中添加小时数
查询员工已雇佣周数
SELECT last_name, (SYSDATE-hire_date)/7 AS WEEKS
FROM employees
WHERE department_id = 90;
MONTHS_BETWEEN (‘01-SEP-95’,‘11-JAN-94’)–>19.6774194
ADD_MONTHS (‘11-JAN-94’,6)–>‘11-JUL-94’
NEXT_DAY (‘01-SEP-95’,‘FRIDAY’)–>‘08-SEP-95’
LAST_DAY(‘01-FEB-95’)–>‘28-FEB-95’
假设SYSDATE为 25-JULY-95
ROUND(SYSDATE,‘MONTH’)–>01-AUG-95
ROUND(SYSDATE ,‘YEAR’)–>01-JAN-96
TRUNC(SYSDATE ,‘MONTH’)–>01-JUL-95
TRUNC(SYSDATE ,‘YEAR’)–>01-JAN-95
months_between
两个日期之间的月数add_months
将月份添加到日期当中next_day
指定日期的下一天last_day
月份的最后一天round
舍入日期trunc
截断日期显示聘用日期不足36个月的所有员工的员工编号、聘用日期、聘用的月数、六个月的复查日期、聘用日期之后的第一个星期五和聘用当月的最后一天。
SELECT employee_id, hire_date,
months_between(SYSDATE, hire_date) TENURE,
add_months(hire_date, 6) REVIEW,
next_day(hire_date, 'FRIDAY'), last_day(hire_date)
FROM employees
WHERE months_between(sysdate, hire_date) < 36;
隐式转换 = 自动转换
VARCHAR2或CHAR<自动互相转换>NUMBER
VARCHAR2或CHAR<自动互相转换>DATE
显式转换 = 手动转换
to_char(date,'format_model')
日期转指定样式SELECT employee_id, to_char(hire_date, 'MM/YY') Month_Hired
FROM employees
WHERE last_name = 'Higgins';
日期格式样式:
YYYY用数字表示的完整年份
YEAR用字母拼写的年份
MM代表月份的两位数值
MONTH月份的全名
MON代表月份的三个字母缩写
DY代表星期几的三个字母缩写
DAY星期几的全名
DD用数字表示的月份中的某日
时间格式转换
HH24:MI:SS AM–> 15:45:32 PM
添加字符串
DD “of” MONTH–> 12 of OCTOBER
时间格式样式:
AM或PM正午指示符
A.M.或P.M带有句点的正午指示符
HH或HH12或HH24天中的小时,或小时(1-12),或小时(0-23),SS秒(0-59)
SSSSS午夜之后的秒数(0-86399)
查询所有员工的姓氏和聘用日期,聘用日期的显示样式为 “17June1987”
SELECT last_name,
to_char(hire_date, 'fmDD Month YYYY') AS HIREDATE
FROM employees;
此为中文版显示方法,在英文版中月份显示为 June 这样的月份
按 “Seventh of June 1994 12:00:00AM” 格式来显示日期
SELECT last_name,
to_char(hire_date,'fmDdspth "of" Month YYYY fmHH:MI:SS AM') HIREDATE
FROM employees;
SELECT to_char(salary, '$99,999.00') SALARY
FROM employees
WHERE last_name = 'Ernst';
NVL (expr1, expr2)
SELECT last_name, salary, NVL(commission_pct, 0),
(salary*12) + (salary*12*NVL(commission_pct, 0)) AN_SAL
FROM employees;
NVL2(expr1, expr2, expr3)
SELECT last_name, salary, commission_pct,
NVL2(commission_pct,
'SAL+COMM', 'SAL') income
FROM employees WHERE department_id IN (50, 80);
NULLIF (expr1, expr2)
SELECT first_name, LENGTH(first_name) "expr1",
last_name, LENGTH(last_name) "expr2",
NULLIF(LENGTH(first_name), LENGTH(last_name)) result
FROM employees;
COALESCE (expr1, expr2, ... exprn)
SELECT last_name,
COALESCE(commission_pct, salary, 10) comm
FROM employees
ORDER BY commission_pct;
如果JOBID为ITPROG,则薪金增加10%;如果JOBID为STCLERK,薪金增加15%;如果JOBID为SAREP,薪金就增加20%。如果是其它职务角色,就不增加薪金
SELECT last_name, job_id, salary,
CASE job_id WHEN 'IT_PROG' THEN 1.10*salary
WHEN 'ST_CLERK' THEN 1.15*salary
WHEN 'SA_REP' THEN 1.20*salary
ELSE salary END "REVISED_SALARY"
FROM employees;
上述语句等效于下述语句
SELECT last_name, job_id, salary,
DECODE(job_id, 'IT_PROG', 1.10*salary,
'ST_CLERK', 1.15*salary,
'SA_REP', 1.20*salary,
salary)
REVISED_SALARY
FROM employees;
月薪范围(Monthly Salary Range)
$0.00-1999.99
$2,000.00-3,999.99
$4,000.00-5,999.99
$6,000.00-7,999.99
$8,000.00-9,999.99
$10,000.00-11,999.99
$12,200.00-13,999.99
$14,000.00或更高税率(Rate)
00%
09%
20%
30%
40%
42%
44%
45%
查询部门80中每位员工的适用税率
SELECT last_name, salary,
DECODE (TRUNC(salary/2000, 0),
0, 0.00,
1, 0.09,
2, 0.20,
3, 0.30,
4, 0.40,
5, 0.42,
6, 0.44,
0.45) TAX_RATE
FROM employees
WHERE department_id = 80;
SELECT employees.employee_id, employees.last_name, employees.department_id,
departments.department_id, departments.location_id
FROM employees, departments
WHERE employees.department_id = departments.department_id;
SELECT last_name, employees.department_id, department_name
FROM employees, departments
WHERE employees.department_id = departments.department_id
AND last_name = 'Matos';
SELECT e.employee_id, e.last_name, e.department_id,
d.department_id, d.location_id
FROM employees e , departments d
WHERE e.department_id = d.department_id;
SELECT e.last_name, e.salary, j.grade_level
FROM employees e, job_grades j
WHERE e.salary
BETWEEN j.lowest_sal AND j.highest_sal;
(+) 可位于 = 任意一侧,但不能两侧都有
SELECT table1.column, table2.column
FROM table1, table2
WHERE table1.column(+) = table2.column;
SELECT table1.column, table2.column
FROM table1, table2
WHERE table1.column = table2.column(+);
查询员工姓氏、部门标识以及部门名称
SELECT e.last_name, e.department_id, d.department_name
FROM employees e, departments d
WHERE e.department_id(+) = d.department_id ;
外联结运算符只能出现在表达式的一侧,即缺少信息的那一侧。它将从一个表中返回在另一个表中没有直接匹配的行。
包含外联结的条件不能使用 IN 运算符,也不能通过 OR 运算符链接到另一个条件。
SELECT worker.last_name || ' works for '
|| manager.last_name
FROM employees worker, employees manager
WHERE worker.manager_id = manager.employee_id ;
SELECT last_name, department_name
FROM employees
CROSS JOIN departments ;
SELECT last_name, department_name
FROM employees, departments;
两表的 location_id 相同,查询出 location_id 相同的行
SELECT department_id, department_name,
location_id, city
FROM departments
NATURAL JOIN locations ;
SELECT department_id, department_name,
departments.location_id, city
FROM departments, locations
WHERE departments.location_id = locations.location_id;
查询限制 department_id 为 20 或 50 的行
SELECT department_id, department_name,
location_id, city
FROM departments
NATURAL JOIN locations
WHERE department_id IN (20, 50);
SELECT l.city, d.department_name
FROM locations l JOIN departments d USING (location_id)
WHERE location_id = 1400;
SELECT e.employee_id, e.last_name, d.location_id
FROM employees e JOIN departments d
USING (department_id) ;
SELECT employee_id, last_name,
employees.department_id, location_id
FROM employees, departments
WHERE employees.department_id = departments.department_id;
SELECT e.employee_id, e.last_name, e.department_id,
d.department_id, d.location_id
FROM employees e JOIN departments d
ON (e.department_id = d.department_id);
SELECT e.last_name emp, m.last_name mgr
FROM employees e JOIN employees m
ON (e.manager_id = m.employee_id);
SELECT employee_id, city, department_name
FROM employees e
JOIN departments d
ON d.department_id = e.department_id
JOIN locations l
ON d.location_id = l.location_id;
SELECT employee_id, city, department_name
FROM employees, departments, locations
WHERE employees.department_id = departments.department_id
AND departments.location_id = locations.location_id;
SELECT e.last_name, e.department_id, d.department_name
FROM employees e
LEFT OUTER JOIN departments d
ON (e.department_id = d.department_id) ;
SELECT e.last_name, e.department_id, d.department_name
FROM employees e, departments d
WHERE d.department_id (+) = e.department_id;
SELECT e.last_name, e.department_id, d.department_name
FROM employees e
RIGHT OUTER JOIN departments d
ON (e.department_id = d.department_id) ;
SELECT e.last_name, e.department_id, d.department_name
FROM employees e, departments d
WHERE d.department_id = e.department_id (+);
SELECT e.last_name, e.department_id, d.department_name
FROM employees e
FULL OUTER JOIN departments d
ON (e.department_id = d.department_id) ;
SELECT e.employee_id, e.last_name, e.department_id,
d.department_id, d.location_id
FROM employees e JOIN departments d
ON (e.department_id = d.department_id)
AND e.manager_id = 149 ;
SELECT AVG(salary), MAX(salary),
MIN(salary), SUM(salary)
FROM employees
WHERE job_id LIKE '%REP%';
SELECT MIN(hire_date), MAX(hire_date)
FROM employees;
返回表中行的数量
SELECT COUNT(*)
FROM employees
WHERE department_id = 50;
显示 EMPLOYEES 表中部门值的数量,不包括空值
SELECT COUNT(commission_pct)
FROM employees
WHERE department_id = 80;
显示 EMPLOYEES 表中不重复的部门值的数量
SELECT COUNT(DISTINCT department_id)
FROM employees;
SELECT AVG(commission_pct)
FROM employees;
SELECT AVG(NVL(commission_pct, 0))
FROM employees;
查询不同部门的员工的平均薪水
SELECT department_id, AVG(salary)
FROM employees
GROUP BY department_id ;
SELECT AVG(salary)
FROM employees
GROUP BY department_id ;
SELECT department_id, AVG(salary)
FROM employees
GROUP BY department_id
ORDER BY AVG(salary);
错误示例:
SELECT department_id, COUNT(last_name)
FROM employees;
纠正后:
SELECT department_id, count(last_name)
FROM employees
GROUP BY department_id;
错误示例:
SELECT department_id, AVG(salary)
FROM employees
WHERE AVG(salary) > 8000
GROUP BY department_id;
纠正后:
SELECT department_id, AVG(salary)
FROM employees
HAVING AVG(salary) > 8000
GROUP BY department_id;
查询最高薪金高于$10,000的部门的部门编号和最高薪金
SELECT department_id, MAX(salary)
FROM employees
GROUP BY department_id
HAVING MAX(salary)>10000 ;
查询工资总额超过$13,000的每个职务的职务标识和月薪总额。该示例排除了销售代表,并按照月薪总额对列表进行排序
SELECT job_id, SUM(salary) PAYROLL
FROM employees
WHERE job_id NOT LIKE '%REP%'
GROUP BY job_id
HAVING SUM(salary) > 13000
ORDER BY SUM(salary);
查询最高平均薪水
SELECT MAX(AVG(salary))
FROM employees
GROUP BY department_id;
内部查询用于确定员工Abel的薪金。外部查询采用内部查询的结果,并使用此结果来显示薪金超过此数额的所有员工。
SELECT last_name
FROM employees
WHERE salary >
(SELECT salary
FROM employees
WHERE last_name = 'Abel');
显示职务标识和员工141相同的员工
SELECT last_name, job_id
FROM employees
WHERE job_id =
(SELECT job_id
FROM employees
WHERE employee_id = 141);
查询职务标识和员工141相同,并且薪金高于员工143的员工
SELECT last_name, job_id, salary
FROM employees
WHERE job_id =
(SELECT job_id
FROM employees
WHERE employee_id = 141)
AND salary >
(SELECT salary
FROM employees
WHERE employee_id = 143);
查询薪金等于最低薪金的所有员工的姓氏、职务标识和薪金
SELECT last_name, job_id, salary
FROM employees
WHERE salary =
(SELECT MIN(salary)
FROM employees);
SELECT department_id, MIN(salary)
FROM employees
GROUP BY department_id
HAVING MIN(salary) >
(SELECT MIN(salary)
FROM employees
WHERE department_id = 50);
查找平均薪资最低的工作
SELECT job_id, AVG(salary)
FROM employees
GROUP BY job_id
HAVING AVG(salary) = (SELECT MIN(AVG(salary))
FROM employees
GROUP BY job_id);
错误示例:
SELECT employee_id, last_name
FROM employees
WHERE salary =
(SELECT MIN(salary)
FROM employees
GROUP BY department_id);
查薪金等于各个部门最低薪金的所有员工
SELECT last_name, salary, department_id
FROM employees
WHERE salary IN (SELECT MIN(salary)
FROM employees
GROUP BY department_id);
查询职务不是IT程序员并且薪金低于任何一个IT程序员的员工,程序员的最高薪金为$9,000
SELECT employee_id, last_name, job_id, salary
FROM employees
WHERE salary < ANY
(SELECT salary
FROM employees
WHERE job_id = 'IT_PROG')
AND job_id <> 'IT_PROG';
查询薪金低于所有职务标识为 IT_PROG 的员工且职务不是 IT_PROG 的员工
SELECT employee_id, last_name, job_id, salary
FROM employees
WHERE salary < ALL
(SELECT salary
FROM employees
WHERE job_id = 'IT_PROG')
AND job_id <> 'IT_PROG';
显示没有任何下属的所有员工
SELECT emp.last_name
FROM employees emp
WHERE emp.employee_id NOT IN
(SELECT mgr.manager_id
FROM employees mgr);
该SQL语句没有返回任何行,因为内部查询返回的其中一个值是空值,所以整个查询不会返回任何行
查询具有下属的员
SELECT emp.last_name
FROM employees emp
WHERE emp.employee_id IN
(SELECT mgr.manager_id
FROM employees mgr);
SELECT last_name FROM employees
WHERE employee_id NOT IN
(SELECT manager_id
FROM employees
WHERE manager_id IS NOT NULL);
使用前缀有和号(&)的变量可以提示用户输入值
SELECT employee_id, last_name, salary, department_id
FROM employees
WHERE employee_id = &employee_num ;
查询会根据 iSQL*Plus 替代变量的职称值检索
SELECT last_name, department_id, salary*12
FROM employees
WHERE job_id = '&job_title' ;
查询满足任何条件的 员工编号 和 任何其它列
SELECT employee_id, &column_name
FROM employees
WHERE &condition;
查询 EMPLOYEES 表中的员 工编号、姓名、职称 和 用户在运行时指定的任何其它列
SELECT employee_id, last_name, job_id,
&column_name
FROM employees
WHERE &condition
ORDER BY &order_column ;
使用 DEFINE 命令创建一个员工编号的 iSQL*Plus 替代变量,并且在运行时显示该员工的员工编号、姓名、薪金和部门编号
DEFINE employee_num = 200;
SELECT employee_id, last_name, salary, department_id
FROM employees
WHERE employee_id = &employee_num ;
SELECT employee_id, last_name, job_id, &&column_name
FROM employees
ORDER BY &column_name;
SET VERIFY ON
SELECT employee_id, last_name, salary, department_id
FROM employees
WHERE employee_id = &employee_num;
SET system variable value
SET ECHO ON
SHOW ECHO
echo ON
如:
SET HEADING OFF
SHOW HEADING
HEADING OFF
创建列标题
COLUMN last_name HEADING 'Employee|Name'
COLUMN salary JUSTIFY LEFT FORMAT $99,990.00
COLUMN manager FORMAT 999999999 NULL 'No manager
显示 LAST_NAME 当前设置
COLUMN last_name
COLUMN last_name CLEAR
9 取消前导零的数字 999999 123
0 加上前导零 099999 001234
$ 浮动的美元记号 $9999 $1234
L 当地货币 L9999 L1234
. 小数点的位置 9999.99 1234.00
, 千位分隔符 9,999 1,234
BREAK ON job_id
CLEAR BREAK
TTITLE [text|OFF|ON]
TTITLE 'Salary|Report'
BTITLE 'Confidential'
创建一个脚本文件,用它创建一个报表,以便显示薪金低于 $15,000 的所有员工的职务标识、姓氏和薪金。添加一个居中的两行题头 “Employee Report” 和一个居中的页脚 “Confidential” 。将职称列重命名为跨两行的 “Job Category” 。将员工姓名列重命名为 “Employee” 。将薪金列重命名为 “Salary” ,并将其格式设置为 $2,500.00 的样式。
SET FEEDBACK OFF
TTITLE 'Employee|Report'
BTITLE 'Confidential'
BREAK ON job_id
COLUMN job_id HEADING 'Job|Category'
COLUMN last_name HEADING 'Employee'
COLUMN salary HEADING 'Salary' FORMAT $99,999.99
REM ** Insert SELECT statement
SELECT job_id, last_name, salary
FROM employees
WHERE salary < 15000
ORDER BY job_id, last_name
/
REM clear all formatting commands ...
SET FEEDBACK ON
COLUMN job_id CLEAR
COLUMN last_name CLEAR
COLUMN salary CLEAR
CLEAR BREAK
...
INSERT INTO departments(department_id, department_name,
manager_id, location_id)
VALUES (70, 'Public Relations', 100, 1700);
隐式:在列列表中省略该列
INSERT INTO departments (department_id,
department_name )
VALUES (30, 'Purchasing');
显试:在 VALUES 子句中指定 NULL 关键字
INSERT INTO departments
VALUES (100, 'Finance', NULL, NULL);
INSERT INTO employees (employee_id,
first_name, last_name,
email, phone_number,
hire_date, job_id, salary,
commission_pct, manager_id,
department_id)
VALUES (113,
'Louis', 'Popp',
'LPOPP', '515.124.4567',
SYSDATE, 'AC_ACCOUNT', 6900,
NULL, 205, 100);
在 EMPLOYEES 表中记录了员工 Raphealy 的信息,将 HIRE_DATE 列设置为1999年2月3日
INSERT INTO employees
VALUES (114,
'Den', 'Raphealy',
'DRAPHEAL', '515.127.4561',
TO_DATE('FEB 3, 1999', 'MON DD, YYYY'),
'AC_ACCOUNT', 11000, NULL, 100, 30);
如果使用下面的语句而不是上述语句,hire_date 的年份将被解释为2099
INSERT INTO employees
VALUES (114,
'Den', 'Raphealy',
'DRAPHEAL', '515.127.4561',
'03-FEB-99',
'AC_ACCOUNT', 11000, NULL, 100, 30);
INSERT INTO departments
(department_id, department_name, location_id)
VALUES (&department_id, '&department_name',&location);
INSERT INTO sales_reps(id, name, salary, commission_pct)
SELECT employee_id, last_name, salary, commission_pct
FROM employees
WHERE job_id LIKE '%REP%';
创建表中行的复本,应在子查询中使用 SELECT *
INSERT INTO copy_emp
SELECT *
FROM employees;
UPDATE employees
SET department_id = 70
WHERE employee_id = 113;
UPDATE copy_emp
SET department_id = 110;
更新员工 114 的 职务 和 薪金,使之与员工 205 的 职务 和 薪金 相同
UPDATE employees
SET job_id = (SELECT job_id
FROM employees
WHERE employee_id = 205),
salary = (SELECT salary
FROM employees
WHERE employee_id = 205)
WHERE employee_id = 114;
根据另一个表中的 值,在 UPDATE 语句中使用 子查询 更新表中的 行
UPDATE copy_emp
SET department_id = (SELECT department_id
FROM employees
WHERE employee_id = 100)
WHERE job_id = (SELECT job_id
FROM employees
WHERE employee_id = 200);
DELETE FROM departments
WHERE department_name = 'Finance';
DELETE FROM copy_emp;
在 DELETE 语句中使用子查询,以便根据另一个表中的值来删除表中的行
DELETE FROM employees
WHERE department_id =
(SELECT department_id
FROM departments
WHERE department_name LIKE '%Public%');
错误示例:
示例尝试从 EMPLOYEES 表中删除部门编号60,但由于部门编号被用作EMPLOYEES表的外键,因而产生一个错误
DELETE FROM departments
WHERE department_id = 60;
下面的语句之所以有效是因为部门70中没有员工
DELETE FROM departments
WHERE department_id = 70;
INSERT INTO
(SELECT employee_id, last_name,
email, hire_date, job_id, salary,
department_id
FROM employees
WHERE department_id = 50)
VALUES (99999, 'Taylor', 'DTAYLOR',
TO_DATE('07-JUN-99', 'DD-MON-RR'),
'ST_CLERK', 5000, 50);
示例中,使用了 WITH CHECKOPTION 关键字。子查询中指定了查询部门 50 中的行,但部门标识在 SELECT 中未指定,而且 VALUES 列表中也未提供部门标识的值。插入这样的行会产生为空值的部门标识,该部门标识不在子查询中
INSERT INTO (SELECT employee_id, last_name, email,
hire_date, job_id, salary
FROM employees
WHERE department_id = 50 WITH CHECK OPTION)
VALUES (99998, 'Smith', 'JSMITH',
TO_DATE('07-JUN-99', 'DD-MON-RR'),
'ST_CLERK', 5000);
示例中INSERT 语句使用了 MANAGER_ID 列的默认值。如果没有为该列定义默认值,则会插入一个空值
INSERT INTO departments
(department_id, department_name, manager_id)
示例使用 UPDATE 语句将部门 10 的 MANAGER_ID 列设置为默认值。如果没有为该列定义默认值,则会将其更改为空值
UPDATE departments
SET manager_id = DEFAULT WHERE department_id = 10;
MERGE INTO copy_emp c
USING employees e
ON (c.employee_id = e.employee_id)
WHEN MATCHED THEN
UPDATE SET
c.first_name = e.first_name,
c.last_name = e.last_name,
...
c.department_id = e.department_id
WHEN NOT MATCHED THEN
INSERT VALUES (e.employee_id, e.first_name, e.last_name,
e.email, e.phone_number, e.hire_date, e.job_id,
e.salary, e.commission_pct, e.manager_id,
e.department_id);
示例将 COPYEMP 表中的 EMPLOYEE_ID与 EMPLOYEES 表中的 EMPLOYEE_ID匹配。如果发现匹配项,就更新COPY_EMP 中的行,使之与 EMPLOYEES 表中的行匹配。如果没找到该行,则将其插入到COPY_EMP表中
MERGE INTO copy_emp c
USING employees e
ON (c.employee_id = e.employee_id)
WHEN MATCHED THEN
UPDATE SET
c.first_name = e.first_name,
c.last_name = e.last_name,
c.email = e.email,
c.phone_number = e.phone_number,
c.hire_date = e.hire_date,
c.job_id = e.job_id,
c.salary = e.salary,
c.commission_pct = e.commission_pct,
c.manager_id = e.manager_id,
c.department_id = e.department_id
WHEN NOT MATCHED THEN
INSERT VALUES (e.employee_id, e.first_name, e.last_name,
e.email, e.phone_number, e.hire_date, e.job_id,
e.salary, e.commission_pct, e.manager_id,
e.department_id);
使用 COMMIT 和 ROLLBACK 语句,可以:
使用COMMIT、SAVEPOINT 和 ROLLBACK 语句可以控制事务处理的逻辑
使用 SAVEPOINT 语句在当前事务处理中创建一个标记
使用 ROLLBACK TO SAVEPOINT 语句回退到该标记处
UPDATE...
SAVEPOINT update_done;
INSERT...
ROLLBACK TO update_done;
进行更改
DELETE FROM employees
WHERE employee_id = 99999;
INSERT INTO departments
VALUES (290, 'Corporate Tax', NULL, 1700);
提交更改
COMMIT;
Commit complete.