1.创建表 关键字:create table 表名
create table 表名(
字段1 类型 [字段类型],
字段n 类型 [字段类型]
)
create table test(
id int(10)not null auto_increment comment 'id',
name varchar(20) not null comment '名字',
age int(3)default 18 comment '年级',
primary key(id)
)engine=InnoDB default charset utf8 comment '测试表'
1.test 表名
2.not null 字段不可以为空
3.int(10) varchar(20) 字段类型
4.default 18 字段默认值为18
5.comment 备注信息
6.engine 存储类型InnoDB
7.charset 字符集 utf8
8.auto_increment 字段自增 一般适用于主键
9.primary key主键
**2.**查看表 关键字:show
show tables 查看数据库下所有数据表
3.模糊查询数据表
show tables like '%数据库表名%'
4.查看表创建语句
show create table '数据表名'
5.查看表结构
desc 表名
6.删除数据表
drop table if exists 表名
7.修改表名
alter table 旧表名 rename to 新表名
8.增加一列 关键字 add
alter table 表名 add 新列名 字段类型 字段选项
例如:
alter table test add age int(3) default 18 not null comment '年龄'
9.删除一列 关键字 drop
alter table test drop age
10.修改字段类型
alter table 表名 modify 字段名 新的字段类型 新的字段选项
例如:
alter table test modify age char(3) default '12' not null
11.字段排到最前 关键字 first
alert table 表名 modify 字段名 类型 字段选项 comment '最前面' first
例如:
alert table test modify age int not null comment '最前面' first
12.修改字段排序
alter table 表名 modify 字段名1 字段类型 字段选项 after 字段名2
例如:
alter table test modify age int not null after name
13.字段重命名
alter table 表名 change 原字段名 新字段名 新的字段类型 [新的字段选项];
例如:
alter table test change name stu_name varchar(20) not null comment '学生姓名'
14.修改表信息选项
alter table 表名 表选项信息
例如:
alter table test engine MyIsam default charset gbk;