建表命令

  1. create table `student` (
  2. s_id integer(11) not null primary key comment '学生ID',
  3. s_name varchar(16) not null collate utf8mb4_unicode_ci comment '学生名字'
  4. ) ENGINE =Innodb default character set utf8mb4 comment '学生表';

表操作

  1. ALTER TABLE tbl_name
  2. [alter_option [, alter_option] ...]
  3. [partition_options]
  4. alter_option: {
  5. table_options
  6. | ADD [COLUMN] col_name column_definition [FIRST | AFTER col_name]
  7. | ADD [COLUMN] (col_name column_definition,...)
  8. | ADD {INDEX | KEY} [index_name] [index_type] (key_part,...) [index_option] ...
  9. | ADD {FULLTEXT | SPATIAL} [INDEX | KEY] [index_name] (key_part,...) [index_option] ...
  10. | ADD [CONSTRAINT [symbol]] PRIMARY KEY [index_type] (key_part,...) [index_option] ...
  11. | ADD [CONSTRAINT [symbol]] UNIQUE [INDEX | KEY] [index_name] [index_type] (key_part,...) [index_option] ...
  12. | ADD [CONSTRAINT [symbol]] FOREIGN KEY [index_name] (col_name,...) reference_definition
  13. | ADD CHECK (expr)
  14. | ALGORITHM [=] {DEFAULT | INPLACE | COPY}
  15. | ALTER [COLUMN] col_name { SET DEFAULT {literal | (expr)} | DROP DEFAULT}
  16. | CHANGE [COLUMN] old_col_name new_col_name column_definition [FIRST | AFTER col_name]
  17. | [DEFAULT] CHARACTER SET [=] charset_name [COLLATE [=] collation_name]
  18. | CONVERT TO CHARACTER SET charset_name [COLLATE collation_name]
  19. | {DISABLE | ENABLE} KEYS
  20. | {DISCARD | IMPORT} TABLESPACE
  21. | DROP [COLUMN] col_name
  22. | DROP {INDEX | KEY} index_name
  23. | DROP PRIMARY KEY
  24. | DROP FOREIGN KEY fk_symbol
  25. | FORCE
  26. | LOCK [=] {DEFAULT | NONE | SHARED | EXCLUSIVE}
  27. | MODIFY [COLUMN] col_name column_definition [FIRST | AFTER col_name]
  28. | ORDER BY col_name [, col_name] ...
  29. | RENAME {INDEX | KEY} old_index_name TO new_index_name | RENAME [TO | AS] new_tbl_name
  30. | {WITHOUT | WITH} VALIDATION
  31. }
  32. -- 跳过partition_options
  33. key_part:
  34. col_name [(length)] [ASC | DESC]
  35. table_option: {
  36. AUTO_INCREMENT [=] value
  37. | AVG_ROW_LENGTH [=] value
  38. | [DEFAULT] CHARACTER SET [=] charset_name
  39. | CHECKSUM [=] {0 | 1}
  40. | [DEFAULT] COLLATE [=] collation_name
  41. | COMMENT [=] 'string'
  42. | COMPRESSION [=] {'ZLIB' | 'LZ4' | 'NONE'}
  43. | CONNECTION [=] 'connect_string'
  44. | {DATA | INDEX} DIRECTORY [=] 'absolute path to directory'
  45. | DELAY_KEY_WRITE [=] {0 | 1}
  46. | ENCRYPTION [=] {'Y' | 'N'}
  47. | ENGINE [=] engine_name
  48. | INSERT_METHOD [=] { NO | FIRST | LAST }
  49. | KEY_BLOCK_SIZE [=] value
  50. | MAX_ROWS [=] value
  51. | MIN_ROWS [=] value
  52. | PACK_KEYS [=] {0 | 1 | DEFAULT}
  53. | PASSWORD [=] 'string'
  54. | ROW_FORMAT [=] {DEFAULT | DYNAMIC | FIXED | COMPRESSED | REDUNDANT | COMPACT} | STATS_AUTO_RECALC [=] {DEFAULT | 0 | 1}
  55. | STATS_PERSISTENT [=] {DEFAULT | 0 | 1}
  56. | STATS_SAMPLE_PAGES [=] value
  57. | TABLESPACE tablespace_name [STORAGE {DISK | MEMORY}]
  58. | UNION [=] (tbl_name[,tbl_name]...)
  59. }
  1. -- 添加字段
  2. alter table student add s_sex tinyint(1) not null default 0 comment '性别' after s_name;
  3. -- 删除字段
  4. ALTER TABLE student DROP COLUMN s_sex;
  5. -- 修改字段
  6. alter table student modify s_sex integer(3) not null default 1 comment '性别2';
  7. -- 查看索引
  8. show index from `tableName`;
  9. -- 添加索引
  10. alter table student add INDEX s_index_name (s_name) ;
  11. -- 删除索引
  12. alter table student drop index s_index_name;

事务和锁

  1. -- 语句规则
  2. START TRANSACTION
  3. [transaction_characteristic [, transaction_characteristic] ...]
  4. transaction_characteristic: {
  5. WITH CONSISTENT SNAPSHOT| READ WRITE| READ ONLY
  6. }
  7. -- WITH CONSISTENT SNAPSHOT 仅在REPEATABLE READ下生效
  8. BEGIN [WORK]
  9. COMMIT [WORK] [AND [NO] CHAIN] [[NO] RELEASE]
  10. ROLLBACK [WORK] [AND [NO] CHAIN] [[NO] RELEASE]
  11. SET autocommit = {0 | 1}

START TRANSACTION or BEGIN start a new transaction.

  • DDL语句不能rollback

隐式提交: 非主动执行commit语句,完成事务

  • DDL语句触发隐式提交
  • 修改表的语句
  • 事务语句和锁语句
    • BEGIN
    • LOCK TABLES
    • SET autocommit = 1 (if the value is not already 1)
    • START TRANSACTION
    • UNLOCK TABLES.

      设置事务隔离级别

      ```sql SET [GLOBAL | SESSION] TRANSACTION transaction_characteristic [, transaction_characteristic] …

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 ;

  1. <a name="YKPdc"></a>
  2. ## 其他
  3. <a name="pOuXX"></a>
  4. ### 查看变量
  5. ```sql
  6. show variables ;
  7. show variables like '%char%';

查看表结构

  1. show create table business;
  2. CREATE TABLE `business` (
  3. `business_id` varchar(64) NOT NULL,
  4. `business_name` varchar(32) NOT NULL,
  5. `business_desc` varchar(256) NOT NULL DEFAULT '',
  6. `user_id` varchar(64) NOT NULL,
  7. `visibility` tinyint(4) NOT NULL DEFAULT '0',
  8. `add_time` timestamp NOT NULL DEFAULT '2019-12-31 16:00:00',
  9. PRIMARY KEY (`business_id`),
  10. UNIQUE KEY `business_business_id_uindex` (`business_id`),
  11. UNIQUE KEY `business_name_index` (`business_name`)
  12. ) ENGINE=InnoDB DEFAULT CHARSET=utf8 COMMENT='业务'; -- CHARSET 表字符集
  13. -- desc查看表结构
  14. desc business;
  15. -- 查看字段的全部信息
  16. show full columns from business;
  17. Field Type Collation Null Key Default Privileges Comments
  18. business_id varchar(64) utf8_general_ci NO PRI "" select,insert,update,references "备注"

查看执行计划

  1. -- DESCRIBE,EXPLAIN 以及 DESC 三个在(MySQL parser)中属于同义词.
  2. -- DESCRIBE 一般用于获取表结构
  3. -- EXPLAIN 获取执行计划,MySQL是如何执行查询的
  4. DESC business;
  5. desc select * from business where business_id = 'xxxx';
  6. explain select * from business where business_id = 'xxxx';

查询计划的输出格式

  1. ![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
  • pssible_keys、可能被选择的索引
  • key、真实使用的索引
  • key_len、被选择索引的长度
  • ref、和索引比较的列
  • rows、估计要检查的行数
  • filtered、按表条件过滤的行百分比
  • extra、额外信息 关注一下filesort,temporary table等等信息
    • Using filesort
    • Using temporary
    • Using where
    • Using index
    • Using index condition

官方测试数据下载