本人SQL新手,五一期间自学了《SQL必知必会》一书,在此简要分享学习心得,若有差错,请各位大佬们多多指教呀!
本人的SQL学习计划是先根据《SQL必知必会》一书入门,了解SQL的整体框架,语法等,在闲暇时间刷题巩固。
首先奉上本人整理的SQL涉及的功能及语法的思维导图,思维导图中整理了各个SQL功能以及实现功能的代码和函数等,还在不断完善更新中~(本人新手,可能有部分功能理解有误,请大家不吝赐教,(●'◡'●))
另外,本着学以致用的心态,本人在SQL学习过程中,坚持码代码实现相关功能,文末也将奉上本人编写的SQL代码以及相关功能说明~(工具是SQLyog,思路依据的是《SQL必知必会》一书),老规矩,同学们觉得有问题或有优化空间的话,请多多留言指教哇!
=======================================================================
/* 检索固定位置和固定行数
SELECT *
FROM employees;
Select distinct employee_id
from employees;
select employee_id
from employees
limit 4,4
order by employee_id; */
/* 多列排序 第一个排序列的值不唯一,需要多列区分
select employee_id, first_name, last_name
from employees
order by first_name,last_name;
*/
/* 升序排序和降序排序
select first_name, last_name, salary
from employees
order by salary desc;
*/
/* 找到工资3000的员工;where 过滤指定的是行。
select first_name,last_name,salary
from employees
where salary >= 3000;
*/
/* 不匹配查询--字符串要用单引号引用
select
first_name,last_name,salary
from
employees
where job_id = 'AD_VP' ;
*/
/* 范围查询
select first_name,last_name,salary
from employees
where salary between 3000 and 5000
order by salary desc;
*/
/* 查询空值
select first_name,last_name,email
from employees
where email is null;
*/
/*逻辑操作符*/
/* AND 操作符
select first_name,last_name,salary,job_id
from employees
where job_id = 'IT_PROG' and salary >5000
order by salary;
*/
/* OR 操作符
select salary,first_name,last_name,job_id
from employees
where job_id ='IT_PROG'
or salary >=10000
order by salary;
*/
/* 求值顺序--AND的优先级更高,会优先和其它语句组合,类似数学运算的乘除;
OR类似加减,
可用圆括号(优先级更高)强制改变运算顺序
select job_id, first_name,last_name,salary
from employees
where (job_id = 'IT_PROG'
or job_id = 'AD_VP')
and salary > 6000;
*/
/* IN 的用法————类似“集合”概念;完成类似 ”OR“语句的操作,学会使用“IN”替代“OR”
select first_name,last_name,salary,job_id
from employees
where job_id in ('AD_VP','IT_PROG')
order by salary;
*/
/*
select first_name,last_name,salary,job_id
from employees
where not job_id in ('AD_VP','IT_PROG')
and salary > 10000
order by salary;
*/
/* 通配符% 匹配多个字符 搜百分号放字符前面表示以百分号后的字符结尾的量,放字符后面表示搜索以字符开头的量
select *
from jobs
where job_title like '%manager%'
order by min_salary;
*/
/*通配符 下划线 一个下划线只匹配单个字符而不是多个字符
select *
from jobs
where job_title like '__________ Manager';
*/
/*组合使用
select *
from employees
where first_name NOT like 'N%'
or first_name Not like 'L%'
order by salary desc;
*/
/* 使用concat拼接两列,并指定别名
select concat(first_name,'(',last_name,')')
from employees
as name
order by salary;
*/
/* 返回当前日期
select now()
*/
/* 文本处理,改变字符串首字母大小写
select first_name,lower(first_name)as first_name_locase
from employees
order by first_name_locase;
*/
/*搜索发音相同的字符
select first_name
from employees
where soundex(first_name)=soundex('vali');
*/
/* 提取时间 年
select first_name,last_name,salary
from employees
where year(hiredate)=2004
order by salary;
/* 提取时间 月
select first_name,last_name,salary
from employees
where month(hiredate)=3
order by salary;
*/
/* 提取时间 日
select first_name,last_name,salary
from employees
where day(hiredate)=3
order by salary;
*/
/* 求平均值,列名不是字符串
select avg(salary)
as avg_salary
from employees;
*/
/* 计数函数的使用,是否将NULL计算在内
select count(*)
from employees;
select count(manager_id)
from employees;
*/
/*最大值 最小值函数使用
select max(first_name)
from employees;
select min(first_name)
from employees;
*/
/*分组数据,Group by和Having*/
/* group by 使用
select job_id,count(*)as num_job
from employees
group by job_id
*/
/* having函数使用,对“列”过滤,再排序
select job_id,count(*)as num_job
from employees
group by job_id
having num_job>=10
order by num_job;
*/
/* 子查询
select first_name,last_name,salary
from employees
where department_id in (select department_id
from departments
where location_id = 1700);
*/
/*作为计算字段使用子查询
select department_name,
manager_id,
(select count(*)
from employees
where employees.department_id=departments.department_id) as depar
from departments
order by manager_id;
*/
/* 内联结 选定的列位于不同的表,如何选取?如何使用表名的缩写-表别名?
select d.`department_name`,e.first_name,e.last_name
from `departments` as d,employees as e
where d.`department_id` = e.`department_id`;
*/
/* 自联结 同一张表中寻找拥有同种属性的对象--问题:相同列会出现多次,下面代码结果同一个对象重复出现
select e1.`employee_id`,e1.first_name,e1.last_name,e1.department_id
from employees as e1,employees as e2
where e1.`department_id`=e2.`department_id`
and e2.`department_id`=100;
*/
/*自然联结--避免自联结结果项重复出现,重点在于采用另一张明确的表来联结
select e.`department_id`,e.`first_name`,e.`last_name`,e.`salary`
from employees as e,departments as d
where e.`department_id`=d.`department_id`
and d.`department_id`=100;
*/
/* 外联结-关键:可以显示没有关联行的行;注意:left outer join指出语句左边表(默认按左边表排序),right outer join指出语句右边表(默认按右边表排序)
select e.`employee_id`,e.`first_name`,e.`last_name`,d.`department_name`,e.`department_id`
from employees as e left outer join departments as d
on e.`department_id`=d.`department_id`;
*/
/*使用带聚集函数的联结。
select d.`department_name`,e.`employee_id`,
count(e.`department_id`) as num_emp
from employees as e inner join departments as d
on e.`department_id`=d.`department_id`
group by e.department_id;
*/
/* 组合查询,多个查询条件,类似OR的功能,在面对多个表调用时,采用这个语句,结构会清晰一些。
select e.`employee_id`,e.`first_name`,e.`last_name`,e.`email`,e.`department_id`
from employees as e
where e.`department_id` in (100,110,120)
union
select e.`employee_id`,e.`first_name`,e.`last_name`,e.`email`,e.`department_id`
from employees as e
where e.`first_name`='Steven'
order by e.`department_id`;
*/
/* 用OR语句替代上述组合查询语句,实现相同功能
select e.`employee_id`,e.`first_name`,e.`last_name`,e.`email`,e.`department_id`
from employees as e
where e.`department_id` in (100,110,120)
or e.`first_name`='Steven'
order by e.`department_id`;
*/
/* 插入
insert into employeess(*)
values(01,
'Lynn',
'Zheng',
'lingling66zh',
'1888',
'AD_VP',
17000,
null,
null,
100,
2020-08-25);
*/
/*插入检索的数据
insert into employees(*)
select(*)
from another_tbl;
select *
into custcopy
from customer
*/
/*更新数据
update employees
set employee_id =250
where employee_id =1;
*/
/*删除数据-注意:这里删除的是整行,部分删除要使用update
delete from employees
where employee_id = 250;
*/
/*创建表,必须给出三个项: 列名,数据类型,是否允许值为null;如何给定默认值
create table test.product250
(prod_id char(10) not null,
department_id int(4) default 1,
prod_name char(254) default 1);
*/
/*更改设定表,添加用add,删除用drop column 列名
alter table test.`product250`
add prod_price int(10) default 1;
*/