mysql - 用户权限相关数据test - 用于用户测试数据information_schema - MySQL本身架构相关数据
展示所有数据库
SHOW DATABASES;
创建数据库
CREATE DATABASE DB_NAME;
使用数据库
USE DB_NAME;
显示当前使用数据库中所有表
SHOW TABLES;
用户管理
创建用户
create user '用户名'@'IP地址' identified by '密码'; -- 用户名@IP地址 用户只能在改IP下才能访问 -- 用户名@192.168.1.% 用户只能在改IP段下才能访问(通配符%表示任意) -- 用户名@% 用户可以再任意IP下访问(默认IP地址为%)
删除用户
drop user '用户名'@'IP地址';
修改用户
rename user '用户名'@'IP地址'; to '新用户名'@'IP地址';;
修改密码
set password for '用户名'@'IP地址' = Password('新密码')
授权管理
show grants for '用户'@'IP地址' -- 查看权限grant 权限 on 数据库.表 to '用户'@'IP地址' -- 授权revoke 权限 on 数据库.表 from '用户'@'IP地址' -- 取消权限CREATE USER 'abcxander'@'localhost';GRANT ALL PRIVILEGES ON *.* to 'abcxander'@'localhost' WITH GRANT OPTION; -- 创建一个可以做任何事情的新用户(如:root)
可以为表的各个列设置一些权限,要使用列权限,请明确指定表,并在特权类型之后提供表的字段。例如,以下语句将允许用户读取员工的姓名和职位,但不能读取同一表中的其他信息,例如薪水。
GRANT SELECT (name, position) on Employee to 'jeffrey'@'localhost';
GRANT USAGE ON *.* TO 'someone'@'localhost' REQUIRE SUBJECT '/CN=www.mydom.com/O=My Dom, Inc./C=US/ST=Oregon/L=Portland' AND ISSUER '/C=FI/ST=Somewhere/L=City/ O=Some Company/CN=Peter Parker/emailAddress=p.parker@marvel.com' AND CIPHER 'SHA-DES-CBC3-EDH-RSA';
insert into 表名 (字段名,字段名...) values (值,值,值...);insert into 表名 (字段名,字段名...) values (值,值,值...),(值,值,值...);insert into 表名 (字段名,字段名...) select (字段名,字段名...) from 表名;
删
delete from 表名;delete from 表名 where id=1 and name='abcx';
改
update 表名 set name = 'abcx' where id>1
查
普通查询
select * from 表名select * from 表名 where id > 1select nid,name,gender as gg from 表名 where id > 1
条件查询
select * from 表 where id > 1 and name != 'abcx' and num = 12;select * from 表 where id between 5 and 16; --BETWEEN 操作符选取介于两个值之间的数据范围内的值select * from 表 where id in (11,22,33);select * from 表 where id not in (11,22,33);select * from 表 where id in (select nid from 表); --可以将查询出的内容当作条件
模糊查询
select * from 表 where name like 'abc%' -- abc开头的所有(多个字符串)select * from 表 where name like 'abc_' -- abc开头的所有(一个字符)select * from 表 where name like 'abc[a-f]' -- [ ] 指定范围 ([a-f]) 或集合 ([abcdef]) 中的任何单个字符select * from 表 where name like 'abc[^a-f]' -- [^] 不属于指定范围 ([a-f]) 或集合 ([abcdef]) 的任何单个字符select * from 表 where name like '%[!0-9]%' -- [!] 排除 匹配内容不包含数字
分页查询
select * from 表 limit 5; -- 前5行select * from 表 limit 4,5; -- 从第4行开始的5行select * from 表 limit 5 offset 4; -- 从第4行开始的5行
查询排序
select * from 表 order by 列 asc; -- 根据 “列” 从小到大排列select * from 表 order by 列 desc; -- 根据 “列” 从大到小排列select * from 表 order by 列1 desc,列2 asc; -- 根据 “列1” 从大到小排列,如果相同则按列2从小到大排序
分组查询
select num from 表 group by num;select num,nid from 表 group by num,nid;select num,nid from 表 where nid > 10 group by num,nid order by nid desc;select num,nid,count(*),sum(score),max(score),min(score) from 表 group by num nid;select num from 表 group by num having max(id) > 10; --特别的:group by 必须在where之后,order by之前
连表查询
无对应关系则不显示select A.num, A.name, B.namefrom A,BWhere A.nid = B.nid自连接,无对应关系则不显示select A.num, A.name, B.namefrom A inner join Bon A.nid = B.nid左连接,A表所有显示,如果B中无对应关系,则值为nullselect A.num, A.name, B.namefrom A left join Bon A.nid = B.nid右连接,B表所有显示,如果B中无对应关系,则值为nullselect A.num, A.name, B.namefrom A right join Bon A.nid = B.nid
组合查询
组合,自动处理重合select nicknamefrom Aunionselect namefrom B组合,不处理重合select nicknamefrom Aunion allselect namefrom B