Oracle
SqlServer2000 2005
MySql
DB2
ORACLE sun SUN
数据库 DataBase db
存储数据
数据 Data
数字 符号 字符 信息
DBMS 数据库管理系统
SQL : 结构化查询语言
笔试: scjp sql 20 - 30%
Oracle 10g XE
Oracle 9i
SQL:
CRUD
增删改查
DDL
DML
DCL: DBA
查询数据库中所有的表:
select table_name from user_tables;
SELECT 要查的数据 FROM 从哪里查
1. 查什么
2. 从哪里查
查询 s_emp 所有行所有列
select * from s_emp ;
指定列查询 (字段名之间使用逗号分隔)
select first_name , last_name from s_emp ;
SELECT last_name, salary * 12, commission_pct FROM s_emp;
使用NVL函数空值问题
SELECT last_name, title, salary*commission_pct/100 + salary FROM s_emp;
SELECT last_name, title, salary*NVL(commission_pct,0)/100 + salary FROM s_emp;
字段别名
as 别名 " 别 名 "
字符连接
select first_name || last_name " 姓 名 " from s_emp ;
DISTINCT 进行数据排重
第二章:限定条件查询
SELECT 要查询的字段落 FROM 哪张表 WHERE 条件
1.查什么
2.从哪里查
3.条件是什么
OrderBy 排序问题:
//默认情况是升序
select last_name , salary from s_emp order by salary;
desc 降序
asc 升序
指定多个字段进行排序
select last_name , salary from s_emp order by salary desc , last_name asc ;
指定列进行排序
SELECT last_name, salary * 12 FROM s_empORDER BY 2;
限定条件:
between and 闭区间
select last_name , salary from s_emp where salary between 1400 and 3000;
select last_name , salary from s_emp where salary > 1400 and salary <3000 ;
in (list )代表范围
select name , region_id from s_Dept where region_id in (1,3,5);
select name , region_id from s_Dept where region_id = 1 or region_id = 3;
like 查询
% 0或多个字符
_ 只有一个
查出所有以 S_ 开头的表名
select table_name from user_tables where table_name like 'S_%';
SELECT table_name FROM user_tables WHERE table_name LIKE ‘S/_%‘ ESCAPE ‘/’;
使用 and or 查询
SELECT last_name, salary, dept_id
FROM s_emp
WHERE salary >= 1000
AND dept_id = 44
OR dept_id = 42;
SELECT last_name, salary, dept_id
FROM s_emp
WHERE salary >= 1000
AND (dept_id = 44
OR dept_id = 42);
Logical comparison operators
= > >= < <= != <> ^=
SQL comparison operators
BETWEEN ... AND... [闭区间 ]
IN(list)
LIKE % _
IS NULL
Logical operators
AND
OR
NOT
三:单行函数
字符
LOWER('SQL Course') sql course
UPPER('SQL Course') SQL COURSE
INITCAP('SQL Course') Sql Course
CONCAT('Good', 'String') GoodString
SUBSTR('String',1,3) Str
LENGTH('String') 6
日期
sysdate
sydate + 1 操作单位是天数
MONTHS_BETWEEN(sysdate , sysdate - 40 ) 无限接近于一个月份小数
select ADD_MONTHS(sysdate, -1) from dual;
求上一个月的第一天
select LAST_DAY( add_months(sysdate , -2 ))+1 from dual;
select ROUND(to_date('2010-1-15','yyyy-mm-dd'),'MONTH') from dual;
数字
转换
to_char
select to_char(sysdate , 'yyyy-mm-dd') from dual;
to_number
to_date
哑表 dual
YYYY represents the full year in numbers.
YEAR represents the year spelled out.
MM represents the 2-digit value for month.
MONTH represents the full name of the month.
DY represents the 3-letter abbreviation of the day of the week.
DAY represents the full name of the day.
1. Select 语句
所有行所有列
select * from table_name
指定列
select 字段, 字段 from table_name ;
排重
order by 排序
select * from table_name order by 字段, 字段2
desc
asc
2 限定条件查询
select * from table_name where 条件
3. 单行函数
nvl(字段,默认值)
字符
数字
日期
sysdate + 1
转换
to_char
to_number
to_date
--求今天是几号
select to_number( to_char(sysdate , 'dd')) from dual;
select to_char(sysdate , 'fmdd') from dual;
--查出 s_emp 中在7月份入职的员工信息
select * from s_emp where to_char( start_date ,'mm' ) = '07';
select * from s_emp where to_number(to_char( start_date ,'mm' ) ) = 7;
-- 求上个月月未(多种形式)
select last_day( add_months(sysdate , -1) ) from dual;
select trunc(sysdate,'MONTH') - 1 from dual;
-- 求年初至今天为止总共天数
select sysdate - trunc(sysdate , 'Year') from dual;
select sysdate - trunc(sysdate ,'YEAR') from dual;
select ceil( sysdate - trunc(sysdate ,'YEAR')) from dual;
select ceil(7.8) from dual;
关联查询:
等值连接
--查出所有员工的姓名及员工所在的部门名称
select last_name , name
from s_emp , s_dept
where s_emp.dept_id = s_dept.id ;
非等值连接
o(∩_∩)o
内连接
外连接
自连接
//交叉连接 迪卡尔积
select last_name 姓名 , name 部门名 from s_emp , s_dept;
关联查询:
SELECT table1.column, table2.column
FROM table1, table2
WHERE table1.column1 = table2.column2;
1.查什么
2.从哪里查
3.条件
select e.last_name , d.name
from s_emp e , s_dept d
where e.dept_id = d.id ;
--查出 Carmen 个人信息及其所在部门名称
--等值连接查询
select e.last_name , d.name
from s_emp e , s_dept d
where e.dept_id = d.id and e.last_name = 'Carmen';
select e.first_name , e.last_name , d.name
from s_emp e , s_dept d
where e.dept_id = d.id and lower(e.first_name) = 'carmen';
--内联语法实现 inner join ...... on
--查出所有员工的姓名及员工所在的部门名称 (内联语法)
select e.last_name , d.name
inner join s_emp e , s_dept d
on e.dept_id = d.id ;
SELECT 表1.字段 , 表2.字段
FROM 表1 inner join 表2
on 关联条件
--查出员工工资大于 1500 所有员工的信息及其所在部门名称
--非等值连接查询
select e.first_name , e.last_name , d.name
from s_emp e , s_dept d
where e.dept_id = d.id and e.salary > 1500
--内联实现形式
select e.first_name , e.last_name , d.name
from s_emp e inner join s_dept d
on e.dept_id = d.id where e.salary > 1500
--自连接: 同一张表中自身关联查询
--少了一个数据(最大领导)
select e.last_name 员工名 , l.last_name 领导名
from s_emp e , s_emp l
where e.manager_id = l.id
--外连接
--右外
select e.last_name 员工名 , l.last_name 领导名
from s_emp e right outer join s_emp l
on l.id = e.manager_id ;
--oracle 特有(+)
select e.last_name 员工名 , l.last_name 领导名
from s_emp e , s_emp l
where l.id = e.manager_id(+) ;
--左外
--oracle 特有(+)
select e.last_name 员工名 , l.last_name 领导名
from s_emp e , s_emp l
where l.id(+) = e.manager_id ;
select e.last_name 员工名 , l.last_name 领导名
from s_emp e right outer join s_emp l
on l.id = e.manager_id ;
oracle特有(+)
1. 有(+) 一边,可能会出现空值
交叉连接
等值连接 非等值
内连接 inner join on
外连接 outer join on
左外 left outer join on
右外 right outer join on
5. 组函数
SELECT column, group_function
FROM table
[WHERE condition]
[GROUP BY group_by_expression]
[HAVING group_condition]
[ORDER BY column];
--查询s_emp 表中,工资最高,最资最低,平均工资,工资总和
--查出各个部门最低工资
select dept_id , min(salary) from s_emp
group by dept_id ;
--查出各个部门最低工资大于 1000
select dept_id , min(salary) from s_emp
group by dept_id
having min(salary) > 1000 ;