CREATE TABLE user (
id BIGINT UNSIGNED PRIMARY KEY NOT NULL AUTO_INCREMENT COMMENT 'id号',
name VARCHAR(50) UNIQUE NOT NULL COMMENT '姓名',
age TINYINT UNSIGNED NOT NULL COMMENT '年龄',
sex ENUM('M', 'W') NOT NULL COMMENT '性别,M男性,W女性'
) ENGINE=INNODB DEFAULT CHARSET=utf8;
查看表结构
mysql复制代码
desc user;
查看建表SQL(可以查看索引、外键等情况)
mysql复制代码
show create table user\G;
删除表
mysql复制代码
drop table user;
CURD 操作
insert 增加
mysql复制代码
# 添加表数据
INSERT INTO user(name, age, sex) VALUES('zhangsan', 18, 'M');
INSERT INTO user(name, age, sex) VALUES('lisi', 19, 'W');
INSERT INTO user(name, age, sex) VALUES('wangwu', 20, 'M');
INSERT INTO user(name, age, sex) VALUES('zhaoliu', 21, 'W');
INSERT INTO user(name, age, sex) VALUES('qianqi', 22, 'M');
INSERT INTO user(name, age, sex) VALUES('zhangsan', 18, 'M'),('lisi', 19, 'W'),('wangwu', 20, 'M'),('zhaoliu', 21, 'W'),('qianqi', 22, 'M');
update user set age = 23 where name = 'zhangsan';
update user set age = age + 1 where id = 3;
delete 删除
mysql复制代码
delete from user where age=23;
delete from user where age between 20 and 22;
delete from user;
select 查询
mysql复制代码
select * from user;
select id, nickname, name, age, sex from user;
select id, name from user;
select id, nickname, name, age, sex from user where sex = 'M' and age >= 20 and age <= 25;
select id, nickname, name, age, sex from user where sex = 'M' and age between 20 and 25;
select id, nickname, name, age, sex from user where sex = 'W' or age >= 22;
去重 distinct
mysql复制代码
select distinct name from user;
空值查询
is [not] null
mysql复制代码
select * from user where name is null;
union 合并查询
一句话:连接两个select查询的SQL语句(全查)
mysql复制代码
SELECT country FROM Websites UNION ALL SELECT country FROM apps ORDER BY country;
注意:union 默认去重,不用修饰distinct,all显示所有重复值
带 in 子查询
not\] in (元素1,元素2,...,元素n)
select * from user where id in (10, 20, 30, 40, 50)
select * from user where id not in (10, 20, 30, 40, 50)
select * from user where id in (select stu_id from grade where average >= 60.0)
复制代码
### 分页查询
```mysql
select id, nickname, name, age, sex from user limit 10;
select id, nickname, name, age, sex from user limit 2000,10;
select * from user limit N offset M;
explain进行分析:查看SQL语句的执行计划
测试代码(自动生成2000000条数据)【测试分页,测试explain所用】
mysql复制代码
# 修改SQL结束的符号
delimiter $
Create Procedure add_t_user (IN n INT)
BEGIN
DECLARE i INT;
SET i=0;
WHILE i<n DO
INSERT INTO t_user VALUES(NULL,CONCAT(i+1,'@fixbug.com'),i+1);
SET i=i+1;
END WHILE;
END$
delimiter ;
call add_t_user(2000000);
面试题:分页的效率低吗?为什么低?怎么修改呢?
低,pageNum的偏移所花费的性能,要先偏移到对应的位置,在进行分页拿去后面的数据记录
修改:添加索引,通过where筛选条件进行偏移
mysql复制代码
select * from t_user limit 1500000, 3;
select * from t_user where id >= 1500000 limit 3;
select id,nickname,name,age,sex from user where sex='M' and age>=20 and age<=25 order by age asc;
select id,nickname,name,age,sex from user where sex='M' and age>=20 and age<=25 order by age desc;
**扩展:**order by 查询特别慢,查看explain进行分析,发现是Using filesort,所以给排序列或者查询列添加索引
分组 group by
mysql复制代码
select sex from user group by sex;
select count(id), sex from user group by sex;
select count(id), age from user group by age having age>20;
**扩展:**group by 查询慢,explain 分析发现是 Using temporary,即产生了一个临时表,因此最好给分组列添加索引
笔试实践题
下表bank_bill是某银行代缴话费的主流水表结构:
字段名
描述
serno
流水号
date
交易日期
accno
账号
name
姓名
amount
金额
brno
缴费网点
1、统计表中缴费的总笔数和总金额
2、给出一个sql,按网点和日期统计每个网点每天的营业额,并按照营业额进行倒序排序
mysql复制代码
CREATE TABLE bank_bill (
serno BIGINT UNSIGNED PRIMARY KEY NOT NULL AUTO_INCREMENT,
date DATE NOT NULL,
accno VARCHAR(100) NOT NULL,
name VARCHAR(50) NOT NULL,
amount DECIMAL(10, 1) NOT NULL,
brno VARCHAR(150) NOT NULL
);
INSERT INTO bank_bill VALUES
('101000', '2021-3-1', '111', 'zhang', 100, '高新区支行'),
('101001', '2021-3-1', '222', 'liu', 200, '碑林区支行'),
('101002', '2021-3-1', '333', 'gao', 300, '高新区支行'),
('101003', '2021-3-1', '444', 'lian', 150, '雁塔区支行'),
('101004', '2021-3-1', '555', 'lan', 360, '雁塔区支行'),
('101005', '2021-3-1', '666', 'wang', 300, '碑林区支行'),
('101006', '2021-3-1', '777', 'wei', 500, '碑林区支行'),
('101007', '2021-3-2', '444', 'lian', 150, '雁塔区支行'),
('101008', '2021-3-2', '555', 'lan', 360, '雁塔区支行'),
('101009', '2021-3-2', '666', 'wang', 300, '碑林区支行'),
('101010', '2021-3-3', '777', 'wei', 500, '碑林区支行');