1. 基础命令
- 默认端口号: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 字段名;或者alter table 表名 drop 字段名
- 删除表:drop table 表名;
- 删除库:drop database 库名;
- 主键约束:primary key
- 唯一约束:unique
- 非空约束:not null
- 默认约束:default
- 外键约束:foreign key(外键)references主表(主键)
- 查看别的数据库的表格:show tables from 表名;
2. where条件查询
- 精确查询:=、!=、>、<、>=、<=
- 模糊查询:like(像)、not like(不像)
- 通配符:%:任意字符、-:单个字符
- 逻辑运算符:
- and:同时满足(优先级大于or)
- or:满足任意条件即可
- 区间运算:between a and b (闭区间)
- 集合运算:in 、not in
- 非空运算:is null 、is not null
3. 针对表内数据的操作
- 增加: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 列名1 ,count(列名2) 别名 from 表名 group by 列名1 having 别名 >2;
- 限制查询结果输出条数:limit 数字
传一个参数(输出前五条数据)
select * from 表名 limit 5;
传两个参数(输出6-15)
select * from 表名 limit 5,10;
:5:从5后开始,10:条数
- 修改表名:alter table 旧表名 rename 新表名;
- 修改表中id字段为sid:alter table 表名 change id sid char;
- 去掉某列:alter table 表名 drop 列名;
- 添加某列:alter table 表名 add 列名 char;
- 修改列为字符型:alter table 表名 modify 列名 char(20);
- 增加多列:alter table 表名 add(xh int(4),zc char(8),ads char(50),);
- 删除多列:alter table 表名 drop xh,zc,ads;
- 添加一个字段设主键约束:alter table 表名 add id sm unsigned primary key auto_increment;
- 关联查询-等值查询:select * from 表名 where a.id=b.id and 条件
- 内连接:select * from 表名1 inner join 表名2 on 表名1.xh=表名2.xh where 条件;
- 左连接:select * from 表名1 left join 表名2 on 表名1.xh=表名2.xh where 条件;
- 右连接:select * from 表名1 right join 表名2 on 表名1.xh=表名2.xh where 条件;
原文链接:https://blog.csdn.net/yuan2019035055/article/details/117573127
MySQL允许远程登录的授权方法
泛授权方式
数据库本地直接登录上数据库:
mysql -h localhost -u root
然后执行以下命令,授权完后直接就可以远程连接上。
mysql>GRANT ALL PRIVILEGES ON *.* TO 'root'@'%' WITH GRANT OPTION; //赋予任何主机上以root身份访问数据的权限
mysql>FLUSH PRIVILEGES;
授权指定IP方式
方法一、本地登入mysql,更改 "mysql" 数据库里的 "user" 表里的 "host" 项,将"localhost"改为"%"
#mysql -u root -proot
mysql>use mysql;
mysql>update user set host = '%' where user = 'root';
mysql>select host, user from user;
方法二、直接授权(推荐)
(1)从任何主机上使用root用户,密码:youpassword(你的root密码)连接到mysql服务器:
# mysql -u root -proot
mysql>GRANT ALL PRIVILEGES ON *.* TO 'root'@'%' IDENTIFIED BY 'youpassword' WITH GRANT OPTION;
(2) 修改配置文件/etc/mysql/my.cof 将bind-address:127.0.0.1修改为bind-address:0.0.0.0
例如,允许地址202.11.10.253上用root用户,密码dboomysql来连接mysql的所有数据库,付给select,insert,update,delete权限。
命令:
# mysql -u root -proot
grant select,insert,update,delete on *.* to root@"202.11.10.253" Identified by "dboomysql";
例如,允许地址202.11.10.253上用root用户,密码dboomysql来连接mysql的所有数据库,付给所有权限。 命令:
# mysql -u root -proot
grant all on *.* to root@"202.11.10.253" Identified by "dboomysql"
操作完后切记执行以下命令刷新权限
FLUSH PRIVILEGES
数据库操作示例
数据库查询全部
Where语句查询
Sum函数求和
Count函数求数据总行数
使用count函数计算age字段不为空的数据总条数
以age字段升序排序
以age字段降序排序
使用max函数求age最大值
使用min函数求age最小值
使用avg函数求age平均值
使用limit函数展示age升序排序后前5条数据
In语句
Between语句
Like条件查询
Like与‘%’配合完成模糊查询
Not like 查询
使用and或者 && 精准查询
使用or或者 || 查询
Is null 查询
Is not null查询
Group by having使用
使用正则表达式匹配miy字段以‘d’开头的数据
使用正则表达式匹配age等于20或者21的数据
查询数据库版本
查询用户
查看数据库名
查看数据库路径
查询操作系统版本
查看当前机器名
查看当前数据库
Left函数猜解用户名第一个字母
Right函数猜解用户名最后一个字母
使用substr猜解数据库名第2个字母
使用mid猜解
使用LOCATE返回字符串1在字符串2中的位置;
Sleep()睡眠函数
Hex()函数
Ascii()函数
Exp()函数
Updatexml()函数;