mysql常用命令
默认端口号:3306
查看服务器版本:select version(); 或者 cmd命令 mysql -verison
登录数据库:mysql -uroot -p
退出数据库:exit/quit
查看当前系统下的数据库:show databases;
创建数据库:create 库名;
使用数据库:use 库名;
查看表:show tables;
建表:create table 表名 (字段名+空格+数据类型);
查看表结构:desc 表名;
添值:insert into 表名 (列名) values (值);
查看表中所有数据:select * from 表名;
查询建表时的结构:show create table 表名;
删除字段中的值:delete from 表名 where 条件;
删除表中的字段:delete from 表名 drop column 字段名;
删除表:drop table 表名;
删除库:drop database 库名;
主键约束:primary key
唯一约束:unique
非空约束:not null
默认约束:default
外键约束:foreign key(外键)references主表(主键)
查看别的数据库的表格:show tables from 表名;
where条件查询
精确查询:=、!=、>、<、>=、<=
模糊查询:like(像)、not like(不像)
通配符:%:任意字符、-:单个字符
逻辑运算符:
and:同时满足(优先级大于or)
or:满足任意条件即可
区间运算:between a and b (闭区间)
集合运算:in 、not in
非空运算:is null 、is not null
针对表内数据的操作
增加:insert into 表名 (列名) values (值);
删除:delete from 表名 where 条件;
查看:select * from 表名 where 条件;
修改:update 表名 set 字段=新值 where 条件;
排序:order by 字段名;(asc 升序、desc降序)
例:select * from 表名 order by 列名1 asc ,列名2 desc;
聚合函数:
sum() 函数:求累加和
例:select sum(字段名) as ‘别名’/别名 from 表名;
count() 函数:同级行数数量
(1)count(*):表示计算表中总的行数,不管某列是否有数值或者是为空
select count(*) from 表名;
(2)count(字段名):表示计算指定列下总的行数,计算或将忽略空值
select count(字段名) from 表名;
avg() 函数:返回一个平均值函数
例:select avg(字段名) as 别名 from 表名;
max() 函数:返回指定列中的最大值
select max(字段名) as 别名 from 表名;
min() 函数: 返回最小值
例:select min(字段名) as 别名 from 表名;
分组:
group by 字段 :将查询结果按一列/多列的值分组,值相等为一列
having 字段:二次判断,用到聚合函数后,又需筛选条件时,having和group by组合用
查看服务器版本:select version(); 或者 cmd命令 mysql -verison
登录数据库:mysql -uroot -p
退出数据库:exit/quit
查看当前系统下的数据库:show databases;
创建数据库:create 库名;
使用数据库:use 库名;
查看表:show tables;
建表:create table 表名 (字段名+空格+数据类型);
查看表结构:desc 表名;
添值:insert into 表名 (列名) values (值);
查看表中所有数据:select * from 表名;
查询建表时的结构:show create table 表名;
删除字段中的值:delete from 表名 where 条件;
删除表中的字段:delete from 表名 drop column 字段名;
删除表:drop table 表名;
删除库:drop database 库名;
主键约束:primary key
唯一约束:unique
非空约束:not null
默认约束:default
外键约束:foreign key(外键)references主表(主键)
查看别的数据库的表格:show tables from 表名;
where条件查询
精确查询:=、!=、>、<、>=、<=
模糊查询:like(像)、not like(不像)
通配符:%:任意字符、-:单个字符
逻辑运算符:
and:同时满足(优先级大于or)
or:满足任意条件即可
区间运算:between a and b (闭区间)
集合运算:in 、not in
非空运算:is null 、is not null
针对表内数据的操作
增加:insert into 表名 (列名) values (值);
删除:delete from 表名 where 条件;
查看:select * from 表名 where 条件;
修改:update 表名 set 字段=新值 where 条件;
排序:order by 字段名;(asc 升序、desc降序)
例:select * from 表名 order by 列名1 asc ,列名2 desc;
聚合函数:
sum() 函数:求累加和
例:select sum(字段名) as ‘别名’/别名 from 表名;
count() 函数:同级行数数量
(1)count(*):表示计算表中总的行数,不管某列是否有数值或者是为空
select count(*) from 表名;
(2)count(字段名):表示计算指定列下总的行数,计算或将忽略空值
select count(字段名) from 表名;
avg() 函数:返回一个平均值函数
例:select avg(字段名) as 别名 from 表名;
max() 函数:返回指定列中的最大值
select max(字段名) as 别名 from 表名;
min() 函数: 返回最小值
例:select min(字段名) as 别名 from 表名;
分组:
group by 字段 :将查询结果按一列/多列的值分组,值相等为一列
having 字段:二次判断,用到聚合函数后,又需筛选条件时,having和group by组合用
浙公网安备 33010602011771号