MySQL数据库基础学习

罗列了一些MySQL的常用语句命令,可以进来看看

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
select user,host,password from mysql,user #查看mysql的用户和密码
mysql -hlocalhost -uroot -p #连接数据库
show databases; #显示数据库
select database(); #显示当前使用的数据库
select user(); #显示当前用户
select version(): 或者select @@version; #显示版本
use 数据库名; #选择数据库
select @@basedir; #显示mysql安装路径
select @@datadir; #显示数据库存放的路径
select @@hostname; #显示主机名
select @@version_compile_os; #显示mysql操作系统版本
select now(); #显示当前时间
create database 数据库名 character set utf8; #创建数据库
drop database 数据库名; #删除数据库
create table users
(id int(10) unsigned not null primary key auto_increment,
username varchar(30) not null,
email varchar(30) not null,
password varchar(32) not null)
default charset=utf8; #创建表(id是无符号整数类型)
create table me like users; #创建表me,结构与users表一致
show tables; #查看表
desc users; #查看表结构
insert into users(username,email,password) values('admin','1','123'); #插入表数据
select hex('admin') #查看admin字符的16进制,使用时前面加上0x,例:
select * from users where user=0x61646d696e;
select * from users limit 2; #查询前两条信息
select * from users limit 2,2; #查询第三条和第四条两条(从第三条开始,查询两条)
select * from users where username like '%admin%'; #查询用户名包含admin的所有信息(这里的%是like查询中的通配符,用来匹配所有字符,所以用户名中包含admin的全部都能显示出来)
select * from users where username='admin'; #查询用户名是admin的信息
update users set username='admin' where id=1; #修改id为1的用户名为admin
delete from users where id=12; #删除id为12的数据
select * from users where id=1 union select * from users where id=3; #联合查询,可以查询多条,要求各边查询的列数一致
select * from users order by 1; #根据第一列排序(可以以此为查询列数的依据)
select * from users where id=-1 union select 1,2,user(),4; #联合查询,只显示后面的查询结果,前面的查询结果为空,所以不显示
select * from users where id=1 and 1=2 union select 1,2,user(),4; #同上
create table ec_users(id int(10) primary key auto_increment,username varchar(32) not null,count_tmp float not null,dept varchar(32) not null)default charset=utf8; #创建表
select dept,count(*) from ec_users where count_tmp>=90 group by dept having count(dept)>1; #统计每个部门成绩大于90的人数
select DISTINCT username from ec_users; #distinct用来去除重复数据
select all dept from ec_users #all表示查询所有(默认为all,重复的也显示)
select * from ec_users where dept is null #查询部门值为空
select * from ec_users where dept is not null #查询部门值不为空
select * from ec_users where id BETWEEN 3 and 5 #查询id为3~5之间的数据
select * from ec_users where id in(1,2,6) #查询id值为1,2,6的数据(过waf有用)
select * from ec_users where id=1 or 'a' in ('b','a'); #过waf用,后面恒为真,'a'字符存在在in('a')里
select * from ec_users where id=1 && id<3; #查询id=1且id<3的
select * from ec_users where id=1 | id<3; #过waf用一个|或一个&可以显示
select * from ec_users where id xor 0; #异或,两个均为真显示0,两个不同为1
select * from ec_users where id not in(1,2,3) #not与!是一个用处,可以用在in、like
alter table users add age int(3) not null; #添加列
alter table users modify age int(4) #修改列的数据类型为int(4)
alter table users change age age int(3) #同上,也是修改列的数据类型,只是需要写两次列名
alter table users rename ress; #修改表名
alter table users rename as ress; #同上,也是修改表名
select count(*) from information_schema.tables #返回系统表里的数据个数
select rand() #返回0~1之间的随机浮点数
select rand()*4 #返回0~4之间的随机浮点数
select floor(rand()*4) #随机生成0~4(不可能是4)之间的整数
select floor(rand()*4)as dumb #并命名为dumb列(as可省略)
#if用法:
select if (判断条件,'语句1','语句2') #条件为真执行语句1,条件为假执行语句2
#十六进制:
SELECT 0x5045 #(这不是一个整数,而会是一个16进制字符串)
SELECT 0x50 + 0x45 #(现在这是整数了)
select concat('0x',hex('c:\\boot.ini')) #另类的使用十六进制方式
#字符串的串联
|| 仅在ANSI模式下的MySQL执行,其他情况下都会当成逻辑操作符并返回0,建议使用下面函数
concat() concat_ws() group_concat()
#绕过MD5哈希检查的例子
用户名:admin
密码:1234 ' AND 1=0 UNION ALL SELECT 'admin','81dc9bdb52d04dc20036dbd8313ed055
其中81dc9bdb52d04dc20036dbd8313ed055 = MD5(1234)
grant 权限1,权限2...,权限n on 数据库名.表名 to 用户名@用户地址 identified by '连接口令'; 赋给用户权限,权限被all privileges或all替代,表示赋给用户全部权限;数据库名.表名被*替代,表示赋给用户操作服务器上所有数据库所有表的权限;用户地址可以使ip地址、机器名、域名,也可以用%表示从任何地址连接;连接口令不能为空,否则创建失败;后面可以加with grant option对象授权,权限传递(我也不懂,就是这么说的)
例: GRANT ALL PRIVILEGES ON *.* TO 'root'@'%' IDENTIFIED BY '123456' WITH GRANT OPTION;
flush privileges; #刷新系统权限表
show variables like "secure_file_priv" #查看可写目录

本文标题:MySQL数据库基础学习

文章作者:暮沉沉

发布时间:2017年08月26日 - 08:08

最后更新:2017年08月26日 - 10:08

原始链接:http://maplege.github.io/2017/08/26/mysql-basic/

许可协议: 署名-非商业性使用-禁止演绎 4.0 国际 转载请保留原文链接及作者。

-------------本文结束感谢您的阅读-------------