建表命令
create table `student` (
s_id integer(11) not null primary key comment '学生ID',
s_name varchar(16) not null collate utf8mb4_unicode_ci comment '学生名字'
) ENGINE =Innodb default character set utf8mb4 comment '学生表';
表操作
ALTER TABLE tbl_name
[alter_option [, alter_option] ...]
[partition_options]
alter_option: {
table_options
| ADD [COLUMN] col_name column_definition [FIRST | AFTER col_name]
| ADD [COLUMN] (col_name column_definition,...)
| ADD {INDEX | KEY} [index_name] [index_type] (key_part,...) [index_option] ...
| ADD {FULLTEXT | SPATIAL} [INDEX | KEY] [index_name] (key_part,...) [index_option] ...
| ADD [CONSTRAINT [symbol]] PRIMARY KEY [index_type] (key_part,...) [index_option] ...
| ADD [CONSTRAINT [symbol]] UNIQUE [INDEX | KEY] [index_name] [index_type] (key_part,...) [index_option] ...
| ADD [CONSTRAINT [symbol]] FOREIGN KEY [index_name] (col_name,...) reference_definition
| ADD CHECK (expr)
| ALGORITHM [=] {DEFAULT | INPLACE | COPY}
| ALTER [COLUMN] col_name { SET DEFAULT {literal | (expr)} | DROP DEFAULT}
| CHANGE [COLUMN] old_col_name new_col_name column_definition [FIRST | AFTER col_name]
| [DEFAULT] CHARACTER SET [=] charset_name [COLLATE [=] collation_name]
| CONVERT TO CHARACTER SET charset_name [COLLATE collation_name]
| {DISABLE | ENABLE} KEYS
| {DISCARD | IMPORT} TABLESPACE
| DROP [COLUMN] col_name
| DROP {INDEX | KEY} index_name
| DROP PRIMARY KEY
| DROP FOREIGN KEY fk_symbol
| FORCE
| LOCK [=] {DEFAULT | NONE | SHARED | EXCLUSIVE}
| MODIFY [COLUMN] col_name column_definition [FIRST | AFTER col_name]
| ORDER BY col_name [, col_name] ...
| RENAME {INDEX | KEY} old_index_name TO new_index_name | RENAME [TO | AS] new_tbl_name
| {WITHOUT | WITH} VALIDATION
}
-- 跳过partition_options
key_part:
col_name [(length)] [ASC | DESC]
table_option: {
AUTO_INCREMENT [=] value
| AVG_ROW_LENGTH [=] value
| [DEFAULT] CHARACTER SET [=] charset_name
| CHECKSUM [=] {0 | 1}
| [DEFAULT] COLLATE [=] collation_name
| COMMENT [=] 'string'
| COMPRESSION [=] {'ZLIB' | 'LZ4' | 'NONE'}
| CONNECTION [=] 'connect_string'
| {DATA | INDEX} DIRECTORY [=] 'absolute path to directory'
| DELAY_KEY_WRITE [=] {0 | 1}
| ENCRYPTION [=] {'Y' | 'N'}
| ENGINE [=] engine_name
| INSERT_METHOD [=] { NO | FIRST | LAST }
| KEY_BLOCK_SIZE [=] value
| MAX_ROWS [=] value
| MIN_ROWS [=] value
| PACK_KEYS [=] {0 | 1 | DEFAULT}
| PASSWORD [=] 'string'
| ROW_FORMAT [=] {DEFAULT | DYNAMIC | FIXED | COMPRESSED | REDUNDANT | COMPACT} | STATS_AUTO_RECALC [=] {DEFAULT | 0 | 1}
| STATS_PERSISTENT [=] {DEFAULT | 0 | 1}
| STATS_SAMPLE_PAGES [=] value
| TABLESPACE tablespace_name [STORAGE {DISK | MEMORY}]
| UNION [=] (tbl_name[,tbl_name]...)
}
-- 添加字段
alter table student add s_sex tinyint(1) not null default 0 comment '性别' after s_name;
-- 删除字段
ALTER TABLE student DROP COLUMN s_sex;
-- 修改字段
alter table student modify s_sex integer(3) not null default 1 comment '性别2';
-- 查看索引
show index from `tableName`;
-- 添加索引
alter table student add INDEX s_index_name (s_name) ;
-- 删除索引
alter table student drop index s_index_name;
事务和锁
-- 语句规则
START TRANSACTION
[transaction_characteristic [, transaction_characteristic] ...]
transaction_characteristic: {
WITH CONSISTENT SNAPSHOT| READ WRITE| READ ONLY
}
-- WITH CONSISTENT SNAPSHOT 仅在REPEATABLE READ下生效
BEGIN [WORK]
COMMIT [WORK] [AND [NO] CHAIN] [[NO] RELEASE]
ROLLBACK [WORK] [AND [NO] CHAIN] [[NO] RELEASE]
SET autocommit = {0 | 1}
START TRANSACTION or BEGIN start a new transaction.
- DDL语句不能rollback
隐式提交: 非主动执行commit语句,完成事务
- DDL语句触发隐式提交
- 修改表的语句
- 事务语句和锁语句
transaction_characteristic: { ISOLATION LEVEL level| access_mode }
level: { REPEATABLE READ| READ COMMITTED| READ UNCOMMITTED| SERIALIZABLE }
access_mode: { READ WRITE| READ ONLY
}
set transaction isolation level read committed ; set global transaction isolation level read committed ;
<a name="YKPdc"></a>
## 其他
<a name="pOuXX"></a>
### 查看变量
```sql
show variables ;
show variables like '%char%';
查看表结构
show create table business;
CREATE TABLE `business` (
`business_id` varchar(64) NOT NULL,
`business_name` varchar(32) NOT NULL,
`business_desc` varchar(256) NOT NULL DEFAULT '',
`user_id` varchar(64) NOT NULL,
`visibility` tinyint(4) NOT NULL DEFAULT '0',
`add_time` timestamp NOT NULL DEFAULT '2019-12-31 16:00:00',
PRIMARY KEY (`business_id`),
UNIQUE KEY `business_business_id_uindex` (`business_id`),
UNIQUE KEY `business_name_index` (`business_name`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8 COMMENT='业务'; -- CHARSET 表字符集
-- desc查看表结构
desc business;
-- 查看字段的全部信息
show full columns from business;
Field Type Collation Null Key Default Privileges Comments
business_id varchar(64) utf8_general_ci NO PRI "" select,insert,update,references "备注"
查看执行计划
-- DESCRIBE,EXPLAIN 以及 DESC 三个在(MySQL parser)中属于同义词.
-- DESCRIBE 一般用于获取表结构
-- EXPLAIN 获取执行计划,MySQL是如何执行查询的
DESC business;
desc select * from business where business_id = 'xxxx';
explain select * from business where business_id = 'xxxx';
查询计划的输出格式
![image.png](https://cdn.nlark.com/yuque/0/2021/png/191132/1630976866178-46a51bb5-6c9a-49fa-8db9-569b8bb1d7e9.png#clientId=u04440e27-3f8c-4&from=paste&height=70&id=u3c9852e0&margin=%5Bobject%20Object%5D&name=image.png&originHeight=106&originWidth=999&originalType=binary&ratio=1&size=23436&status=done&style=none&taskId=u76b18ec9-b6ad-40ac-92b7-6b5b6287551&width=662.5)
- id、select 标识符
- select_type、select类型
- simple (没有使用子查询或者union链接查询)
- table、表信息
- type、描述表是如何进行链接的
- system 表只有一行,特殊的
const
join类型 - const 最多只有一行匹配
- 出现在主键和唯一索引,他们都只会匹配一条数据
- eq_ref
- ref 最左匹配 & 选择的key不是主键和唯一索引, 最好是使用
=
或者是!=
- fulltext 使用fulltext索引
- ref_or_null
- index_merge
- unique_subquery
- index_subquery
- range
- index
- ALL
- system 表只有一行,特殊的
- pssible_keys、可能被选择的索引
- key、真实使用的索引
- key_len、被选择索引的长度
- ref、和索引比较的列
- rows、估计要检查的行数
- filtered、按表条件过滤的行百分比
- extra、额外信息 关注一下filesort,temporary table等等信息
- Using filesort
- Using temporary
- Using where
- Using index
- Using index condition