mysql常用命令行指令整理

 MySQL数据库

类型:关系型数据库

优势:

  1. 复杂的查询:可以使用SQL语句在一个或者多个表之间进行复杂的查询

  2. 事务支持:可以提高安全性能

一. 进入MySQL数据库

mysql -h主机名 -u用户名 -p   输入密码
简写:
mysql -u root -p  输入密码

 

二. 介绍数据库

MySQL->小数据库->数据表->字段->数据

三. 对于数据库的操作

命令:库和表:  create 增加(创建)  drop删除  alter(改)  show

1、库

查看所有数据库          show databases;
进入数据库              use 库名
查看当前所在的数据库     select database();
创建数据库              create database mysql;
创建一个不存在的数据库   create database if not exists mysql;
删除数据库              drop database 库名;
drop database if exists 库名; # 如果不存在, 防止报错

2、表

查看表                   show tables;
查看表结构               desc 表名;
创建一个表b和a一样        create table b like a;
创建表
create table 表名(
  字段名1  类型  约束条件,
  字段名2  类型  约束条件,
  …
);
删除表                   drop table 表名;

字段类型

(1) 数值类型

类型大小范围(有符号)范围(无符号)用途
tinyint1字节-128~1270~255最小整数值(年龄,状态)
smallint2字节-32768-327670-65535大的整数值
int4字节 0~到4开头的十位大的整数值
float4字节  单精度浮点型
double8字节  双精度浮点型
decimal   更加精准的小数类型

(2) 日期和时间类型

类型大小范围格式用途
date3个字节1000-01-01~9999-12-31YYYY-MM-DD存储日期值
time3个字节-838:59:59 ~838:59:59HH:MM:SS存储时间值
year1个字节1901~2155YYYY存储年份值
datatime8个字节1000-01-01 00:00:00~9999-12-31 23:59:59YYYY-MM-DD HH:MM:SS混合日期

(3) 字符串类型

类型大小用途
char0-255字节存储定长字符串
varchar0-255字节存储变长字符串
text0-65535字节长文本数据
enum('w','m')65535个成员枚举:可赋予某个枚举成员来存储
set('w','m')64个成员集合: 可赋予多个集合成员,用逗号隔开
create table if not exists mystring(  -- 创建表
    username varchar(10),
    password char(6),
    article text,
    sex enum('w','m'),
    age int,
    num float
);

四. 数据的增删改查

增加: insert into  删除: delete from  修改: update  查询: select

1) insert 添加
指定字段添加值
insert into 表名(字段名1,字段名2,..) values(值1,值2,..)
insert into mystring(username,id,name) values('dsadas',21312312,'312321da');
不指定字段添加值(有多少字段 就要添加多少个值 一一对应)
insert into 表名 values(值1,值2,..)
insert into mystring values('sdsadas',1312312,'212321da'); 
添加多个值
不指定字段添加多个值
insert into 表名 values(值1,值2,..), (值1,值2,..),..
insert into mystring values('b',52,'b52'),('c',62,'c62');
指定字段添加多个值
insert into 表名(字段名1,字段2,..) values(值1,值2,..),(值1,值2,..),…
insert into mystring(username,id,name) values('2b',52,'b52'),('3c',62,'c62');
2) select 查询
主体结构:
select 字段 from 表名 [where条件][group by having][order by]
不指定字段查询
select * from 表名;
指定字段查询
select 字段1,字段2,.. from 表名;
给查询字段起别名
select 字段名 别名, 字段名 别名 from 表名;
select 字段名 as 别名, 字段名 as 别名 from 表名;
3) delete 删除
主体结构:
delete from 表名 [where]
注意:
where 如果不加 则删除所有数据
4) update 修改
主体结构:
update 表名 set 字段名=值 [, 字段名=值,..][where]
注意:
where 如果不加 会修改一列的数据

where条件:(1-6为基础) 

(1) 比较运算符 >、<、>=、<=、!或<>、=
1、> 大于 2、< 小于 3、>= 大于 4、<= 小于 5、! 或<> 不等于 6、= 等于
例:select * from a where id != 1;
(2) 逻辑运算符
1、and 逻辑与(并且) 2、or 逻辑或 3、between and 在…之间 包含值的本身
4、not between and 不在..之间 5、in 在..里 6、not in 不在..里
select * from a where id between 3 and 9 and id<3 or id >9;
select * from a where id in(1,2,10) and id not in (5,8,12);
(3) 子查询sql 的条件 还是一条sql语句
select * from a where id in (select id from user where password =123456);
(4) oder by排序
order by 字段名 asc/desc(升序/降序)
select * from a order by id desc; -- 按照id降序
select * from a order by id asc; -- 按照id升序
select * from a where age > 72 order by age;
注意:不加asc 默认升序
order by 要放在所有数据都处理完毕 再将数据排序显示
(5) is 或 not is
因为null是一个特殊的值不能使用比较运算符操作
select * from a where username is null;
select * from a where username is not null;
(6) limit 取值
limit x,y  即从x+1的位置取y条数据
limit y 从索引0的位置取出y条数据
select * from a order by age desc limit 0,2; -- 0表示从0开始第一条数据 2 表示查询两条即 1,2条数据
select * from a order by age desc limit 2;
select * from a where age between 112 and 255 and username is not null order by id desc limit 1;
(7) MySQL 聚合函数
count(字段名) 统计个数
select count(*) from a;
max(字段名) 最大值
select max(id) from a;
min(字段名) 最小值
sum(字段名) 求和
avg(字段名) 平均数
select count(*) as con,max(age) as mage,min(age) as minage,sum(age) sumage,avg(age) as avgage from a;
(8) group by 分组

select classid,count(*) as con from a group by classid;
select sex,count(*) as con from a group by sex;
select sex,count(*) as con from a group by sex order by con desc;
select classid,sex,count(*) from a group by classid,sex;
having 条件:
select classid,sex,count(*) as con from a group by classid,sex having con>1; # 按照班级和性别划分 查询人数>1
select classid,sex,count(*) as con from a group by classid,sex having con>1 and sex='w'; # 查询 人数>1且性别为w
select classid,sex,count(*) as con from a group by classid,sex having classid in('python1708');
(9) like 模糊查询

'%value%' 值包含就查询
select * from a where username like '%四%'
'value%' 以value值作为开头的数据
select * from a where username like ‘四%';
'%value' 以value值作为结尾的数据
select * from a where username like ‘%张';
select * from a where username like '%张%' and age>72 order by age desc limit 2;
(10) distinct 去除重复数据

select distinct age from a;
select age from a group by age;

 

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值