mysql基本SQL语句
1、常见命令
mysql -u root -p # 连接数据库命令
show databases; # 列出所有的数据库
use test; # 进入test 数据库
desc user;# 查看数据库表结构命令
create database 数据库名; # 创建数据库文件
2、数据类型与约束
作用:为了更加精准的存储数据,保证数据的有效性,需要结合数据类型和约束来限制数据的存储
数据类型:
整型:int
小数:decimal
字符串:varchar
日期时间:datetime
约束:
主键:primary key:物理上存储的顺序
一般设置主键时,往往会使用id作为字段名,主键设置一般的要求,整数/ 无符号/ 自动递增/ 不能为空
当存在主键时,表中的物理顺序就被固定下来,当数据被清空时,新数据的顺序不会从1开始计数,而是从之前存入数据的顺序的下一位继续记录能够保证数据在顺序上是唯一的。
非空:not null : 此字段不允许调谐空值
唯一:unique:此字段不允许重复
默认值 default : 当不填写此值时会使用默认值,如果填写时以填写为准
外键: foreign key :维护两个表之间的关联关系
3、SQL语言
select * from users; ------------注意:sql语句不区分大小写,语句结束需要跟上分号
create database foo; 创建数据库foo
1)数据库表创建语句:
drop table if exists goods; 表如果存在就删除
create table goods(
goodsName varchar(20),
price decimal(4,2)
)
2)创建数据库表
1 -- 需求 创建商品表 字段包括 id(主键),商品名称 价格 数量 公司 北湖 2 drop table if exists goods; 3 create table goods ( 4 id int unsigned primary key auto_increment, 5 goodsName varchar(20), 6 price decimal(4,2), 7 num int, 8 company varchar(20), 9 remark varchar(30) 10 );
3)插入数据
-- 主键列是自增长的,插入时需要占位,通常使用0或者default 或者null 来占位,插入成功后以实际 -- 数据为准 -- 语法: insert into table_name values (),(); -- 插入的数据个数 与字段数必须匹配,数据类型也要对应 -- 注意:插入多条数据时,每条数据用逗号隔开 insert into goods values (0,'laptop',4700,20,'HUAWEI','china'), (0,'pad',2500,10,'HUAWEI','china'), (0,'smartphone',4500,30,'HUAWEI','china'); -- 插入指定字段数据 insert into goods(goodsName) values('watch');
4)数据修改
-- 补充watch 的信息 -- 更改数据表中某个字段的类型 修改数据时 必须要条条件筛选where ,更新时 要备份数据表 alter table goods modify price decimal(6,2); update goods set price = 4700,num=29,company='HW',remark='USA' where id =4;
5)删除一条数据
-- 删除一条数据 -- 语法: delete from table_name where id = 3 delete from goods where id=3;
6)更改表结构 alter 关键字 alter table table_name add 字段名 类型; # 添加字段类型
alter table table_name modify 字段名 类型;# 更改字段类型
-- 添加字段类型 alter table goods add is_delete int; -- 更改字段类型 alter table goods modify is_delete varchar(20);
7)三种删除数据方法
-- delete from 表名;清空数据,只清空数据,保留表结构,但是不会重置主键计数
-- truncate table 表名; 清空表数据(只清空数据,保留结构),并会重置主键计数
--drop table 表名; 删除表,包括表结构和数据
--删除速度 drop > truncate > delete
4、查询SQL语句(重点)
1)--基本查询: 查询所有、 查询指定字段、 起别名 、去重
-- 基本查询: 查询所有、 查询指定字段、 起别名 、去重 select * from goods; select goodsName,price from goods; select goodsName as g ,price as p from goods; -- 去重 根据所给字段,将字段中数据相同的数据合并保留一个 -- 需求 查询当前一共有几家公司 select distinct(company) from goods;
2)-- 条件查询 : 比较运算/ 逻辑运算符
-- 需求4: 查询价格等于30 并且出自拼多多的所有商品 -- 条件查询 where 条件 select * from goods where price = 20; -- 当查询有多个条件时 使用and or select * from goods where price= 20 and company = '拼夕夕'; -- 比较运算符 查询大于3000的商品 select * from goods where price > 3000;
3)模糊查询 like %匹配任意多个字符
--语法 where 字段 like '%信息%' _匹配单个字符
-- 模糊查询 where 字段 like '%信息%' %匹配任意多个字符,_匹配单个字符 -- 模糊查询时 注意要设置条件,避免遗漏数据 select * from goods where remark like '%c%';
4)范围查询 between 数字 and 数字 ; in 条件查询范围 表示在某个不连续的范围内
select * from goods where price between 300 and 3000; select * from goods where price in (20,300,2500);
5)空判断 is null ; is not null;
-- 注意 null 与 ''是两码事,null是空,'' 是空字符
select * from goods where remark is null; select * from goods where price is not null;
6)排序
-- 需求8 查询所有商品信息,按照价格从大到小排序,价格相同时,按照数量少到多排序
--排序: order by 字段名 asc(升序)/desc(降序)
-- 实现按照价格升序排序 默认情况 order by 是按照升序来进行排列的 select * from goods order by price asc; -- 实现按照价格升序,价格相同时按照num降序排序 select * from goods order by price asc,num desc;
7) 聚合函数
-- 需求9: 查询一下信息:商品信息总条数;最高商品价格;最低商品价格;商品平均价格;pad的总数量
select count(*) from goods; select max(price) from goods; select avg(price) from goods; select min(price) from goods; select count(*) from goods where goodsName = 'pad';
8) 分组
-- 需求10: 查询每家公司的商品数量
-- 语法 group by 字段; 可以根据给出字段数据进行数据分组
-- 一般情况下,分组需要配合聚合函数一起使用,目的是对分组后的数据进行进一步统计
-- 需求10: 查询每家公司的商品数量 -- 语法 group by 字段; 可以根据给出字段数据进行数据分组 -- 一般情况下,分组需要配合聚合函数一起使用,目的是对分组后的数据进行进一步统计 select * from goods; select company ,count(*) from goods group by company; -- 分组后再进行条件筛选 select company from goods group by company; -- 查询 没有拼夕夕公司的公司 select company from goods group by company having company != '拼夕夕'; -- 获取公司商品最贵的价格 select company , max(price) from goods group by company;
9)分页查询
--需求11:查询当前表当中第5-10行的所有数据
-- 分页 使用:limit 起始索引,数据行数
-- 索引:计算机中的索引是从0开始的
-- 需求11:查询当前表当中第5-10行的所有数据 -- 分页 使用:limit 起始索引,数据行数 -- 索引:计算机中的索引是从0开始的 select * from goods; select * from goods limit 2,4; # 从第二行开始 查询4行数据 select * from goods limit 0,1; # 获取第一行数据 -- 分页查询: 公式limit (n-1)*m,m; m为每页索要显示的数据,n为显示第n页的数据 -- 这个分页的本质 就是limit的起始索引,显示行数 select * from goods limit 6,2; # 每页显示2行,显示第4页数据
-- 需求 创建商品表 字段包括 id(主键),商品名称 价格 数量 公司 北湖 drop table if exists goods; create table goods ( id int unsigned primary key auto_increment, goodsName varchar(20), price decimal(6,2), num int, company varchar(20), remark varchar(30) ); -- 主键列是自增长的,插入时需要占位,通常使用0或者default 或者null 来占位,插入成功后以实际 -- 数据为准 -- 语法: insert into table_name values (),(); -- 插入的数据个数 与字段数必须匹配,数据类型也要对应 -- 注意:插入多条数据时,每条数据用逗号隔开 insert into goods values (0,'laptop',4700,20,'HUAWEI','china'), (0,'pad',200,10,'拼夕夕','china'), (0,'大蒜',20,10,'拼夕夕','china'), (0,'勺子',20,10,'淘宝','china'), (0,'球鞋',300,10,'狗东','china'), (0,'跑步机',1500,10,'狗东','china'), (0,'pad',2500,10,'HUAWEI','china'), (0,'smartphone',4500,30,'HUAWEI','china'); -- 插入指定字段数据 insert into goods(goodsName) values('watch'); -- 需求2 修改商品数据,修改商品数据1条,删除1条商品数据 -- 修改 -- 先查询所有数据,便于观察数据内容 select * from goods; show variables like '%char%'; set character_set_database=utf8; set character_set_filesystem=utf8; show create table goods; default-character-set= utf8; alter table goods default character set utf8 collate utf8_general_ci; -- 补充watch 的信息 -- 更改数据表中某个字段的类型 alter table goods modify price decimal(6,2); update goods set price = 4700,num=29,company='HP',remark='USA' where id =4; rollback; -- 删除一条数据 -- 语法: delete from table_name where id = 3 delete from goods where id=3; -- 添加字段类型 alter table goods add is_delete int; -- 更改字段类型 alter table goods modify is_delete varchar(20); -- 基本查询: 查询所有、 查询指定字段、 起别名 、去重 select * from goods; select goodsName,price from goods; select goodsName as g ,price as p from goods; -- 去重 根据所给字段,将字段中数据相同的数据合并保留一个 -- 需求 查询当前一共有几家公司 select distinct(company) from goods; -- 需求4: 查询价格等于30 并且出自拼多多的所有商品 -- 条件查询 where 条件 select * from goods where price = 20; -- 当查询有多个条件时 使用and or select * from goods where price= 20 and company = '拼夕夕'; -- 比较运算符 查询大于3000的商品 select * from goods where price > 3000; -- 模糊查询 where 字段 like '%信息%' %匹配任意多个字符,_匹配单个字符 -- 模糊查询时 注意要设置条件,避免遗漏数据 select * from goods where remark like '%c%'; select * from goods where price between 300 and 3000; select * from goods where price in (20,300,2500); select * from goods where remark is null; select * from goods where price is not null; -- 实现按照价格升序排序 默认情况 order by 是按照升序来进行排列的 select * from goods order by price asc; -- 实现按照价格升序,价格相同时按照num降序排序 select * from goods order by price asc,num desc; -- 需求9: 查询一下信息:商品信息总条数;最高商品价格;最低商品价格;商品平均价格;pad的总数量 select count(*) from goods; select max(price) from goods; select avg(price) from goods; select min(price) from goods; select count(*) from goods where goodsName = 'pad'; -- 需求10: 查询每家公司的商品数量 -- 语法 group by 字段; 可以根据给出字段数据进行数据分组 -- 一般情况下,分组需要配合聚合函数一起使用,目的是对分组后的数据进行进一步统计 select * from goods; select company ,count(*) from goods group by company; -- 分组后再进行条件筛选 select company from goods group by company; -- 查询 没有拼夕夕公司的公司 select company from goods group by company having company != '拼夕夕'; -- 获取公司商品最贵的价格 select company , max(price) from goods group by company; -- 需求11:查询当前表当中第5-10行的所有数据 -- 分页 使用:limit 起始索引,数据行数 -- 索引:计算机中的索引是从0开始的 select * from goods; select * from goods limit 2,4; # 从第二行开始 查询4行数据 select * from goods limit 0,1; # 获取第一行数据 -- 分页查询: 公式limit (n-1)*m,m; m为每页索要显示的数据,n为显示第n页的数据 -- 这个分页的本质 就是limit的起始索引,显示行数 select * from goods limit 6,2; # 每页显示2行,显示第4页数据