您的位置:首页 > 博客中心 > 数据库 >

SQL基础学习笔记(一)

时间:2022-03-14 05:43

感觉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方式
<span style="font-family:KaiTi_GB2312;font-size:18px;"><strong>select employee_id , department_id , department_name
from employees join departments
using(department_id)</strong></span>

这种方式有缺陷,比如说,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无效,查阅资料,补充重点
删除表


<span style="font-family:KaiTi_GB2312;font-size:18px;"><strong>drop table emp5;</strong></span>

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

<span style="font-family:KaiTi_GB2312;font-size:18px;"><strong>SQL> truncate table emp3;</strong></span>

作用:表结构不变,清空所有数据


本类排行

今日推荐

热门手游