a. 用户管理
|
1
2
3
4
5
6
7
8
|
#创建用户 create user ‘用户名‘@‘IP地址‘ identified by ‘密码‘;#删除用户 drop user ‘用户名‘@‘IP地址‘;#修改用户 rename user ‘用户名‘@‘IP地址‘; to ‘新用户名‘@‘IP地址‘;;#修改密码 set password for ‘用户名‘@‘IP地址‘ = Password(‘新密码‘) |
b. 授权管理
|
1
2
3
|
show grants for ‘用户‘@‘IP地址‘ # -- 查看权限grant 权限 on 数据库.表 to ‘用户‘@‘IP地址‘ # -- 授权revoke 权限 on 数据库.表 from ‘用户‘@‘IP地址‘ # -- 取消权限 |
a. 创建数据库
|
1
2
3
4
5
6
|
# utf-8CREATE DATABASE 数据库名称 DEFAULT CHARSET utf8 COLLATE utf8_general_ci;create database olddog CHARACTER SET utf8 COLLATE utf8_general_ci;# gbkCREATE DATABASE 数据库名称 DEFAULT CHARACTER SET gbk COLLATE gbk_chinese_ci; |
b. 数据库常用操作
|
1
2
3
4
5
|
show create database oldboy\G; #查看创建库的信息show databases;show databases like "%old%";select database(); #查看进入的数据库 |
c. 常用命令
select version(); #查看版本select user(); #查看当前的用户select now(); #查看当前时间help create database #查看创建数据库帮助show character set; #查看字符集 create database oldboy CHARACTER SET utf8 COLLATE utf8_general_ci; #创建数据库grant all on oldboy.* to tom@localhost identified by ‘123456‘; #授权用户show grants for tom@localhost; #查看用户的权限select user,host from mysql.user; #查看有哪些用户use test;mysql> create table test( -> id int(4), -> name varchar(16) -> )ENGINE=innodb default charset=utf8;show create table test\G; #查看创建的表desc test; #查看表结构insert into test values(1,‘oldboy‘); #插入数据update 表名 set 字段=“” where 字段......; #修改字段数据delete from 表名 where 条件; #删除字段数据 2、查询(DQL) select user,host,password from mysql.user; #正常查询 select user,host,password from mysql.user order by user asc; #升序查询 select user,host,password from mysql.user order by user desc; #倒序查询 3、数据操作语言(DML)INSERT UPDATE DELETE delete from mysql.user where user="tom"; 4、事物处理语言(DPL)BEGIN TRANSACYION,COMMIT,ROLLBACK5、数据控制语言(DCL)GRANT REVOKE6、数据定义原因(DDL)CREATE DROP ALTER |
|
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
|
#创建表 create table 表名( 列名 类型 是否可以为空, 列名 类型 是否可以为空)ENGINE=InnoDB DEFAULT CHARSET=utf8 #删除表 drop table 表名 #清空表 delete from 表名truncate table 表名 #推荐使用 #修改表 #添加列:alter table 表名 add 列名 类型 #删除列:alter table 表名 drop column 列名 #修改列: alter table 表名 modify column 列名 类型; -- 类型 alter table 表名 change 原列名 新列名 类型; -- 列名,类型 #添加主键: alter table 表名 add primary key(列名); #删除主键: alter table 表名 drop primary key; alter table 表名 modify 列名 int, drop primary key; #自增主键修改 alter table db1 AUTO_INCREMENT=10; #添加外键:alter table 从表 add constraint 外键名称(形如:FK_从表_主表) foreign key 从表(外键字段) references 主表(主键字段); #删除外键:alter table 表名 drop foreign key 外键名称 #修改默认值:ALTER TABLE testalter_tbl ALTER i SET DEFAULT 1000; #删除默认值:ALTER TABLE testalter_tbl ALTER i DROP DEFAULT; #修改表名rename table 表名old to 表名new;alter table 表名old rename to 表名new; |
a. 增
|
1
2
3
|
insert into 表 (列名,列名...) values (值,值,值...)insert into 表 (列名,列名...) values (值,值,值...),(值,值,值...)insert into 表 (列名,列名...) select (列名,列名...) from 表 |
b. 删
|
1
2
|
delete from 表delete from 表 where id=1 and name=‘alex‘ |
c. 改
|
1
|
update 表 set name = ‘alex‘ where id>1 |
d. 查
|
1
2
3
|
select * from 表select * from 表 where id > 1select nid,name,gender as gg from 表 where id > 1 |
|
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
|
1、增insert into 表 (列名,列名...) values (值,值,值...);insert into 表 (列名,列名...) values (值,值,值...),(值,值,值...);insert into 表 (列名,列名...) select (列名,列名...) from 表;insert into tb11(name,age) values(‘alex‘,12);insert into tb11(name,age) values(‘alex‘,12),(‘root‘,18);insert into tb12(name,age) select name,age from tb11;2、删delete from 表;delete from 表 where id=1 and name=‘alex‘;delete from tb12 where id >=2 or name=‘alex‘;3、改update 表 set name=‘alex‘ where id>1;update tb12 set name=‘alex‘ where id>12 and name=‘xx‘update tb12 set name=‘alex‘,age=19 where id>12 and name=‘xx‘4、查select * from tb12; select id,name from tb12;select id,name as cname from tb12 where id > 10 or name =‘xxx‘; |
1 a、条件 2 select * from 表 where id > 1 and name != ‘alex‘ and num = 12; 3 4 select * from 表 where id between 5 and 16; 5 6 select * from 表 where id in (11,22,33) 7 select * from 表 where id not in (11,22,33) 8 select * from 表 where id in (select nid from 表) 9 10 b、通配符 11 select * from 表 where name like ‘ale%‘ - ale开头的所有(多个字符串) 12 select * from 表 where name like ‘ale_‘ - ale开头的所有(一个字符) 13 14 c、限制 15 select * from 表 limit 5; - 前5行 16 select * from 表 limit 4,5; - 从第4行开始的5行 17 select * from 表 limit 5 offset 4 - 从第4行开始的5行 18 19 d、排序 20 select * from 表 order by 列 asc - 根据 “列” 从小到大排列 21 select * from 表 order by 列 desc - 根据 “列” 从大到小排列 22 select * from 表 order by 列1 desc,列2 asc - 根据 “列1” 从大到小排列,如果相同则按列2从小到大排序 23 24 e、分组 25 select num from 表 group by num 26 select num,nid from 表 group by num,nid 27 select num,nid from 表 where nid > 10 group by num,nid order nid desc 28 select num,nid,count(*),sum(score),max(score),min(score) from 表 group by num,nid 29 30 select num from 表 group by num having max(id) > 10 31 32 特别的:group by 必须在where之后,order by之前 33 34 f、连表 35 select * from userinfo5,department5 36 37 select * from userinfo5,department5 where userinfo5.part_id = department5.id 38 39 40 select * from userinfo5 left join department5 on userinfo5.part_id = department5.id 41 # userinfo5左边全部显示 42 43 44 # select * from userinfo5 right join department5 on userinfo5.part_id = department5.id 45 # department5右边全部显示 46 47 48 select * from userinfo5 innder join department5 on userinfo5.part_id = department5.id 49 将出现null时一行隐藏 50 51 52 53 select * from department5 54 left join userinfo5 on userinfo5.part_id = department5.id 55 left join userinfo6 on userinfo5.part_id = department5.id
原文:https://www.cnblogs.com/ppf3678/p/10171447.html