- 创建数据表
普通创建表:create table 表名(字段名 字段类型[字段属性],字段名 字段类型[字段属性],…)[表选项]
create table mydatabase.class( -- mydatabase是已经存在的数据库 name varchar(10) )charset utf8;
- 查询表
show tables; – 查看所有表
show tables like ‘匹配模式’; – 查看匹配数据表
describe 表名; – 显示表结构
desc 表名l; – 显示表结构
show columns from 表名; – 显示表结构
show create table 表名; – 显示表创建语句
- 表选项(表属性):与数据库选项类似
engine:存储引擎,mysql提供的具体存储数据的方式,默认是innodb
charset:字符集,只对当前表有效
collate:校对集alter table 表名 charset gbk;
- 复制已有表结构(只复制表结构)
create table 新表明 like 表名;
- 修改表名
rename table student to my_student;
- 修改表选项
alter table 表名 表选项 新值
- 新增字段(默认是加到表的最后面)
alter table my_student add column age int; -- 默认加在最后面
- 字段位置:
- alter table my_student add id int first;
- 修改字段名(需要指明新字段名的属性)
alter table my_student change age nianji int;
- 修改字段类型(属性)
alter table my_student modify name varchar(20);
- 删除字段
alter table my_student drop nianji;
- 删除表(可以同时删除多个表)
drop table 表名[,表名2,…]