前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >MySQL数据库,DDL常见操作汇总(二)

MySQL数据库,DDL常见操作汇总(二)

作者头像
用户1289394
发布2021-11-05 09:24:12
6450
发布2021-11-05 09:24:12
举报
文章被收录于专栏:Java学习网

修改表名

alter table 表名 rename [to] 新表名;

表设置备注

alter table 表名 comment '备注信息';

复制表

只复制表结构

create table 表名 like 被复制的表名;

如:

mysql> create table test12 like test11;

Query OK, 0 rows affected (0.01 sec)

mysql> select * from test12;

Empty set (0.00 sec)

mysql> show create table test12;

+--------+-------+

| Table | Create Table

+--------+-------+

| test12 | CREATE TABLE `test12` (

`a` int(11) NOT NULL AUTO_INCREMENT COMMENT '字段a',

`b` int(11) NOT NULL COMMENT '字段b',

PRIMARY KEY (`a`)

) ENGINE=InnoDB DEFAULT CHARSET=utf8 |

+--------+-------+

1 row in set (0.00 sec)

复制表结构+数据

create table 表名 [as] select 字段,... from 被复制的表 [where 条件];

如:

mysql> create table test13 as select * from test11;

Query OK, 1 row affected (0.02 sec)Records: 1 Duplicates: 0 Warnings: 0

mysql> select * from test13;

+---+-----+

| a | b |

+---+-----+

| 1 | 100 |

+---+-----+

1 row in set (0.00 sec)

表结构和数据都过来了。

表中列的管理

添加列

alter table 表名 add column 列名 类型 [列约束];

⽰例:

mysql> drop table IF EXISTS test14;

Query OK, 0 rows affected, 1 warning (0.00 sec)

mysql>

mysql> create table test14(

-> a int not null AUTO_INCREMENT PRIMARY KEY comment '字段a'

-> );

Query OK, 0 rows affected (0.02 sec)

mysql> alter table test14 add column b int not null default 0 comment

'字段b';

Query OK, 0 rows affected (0.03 sec)

Records: 0 Duplicates: 0 Warnings: 0

mysql> alter table test14 add column c int not null default 0 comment

'字段c';

Query OK, 0 rows affected (0.05 sec)

Records: 0 Duplicates: 0 Warnings: 0mysql> insert into test14(b) values (10);

Query OK, 1 row affected (0.00 sec)

mysql> select * from test14;

c

+---+----+---+

| a | b | c |

+---+----+---+

| 1 | 10 | 0 |

+---+----+---+

1 row in set (0.00 sec)

修改列

alter table 表名 modify column 列名 新类型 [约束];

或者

alter table 表名 change column 列名 新列名 新类型 [约束];

2种⽅式区别:modify不能修改列名,change可以修改列名

我们看⼀下test14的表结构:

mysql> show create table test14;

+--------+--------+

| Table | Create Table |

+--------+--------+

| test14 | CREATE TABLE `test14` (

`a` int(11) NOT NULL AUTO_INCREMENT COMMENT '字段a',

`b` int(11) NOT NULL DEFAULT '0' COMMENT '字段b',

`c` int(11) NOT NULL DEFAULT '0' COMMENT '字段c',

PRIMARY KEY (`a`)

) ENGINE=InnoDB AUTO_INCREMENT=2 DEFAULT CHARSET=utf8 |

+--------+--------+

1 row in set (0.00 sec)

我们将字段c名字及类型修改⼀下,如下:

mysql> alter table test14 change column c d varchar(10) not null

default '' comment '字段d';

Query OK, 0 rows affected (0.01 sec)Records: 0 Duplicates: 0 Warnings: 0

mysql> show create table test14;

;;

+--------+--------+

| Table | Create Table |

+--------+--------+

| test14 | CREATE TABLE `test14` (

`a` int(11) NOT NULL AUTO_INCREMENT COMMENT '字段a',

`b` int(11) NOT NULL DEFAULT '0' COMMENT '字段b',

`d` varchar(10) NOT NULL DEFAULT '' COMMENT '字段d',

PRIMARY KEY (`a`)

) ENGINE=InnoDB AUTO_INCREMENT=2 DEFAULT CHARSET=utf8 |

+--------+--------+

1 row in set (0.00 sec)

删除列

alter table 表名 drop column 列名;

⽰例:

mysql> alter table test14 drop column d;

Query OK, 0 rows affected (0.05 sec)

Records: 0 Duplicates: 0 Warnings: 0

mysql> show create table test14;

+--------+--------+

| Table | Create Table |

+--------+--------+

| test14 | CREATE TABLE `test14` (

`a` int(11) NOT NULL AUTO_INCREMENT COMMENT '字段a',

`b` int(11) NOT NULL DEFAULT '0' COMMENT '字段b',

PRIMARY KEY (`a`)

) ENGINE=InnoDB AUTO_INCREMENT=2 DEFAULT CHARSET=utf8 |

+--------+--------+

1 row in set (0.00 sec)

本文参与 腾讯云自媒体同步曝光计划,分享自微信公众号。
原始发表:2021-10-23,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 Java学习网 微信公众号,前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体同步曝光计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
相关产品与服务
云数据库 SQL Server
腾讯云数据库 SQL Server (TencentDB for SQL Server)是业界最常用的商用数据库之一,对基于 Windows 架构的应用程序具有完美的支持。TencentDB for SQL Server 拥有微软正版授权,可持续为用户提供最新的功能,避免未授权使用软件的风险。具有即开即用、稳定可靠、安全运行、弹性扩缩等特点。
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档