本文整理自网络,侵删。
我们在创建表的过程中难免会考虑不周,因此后期会修改表
修改表需要用到alter table语句
修改表名
代码如下:
mysql> alter table student rename person;
Query OK, 0 rows affected (0.03 sec)
这里的student是原名,person是修改过后的名字
mysql> alter table student rename person;
Query OK, 0 rows affected (0.03 sec)
这里的student是原名,person是修改过后的名字
用rename来重命名,也可以使用rename to
还有一种方法是rename table old_name to new_name
修改字段的数据类型
代码如下:
mysql> alter table person modify name varchar(20);
Query OK, 0 rows affected (0.18 sec)
Records: 0 Duplicates: 0 Warnings: 0
此处modify后面的name为字段名,我们将原来的varchar(25)改为varchar(20)
mysql> alter table person modify name varchar(20);
Query OK, 0 rows affected (0.18 sec)
Records: 0 Duplicates: 0 Warnings: 0
此处modify后面的name为字段名,我们将原来的varchar(25)改为varchar(20)
修改字段名
代码如下:
mysql> alter table person change stu_name name varchar(25);
Query OK, 0 rows affected (0.20 sec)
Records: 0 Duplicates: 0 Warnings: 0
这里stu_name是原名,name是新名
mysql> alter table person change stu_name name varchar(25);
Query OK, 0 rows affected (0.20 sec)
Records: 0 Duplicates: 0 Warnings: 0
这里stu_name是原名,name是新名
需要注意的是不管改不改数据类型,后面的数据类型都要写
如果不修改数据类型只需写成原来的数据类型即可
tips:我们同样可以使用change来达到modify的效果,只需在其后写一样的字段名
增加无完整性约束条件的字段
代码如下:
mysql> alter table person add sex boolean;
Query OK, 0 rows affected (0.21 sec)
Records: 0 Duplicates: 0 Warnings: 0
此处的sex后面只跟了数据类型,而没有完整性约束条件
mysql> alter table person add sex boolean;
Query OK, 0 rows affected (0.21 sec)
Records: 0 Duplicates: 0 Warnings: 0
此处的sex后面只跟了数据类型,而没有完整性约束条件
增加有完整性约束条件的字段
代码如下:
mysql> alter table person add age int not null;
Query OK, 0 rows affected (0.17 sec)
Records: 0 Duplicates: 0 Warnings: 0
此处增加了一条age字段,接着在后面加上了not null完整性约束条件
mysql> alter table person add age int not null;
Query OK, 0 rows affected (0.17 sec)
Records: 0 Duplicates: 0 Warnings: 0
此处增加了一条age字段,接着在后面加上了not null完整性约束条件
增加额外的完整性约束条件
代码如下:
mysql> ALTER TABLE person ADD PRIMARY KEY(id);
Query OK, 0 rows affected (0.18 sec)
Records: 0 Duplicates: 0 Warnings: 0
这里同样也用于多字段设置
mysql> ALTER TABLE person ADD PRIMARY KEY(id);
Query OK, 0 rows affected (0.18 sec)
Records: 0 Duplicates: 0 Warnings: 0
这里同样也用于多字段设置
在表头添加字段
代码如下:
mysql> alter table person add num int primary key first;
Query OK, 0 rows affected (0.20 sec)
Records: 0 Duplicates: 0 Warnings: 0
默认情况下添加字段都是添加到表尾,在添加语句后面加上first就能添加到表头
mysql> alter table person add num int primary key first;
Query OK, 0 rows affected (0.20 sec)
Records: 0 Duplicates: 0 Warnings: 0
默认情况下添加字段都是添加到表尾,在添加语句后面加上first就能添加到表头
在指定位置添加字段
代码如下:
mysql> alter table person add birth date after name;
Query OK, 0 rows affected (0.20 sec)
Records: 0 Duplicates: 0 Warnings: 0
这里添加一条新字段放在name字段后面
书籍
mysql> alter table person add birth date after name;
Query OK, 0 rows affected (0.20 sec)
Records: 0 Duplicates: 0 Warnings: 0
这里添加一条新字段放在name字段后面
tips:表中字段的排序对表不会有什么影响,不过更合理的排序能便于理解表
相关阅读 >>
如何解决mysql报错“#1067 invalid default value”
windows 10 + mysql 5.5安装及使用教程分享
更多相关阅读请进入《mysql》频道 >>

数据库系统概念 第6版
本书主要讲述了数据模型、基于对象的数据库和XML、数据存储和查询、事务管理、体系结构等方面的内容。
相关推荐
评论
管理员已关闭评论功能...
- 欢迎访问木庄网络博客
- 可复制:代码框内的文字。
- 方法:Ctrl+C。