码迷,mamicode.com
首页 > 数据库 > 详细

Oracle442个应用场景------------基础应用场景

时间:2015-07-09 08:33:24      阅读:170      评论:0      收藏:0      [点我收藏+]

标签:

 

/////////////////基础知识//////////////////

 


应用场景178:最简单的select语句

SELECT * FROM Employees;

应用场景179:指定要查询的列

COL EMP_NAME FORMAT A20
COL SEX FORMAT A10
COL TITLE FORMAT A10
SELECT Emp_name, Sex, Title FROM HRMAN.Employees;


应用场景180:使用DISTINCT关键字

SELECT Title FROM HRMAN.Employees;

SELECT DISTINCT Title FROM HRMAN.Employees;

应用场景181:使用ROWNUM

COL EMP_NAME FORMAT A20
COL SEX FORMAT A10
COL TITLE FORMAT A10
SELECT Emp_name, Sex, Title FROM HRMAN.Employees
WHERE ROWNUM<=3;

应用场景182:改变显示的列标题

COL 姓名 FORMAT A20
COL 性别 FORMAT A4
COL 职务 FORMAT A10
COL 身份证 FORMAT A20
SELECT EMP_NAME AS 姓名, SEX AS 性别, TITLE AS 职务, WAGE AS 工资, IDCARD AS 身份证 FROM HRMAN.Employees;


应用场景183:设置查询条件


COL EMP_NAME FORMAT A20
SELECT Emp_Name, Wage FROM HRMAN.Employees WHERE Wage > 3000 AND Wage < 4000;


应用场景184:在查询条件中使用BITWEEN 关键字

COL EMP_NAME FORMAT A20
SELECT Emp_Name, Wage FROM HRMAN.Employees WHERE Wage BETWEEN 3000 AND 4000;


应用场景185:在查询条件中使用IN关键

COL EMP_NAME FORMAT A20
COL TITLE FORMAT A20
SELECT EMP_NAME, TITLE, WAGE FROM HRMAN.Employees WHERE Emp_name IN (‘张三‘, ‘李四‘, ‘王五‘);

应用场景186:实现模糊查询

COL EMP_NAME FORMAT A20
COL TITLE FORMAT A20
COL IDCard FORMAT A20
SELECT EMP_NAME, TITLE, IDCard FROM HRMAN.Employees
WHERE IDCard LIKE ‘%ddd%‘;

COL EMP_NAME FORMAT A20
COL TITLE FORMAT A20
COL IDCard FORMAT A20
SELECT EMP_NAME, TITLE, IDCard FROM HRMAN.Employees
WHERE IDCard LIKE ‘110123_adx_‘;


应用场景187:排序结果集

COL EMP_NAME FORMAT A20
COL TITLE FORMAT A20
COL IDCard FORMAT A20
SELECT EMP_NAME, TITLE, IDCard FROM HRMAN.Employees
ORDER BY Emp_name;

COL EMP_NAME FORMAT A20
COL TITLE FORMAT A20
SELECT EMP_NAME, TITLE, Wage FROM HRMAN.Employees
ORDER BY Wage DESC;


应用场景188:对多列进行排序

COL EMP_NAME FORMAT A20
COL SEX FORMAT A20
SELECT EMP_NAME, Sex, Wage FROM HRMAN.Employees
ORDER BY Sex, Wage;


应用场景189:使用分组统计

COL 职务 FORMAT A10
SELECT Title AS 职务, AVG(Wage) AS 平均公资 FROM HRMAN.Employees GROUP BY Title;

COL Sex FORMAT A10
COL Title FORMAT A10
SELECT Sex, Title, AVG(Wage) FROM HRMAN.Employees GROUP BY Title;

SELECT Dep_Id, AVG(Wage) FROM HRMAN.Employees
GROUP BY Dep_Id HAVING AVG(Wage) > 4000;


应用场景190:连接查询

内连接
COL Dep_name FORMAT A20
COL Emp_name FORMAT A20
SELECT  t1.Dep_name, t2.Emp_name FROM HRMAN.Departments t1, HRMAN.Employees t2
WHERE t1.Dep_id=t2.Dep_id;

外链接:
COL Dep_name FORMAT A20
COL Emp_name FORMAT A20
SELECT  t1.Dep_name, t2.Emp_name FROM HRMAN.Departments t1 INNER JOIN HRMAN.Employees t2
ON t1.Dep_id=t2.Dep_id;


COL Dep_name FORMAT A20
COL Emp_name FORMAT A20
SELECT  t1.Dep_name, t2.Emp_name FROM HRMAN.Departments t1 LEFT JOIN HRMAN.Employees t2
ON t1.Dep_id=t2.Dep_id;

COL Dep_name FORMAT A20
COL Emp_name FORMAT A20
SELECT  t1.Dep_name, t2.Emp_name FROM HRMAN.Employees t2 RIGHT JOIN HRMAN.Departments t1
ON t1.Dep_id=t2.Dep_id;


COL Dep_name FORMAT A20
COL Emp_name FORMAT A20
SELECT  t1.Dep_name, t2.Emp_name FROM HRMAN.Employees t2 FULL JOIN HRMAN.Departments t1
ON t1.Dep_id=t2.Dep_id;

交叉连接
COL Dep_name FORMAT A20
COL Emp_name FORMAT A20
SELECT  t1.Dep_name, t2.Emp_name FROM HRMAN.Employees t2 CROSS JOIN HRMAN.Departments t1;


应用场景191:在连接查询中对空值中对空值的判断

COL Dep_name FORMAT A20
COL Emp_name FORMAT A20
SELECT  t1.Dep_name, t2.Emp_name FROM HRMAN.Departments t1 LEFT JOIN HRMAN.Employees t2
ON t1.Dep_id=t2.Dep_id;

COL Dep_name FORMAT A20
COL Emp_name FORMAT A20
SELECT  t1.Dep_name, t2.Emp_name FROM HRMAN.Departments t1 LEFT JOIN HRMAN.Employees t2
ON t1.Dep_id=t2.Dep_id
WHERE t2.Emp_id IS NULL;


应用场景192:一个简单地子查询

查询办公室的所有员工:
COL Emp_name FORMAT A20
COL Title FORMAT A20
SELECT Emp_Name, Title FROM HRMAN.Employees WHERE Dep_Id =
(SELECT Dep_Id FROM HRMAN.Departments WHERE Dep_name = ‘办公室‘);

返回两个部门的值:
COL Emp_name FORMAT A20
COL Title FORMAT A20
SELECT Emp_Name, Title FROM HRMAN.Employees WHERE Dep_Id =
(SELECT Dep_Id FROM HRMAN.Departments WHERE Dep_name = ‘办公室‘ OR Dep_name = ‘人事部‘);


应用场景193:在查询中使用具和函数返回值

统计表中所有工资低于品级工资的员工的信息

COL Emp_name FORMAT A20
COL Title FORMAT A20
SELECT Emp_name, Title, Wage FROM HRMAN.Employees WHERE Wage <
(SELECT AVG(Wage) FROM HRMAN.Employees);

应用场景194:IN关键字与返回值的子查询

查询办公室和人事部中的员工信息

COL Emp_name FORMAT A20
COL Title FORMAT A20
SELECT Emp_name, Title, Wage FROM HRMAN.Employees WHERE Dep_Id IN
(SELECT Dep_Id FROM HRMAN.Departments WHERE Dep_name = ‘办公室‘ OR Dep_name = ‘人事部‘);


应用场景195:EXISTS关键字与子查询

查询人事部中的员工信息
COL Emp_name FORMAT A20
COL Title FORMAT A20
SELECT Emp_name, Title, Wage FROM HRMAN.Employees e
WHERE EXISTS
(SELECT Dep_id FROM HRMAN.Departments d
WHERE e.Dep_id = d.Dep_id AND d.Dep_name=‘人事部‘);

使用IN关键字:
COL Emp_name FORMAT A20
COL Title FORMAT A20
SELECT Emp_name, Title, Wage FROM HRMAN.Employees e
WHERE e.Dep_id IN
(SELECT Dep_id FROM HRMAN.Departments d
WHERE e.Dep_id = d.Dep_id AND d.Dep_name=‘人事部‘);


应用场景196:使用UNION关键字的合并查询

从表中Employee中查询各个部门的部门经理

COL Dep_name FORMAT A20
COL Emp_name FORMAT A20
SELECT Dep_Id, Dep_Name FROM HRMAN.Departments
UNION
SELECT Dep_Id, Emp_Name FROM HRMAN.Employees WHERE Title = ‘部门经理‘;


工资大于3000的员工记录:

COL Emp_name FORMAT A20
COL Title FORMAT A20
SELECT Emp_name, Title, Wage FROM HRMAN.Employees
UNION
SELECT Emp_name, Title, Wage FROM HRMAN.Employees WHERE Wage > 3000;

高效率:
COL Emp_name FORMAT A20
COL Title FORMAT A20
SELECT Emp_name, Title, Wage FROM HRMAN.Employees
UNION ALL
SELECT Emp_name, Title, Wage FROM HRMAN.Employees WHERE Wage > 3000;


应用场景197:使用select语句中的DECODE函数

SELECT Emp_name, DECODE(Sex, ‘男‘, ‘先生‘, ‘女‘, ‘女士‘, ‘未知‘) AS Sex
FROM HRMAN.Employees;


应用场景198:使用select与剧中CASE函数

SELECT Emp_name, CASE Sex WHEN ‘男‘ THEN ‘先生‘ WHEN ‘女‘ THEN ‘女士‘ ELSE ‘未知‘ END AS Sex
FROM HRMAN.Employees;


查询表Employees中的员工工资级别:

SELECT Emp_name, Wage, CASE WHEN Wage<=3000 THEN ‘低‘ WHEN Wage>3000 AND Wage<5000 THEN ‘中‘ WHEN Wage>=5000 THEN ‘高‘ END AS GRADE
FROM HRMAN.Employees;


应用场景199:保存查询结果

将办公室的所有员工的姓名和职务信息保存到表中OFFICE:

COL Emp_name FORMAT A20
COL Title FORMAT A20
CREATE TABLE HRMAN.Office AS
SELECT e.Emp_Name, e.Title
FROM HRMAN.Employees e, HRMAN.Departments d
WHERE e.Dep_id = d.Dep_Id AND d.Dep_Name = ‘办公室‘;
SELECT * FROM HRMAN.Office;


应用场景200:插入数据语句

INSERT INTO HRMAN.Departments VALUES(100, ‘公关部‘);
SELECT * FROM HRMAN.Departments;

INSERT INTO HRMAN.Employees (Emp_Name, Sex,Title,  IdCard, Dep_Id)
VALUES (‘小明‘, ‘男‘, ‘职员‘, ‘110123456789‘, 2);


应用场景201:在插入数据时利用默认值

ALTER TABLE HRMAN.Employees ADD InputDate date DEFAULT(sysdate);

INSERT INTO HRMAN.Employees (Emp_Name, Sex,Title,  IdCard, Wage, Dep_Id)
VALUES (‘小李‘, ‘男‘, ‘职员‘, ‘210123456789‘, 2500, 2);


应用场景202:修改数据语句

将表中的所有记录工资增加10%:

UPDATE HRMAN.Employees SET Wage=Wage*1.1;

将表中所有部门为"办公室"的员工工资增加10%

UPDATE HRMAN.Employees SET Wage=Wage*1.1
WHERE Dep_id = (SELECT Dep_id FROM HRMAN.Departments WHERE Dep_name = ‘办公室‘);


应用场景203:修改数据时不允许在唯一性约束列中使用相同的值

ALTER TABLE HRMAN.Employees
ADD CONSTRAINT UK_EMPNAME
UNIQUE(Emp_name);

UPDATE HRMAN.Employees SET Emp_name=‘张三‘ WHERE Emp_name=‘李四‘;

应用场景204:修改数据是不能违反检查约束

ALTER TABLE HRMAN.Employees
ADD CONSTRAINT CK_EMPWAGE CHECK(WAGE>0);

UPDATE HRMAN.Employees SET Wage=-1 WHERE Emp_Name=‘张三‘;

应用场景205:修改数据时不能违反外键约束

为表HRMAN。Departments的DEP_id列和表HRMAN.Employees的DEP_ID列中创建外键约束
ALTER TABLE HRMAN.Employees
ADD CONSTRAINT FK_EMP_DEPID
FOREIGN KEY(Dep_id) REFERENCES HRMAN.Departments(Dep_id);

UPDATE HRMAN.Employees SET Dep_id=200 WHERE Emp_Name=‘张三‘;

应用场景206:删除数据语句

DELETE FROM HRMAN.Employees WHERE Emp_Name = ‘小明‘;

TRUNCATE TABLE HRMAN.Employees;

版权声明:欢迎转载,希望在你转载的同时,添加原文地址,谢谢配合

Oracle442个应用场景------------基础应用场景

标签:

原文地址:http://blog.csdn.net/u011225629/article/details/46811883

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!