#分页查询
/*
应用场景:当要显示的数据。一夜显示不全的时候,需要分页提交sql请求
语法:
select 查询列表
from 表
【join type join 表2
on 连接条件
where 筛选条件
group by 分组字段
having 分组后的筛选
order by 排序后的字段】
limit offest,size;
offest 要显示条目的起始索引(起始索引从0开始)
size 要显示的条目个数
特点:
①limit语句放在查询语句的最后
②公式
要显示的页数 page,每页的条目数size
select 查询列表
from 表
limit (page-1)*size,size;
size=10
page
0 10
1 20
2 30
*/
#查询前五条员工信息
select * from employees limit 0,5;
select * from employees limit 5;
#查询第11-25条
select * from employees limit 11,15;
#有奖金的员工信息,并且工资较高的前10名显示出来
SELECT
*
FROM
employees
WHERE
commission_pct IS NOT NULL
ORDER BY
salary DESC
LIMIT 10;
#各个部门中的最高工资中最低的那个部门的最低工资是多少
SELECT
min( salary ),
e.department_id
FROM
employees e
WHERE
e.department_id =(
SELECT
department_id
FROM
employees
GROUP BY
department_id
ORDER BY
max( salary )
LIMIT 1
);
#联合查询
/*
union联合 合并:将多条查询语句的结果合成一个结果
语法:
查询语句1
union
查询语句2
union
......
应用场景:
要查询的结果来自多个表,且多个表没有直接关系,但查询的信息一致
特点:★★★
1、要求多条查询语句的查询列数是一致的
2、要求多条查询语句的查询的每一列的类型和顺序最好一致
3、union关键字默认去重,如果使用union all 可以包含重复项
*/
#查询部门编号>90或邮箱包含a的员工信息
select * from employees where email like '%a%' or department_id>90;
select * from employees where email like '%a%' union select * from employees where department_id>90;
#DML语言
/*
数据的操作语言:
插入:insert
修改:update
删除:delete
*/
#一、插入语句
/*
语法:
insert into 表名(列名,...) values(值1,...)
表名
*/
#二、修改语句
/*
1、修改单表的记录
语法:
update 表名
set 列=新值,列=新值,...
where 筛选条件;
2、修改多表的记录【补充】
语法:
sql92语法:
update 表1 别名,表2 别名
set 列=值,...
where 连接条件
and 筛选条件;
sql99语法:
update 表1 别名
inner|left|ringht join 表2,别名
on 连接条件
set 列=值,...
where 筛选条件;
*/
#三、删除语句
/*
方式一:delete
语法:
1、单表删除
delete from 表名 where 筛选条件
2、多表的删除
sql92语法:
delete 表1 别名,表2 别名
from 表1 别名,表2 别名
where 连接条件
and 筛选条件;
sql99语法:
delete 表1 别名,表2 别名
from 表1 别名,表2 别名
inner|left|right join 表2 别名 on 连接条件
where 筛选条件;
方式二:truncate
语法: truncate table 表名;
*/
/*
delete 和 truncate ★★★
1、delete可以加where条件,truncate不能加
2、truncate删除,效率高一点
3、delete删除后,再插入数据,自增长列的值从断点开始,truncate删除后,再插入数据,自增长列的值从1开始。
4、truncate 删除没有返回值,delete删除有返回值
5、truncate 删除不能回滚,delete删除可以回滚
*/
#DDL语言
/*
数据定义语言
库和表的管理
一、库的管理
创建、修改、删除
二、表的管理
创建、修改、删除
创建:create
修改:alter
删除:drop
*/
#一、库的创建
/*
语法:
create database if not exists 库名
*/
#更改库的字符集
#alter database 库名 CHARACTER set 字符集;
#二、表的管理
#1、表的创建
/*
create table 表名(
列名 列的类型【(长度) 约束】,
列名 列的类型【(长度) 约束】,
列名 列的类型【(长度) 约束】,
......
列名 列的类型【(长度) 约束】
)
*/
#2、表的修改
/*
①修改列名
alter table 表名 change COLUMN 旧列名 新列名 类型;
②修改列的类型或约束
alter table 表名 modify column 旧列名 类型;
③添加列
alter table 表名 Add column 新加列名 类型;
④删除列
alter table 表名 drop column 列名;
⑤修改表名
alter table 旧表名 remove to 新表名;
*/
#3、表的删除
# drop table if not exists 表名;
#4、表的复制
/*
1、仅仅复制表的结构
create table 新表名 like 旧表名;
2、复制表的结构外加数据
create table 新表名 select * from 旧表名
3、只复制部分列
create table 新表名 select * from 旧表名 where 旧表中的列名='某一行具体数值';
4、仅仅复制某些字段
create table 新表名 select 列名1,列名2... from 旧表名 where 0; (这边0解释一下,为了结果为false然后列值不取只取列名)
*/
mysql自学笔记六(Navicat Premium 15)
最新推荐文章于 2024-08-06 11:11:43 发布