Open luisedware opened 7 years ago
INSERT
INSERT SET-SELECT
WHERE
GROUP BY
HAVING
ORDER BY
LIMIT
brew install mysql
launchctl load -w ~/Library/LaunchAgents/homebrew.mxcl.mysql.plist"
launchctl unload -w ~/Library/LaunchAgents/homebrew.mxcl.mysql.plist"
mysql
mysql -uroot -p
mysql --version
mysql -uroot -P6606 -p
shell>mysql -uroot -proot --prompt 提示符
prompt 提示符
prompt
prompt \h-\u-\p-\D
SELECT VERSION();
SELECT NEW();
SELECT USER();
SHOW DATABASES;
SHOW WARINGS;
创建数据库 - CREATE {DATABASE | SCHEMA} [IF NOT EXISTS] db_name [DEFAULT] CHARACTER SET [=] charset_name;
CREATE {DATABASE | SCHEMA} [IF NOT EXISTS] db_name [DEFAULT] CHARACTER SET [=] charset_name;
修改数据库 - ALTER {DATABASE | SCHEMA} [db_name] [DEFAULT] CHARACTER SET [=] charset_name;
ALTER {DATABASE | SCHEMA} [db_name] [DEFAULT] CHARACTER SET [=] charset_name;
删除数据库 - DROP {DATABASE | SCHEMA} [IF EXISTS] db_name
DROP {DATABASE | SCHEMA} [IF EXISTS] db_name
zerofill
TINYINT
SMALLINT
MEDIUMINT
INT
BIGINT
FLOAT
DOUBLE
DECIMAL
YEAR
TIME
DATE
DATETIME
TIMESTAMP
CHAR
VARCHAR
TINYTEXT
TEXT
MEDIUMTEXT
LONGTEXT
ENUM
SET
use {database}
CREATE TABLE [IF NOT EXISTS] table_name(column_name data_type,....)
SHOW TABLES [FROM db_name] [LIKE 'pattern'|WHERE expr]
SHOW COLUMNS FROM tbl_name
INSERT [INTO] tal_name[(col_name)VALUES(val,..)]
SELECT expr,... FROM tbl_name
NULL
NOT NULL
AUTO_INCREMENT
PRIMARY KEY
UNIQUE KEY
DEFAULT
FOREIGN KEY
default-storage-engine=INNODB
CASCADE
SET NULL
RESTRICT
NO ACTION
ALTER TABLE tbl_name ADD [COLUMN] col_name column_definition [FIRST | AFTER col_name]
ALTER TABLE tbl_name ADD [COLUMN] (col_name column_definition,...)
ALTER TABLE tbl_name DROP [COLUMN] col_name
ALTER TABLE tbl_name ADD [CONSTRAINT [symbol]] PRIMARY KEY [index_type](index_col_name,...)
ALTER TABLE tbl_name ADD [CONSTRAINT [symbol]] UNIQUE [INDEX|KEY][index_name][index_type](index_col_name,...)
ALTER TABLE tbl_name ADD [CONSTRAINT [symbol]] FOREIGN KEY [index_name](index_col_name,...) reference_definition
ALTER TABLE tbl_name ALTER [COLUMN] col_name {SET DEFAULT literal | DROP DEFAULT}
ALTER TABLE tbl_name DROP PRIMARY KEY
ALTER TABLE tbl_name DROP {INDEX | KEY} index_name
ALTER TABLE tbl_name DROP FOREIGN KEY fk_symbol
ALTER TABLE tbl_name RENAME [TO|AS] new_tbl_name
RENAME TABLE tbl_name TO new_tbl_name [,tbl_name2 To ewn_tbl_name2,...]
ALTER TABLE tbl_name MODIFY [COLUMN] col_name column_definition [FIRST | AFTER col_name]
ALTER TABLE tbl_name CHANGE [COLUMN] old_col_name new_col_name column_definition [FIRST|AFTER col_name]
INSERT [INFO] tbl_name [(col_name,...)] {VALUES | VALUE}({expr|DEFAULT},...),(...)
INSERT [INTO] tbl_name SET col_name={expr | DEFAULT},...
INSERT [INTO] tbl_name [(col_name,...)] SELECT ...
UPDATE
UPDATE [LOW_PRIORITY] [IGNORE] table_reference SET col_name1={expr | DEFAULT}[,col_name2={expr2|DEFAULT}]...[WHERE where_condition]
DELETE
DELETE FROM tbl_name [WHERE where_condition]
SELECT
SELECT select_expr[,select_expr ...]
[
FROM table_references
[WHERE where_condition]
[GROUP BY {col_name | position} [ASC | DESC], ...]
[HAVING where_condition]
[ORDER BY {col_name | expr | position} [ASC | DESC],...]
[LIMIT {[offset,] row_count | row_count OFFSET offset}]
]
[ AS ]
LIMIT 2 OFFSET 2
SELECT * FROM t1 WHERE col1 = (SELECT col2 FROM t2)
SELECT * FROM t1
SELECT col2 FROM t2
INSERT [INTO] tbl_name [(col_name)] SELECT...
UPDATE table_references
SET col_name1={expr1|DEFAULT}
[,col_name2={expr2|DEFAULT}]
CREATE TABLE [IF NOT EXISTS] tbl_name [(create_definition,...)] select_statement
table_reference
tbl_name [[AS] alias] | table_subquery [AS] alias
{[INNER | CROSS] JOIN | {LEFT | RIGHT} [OUTER] JOIN}
ON conditional_expr
SELECT * FROM A LEFT JOIN B [WHERE where_condition]
SELECT * FROM A,B,(,C)
SELECT * FROM A CROSS JOIN B (CROSS JOIN C)
SELECT * FROM A,B WHERE A.id = B.id
SELECT * FROM A INNER JOIN B ON A.id = B.id
ON A.id = B.id
ON A.id > B.id
SELECT * FROM A T1 INNER JOIN A T2 ON T1.id = T2.pid
SELECT * FROM A UNION SELECT * FROM B UNION ...
SELECT * FROM A LEFT JOIN B ON A.id = B.id UNION SELECT * FROM A RIGHT JOIN B ON A.id = B.id
DELETE tbl_name[.*] [,tbl_name[.*]] ... FROM table_references [WHERE where_condition]
总结的很到位啊,赞
目录
INSERT
INSERT SET-SELECT
WHERE
语句进行条件查询GROUP BY
语句对查询结果分组HAVING
语句设置分组条件ORDER BY
语句对查询结果排序LIMIT
语句限制查询数量第 1 章 初涉 MySQL
MySQL 概述
MySQL 的安装与配置
brew install mysql
启动与停止 MySQL 服务
launchctl load -w ~/Library/LaunchAgents/homebrew.mxcl.mysql.plist"
launchctl unload -w ~/Library/LaunchAgents/homebrew.mxcl.mysql.plist"
<a id="1.4>登录与退出
mysql
命令mysql
命令例子mysql -uroot -p
mysql --version
mysql -uroot -P6606 -p
mysql
CLI 环境中输入修改 MySQL 提示符
shell>mysql -uroot -proot --prompt 提示符
prompt 提示符
prompt
提示符参数:prompt
提示符参数用法:prompt \h-\u-\p-\D
MySQL 常用命令以及语法规范
SELECT VERSION();
SELECT NEW();
SELECT USER();
SHOW DATABASES;
SHOW WARINGS;
操作数据库
创建数据库 -
CREATE {DATABASE | SCHEMA} [IF NOT EXISTS] db_name [DEFAULT] CHARACTER SET [=] charset_name;
修改数据库 -
ALTER {DATABASE | SCHEMA} [db_name] [DEFAULT] CHARACTER SET [=] charset_name;
删除数据库 -
DROP {DATABASE | SCHEMA} [IF EXISTS] db_name
第 2 章 数据类型与操作数据表
数据类型之整型
zerofill
后,会自动在数值填充 0 以达到指定长度。TINYINT
SMALLINT
MEDIUMINT
INT
BIGINT
数据类型之浮点型
FLOAT
[(M,D)]:M 是数字总位数,D 是小数点后面的位数。如果 M 和 D 被省略,根据硬件允许的限制来保存值。单精度浮点数精确到大约 7 位小数位。DOUBLE
[(M,D)]DECIMAL
[(M,D)]:可存储比 BIGINT 更大的整数,也可以用于存储精确的小数。数据类型之日期时间型
YEAR
TIME
DATE
DATETIME
TIMESTAMP
BIGINT
存储数据类型之字符型
CHAR
:M 个字节,0 <= M <= 255VARCHAR
:L + 1 个字节,其中 L <= M 且 0 <= M <= 65535TINYTEXT
:L + 1 个字节,其中 L < 2^8TEXT
:L + 2 个字节,其中 L < 2^16MEDIUMTEXT
:L + 3 个字节,其中 L < 2^24LONGTEXT
:L + 4 个字节,其中 L < 2^32ENUM
('value1','value2',...):1 或 2 个字节,取决于枚举值的个数(最多 65535 个值)SET
('value1','value2',...):1、2、3、4 或者 8 个字节,取决于 set 成员的数目(最多64个)创建数据表
use {database}
CREATE TABLE [IF NOT EXISTS] table_name(column_name data_type,....)
查看数据表
SHOW TABLES [FROM db_name] [LIKE 'pattern'|WHERE expr]
查看数据表结构
SHOW COLUMNS FROM tbl_name
记录的插入与查找
INSERT [INTO] tal_name[(col_name)VALUES(val,..)]
SELECT expr,... FROM tbl_name
空值与非空
NULL
,字段值可以为空NOT NULL
,字段值禁止为空自动编号
AUTO_INCREMENT
,自动编号,且必须与主键组合使用初涉主键约束
PRIMARY KEY
AUTO_INCREMENT
必须和PRIMARY KEY
一起使用,PRIMARY KEY
不必须和AUTO_INCREMENT
一起使用初涉唯一约束
UNIQUE KEY
初涉默认约束
DEFAULT
第 3 章 约束以及修改数据表
外键约束的要求解析
DEFAULT
(默认约束)NOT NULL
(非空约束)UNIQUE KEY
(唯一约束)PRIMARY KEY
(主键约束)FOREIGN KEY
(外键约束)default-storage-engine=INNODB
外键约束的参照操作
CASCADE
:从父表删除或更新且自动删除或更新子表中匹配的行SET NULL
:从父表删除或更新行,并设置子表中的外键列为 NULL。如果使用该选项,必须保证子表列没有指定 NOT NULLRESTRICT
:拒绝对父表的删除或更新操作NO ACTION
:标准 SQL 的关键字,在 MySQL 中与 RESTRICE 相同表级约束与列级约束
修改数据表-添加/删除列
ALTER TABLE tbl_name ADD [COLUMN] col_name column_definition [FIRST | AFTER col_name]
ALTER TABLE tbl_name ADD [COLUMN] (col_name column_definition,...)
ALTER TABLE tbl_name DROP [COLUMN] col_name
修改数据表--添加约束
ALTER TABLE tbl_name ADD [CONSTRAINT [symbol]] PRIMARY KEY [index_type](index_col_name,...)
ALTER TABLE tbl_name ADD [CONSTRAINT [symbol]] UNIQUE [INDEX|KEY][index_name][index_type](index_col_name,...)
ALTER TABLE tbl_name ADD [CONSTRAINT [symbol]] FOREIGN KEY [index_name](index_col_name,...) reference_definition
ALTER TABLE tbl_name ALTER [COLUMN] col_name {SET DEFAULT literal | DROP DEFAULT}
修改数据表--删除约束
ALTER TABLE tbl_name DROP PRIMARY KEY
ALTER TABLE tbl_name DROP {INDEX | KEY} index_name
ALTER TABLE tbl_name DROP FOREIGN KEY fk_symbol
修改数据表--修改列定义和更名数据表
ALTER TABLE tbl_name RENAME [TO|AS] new_tbl_name
RENAME TABLE tbl_name TO new_tbl_name [,tbl_name2 To ewn_tbl_name2,...]
ALTER TABLE tbl_name MODIFY [COLUMN] col_name column_definition [FIRST | AFTER col_name]
ALTER TABLE tbl_name CHANGE [COLUMN] old_col_name new_col_name column_definition [FIRST|AFTER col_name]
小结
NOT NULL
(非空约束)PRIMARY KEY
(主键约束)UNIQUE KEY
(唯一约束)DEFAULT
(默认约束)FOREIGN KEY
(外键约束)第 4 章 操作数据表中的记录
插入记录
INSERT
INSERT [INFO] tbl_name [(col_name,...)] {VALUES | VALUE}({expr|DEFAULT},...),(...)
插入记录
INSERT SET-SELECT
INSERT [INTO] tbl_name SET col_name={expr | DEFAULT},...
INSERT [INTO] tbl_name [(col_name,...)] SELECT ...
单表更新记录 UPDATE
UPDATE
UPDATE [LOW_PRIORITY] [IGNORE] table_reference SET col_name1={expr | DEFAULT}[,col_name2={expr2|DEFAULT}]...[WHERE where_condition]
单表删除记录 DELETE
DELETE
DELETE FROM tbl_name [WHERE where_condition]
查询表达式解析
SELECT
SELECT select_expr[,select_expr ...]
[
FROM table_references
[WHERE where_condition]
[GROUP BY {col_name | position} [ASC | DESC], ...]
[HAVING where_condition]
[ORDER BY {col_name | expr | position} [ASC | DESC],...]
[LIMIT {[offset,] row_count | row_count OFFSET offset}]
]
[ AS ]
alias_name 为其赋予别名GROUP BY
,ORDER BY
或HAVING
子句WHERE
语句进行条件查询WHERE
- 条件表达式WHERE
子句,则显示所有记录WHERE
表达式中,可以使用 MySQL 支持的函数或者运算符GROUP BY
语句对查询结果分组[GROUP BY {col_name | position} [ASC | DESC], ...]
HAVING
语句设置分组条件[HAVING where_condition]
ORDER BY
语句对查询结果排序[ORDER BY {col_name | expr | position} [ASC | DESC],...]
LIMIT
语句限制查询数量[LIMIT {[offset,] row_count | row_count OFFSET offset}]
LIMIT 2 OFFSET 2
第 5 章 子查询与连接
子查询简介
SELECT * FROM t1 WHERE col1 = (SELECT col2 FROM t2)
;SELECT * FROM t1
,称为 Outer Query / Outer StatementSELECT col2 FROM t2
,称为 SubQuery由比较运算符引发的子查询
由 [NOT] IN/EXISTS 引发的子查询
使用 INSERT...SELECT 插入记录
INSERT [INTO] tbl_name [(col_name)] SELECT...
<a id="5.5"多表更新>
UPDATE table_references
SET col_name1={expr1|DEFAULT}
[,col_name2={expr2|DEFAULT}]
[WHERE where_condition]
多表更新之一步到位
CREATE TABLE [IF NOT EXISTS] tbl_name [(create_definition,...)] select_statement
连接的语法结构
table_reference
- 数据表参照tbl_name [[AS] alias] | table_subquery [AS] alias
{[INNER | CROSS] JOIN | {LEFT | RIGHT} [OUTER] JOIN}
table_reference
ON conditional_expr
连接类型
关于连接的几点说明
SELECT * FROM A LEFT JOIN B [WHERE where_condition]
SELECT * FROM A,B,(,C)
或者SELECT * FROM A CROSS JOIN B (CROSS JOIN C)
SELECT * FROM A,B WHERE A.id = B.id
或者SELECT * FROM A INNER JOIN B ON A.id = B.id
ON A.id = B.id
ON A.id > B.id
SELECT * FROM A T1 INNER JOIN A T2 ON T1.id = T2.pid
SELECT * FROM A UNION SELECT * FROM B UNION ...
SELECT * FROM A LEFT JOIN B ON A.id = B.id UNION SELECT * FROM A RIGHT JOIN B ON A.id = B.id
多表删除
DELETE tbl_name[.*] [,tbl_name[.*]] ... FROM table_references [WHERE where_condition]