DDL 查询和创建数据库
查询所有数据库
show databases;
查询mysql数据库的创建语句
show create database mysql;
创建数据库
create database 数据库名称
创建数据库db2 如果不存在则创建
create database if not exists db2;
创建数据库,并指定字符集
create database 数据库名称 character set 字符集名称;
create database db3 character set utf8;
查看数据库名称
show create database db3;
修改删除使用数据库
修改字符集
alter database db1 character set utf8;
删除数据库
drop database db2;
drop database db2 if exists db2;
使用数据库
use db4;
查询当前正在使用的数据库
select database();
查询数据表
查询所有的数据表
show tables;
查询数据表结构
desc user;
查看数据库中的表字符集
show table status from mysql like 'user';
创建数据表
创建一个product商品表(商品编号,商品名称,商品价格,商品库存,上架时间)
create table product(
id int,
name varchar(20),
price double,
stock int,
insert_time date);
查看product 详细结构
desc product;
修改数据表
修改表名:
alter table product rename to product2;
修改表的字符集
alter table product2 character set gbk;
单独修改一列
alter table product2 add color varchar(10);
修改某列的数据类型
alter table product2 modify color int;
修改列名和数据类型
alter table product2 change color address varchar(20);
删除某一列
alter table product2 drop address;
删除数据表
drop table product2;
drop table if exists product2;
新增表数据
给全部列添加数据:
insert into product (id,name,price,stock,insert_time) values (1,'手机',1999.99,25,'2020-02-02');
insert into product values (2,'电脑',3999.99,90,'2020-03-03');
给指定的列添加数据
insert into product (id,name,price) values (2,'电脑',3999.99);
批量添加数据
insert into product values (4,'洗衣机',800,15,'2021-02-05'),(5,'微波炉',1800,150,'2022-02-05');
修改数据和删除数据
update product set price=3500 where name ='手机';
update product set price=1800,stock=36 where name ='电脑';
delete from product where name ='微波炉';
delete from product where stock=36;
表查询
查询全部
select * from product;
查询指定的列
select NAME,price,brand from product;
查询品牌
select brand from product;
查询品牌去掉重复
select distinct brand from product;
计算列的值
select NAME,stock+10 from product;
select NAME,ifnull(stock,0)+10 from product;
起别名查询
select NAME,ifnull(stock,0)+10 as getSum from product;
select NAME,ifnull(stock,0)+10 getSum from product;
条件查询
查询库存大于20的商品信息
select * from product where stock>20;
查询品牌为华为的商品信息
select * from product where brand='华为';
查询金额在4000-6000之间的商品信息
select * from product where price>4000 and price<=6000;
select * from product where price between 4000 and 6000;
查询库存为14,30,20
select * from product where stock=14 or stock=30 or stock=23;
select * from product where stock in(14,30,23);
查询库存不为null的商品信息
select * from product where stock is null;
查询库存不为null的商品信息
select * from product where stock is not null;
查询名称以小米为开头的商品信息
select * from product where NAME like '小米%';
查询名称第二字是为的商品信息
select * from product where NAME like '_为%';
查询名称为四个字符的商品信息
select * from product where NAME like '____';
查询名称中包含电脑的商品信息
select * from product where NAME like '%电脑%';
聚合函数的查询
计算product表中总记录条数
select count(*) from product;
获取最高价格
select max(price) from product;
获取最低库存 select min(stock) from product;
获取总库存的数据
select sum(stock) from product;
获取品牌为苹果的总库存的数量
select sum(stock) from product where brand='苹果';
获取品牌为小米的平均商品价格
select avg(price) from product where brand='小米';
排序查询
按照库存升序进行排序
select * from product order by stock asc;
查询名称中包含手机的商品信息,按照金额降序排序
select * from product where NAME like '%手机%'order by price desc;
按照金额升序进行排序,如果金额相同,按照库存降序进行排序
select * from product order by price asc,stock desc;
分组查询
按照品牌分组,获取每组商品的总金额
select brand,sum(price) from product group by brand;
对金额大于4000元商品,按照品牌分组,获取每组商品的总金额
select brand,sum(price) from product where price >4000 group by brand;
对金额大于4000元的商品,按照品牌分组,获取每组的总金额,值显示总金额大于7000元
select brand,sum(price) getSum from product where price >4000 group by brand having getSum>7000;
对金额大于4000元的商品,按照品牌分组,获取每组的总金额,值显示总金额大于7000元,并按照总金额降序进行排列
select brand,sum(price) getSum from product where price >4000 group by brand having getSum>7000 order by getSum desc;
分页查询
每页显示三条数据
第一页
select * from product limit 0,3;
第二页
select * from product limit 3,3;
第三页
select * from product limit 6,3;