什么是表: 关系型数据库中保存数据的单元,类似于Excel中的表格,创建表时需要指定字段信息.
Myisam
: 只支持数据基础的增删改查,不支持高级操作, 如:事务,外键等InnoDB
: 支持高级操作,默认为InnoDB格式: create table 表明(字段1名 字段1类型,字段2名 字段2类型...);
例如:
create table user(
id int,
username char(32),
password char(32)
);
show tables;
show create table 表名;
create table t1(
id int,
name varchar(10)
)engine = myisam charset=utf8;
查看:
show create table t1;
desc 表名;
例如: desc student;
rename table 原名 to 新名;
例如: rename table student to t_stu;
alter table 表名 engine=myisam charset=gbk;
例如: alter table hero engine=myisam charset=gbk;
最后位置添加
alter table 表名 add 字段 类型;
例如: 给hero表添加age字段,值为int;
alter table hero add age int;
最前面位置添加
alter table 表名 add 字段 类型 first;
例如: alter table hero add money int first;
在某个字段后面添加
alter table 表名 add 字段 类型 after 字段;
例如: alter table hero add genter varchar(5) after name;
删除表字段
alert table 表名 drop 字段;
例如: alter table hero drop money;
修改表字段名和类型
alter table 表名 change 原字段名 新字段名 类型;
例如: alter table hero change name heroname varchar(10);
修改表字段类型和位置
-- 把age位置调到第一
alter table hero modify age int first;
-- 把age位置调到gender后面:
alter table hero modify age int after xxx;
drop table 表明;
例如: drop table hero;
truncate
关键字
删除t1表并重新创建t1表
truncate table t1;
truncate
,delete
,drop
的区别:
delete
: 删除表中的数据 自增数值不清0 支持事务drop
: 删除表 不支持事务truncate
: 删除表并且创建一个新表 自增数值清零不支持事务原文:https://www.cnblogs.com/zpKang/p/12997410.html