当前位置:   article > 正文

MySQL系列复习(4)DDL常见操作_drop table tablefortest if exists

drop table tablefortest if exists

目录

1、库的管理

1.1、创建库

1.2、删除库

1.3、建库通用的写法

1.4、示例

2、表管理

2.1、创建表

2.1.1、格式

2.1.2、注意

2.1.2、约束说明

2.2、删除表

2.3、修改表名

2.4、表设置备注

2.5、复制表

2.6、复制表结构+数据

3、表中列的管理

3.1、添加列

3.2、修改列

3.3、删除列


环境:mysql5.7.30,cmd命令中进⾏演⽰。

DDL:Data Define Language数据定义语⾔,主要⽤来对数据库、表进⾏⼀些管理操作。如:建库、删库、建表、修改表、删除表、对列的增删改等等。
⽂中涉及到的语法⽤[]包含的内容属于可选项,下⾯做详细说明。

1、库的管理

1.1、创建库

create database [if not exists] 库名 [default character set=utf8mb4];

1.2、删除库

drop database [if exists] 库名;

1.3、建库通用的写法

drop database if exists 旧库名;

create database 新库名;

1.4、示例

  1. mysql> drop database if exists test01;
  2. Query OK, 6 rows affected (0.17 sec)
  3. mysql> show databases like 'test01';
  4. Empty set (0.00 sec)
  5. mysql> create database test01;
  6. Query OK, 1 row affected (0.01 sec)
  7. mysql> show databases like 'test01';
  8. +-------------------+
  9. | Database (test01) |
  10. +-------------------+
  11. | test01 |
  12. +-------------------+
  13. 1 row in set (0.00 sec)
  14. mysql>

2、表管理

2.1、创建表

2.1.1、格式

create table 表名(
    字段名1 类型[(宽度)] [约束条件] [comment '字段说明'],
    字段名2 类型[(宽度)] [约束条件] [comment '字段说明'],
    字段名3 类型[(宽度)] [约束条件] [comment '字段说明']
) [表的⼀些设置];

2.1.2、注意

  • 1. 在同⼀张表中,字段名不能相同;
  • 2. 宽度和约束条件为可选参数,字段名和类型是必须的;
  • 3. 最后⼀个字段后不能加逗号;
  • 4. 类型是⽤来限制字段,必须以何种数据类型来存储记录;
  • 5. 类型其实也是对字段的约束(约束字段下的记录必须为XX类型);
  • 6. 类型后写的约束条件是在类型之外的额外添加的约束。

2.1.2、约束说明

1)not null:标识该字段不能为空

  1. mysql> use test01;
  2. Database changed
  3. mysql> select database();
  4. +------------+
  5. | database() |
  6. +------------+
  7. | test01 |
  8. +------------+
  9. 1 row in set (0.00 sec)
  10. mysql> create table test1(a int not null comment '字段a');
  11. Query OK, 0 rows affected (0.08 sec)
  12. mysql> desc test1;
  13. +-------+---------+------+-----+---------+-------+
  14. | Field | Type | Null | Key | Default | Extra |
  15. +-------+---------+------+-----+---------+-------+
  16. | a | int(11) | NO | | NULL | |
  17. +-------+---------+------+-----+---------+-------+
  18. 1 row in set (0.04 sec)
  19. mysql> insert into test1 values(null);
  20. ERROR 1048 (23000): Column 'a' cannot be null
  21. mysql> insert into test1 values(1);
  22. Query OK, 1 row affected (0.04 sec)
  23. mysql> select * from test1;
  24. +---+
  25. | a |
  26. +---+
  27. | 1 |
  28. +---+
  29. 1 row in set (0.00 sec)
  30. mysql>

2)default value:为该字段设置默认值,默认值为value

  1. mysql> create table test2(
  2. -> a int not null comment '字段a',
  3. -> b int not null default 0 comment '字段b'
  4. -> );
  5. Query OK, 0 rows affected (0.09 sec)
  6. mysql> desc test2;
  7. +-------+---------+------+-----+---------+-------+
  8. | Field | Type | Null | Key | Default | Extra |
  9. +-------+---------+------+-----+---------+-------+
  10. | a | int(11) | NO | | NULL | |
  11. | b | int(11) | NO | | 0 | |
  12. +-------+---------+------+-----+---------+-------+
  13. 2 rows in set (0.00 sec)
  14. mysql> insert into test2(a) values(1);
  15. Query OK, 1 row affected (0.04 sec)
  16. mysql> select * from test2;
  17. +---+---+
  18. | a | b |
  19. +---+---+
  20. | 1 | 0 |
  21. +---+---+
  22. 1 row in set (0.00 sec)
  23. mysql>

上⾯插⼊时未设置b的值,⾃动取默认值0。

3)primary key:标识该字段为该表的主键,可以唯⼀的标识记录,插⼊重复的会报错
两种写法,如下:
方式1:跟在列后,如下:

  1. mysql> create table test3(
  2. -> a int not null comment '字段a' primary key
  3. -> );
  4. Query OK, 0 rows affected (0.09 sec)
  5. mysql> desc test3;
  6. +-------+---------+------+-----+---------+-------+
  7. | Field | Type | Null | Key | Default | Extra |
  8. +-------+---------+------+-----+---------+-------+
  9. | a | int(11) | NO | PRI | NULL | |
  10. +-------+---------+------+-----+---------+-------+
  11. 1 row in set (0.00 sec)
  12. mysql> insert into test3(a) values(1);
  13. Query OK, 1 row affected (0.04 sec)
  14. mysql> insert into test3(a) values(1);
  15. ERROR 1062 (23000): Duplicate entry '1' for key 'PRIMARY'
  16. mysql> select * from test3;
  17. +---+
  18. | a |
  19. +---+
  20. | 1 |
  21. +---+
  22. 1 row in set (0.00 sec)
  23. mysql>

方式2:在所有列定义之后定义,如下:

  1. mysql> drop table IF EXISTS test4;
  2. ERROR 2006 (HY000): MySQL server has gone away
  3. No connection. Trying to reconnect...
  4. Connection id: 14
  5. Current database: test01
  6. Query OK, 0 rows affected, 1 warning (0.02 sec)
  7. mysql> create table test4(
  8. -> a int not null comment '字段a',
  9. -> b int not null default 0 comment '字段b',
  10. -> primary key(a)
  11. -> );
  12. Query OK, 0 rows affected (0.07 sec)
  13. mysql> desc test4;
  14. +-------+---------+------+-----+---------+-------+
  15. | Field | Type | Null | Key | Default | Extra |
  16. +-------+---------+------+-----+---------+-------+
  17. | a | int(11) | NO | PRI | NULL | |
  18. | b | int(11) | NO | | 0 | |
  19. +-------+---------+------+-----+---------+-------+
  20. 2 rows in set (0.01 sec)
  21. mysql> insert into test4(a,b) values(1,1);
  22. Query OK, 1 row affected (0.02 sec)
  23. mysql> select * from test4;
  24. +---+---+
  25. | a | b |
  26. +---+---+
  27. | 1 | 1 |
  28. +---+---+
  29. 1 row in set (0.02 sec)
  30. mysql> insert into test4(a,b) values(1,2);
  31. ERROR 1062 (23000): Duplicate entry '1' for key 'PRIMARY'
  32. mysql>

插⼊重复的值,会报违法主键约束
方式2⽀持多字段作为主键,多个之间⽤逗号隔开,语法:primary key(字段1,字段2,字段n),示例:

  1. mysql> drop table IF EXISTS test7;
  2. Query OK, 0 rows affected, 1 warning (0.00 sec)
  3. mysql> create table test7(
  4. -> a int not null comment '字段a',
  5. -> b int not null comment '字段b',
  6. -> PRIMARY KEY (a,b)
  7. -> );
  8. Query OK, 0 rows affected (0.03 sec)
  9. mysql> desc test7;
  10. +-------+---------+------+-----+---------+-------+
  11. | Field | Type | Null | Key | Default | Extra |
  12. +-------+---------+------+-----+---------+-------+
  13. | a | int(11) | NO | PRI | NULL | |
  14. | b | int(11) | NO | PRI | NULL | |
  15. +-------+---------+------+-----+---------+-------+
  16. 2 rows in set (0.00 sec)
  17. mysql> insert into test7(a,b) values(1,1);
  18. Query OK, 1 row affected (0.04 sec)
  19. mysql> select * from test7;
  20. +---+---+
  21. | a | b |
  22. +---+---+
  23. | 1 | 1 |
  24. +---+---+
  25. 1 row in set (0.00 sec)
  26. mysql> insert into test7(a,b) values(1,1);
  27. ERROR 1062 (23000): Duplicate entry '1-1' for key 'PRIMARY'
  28. mysql>

4)foreign key:为表中的字段设置外键
语法:
foreign key(当前表的列名) references 引⽤的外键表(外键表中字段名称)

  1. mysql> drop table IF EXISTS test6;
  2. Query OK, 0 rows affected, 1 warning (0.00 sec)
  3. mysql> drop table IF EXISTS test5;
  4. Query OK, 0 rows affected, 1 warning (0.00 sec)
  5. mysql>
  6. mysql> create table test5(
  7. -> a int not null comment '字段a' primary key
  8. -> );
  9. Query OK, 0 rows affected (0.02 sec)
  10. mysql> desc test5;
  11. +-------+---------+------+-----+---------+-------+
  12. | Field | Type | Null | Key | Default | Extra |
  13. +-------+---------+------+-----+---------+-------+
  14. | a | int(11) | NO | PRI | NULL | |
  15. +-------+---------+------+-----+---------+-------+
  16. 1 row in set (0.00 sec)
  17. mysql>
  18. mysql>
  19. mysql> create table test6(
  20. -> b int not null comment '字段b',
  21. -> ts5_a int not null,
  22. -> foreign key(ts5_a) references test5(a)
  23. -> );
  24. Query OK, 0 rows affected (0.03 sec)
  25. mysql> desc test6;
  26. +-------+---------+------+-----+---------+-------+
  27. | Field | Type | Null | Key | Default | Extra |
  28. +-------+---------+------+-----+---------+-------+
  29. | b | int(11) | NO | | NULL | |
  30. | ts5_a | int(11) | NO | MUL | NULL | |
  31. +-------+---------+------+-----+---------+-------+
  32. 2 rows in set (0.01 sec)
  33. mysql> insert into test5(a) values(1);
  34. Query OK, 1 row affected (0.04 sec)
  35. mysql> insert into test6(b,test6.ts5_a) values(1,1);
  36. Query OK, 1 row affected (0.01 sec)
  37. mysql> select * from test6;
  38. +---+-------+
  39. | b | ts5_a |
  40. +---+-------+
  41. | 1 | 1 |
  42. +---+-------+
  43. 1 row in set (0.00 sec)
  44. mysql> select * from test5;
  45. +---+
  46. | a |
  47. +---+
  48. | 1 |
  49. +---+
  50. 1 row in set (0.00 sec)
  51. mysql> insert into test6(b,test6.ts5_a) values(2,2);
  52. ERROR 1452 (23000): Cannot add or update a child row: a foreign key constraint fails (`test01`.`test6`, CONSTRAINT `test6_ibfk_1` FOREIGN KEY (`ts5_a`) REFERENCES `test5` (`a`))
  53. mysql>

说明:表⽰test6中ts5_a字段的值来源于表test5中的字段a。
注意⼏点:

  • 两张表中需要建⽴外键关系的字段类型需要⼀致
  • 要设置外键的字段不能为主键
  • 被引⽤的字段需要为主键
  • 被插⼊的值在外键表必须存在,如上⾯向test6中插⼊ts5_a为2的时候报错了,原因:2的值在test5表中不存在

5)unique key(uq):标识该字段的值是唯⼀的
⽀持⼀个到多个字段,插⼊重复的值会报违反唯⼀约束,会插⼊失败。

定义有2种⽅式。
方式1:跟在字段后,如下:

  1. mysql> drop table IF EXISTS test8;
  2. Query OK, 0 rows affected, 1 warning (0.00 sec)
  3. mysql> create table test8(
  4. -> a int not null comment '字段a' unique key
  5. -> );
  6. Query OK, 0 rows affected (0.05 sec)
  7. mysql> desc test8;
  8. +-------+---------+------+-----+---------+-------+
  9. | Field | Type | Null | Key | Default | Extra |
  10. +-------+---------+------+-----+---------+-------+
  11. | a | int(11) | NO | PRI | NULL | |
  12. +-------+---------+------+-----+---------+-------+
  13. 1 row in set (0.01 sec)
  14. mysql> insert into test8(a) values(1);
  15. Query OK, 1 row affected (0.01 sec)
  16. mysql> insert into test8(a) values(1);
  17. ERROR 1062 (23000): Duplicate entry '1' for key 'a'
  18. mysql> select * from test8;
  19. +---+
  20. | a |
  21. +---+
  22. | 1 |
  23. +---+
  24. 1 row in set (0.00 sec)
  25. mysql>

方式2:所有列定义之后定义,如下:

  1. mysql> drop table IF EXISTS test9;
  2. Query OK, 0 rows affected, 1 warning (0.01 sec)
  3. mysql> create table test9(
  4. -> a int not null comment '字段a',
  5. -> unique key(a)
  6. -> );
  7. Query OK, 0 rows affected (0.03 sec)
  8. mysql> desc test9;
  9. +-------+---------+------+-----+---------+-------+
  10. | Field | Type | Null | Key | Default | Extra |
  11. +-------+---------+------+-----+---------+-------+
  12. | a | int(11) | NO | PRI | NULL | |
  13. +-------+---------+------+-----+---------+-------+
  14. 1 row in set (0.00 sec)
  15. mysql> insert into test9(a) values(1);
  16. Query OK, 1 row affected (0.02 sec)
  17. mysql> insert into test9(a) values(1);
  18. ERROR 1062 (23000): Duplicate entry '1' for key 'a'
  19. mysql>

方式2⽀持多字段,多个之间⽤逗号隔开,语法:primary key(字段1,字段2,字段n),示例:

  1. mysql> drop table IF EXISTS test10;
  2. Query OK, 0 rows affected, 1 warning (0.00 sec)
  3. mysql> create table test10(
  4. -> a int not null comment '字段a',
  5. -> b int not null comment '字段b',
  6. -> unique key(a,b)
  7. -> );
  8. Query OK, 0 rows affected (0.06 sec)
  9. mysql> desc test10;
  10. +-------+---------+------+-----+---------+-------+
  11. | Field | Type | Null | Key | Default | Extra |
  12. +-------+---------+------+-----+---------+-------+
  13. | a | int(11) | NO | PRI | NULL | |
  14. | b | int(11) | NO | PRI | NULL | |
  15. +-------+---------+------+-----+---------+-------+
  16. 2 rows in set (0.00 sec)
  17. mysql> insert into test10(a,b) values(1,1);
  18. Query OK, 1 row affected (0.04 sec)
  19. mysql> insert into test10(a,b) values(1,1);
  20. ERROR 1062 (23000): Duplicate entry '1-1' for key 'a'
  21. mysql> select * from test10;
  22. +---+---+
  23. | a | b |
  24. +---+---+
  25. | 1 | 1 |
  26. +---+---+
  27. 1 row in set (0.00 sec)
  28. mysql>

6)auto_increment:标识该字段的值⾃动增长(整数类型,⽽且为主键)

  1. mysql> drop table IF EXISTS test11;
  2. Query OK, 0 rows affected, 1 warning (0.00 sec)
  3. mysql> create table test11(
  4. -> a int not null AUTO_INCREMENT KEY comment '字段a',
  5. -> b int not null comment '字段b'
  6. -> );
  7. Query OK, 0 rows affected (0.05 sec)
  8. mysql> desc test11;
  9. +-------+---------+------+-----+---------+----------------+
  10. | Field | Type | Null | Key | Default | Extra |
  11. +-------+---------+------+-----+---------+----------------+
  12. | a | int(11) | NO | PRI | NULL | auto_increment |
  13. | b | int(11) | NO | | NULL | |
  14. +-------+---------+------+-----+---------+----------------+
  15. 2 rows in set (0.01 sec)
  16. mysql> insert into test11(b) values(10);
  17. Query OK, 1 row affected (0.01 sec)
  18. mysql> insert into test11(b) values(20);
  19. Query OK, 1 row affected (0.02 sec)
  20. mysql> select * from test11;
  21. +---+----+
  22. | a | b |
  23. +---+----+
  24. | 1 | 10 |
  25. | 2 | 20 |
  26. +---+----+
  27. 2 rows in set (0.00 sec)
  28. mysql>

字段a为⾃动增长,默认值从1开始,每次+1
关于⾃动增长字段的初始值、步长可以在mysql中进⾏设置,比如设置初始值为1万,每次增长10

注意:

⾃增长列当前值存储在内存中,数据库每次重启之后,会查询当前表中⾃增列的最⼤值作为当前值,如果表数据被清空之后,数据库重启了,⾃增列的值将从初始值开始。
我们来演⽰⼀下:

  1. mysql> delete from test11;
  2. Query OK, 0 rows affected (0.00 sec)
  3. mysql> insert into test11(b) values(10);
  4. Query OK, 1 row affected (0.04 sec)
  5. mysql> select * from test11;
  6. +---+----+
  7. | a | b |
  8. +---+----+
  9. | 3 | 10 |
  10. +---+----+
  11. 1 row in set (0.00 sec)
  12. mysql>

上⾯删除了test11数据,然后插⼊了⼀条,a的值为3,执⾏下⾯操作:
删除test11数据,重启mysql,插⼊数据,然后看a的值是不是被初始化了?如下:

  1. mysql> delete from test11;
  2. Query OK, 1 row affected (0.01 sec)
  3. mysql> select * from test11;
  4. Empty set (0.00 sec)
  5. mysql> exit
  6. Bye
  1. PS C:\Windows\system32> net stop mysql;
  2. MySQL 服务正在停止.
  3. MySQL 服务已成功停止。
  4. PS C:\Windows\system32> net start mysql;
  5. MySQL 服务正在启动 .
  6. MySQL 服务已经启动成功。
  7. PS C:\Windows\system32>
  8. PS C:\Windows\system32> mysql -uroot -p
  9. Enter password: ****
  10. Welcome to the MySQL monitor. Commands end with ; or \g.
  11. Your MySQL connection id is 2
  12. Server version: 5.7.30 MySQL Community Server (GPL)
  13. Copyright (c) 2000, 2020, Oracle and/or its affiliates. All rights reserved.
  14. Oracle is a registered trademark of Oracle Corporation and/or its
  15. affiliates. Other names may be trademarks of their respective
  16. owners.
  17. Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.
  18. mysql> use test01;
  19. Database changed
  20. mysql> insert into test11(b) value(100);
  21. Query OK, 1 row affected (0.04 sec)
  22. mysql> select * from test11;
  23. +---+-----+
  24. | a | b |
  25. +---+-----+
  26. | 1 | 100 |
  27. +---+-----+
  28. 1 row in set (0.00 sec)
  29. mysql>

2.2、删除表

删除表结构和数据。

drop table [if exists] 表名;

  1. mysql> create table test12(
  2. -> a int not null comment '字段a' primary key,
  3. -> b int not null comment '字段b'
  4. -> );
  5. Query OK, 0 rows affected (0.05 sec)
  6. mysql> insert into test12(a,b) values(1,1);
  7. Query OK, 1 row affected (0.04 sec)
  8. mysql> select * from test12;
  9. +---+---+
  10. | a | b |
  11. +---+---+
  12. | 1 | 1 |
  13. +---+---+
  14. 1 row in set (0.00 sec)
  15. mysql> drop table test12;
  16. Query OK, 0 rows affected (0.04 sec)
  17. mysql> show tables;
  18. +------------------+
  19. | Tables_in_test01 |
  20. +------------------+
  21. | test1 |
  22. | test10 |
  23. | test11 |
  24. | test2 |
  25. | test3 |
  26. | test4 |
  27. | test5 |
  28. | test6 |
  29. | test7 |
  30. | test8 |
  31. | test9 |
  32. +------------------+
  33. 11 rows in set (0.00 sec)
  34. mysql>

2.3、修改表名

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

  1. mysql> show tables;
  2. +------------------+
  3. | Tables_in_test01 |
  4. +------------------+
  5. | test1 |
  6. | test10 |
  7. | test11 |
  8. | test2 |
  9. | test3 |
  10. | test4 |
  11. | test5 |
  12. | test6 |
  13. | test7 |
  14. | test8 |
  15. | test9 |
  16. +------------------+
  17. 11 rows in set (0.00 sec)
  18. mysql> alter table test9 rename to test99;
  19. Query OK, 0 rows affected (0.04 sec)
  20. mysql> show tables;
  21. +------------------+
  22. | Tables_in_test01 |
  23. +------------------+
  24. | test1 |
  25. | test10 |
  26. | test11 |
  27. | test2 |
  28. | test3 |
  29. | test4 |
  30. | test5 |
  31. | test6 |
  32. | test7 |
  33. | test8 |
  34. | test99 |
  35. +------------------+
  36. 11 rows in set (0.00 sec)
  37. mysql>

2.4、表设置备注

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

  1. mysql> alter table test99 comment '测试表99';
  2. Query OK, 0 rows affected (0.04 sec)
  3. Records: 0 Duplicates: 0 Warnings: 0
  4. mysql> show create table test99;
  5. +--------+-----------------------------------------------------------------------------------------------------------------------------------------------------+
  6. | Table | Create Table |
  7. +--------+-----------------------------------------------------------------------------------------------------------------------------------------------------+
  8. | test99 | CREATE TABLE `test99` (
  9. `a` int(11) NOT NULL COMMENT '字段a',
  10. UNIQUE KEY `a` (`a`)
  11. ) ENGINE=InnoDB DEFAULT CHARSET=utf8 COMMENT='测试表99' |
  12. +--------+-----------------------------------------------------------------------------------------------------------------------------------------------------+
  13. 1 row in set (0.00 sec)
  14. mysql>

2.5、复制表

只复制表结构

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

  1. mysql> create table test12 like test11;
  2. Query OK, 0 rows affected (0.03 sec)
  3. mysql> select * from test12;
  4. Empty set (0.00 sec)
  5. mysql> desc test12;
  6. +-------+---------+------+-----+---------+----------------+
  7. | Field | Type | Null | Key | Default | Extra |
  8. +-------+---------+------+-----+---------+----------------+
  9. | a | int(11) | NO | PRI | NULL | auto_increment |
  10. | b | int(11) | NO | | NULL | |
  11. +-------+---------+------+-----+---------+----------------+
  12. 2 rows in set (0.00 sec)
  13. mysql> show create table test12;
  14. +--------+-------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+
  15. | Table | Create Table |
  16. +--------+-------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+
  17. | test12 | CREATE TABLE `test12` (
  18. `a` int(11) NOT NULL AUTO_INCREMENT COMMENT '字段a',
  19. `b` int(11) NOT NULL COMMENT '字段b',
  20. PRIMARY KEY (`a`)
  21. ) ENGINE=InnoDB DEFAULT CHARSET=utf8 |
  22. +--------+-------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+
  23. 1 row in set (0.00 sec)
  24. mysql>

2.6、复制表结构+数据

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

  1. mysql> create table test13 as select * from test11;
  2. Query OK, 1 row affected (0.04 sec)
  3. Records: 1 Duplicates: 0 Warnings: 0
  4. mysql> select * from test13;
  5. +---+-----+
  6. | a | b |
  7. +---+-----+
  8. | 1 | 100 |
  9. +---+-----+
  10. 1 row in set (0.00 sec)
  11. mysql>

表结构和数据都过来了。

3、表中列的管理

3.1、添加列

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

  1. mysql> drop table IF EXISTS test14;
  2. Query OK, 0 rows affected, 1 warning (0.00 sec)
  3. mysql> create table test14(
  4. -> a int not null AUTO_INCREMENT PRIMARY KEY comment '字段a'
  5. -> );
  6. Query OK, 0 rows affected (0.03 sec)
  7. mysql> desc test14;
  8. +-------+---------+------+-----+---------+----------------+
  9. | Field | Type | Null | Key | Default | Extra |
  10. +-------+---------+------+-----+---------+----------------+
  11. | a | int(11) | NO | PRI | NULL | auto_increment |
  12. +-------+---------+------+-----+---------+----------------+
  13. 1 row in set (0.00 sec)
  14. mysql> alter table test14 add column b int not null default 0 comment '字段b';
  15. Query OK, 0 rows affected (0.07 sec)
  16. Records: 0 Duplicates: 0 Warnings: 0
  17. mysql> alter table test14 add column c int not null default 0 comment '字段c';
  18. Query OK, 0 rows affected (0.06 sec)
  19. Records: 0 Duplicates: 0 Warnings: 0
  20. mysql> desc test14;
  21. +-------+---------+------+-----+---------+----------------+
  22. | Field | Type | Null | Key | Default | Extra |
  23. +-------+---------+------+-----+---------+----------------+
  24. | a | int(11) | NO | PRI | NULL | auto_increment |
  25. | b | int(11) | NO | | 0 | |
  26. | c | int(11) | NO | | 0 | |
  27. +-------+---------+------+-----+---------+----------------+
  28. 3 rows in set (0.00 sec)
  29. mysql> insert into test14(b) values(10);
  30. Query OK, 1 row affected (0.01 sec)
  31. mysql> select * from test14;
  32. +---+----+---+
  33. | a | b | c |
  34. +---+----+---+
  35. | 1 | 10 | 0 |
  36. +---+----+---+
  37. 1 row in set (0.00 sec)
  38. mysql>

3.2、修改列

alter table 表名 modify column 列名 新类型 [约束];
或者
alter table 表名 change column 列名 新列名 新类型 [约束];
2种⽅式区别:modify不能修改列名,change可以修改列名
我们看⼀下test14的表结构:

  1. mysql> show create table test14;
  2. +--------+------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+
  3. | Table | Create Table |
  4. +--------+------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+
  5. | test14 | CREATE TABLE `test14` (
  6. `a` int(11) NOT NULL AUTO_INCREMENT COMMENT '字段a',
  7. `b` int(11) NOT NULL DEFAULT '0' COMMENT '字段b',
  8. `c` int(11) NOT NULL DEFAULT '0' COMMENT '字段c',
  9. PRIMARY KEY (`a`)
  10. ) ENGINE=InnoDB AUTO_INCREMENT=2 DEFAULT CHARSET=utf8 |
  11. +--------+------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+
  12. 1 row in set (0.00 sec)
  13. mysql>

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

  1. mysql> alter table test14 change column c d varchar(10) not null default '' comment '字段d';
  2. Query OK, 1 row affected (0.06 sec)
  3. Records: 1 Duplicates: 0 Warnings: 0
  4. mysql> desc test14;
  5. +-------+-------------+------+-----+---------+----------------+
  6. | Field | Type | Null | Key | Default | Extra |
  7. +-------+-------------+------+-----+---------+----------------+
  8. | a | int(11) | NO | PRI | NULL | auto_increment |
  9. | b | int(11) | NO | | 0 | |
  10. | d | varchar(10) | NO | | | |
  11. +-------+-------------+------+-----+---------+----------------+
  12. 3 rows in set (0.00 sec)
  13. mysql> show create table test14;
  14. +--------+---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+
  15. | Table | Create Table |
  16. +--------+---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+
  17. | test14 | CREATE TABLE `test14` (
  18. `a` int(11) NOT NULL AUTO_INCREMENT COMMENT '字段a',
  19. `b` int(11) NOT NULL DEFAULT '0' COMMENT '字段b',
  20. `d` varchar(10) NOT NULL DEFAULT '' COMMENT '字段d',
  21. PRIMARY KEY (`a`)
  22. ) ENGINE=InnoDB AUTO_INCREMENT=2 DEFAULT CHARSET=utf8 |
  23. +--------+---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+
  24. 1 row in set (0.00 sec)
  25. mysql>

3.3、删除列

alter table 表名 drop column 列名;

  1. mysql> alter table test14 drop column d;
  2. Query OK, 0 rows affected (0.11 sec)
  3. Records: 0 Duplicates: 0 Warnings: 0
  4. mysql> desc test14;
  5. +-------+---------+------+-----+---------+----------------+
  6. | Field | Type | Null | Key | Default | Extra |
  7. +-------+---------+------+-----+---------+----------------+
  8. | a | int(11) | NO | PRI | NULL | auto_increment |
  9. | b | int(11) | NO | | 0 | |
  10. +-------+---------+------+-----+---------+----------------+
  11. 2 rows in set (0.00 sec)
  12. mysql> show create table test14;
  13. +--------+------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+
  14. | Table | Create Table |
  15. +--------+------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+
  16. | test14 | CREATE TABLE `test14` (
  17. `a` int(11) NOT NULL AUTO_INCREMENT COMMENT '字段a',
  18. `b` int(11) NOT NULL DEFAULT '0' COMMENT '字段b',
  19. PRIMARY KEY (`a`)
  20. ) ENGINE=InnoDB AUTO_INCREMENT=2 DEFAULT CHARSET=utf8 |
  21. +--------+------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+
  22. 1 row in set (0.00 sec)
  23. mysql>

 

 

 

 

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/羊村懒王/article/detail/265557
推荐阅读
相关标签
  

闽ICP备14008679号