查询当前数据库所有表:
show tables;
查询表结构:
desc tablename;
查询指定表的建表语句:
show create table tablename;
创建表:
create table tablename(
字段1 字段1类型 [comment 字段1注释],
字段2 字段2类型 [comment 字段2注释],
字段3 字段3类型 [comment 字段3注释],
字段4 字段4类型 [comment 字段4注释]
) [comment 表注释];
例如以下创建了一个user表
create table tb_user(
id int primary key auto_increment comment '主键id',
name varchar(11) comment '姓名',
age int comment '年龄',
address varchar(32) comment '地址'
) comment '用户表';
修改表:
新增表字段:
alter table tablename add 字段名 类型(长度) [comment] [约束];
alter table user add idcard char(18) comment '身份证号码';
修改表字段:
# 修改数据类型
alter table tablename modify 字段名 新数据类型(长度);
alter table user modify idcard char(18) comment '身份号码';
# 修改字段名和字段类型
alter table tablename change 旧字段名 新字段名 类型(长度) [comment] [约束];
alter table user change idcard id_card varchar(18);
删除字段:
alter table tablename drop 字段名;
修改表名:
alter table tablename rename to newtablename;
alter table user rename to t_user;
删除表:
drop table [if exists] tablename;
删除指定表,并重新创建该表(了解)
truncate table tablename;