1.查看当前数据库的所有表
show tables;
2.创建表
create table 数据表名字 (字段 类型 约束[, 字段 类型 约束]);
auto_increment 表示自动增长
多个约束,不分先后顺序
设置主键,可以不写not null
-- 创建表 students
create table students(
id int unsigned primary key auto_increment not null,
name varchar(10) not null,
age tinyint unsigned default 0,
gender enum('男','女','中性','保密') default '保密',
class_id int unsigned not null
);
3.查看创建的表
show create table students;
4.查看表结构—使用频率相当高
desc students;
5.修改表结构—alter操作
- 添加字段 alter table 表名 add 列名 类型/约束;
alter table students add birthday datetime default "2011-11-11 11:11:11";
- 修改字段:不改变字段名,仅改变类型
alter table students modify birthday date default "2011-11-11";
- 修改字段:改变字段名(alter table 表名 change 原列名 新列名 类型及约束;)
alter table students change birthday birth date default "2011-11-11";
- 删除字段
alter table students drop birth;
- 删除表xx
drop table xx;