四时宝库

程序员的知识宝库

MySQL之表操作知识梳理(mysql 表操作)

1、创建表

  • (1)查看帮助
mysql> help create table;
  • (2)直接创建表
CREATE TABLE `tb_name` (
  create_definition,...
)
[table_options]
[partition_options]
  • (3)根据旧表创建新表
方式一: 只包含表结构,(不包含索引)
mysql> create table tb_new as select * from tb_old where 1=2;

mysql> create table tb_new as select * from tb_old limit 0;

mysql> create table tb_new as select col1, col2 from tb_old where 1=2;

mysql> create table tb_new as select col1, col2 from tb_old limit 0;
方式二: 通过复制现存的表结构创建, 不会复制数据,(同时包含表结构和索引)
mysql> create table tb_new like tb_old;
方式三: 通过查询现存的表创建, 包括查询到的数据,(不包含索引)
mysql> create table tb_new select * from tb_old limit 5;

2、删除表

查看帮助
mysql> help drop table;

删除表
mysql> drop table tb_name;

3、修改表

查看帮助
mysql> help alter table;

添加字段
mysql> alter table tb_name add col_name definition [first|after col];

删除字段
mysql> alter table tb_name drop col_name;

modify只可以修改现有字段的类型、默认值。
mysql> alter table tb_name modify col_name definition [first|after col];

change可以修改表中字段名、类型、默认值。
mysql> alter table tb_name change col_name col_new definition [first|after col];

4、改表名

mysql> rename table tb_old to tb_new;

mysql> alter table tb_old rename to tb_new;

5、查看表

查看所有表
mysql> show tables [from db_name];

查看表结构
mysql> desc tb_name;

查看表创建语句
mysql> show create table tb_name;

6、查看表状态

mysql> show table status;

mysql> show table status like 'tb_name';

mysql> show table status where name='tb_name';

发表评论:

控制面板
您好,欢迎到访网站!
  查看权限
网站分类
最新留言
    友情链接