? (\?) Synonym for `help'. # 帮助信息 clear (\c) Clear the current input statement. 清空此行sql connect (\r) Reconnect to the server. Optional arguments are db and host. delimiter (\d) Set statement delimiter. edit (\e) Edit command with $EDITOR. ego (\G) Send command to mysql server, display result vertically. 格式化输出 exit (\q) Exit mysql. Same as quit. 退出登陆 ctrl(control) + d go (\g) Send command to mysql server. help (\h) Display this help. nopager (\n) Disable pager, print to stdout. tee (\T) Set outfile [to_outfile]. Append everything into given outfile. # 记录日志(语句+结果) eg:tee /tmp/mysql.log notee (\t) Don't write into outfile. 不记录日志 pager (\P) Set PAGER [to_pager]. Print the query results via PAGER. print (\p) Print current command. prompt (\R) Change your mysql prompt. quit (\q) Quit mysql. rehash (\#) Rebuild completion hash. source (\.) Execute an SQL script file. Takes a file name as an argument. 导入脚步,相当于 < status (\s) Get status information from the server. system (\!) Execute a system shell command. use (\u) Use another database. Takes database name as argument. charset (\C) Switch to another charset. Might be needed for processing binlog with multi-byte charsets. warnings (\W) Show warnings after every statement. nowarning (\w) Don't show warnings after every statement. resetconnection(\x) Clean session context.
# 用户管理 create user xxx@"白名单" indentified by "password" drop user alter user select user,host from mysql.user; # 权限 ## 查看所有权限列表 show privileges; all with grant option
# 查看用户权限 show grant UserName@"白名单" select * from mysql.user\G;
DCL
授权
1 2 3 4
grant 权限 on 对象 to 用户 identified by “密码” # mysql 8.0+:(中文表示,可自定制) create user 用户 identified by “密码” grant 权限1,权限2,权限3... on 对象 to 用户 identified by “密码”
权限: ALL: 管理员(不包含“ Grant option”,给他人授权) 权限1,权限2,权限3…: 普通人员(开发人员) Grant option
对象范围: 库,表
“.”
—> chmod -R 755 /
管理员
userName.*
—> chmod -R 755 userName/
普通用户
userName.t1
—> chmod -R 755 userName/t1
1 2
# 授权 grant create update, select ... on 库名.表的范围[*(所有), 表名1] to userName@“白名单”
# 或者 UPDATE student SET sname="里斯" WHERE id=4; UPDATE student SET sname="里斯" WHERE sage=4;
修改后,如下所示
需求二:
将所有表内成员的年龄+10;
1 2
UPDATE student SET sage=sage + 10 # UPDATE student SET sage+=10(错误写法,开发时候用的什么sage ++, sage +=,在这里都不允许)
需求三:将所有表内成员的年龄+10,除了里斯
1 2 3
UPDATE student SET sage=sage + 10 WHERE sname != "里斯"; # 当然也可以这样写; UPDATE student SET sage=sage + 10 WHERE sname = "赵一" OR sname = "王二" OR sname="张三" ;
AND: 执行均满足
OR: 满足其一执行
where 见下文
delete
1 2 3 4 5 6
# 删除指定数据 DELETE FROM tableName [WHERE id=1];
# 清空标中所有数据 DELETE FROM student; truncate table student;
区别:
delete: DML操作, 是逻辑性质删除,逐行进行删除,速度慢.
truncate: DDL操作,对与表段中的数据页进行清空,速度快.
伪删除:用update来替代delete,最终保证业务中查不到(select)
1 2 3 4 5 6 7
1.添加状态列 ALTER TABLE stuent ADD state TINYINT NOT NULL DEFAULT 1 ; SELECT * FROM stuent; 2. UPDATE 替代 DELETE UPDATE stuent SET state=0 WHERE id=6; 3. 业务语句查询 SELECT * FROM stu WHERE state=1;
拓展
1 2 3
DELETE FROM student; DROP TABLE student; truncate table student;
以上三条删除语句有何区别?
同:三者都是删除语句,均可删除
异:
DELETE FROM student:
逻辑上逐行删除,数据过多,操作很慢
并没有真正的从磁盘上删除,知识在磁盘上打上标记,磁盘空间不立即释放。HWM高位线不会降低
DROP TABLE student;
将表结构(元数据)和数据行,物理层次删除
truncate truncate table student;
清空表段中的所有数据页,物理层次删除全表数据,磁盘空间立即释放。HWM高位线降低
DQL 数据查询语言
show类
1 2 3 4 5 6 7
show databases; show CREATE DATABASE databaseName;
select FROM 表1,表2..., WHERE 过滤条件1,过滤条件2,过滤条件3 ... GROUP BY 条件列1,条件列2,条件列3 ... # selct_list 列名 HAVING 过滤条件1,过滤条件2,过滤条件3 ... ORDER BY 条件列1,条件列2,条件列3 ... LIMIT 限制条件;
单表子句-from
1 2 3 4 5 6 7 8
SELECT 列1,列2 FROM 表 SELECT * FROM 表
# EG # 查询student中所有的数据(不要对大表进行操作) SELECT * FROM stu ; # 查询stu表中,学生姓名和入学时间 SELECT sname , intime FROM stuent;
单表子句-where
1 2 3 4 5 6 7 8 9 10 11 12 13
SELECT col1,col2 FROM TABLE WHERE colN 条件; # where 操作符(>、<、>=、 <=、 <>、in、like、and、or) SELECT col1,col2 FROM TABLE WHERE = 条件;
# where 模糊查询 SELECT * FROM city WHERE district LIKE 'guang%'; % : 表示任意0个或多个字符。可匹配任意类型和长度的字符,有些情况下若是中文,请使用两个百分号(%%)表示。 _ : 表示任意单个字符。匹配单个任意字符,它常用来限制表达式的字符长度语句 [] : 表示括号内所列字符中的一个(类似正则表达式)。指定一个字符、字符串或范围,要求所匹配对象为它们中的任一个。
# where配合between...and... SELECT * FROM city WHERE population >1000000 AND population <2000000; SELECT * FROM city WHERE population BETWEEN 1000000 AND 2000000;