数据库技术(1)-SQL语句
(1)基本SQL语句
SQL语句分为一下三种类型:
基本SQL语句
select * from employee;
select employee_id,lat_name,email from employees;
- 注:sql对大小写不敏感
- 日期可以进行加减,但是不可以进行乘除运算
- 空值是无效的,未指定的,未知或不可预知的值
- 空值不是空格或者0
- 包含空值的数学表达式的值都为空值
- 连接符
- 把列与列,列与字符连接起来
- 用 '||'表示,相当于连接符号+s
- 可以用来合成列
- eg.
- select last_name ||' `s job_id is '|job_id
- select employee_id id,last_name name,12*salary annual_sal from [table_name]
- select employee_id as id .... as可加可不加
- select employee_id as "id",last_name "Name",12*salary annual_sal from employee (当需要确定大小写时一定需要加上引号)
- select distinct department_id from employees;
(2) 过滤和排序数据
- 过滤
- eg1.
- select last_name,hire_date from employees where to_char(hire_datem'yyyy-mm-dd')='1994-06-07'
- 过滤条件
- between and(适合连续值):包含边界
- in(适合离散值)
- select last_name,department_id,salary from employees
where department_id in(70,80,90)
- like查询
- eg.员工中含有字符a的员工有哪些
- select last_name,department_id,salary from employees where last_name like '%a%'
- % 表示从0-无穷多个
- eg.员工中名字的第二位是a的员工有哪些
- select last_name,department_id,salary from employees where last_name like '_a%'
- eg.员工中名字的第三位是a的员工有哪些
- select last_name,department_id,salary from employees where last_name like '__a%'
- eg.查询员工中的名字含有下划线的人有哪些
- 假设有个员工为'wgha_lanan'
- select last_name like '%#_%' escape '#'
- select last_name like '%\_%' escape '\'
- is null 与 is not null
- 排序 Order by
- select last_name,department_id,salary from employees
where department_id=80
order by salary asc(升序)
- select last_name,department_id,salary from employees
where department_id=80
order by salary asc,last_name asc
(3) 单行函数(dual对应一个虚表)
select last_name "last_name",job_id "job_id" when 'AD_PERS' then 'A'
when 'ST_MAN' then 'B'
when 'IT_PROG' then 'C'
when 'SA_REP' then 'D'
when 'ST_CLERK' then 'E' end "Grade"
from employees
- decode表达式
select last_name,job_id,decode(job_id,'AD_PERS','A',
'ST_MAN','B',
'IT_PROG','C',
'SA_REP','D',
'ST_CLERK','E') "Grade"
from employees
(4)多表查询
查询出公司员工的last_name,department_name,city:
select last_name,department_name,city
from department d,employees e,location l
where d.department_id=e.department_id and d.location_id=l.location_id;
查询出last_name 为'Chen'的manager的信息 (员工的employee_id等于员工的manager_id)
0) 例如:老张的员工号为:"1001",我的员工号为:"1002"
我的manager_id为 "1001"-----我的manager是"老张"
1) 通过两条sql语句进行查询
select manager_id
from employees
where lower(last_name)='chen' --- 返回的结果为108
select *
from employees
where employee_id=108
2) 同过一条sql查询(自连接)
select m.*
from employees e,employees m
where e.manager_id=m.employee_id and e.last_name='Chen'
3) 通过一条sql查询(子查询)
select *
from employees
where employee_id=(
select manager_id
from employees
where last_name='Chen'
)
查询每个员工的last_name 和 GRADE_LEVEL (在JOB_GRADES 表中)-----非等值连接
select last_name,GRADE_LEVEL
from employees e,job_grades j
where e.salary>=j.lowest_sal and e.salary<=j.hightest_sal
select last_name,e.department_id,department_name
from employee e,department d
where e.department_id=d.department_id(+)
select last_name.e.department_id,department_name
from employee e,department d
where e.department(+)=d.departemnt_id
理解(+)的位置: 以左外连为例,因为在左表需要返回更多的记录,右表就需要加上更多的
记录,因此在右表的链接条件上加上"(+)"
1) 左外连接
select last_name,department_name
from employees e left join departments d
on e.department_id=d.department_id
2) 右外连接
select last_name,department_name
from employees e right join in departments d
on e.departemnt_id=d.department_id
3) 满外连接
select last_name,departemnt_name
from employees e full join in departemnnts d
on e.departemnt_id=d.departemnt_id
1) 缺点:要求两个表中的必须拥有一样会的别名
select *
from employees join departments
using(department_id)
2)
select *
from employees e join departments d
on e.department_id=d.department_id
3) 多表连接
select e.last_name,d.departemnt_name,l.city
from employees e join departments
on e.department_id=d.department_id
join locations l
on d.location_id=l.location_id
(5) 分组函数
分组函数作用于一组数据,并对一组数据返回一个值
不能在WHERE子句中使用组函数
可以在HAVING子句中使用组函数
组函数类型
组函数应用:
select AVG(SALARY) MAX(SALARY) MIN(SALARY) SUM(SALARY)
from employees;
NVL函数使分组隐函数无法忽略空值:
select AVG(NVL(commission_pot,0))
from employees;
COUNT(DISTINCE expr) 返回expr非空且不重复的记录总数
select COUNT(DISTINCT department_id)
from employees;
求出EMPLOYEES表中各部门的平均工资(按照不同部门进行分组)
select department_id,AVG(salary)
from employees
where department_id in(40,60,80)
group by department_id
在SELECT 列表中所有未包含在组函数中的列都应该包含在GROUP BY子句中
HAVING:
select department_id,avg(salary)
from employees
having avg(salary)>6000
group by department_id
order by department_id asc
嵌套组函数
select max(avg(salary))
from employees
查询公司在1995-1998之间,每年雇佣的人数的格式
select count(*) "total"
count(decode(to_char(hire_date,'yyyy').'1995',1,null)) "1995"
count(decode(to_char(hire_date,'yyyy').'1996',1,null)) "1996" count(decode(to_char(hire_date,'yyyy').'1997',1,null)) "1997" count(decode(to_char(hire_date,'yyyy').'1998',1,null)) "1998"
from employees
where to_char(hire_date,'yyyy') in (1995,1996,1997,1998)
(6) 子查询
--- 谁的工资比Abel高?
select last_name,salary
from employees
where salary>( select salary
from employees
where last_name='Abel')
--- 查询员工名为Chen的manager的信息
select last_name,salary
from employees
where employee_id=( select manager_id
from employees
where last_name='Chen')
--- 查询平均工资最高的job信息
7.创建和管理表(DDL)
常见的数据库对象:
- 表:基本的数据存储集合,由行和列组成
- 视图:从表中抽出的逻辑上相关的数据集合
- 序列: 提供有规律的数值
- 索引: 提高查询的效率
- 同义词: 给对象起别名
表名和列名:
数据类型:
Oracle数据库中的表:
1.查看用户创建的表:
select * from user_tables;
2.创建表的第一种方式(白手起家):
create table emp1(
id number(10),
name varchar2(20),
salary number(10,2), //整个10位,小数位2位
hire_date date
)
3.显示表结构
desc emp1
4.创建表的第二种方式(依托于现有的表):
create table emp2
as
select employee_id id,last_name name,hire_date,salary
from employees
5. 修改表*(修改数据类型必须是空表):
alter table emp1
add(email varchar2(20))
alter table emp1
modify(id number(15))
6.删除表
drop tabel emp5;
7.清空表(只是清除表中的数据)
truncate table emp3(不能回滚)
8.改名
rename emp2 to employees2;
8.数据处理
创建空表
create table emp1
as
select employee_id,last_name,hire_date,salary from employees
where 1=2
插入一条数据(数据格式一一对应)
insert into emp1
values(1001,'AA',sysdate,10000)
insert into emp1
values(1002,'BB',tochar('1998-12-21','yyyy-mm-dd'),20000)
insert into emp1
values(1003,'CC',to_date('1999-12-21','yyyy-mm-dd'),null)
插入不足值的数据 要求类型与数据一一对应
insert into emp1(employee_id,last_name,hire_date)
values(1004,'DD',to_date('1999-12-21','yyyy-mm-dd'))
从其他表中拷贝数据:
insert into emp1(employee_id,hire_date,last_name,salary)
select employee_id,hire_date,last_name,salary
from employees
创建脚本:
insert into emp1(employee_id,,last_name,salry,hire_date)
values(&id,'&name',&salary,'&hire_date')
更改数据 UPDATE
update emp1
set salary=12000
where employee_id=179
提交
commit;
删除数据:DELETE语句
从employees1表中删除dept1部门名称中含pubic字符的部门id
delete from employees1
where department_id=(
select department_id
from depaetments
where department_name like='Public'
)
9.约束
创建表:
create table emp2(
id number(10) contraint emp2_id_nn not null, // 显示声明约束
name varchar2(20) not null,
salary(10,2)
)
显示表:
desc emp2
创建表
create table emp3(
---列级约束
id number(10) contraint_emp3_id_uk unique,
name varchar2(20) contraint emp3_name_nn not null,
email varchar(20),
salary number(10,2),
---表级约束
contraint emp3_email_uk unqiue(email)
)
创建表
create table emp4(
---列级约束
id number(10) contraint_emp4_id_pk primary key,
name varchar2(20) contraint emp4_name_nn not null,
email varchar(20),
salary number(10,2),
---表级约束
contraint emp3_email_uk unqiue(email)
contraint emp6_id_fk foreign key(department_id) references departments(department_id)
)
10.视图:view
1.创建视图
create view empview
as
select employee_id,last_name,salary
from employees
where department_id=80
2.视图修改
3.视图中删除
delete from empview
where employee_id=176
4.基于多个表创建视图
create view empview2
as
select employee_id,id,last_name,name,department_name
from employee e,department d
where e.department_id=d.department_id
5.修改视图
使用 create or replace view子句修改视图
6.屏蔽DML操作,可以使用with read only 选项屏蔽对视图的DML的操作
with read only
7.删除视图
drop view [view_names];
8.top-n分析:
查询工资排名前10的工资:
select rownum,employee_id,last_name,salary
from(
select rownum,employee_id,last_name,salary
from employees
order by salary desc
)
where rownum<=10
查询40-50
select rn,employee_id,last_name,salary
from(
select rownum rn,employee_id,last_name,salary
from(
select employee_id,last_name,salary
from employees
order by salary desc
)
)
where rn>40 and rn<=50
9.对Orac对象le进行分列
select employee_id,last_name,salary
from(
select rownum rn,employee_id,last_name,salary
from employees
) e
where e.rn<=pageNo*pageSize and e.rn>(pageNo-1)*pageSize
11.其他数据库对象
序列
修改序列的注意事项:
使用序列:会出现"裂缝"现象
索引
创建索引
什么时候创建索引
什么时候不要创建索引:
同义词
1.创建一个序列:
create sequence empseq
increment by 10 --每次增长10
start with 10 --从10开始增长
maxvalue 100 --提供的最大值
cycle --需要循环
nocache --不需要缓存
2.// 首次必须先进行nextval
select empseq.curval from dual
select empseq.nextval from dual
// 序列的使用
create table emp01
as
select employee_id,last_name,salary
from employees
where 1=2
3.// 修改序列
alter sequence empseq
increment by 12
nocycle
4.创建索引
create index emp01_id_ix
on emp01(employee_id)
5.删除索引
6.创建同义词
CREATE SYNONYM e FOR employees
12.控制用户权限
数据库安全性:
- 系统安全性
- 数据安全性
系统权限:对于数据库的权限
- 有一百多种权限
- 数据库管理员具有高级权限以完成管理任务
对象权限:操作数据库对象的权限
1.创建用户
create user xiaoyao
identified by xiaoyao
2.用户的系统权限-创建会话
grant create session
to xiaoyao
grant create table
to xiaoyao
3.创建用户表空间
alter user xioayao quota unlimted(5m)
on users;
4.更改用户账户密码
alter user xiaoyao
identified by xiaoyaotest
5.创建角色
create role my_role;
6.赋予角色权限
grant create session,create_tabe,create view to my_role
create user xiaoyao02
grant my_role to xiaoyao02
7.对象权限
grant select,update
on scott.employees
to xiaoyao
8.with grant options:表示可以可别人授予权限
public 关键字:表示给所有用户权限
9.revoke 语句表示收回已授予的权限
13.SET操作符
列数必须对应相同 实现两个表的并集
select emplyee_id,department_id
from employees01
union all
select employee_id,department_id
from employee02
14.高级子查询
1.多列子查询
开始实现:
select employee_id,manager_id,department_id
from employees e1
where manager_id in (
select manager_id
from employees
where employee_id in (141,174)
) and department_id in (
select department_id
from employees
where employee_id in (141,174)
)
and empoyee_id not in(141,174)
多列子查询:
select employee_id,manager_id,department_id
from employees e1
where (manager_id,department_id) in (
select (manager_id,department_id)
from employees
where employee_id in (141,174)
)
and employee_id not in (141,174)
2.在FROM 子句中使用子查询
select last_name,department_id,salary
from employees e1
where salary> (
select avg(salary)
from employees e2
where e1.department_id=e2.department_id
group department_id
)
3. 相关子查询:按照一行接一行的顺序只能执行,主查询的每一行都执行一次子查询
select last_name,salary,department_id
from employees outer
where salary>(
select avg(salary)
from employees
where department_id=outer.department_id
)
4.EXISTS 操作符
- 检查在子查询中是否存在满足条件的行
5. NOT EXISTS 操作符
6.with子句