创建数据表 create

  1. create table tb (
  2. 列名1 数据类型,
  3. 列名2 数据类型,
  4. ...
  5. );

复制数据表 like

仅表格式,不含数据

  1. create table tb_new like tb_old;

删除数据表 drop / trunctae

  1. drop table if exists tb;

(快速删除,在事务之外)

  1. trunctae table tb;

查看 show

查看所有数据表

  1. show tables [from 库名];

查看表结构

  1. show columns from tb;/ desc tb;

查看表创建语句

  1. show create table tb;

查看指定表情况

  1. desc tb;

查看表内索引

  1. show indexes from tb\G;

查看表在电脑中的位置

  1. show variables like '%datadir%';

索引

创建索引原则:数据量超过1万,经常被查询,字符较少

创建表时创建索引(可建多个索引)

  1. create table tb(...,index[索引名](col));

添加索引

  1. create index 索引名 on tb(col);
  1. alter table tb add index[索引名](col);

查看索引

  1. show index from tb;

删除索引

  1. drop index 索引名 on tb;

修改 alter

修改表名称

  1. alter table 旧表名 rename 新表名;/ rename table 旧表名 to 新表名;

修改表编码方式

  1. alter table tb character set gbk/utf8;

添加列

  1. alter table tb add col 数据类型 [first/after 列名];

删除列

  1. alter table tb drop col_1, drop col_2, add...;

修改列定义

  1. alter table tb modify col 数据类型 [first/after 列名];

修改列定义和列名称

  1. alter table tb change 旧列名 新列名 数据类型 [first/after 列名];