数据表的基本操作
1.创建表的语法形式
create table 表名
(
字段名 数据类型 //约束条件,
字段名 数据类型 //约束条件,
字段名 数据类型 //约束条件,
字段名 数据类型 //约束条件);
2.查看当前数据库的所有数据表
show tables;
3.约束条件
主键 primary key
自增 auto_increment
非空 not null
唯一 unique
默认 default
外键 constraint 外键名 foreign key(从表中需要设置外键约束的字段名) references 主表名 (主表中主键的字段名)
4.设置表的存储引擎
create table 表名
(
…
…
)engine=存储引擎;
5.查看表基本结构
describe 表名;
6.查看建表语句
show create table 表名;
7.修改表名
alter table 旧表名 rename 新表名;
8.修改字段数据类型
alter table 表名 modify 字段名 新数据类型;
9.修改字段名
alter table 表名 change 旧字段名 新字段名 数据类型;
10.添加字段
在表的最后一列添加字段
alter table 表名 add 字段名 数据类型;
在表的第一列添加字段
alter table 表名 add 字段名 数据类型 first;
在表指定列之后添加字段
alter table 表名 字段名 数据类型 after 字段名;
11.删除字段
alter table 表名 drop 字段名;
12.修改字段顺序
alter table 表名 modify 字段名 数据类型 first | after 字段名;
13.修改存储引擎
alter table 表名 engine=存储引擎;
14.删除没有关联的表
drop table [if exists] 表名1,表名2…;
15.删除被其他表关联的主表
alter table 表名 drop foreign key 外键名;