SQL基础学习笔记(一)

感觉SQL,学的云里雾里的,整理一下笔记吧。

SQL语言分为三种:

DML: Data Manipulation Language 数据操纵语言
DDL:  Data Definition Language 数据定义语言

DCL:  Data Control Language 数据控制语言

select * (表示查询表中所有的列 ) from employees;
select employee_id , last_name,email from employees;(查询指定)

SELECT   标识 选择哪些列。

FROM     标识从哪个表中选择。

注意:
SQL 语言大小写不敏感。 
SQL 可以写在一行或者多行
关键字不能被缩写也不能分行
各子句一般要分行写
使用缩进提高语句的可读性。

多表查询,有n个表,就要注意有n-1个链接关系(注意笛卡尔集)
不注意笛卡尔集情况,假设员工表有107个员工数据,部门表有27个部门ID数据,员工表通过department_id和部门表链接,
不写链接关系,

select e.department_id,d.department_name
from employees e,departments d;

107*27 = 2889,多对多关系,致使本来是两个表共享的同一列,成为两个列
解决笛卡尔集问题的两种方式:

方式一:

select e.employee_id , d.department_id , d.department_name
from employees e , departments d
where e.department_id = d.department_id
方式二:

select e.employee_id , d.department_id , d.department_name
from employees e join departments d
on e.department_id = d.department_id

加多少表就join ... on

select employee_id , d.department_id , department_name ,city 
from employees e join departments d 
on e.department_id = d.department_id
join locations l
on d.location_id = l.location_id
using方式
select employee_id , department_id , department_name
from employees join departments
using(department_id)
这种方式有缺陷,比如说,employee表员工的id是employee_id ,而departments表的员工id是dpart_id
using(),就不好用了

还有一种

select employee_id , department_id , department_name
from employees natural join departments

这种方式自动加入多种等价关系

外连接:
比如当一个没有部门时,那个人的信息就会不显示,而使用外连接,就可以将其显示
左外连接:PS:outer可以省略
select employee_id , d.department_id , department_name
from employees e , departments d
where e.department_id = d.department_id(+)
--where e.department_id(+) = d.department_id

SQL99语法,解决左外连接
select employee_id , d.department_id , department_name
from employees e left outer join departments d 
on e.department_id = d.department_id

右外连接:

select employee_id , d.department_id , department_name
from employees e right outer join departments d 
on e.department_id = d.department_id

满外连接;'
select employee_id , d.department_id , department_name
from employees e full outer join departments d 
on e.department_id = d.department_id

自连接:
查询chen员工的老板的个人信息
select emp.last_name, mana.last_name , mana.salary,mana.email
from employees emp ,employees mana
where emp.manager_id = mana.employee_id and lower(emp.last_name) = 'chen'
分组函数(多行函数):

分组函数作用于一组数据,并对一组数据返回一个值
max(salary) , min() , count(*)->任意数据类型
avg(),sum() ->数组
查询相应的列,只要不是组函数,都应该出现在group by中
但是

select avg(salary) "averge"
from employees


group by department_id
order by department_id 
可以

过滤:有where,having

区别:不能再where中使用组函数,可以在having中使用

--求出各部门中平均工资大于6000的部门,以及其平均工资
select department_id , avg(salary)
from employees
having avg(salary) > 6000
group by(department_id)
where放在from 后面

嵌套组函数
--所有部门中工资最大值

select  max(avg(salary))
from employees
group by department_id

计算1995,1996,1997,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')

注意,count(decode(to_char(hire_date,'yyyy'),'1995',1,0)) "1995" 不是null,如果是0,相当于
count(0)与count(*)一样,来一条数据就加1
如果子查询返回的结果是空值,不会报错

创建表

/*
--第一种方式
create table emp1(
id number(10),
name varchar2(20),
salary number(10,2),
hire_date date
)
*/
--第二种方式,以现有employees表来创建表,并且把原来的表的数据,都导入新表中了
create table emp2
as 
select employee_id id ,last_name name , hire_date , salary
from employees
如果不想要所有的数据,可以加过滤条件
create table emp3
as 
select employee_id id ,last_name name , hire_date , salary
from employees  
where department_id = 80  
如果不想要任何数据:可以使过滤条件找不到,或者是where 条件,条件只要是空,就行,比如where 1=2,肯定没有满足的条件
create table emp4
as 
select employee_id id ,last_name name , hire_date , salary
from employees  
where department_id = 800000
修改表
--修改 
ALTER TABLE emp1
--ADD (eamil varchar2(20))--增加
--modify (id number(15))--修改
--modify (salary number(20,2) default 2000)--默认初始值2000,之前的数据是不会改变的,只改变后来的数据
--modify (email number(20))--修改数据类型,前提是email没有数据,有数据就改不了

除了修改数据类型,前提是不能有数据,其他的有没有数据都行
删除列

ALTER TABLE emp1
DROP COLUMN eamil

重命名列
ALTER TABLE emp1
rename column salary to sal
rollback对DDL无效,查阅资料,补充重点
删除表

drop table emp5;

清空表 ->清空标表中数据

truncate table emp3;
作用:表结构不变,清空所有数据

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值