暂无图片
暂无图片
暂无图片
暂无图片
暂无图片

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

Java学习网 2021-10-23
323

修改表名

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)


文章转载自Java学习网,如果涉嫌侵权,请发送邮件至:contact@modb.pro进行举报,并提供相关证据,一经查实,墨天轮将立刻删除相关内容。

评论