MySQL 入门
文章目录
-
-
- `MySQL 入门`
-
- `1. 查询语句`
-
- `1.1 SQL的分类`
- `1.2 单表select语句`
- `1.3 多表select语句`
- `1.4 单行函数`
- `1.5 聚合函数`
- `1.6 子查询`
- `2. 数据库与表操作`
-
- `2.1 数据库操作`
- `2.2 表操作`
- `2.3 数据操作`
- `2.4 视图`
- `3. 存储过程`
-
- `3.1 入门`
- `3.2 变量`
- `3.3 参数传递`
- `3.4 条件`
- `3.5 循环`
- `3.6 游标`
- `3.7 句柄`
- `3.8 存储函数`
- `4. 触发器`
-
- `4.1 概述`
- `4.2 操作`
- `4.3 new,old`
- `5. 索引`
-
- `5.1 普通索引`
- `5.2 唯一索引`
- `5.3 主键索引`
- `5.4 组合索引`
- `5.5 全文索引`
- `5.6 空间索引`
1. 查询语句
1.1 SQL的分类
1.2 单表select语句
select...from...
select * from employees;
select employee_id, salary from employees;
列的别名
select employee_id emp_id from employees;
select employe_id as emp_id from employees;
去除重复行
select distinct department_id from employees;
select salary, distinct department_id from employees;
select distinct department_id, salary, from employees;
空值参与运算
select
employee_id, salary "月工资", salary * (1 + commision_pct) * 12 "年工资"
from
employees;
select
employee_id, salary "月工资", salary * (1 + ifnull(commision_pct, 0)) * 12 "年工资"
from
employees;
着重号
select * from order;
select * from `order`;
查询常数
select "Mysql", employee_id, last_name from employees;
显示表结构
describe employees;
desc employees;
过滤数据
select * from employees where department_id = 90;
运算符
select last_name, salary, commision_pct from employees where commision_pct is null;
select least('g', 'b', 'r'), greatest('g', 'T', 'c') from dual;
select employee_id, last_name, salary from employees where salary between 6000 and 8000;
select last_name, salary, department_id from employees where department_id in (10, 20, 30);
select last_name from employees where last_name like '_\_a%';
select
last_name, salary, department_id
from
employees
where
department_id = 50 and salary >= 10000;
排序
select employee_id, last_name, salary from employees order by salary desc;
分页
select employee_id, last_name, salary from employees limit 0, 20;
1.3 多表select语句
笛卡尔积
select employee_id, department_name from employees, department;
等值连接
select
e.employee_id, d.department_name
from
employees e, departments d
where
e.department_id = d.department_id;
非等值连接
select
e.last_name, e.salary, j.grade_level
from
employees e, job_grades j
where
e.salary between j.lowest_sal and j.highest_salary;
自连接
select
e.employee_id, e.last_name, m.employee_id, m.last_name
from
employees e, employees m
where
e.manage_id = m.employee_id;
非自连接
内连接
外连接
select
e.employee_id, d.department_name
from
employees e, departments d
where
e.department_id = d.department_id(+);
select
e.employee_id, d.department_name
from
employees e
left outer join
departments d
on
e.department_id = d.department_id;
select
e.employee_id, d.department_name
from
departments d
right outer join
employees e
on
d.department_id = e.department_id;
select
e.employee_id, d.department_name
from
departments d
full outer join
employees e
on
d.department_id = e.department_id;
union