Mysql数据表操作命令有哪些

x33g5p2x  于2021-03-14 发布在 Mysql  
字(1.2k)|赞(0)|评价(0)|浏览(349)

注意 : 对表的操作必须要选定数据库.

创建数据表

create table 表名 (
字段名 字段类型 [字段属性], 
字段名 字段类型 [字段属性], 
...
) [表选项];
create table class(

id int zerofill auto_increment primary key,
name char(20) not null,
age tinyint unsigned not null

) charset utf8;

复制已有表结构(只复制结构,不复制数据)

create table 新表名 like 需要复制结构的表名;

显示所有表

show tables;

显示表结构

desc 表名;

+-------+---------------------------+------+-----+---------+----------------+
| Field | Type                      | Null | Key | Default | Extra          |
+-------+---------------------------+------+-----+---------+----------------+
| id    | int(10) unsigned zerofill | NO   | PRI | NULL    | auto_increment |
| name  | char(20)                  | NO   |     | NULL    |                |
| age   | tinyint(4)                | NO   |     | NULL    |                |
+-------+---------------------------+------+-----+---------+----------------+
  • Field : 字段名称.
  • Type : 字段类型.
  • Null : 是否允许未空,yes表示允许.
  • Key : 索引.
  • Default : 默认值, NULL表示默认为NULL.
  • Extra : 额外属性.

显示表创建语句

show create table 表名;

修改表属性(表属性就是engine,charset,collate)

alter table class 

如: 
alter table class engine innodb,charset utf8;

修改表名

rename table 旧表名 to 新表名;

新增字段

alter table 表名 add 字段名 字段类型 [字段属性] [位置];

如:
alter table 表名 add age tinyint not null unsigned comment '年龄' after height;
在表中height字段后面添加age字段无符号,微整型.

修改字段名(修改字段名一定要就会清空字段属性,所以连字段属性也要设置,不要以为仅仅修改表名就行了)

alter table 表名 change 旧字段名 新字段名 字段属性 新位置;

修改字段类型/属性

alter table 表名 modify 字段名 字段属性 [位置];

删除字段

alter table 表名  字段名;

删除表

drop table 表1,表2;

相关文章