修改表指的是修改数据库之后中已经存在的数据表的结构。
mysql使用alter table语句修改表。常用的修改表的操作有修改表名、修改字段数据类型或者字段名、增加和删除字段、修改字段的排列位置、更改表的存储引擎、删除表的外键约束等。
一、mysql修改表名
语法:
ALTER TABLE <旧表名> RENAME [to] <新表名>
上面语句中的to为可选,存在与否不影响运行结果。
举个栗子:
将test_user_2这个数据表,改名为test_user_two;
首先我们查看下原来的内容;
mysql> show tables;
+-------------------+
| Tables_in_test_db |
+-------------------+
| test_dept |
| test_user |
| test_user_2 |
| test_user_3 |
| test_user_4 |
| test_user_5 |
| test_user_6 |
| test_user_7 |
| test_user_8 |
| test_user_9 |
+-------------------+
10 rows in set (0.00 sec)
mysql>
然后我们执行语句:
mysql> alter table test_user_2 rename test_user_two;
Query OK, 0 rows affected (0.03 sec)
mysql>
然后再看下是否修改完毕了;
mysql> show tables;
+-------------------+
| Tables_in_test_db |
+-------------------+
| test_dept |
| test_user |
| test_user_3 |
| test_user_4 |
| test_user_5 |
| test_user_6 |
| test_user_7 |
| test_user_8 |
| test_user_9 |
| test_user_two |
+-------------------+
10 rows in set (0.00 sec)
mysql>
注意:修改的只是数据表名,实际上字段和数据内容都没有发生变化。
二、mysql修改数据的字段类型
修改字段的数据类型,就是把字段的数据类型转换成另一种数据类型。
语法:
alter table <表名> modify <字段名> <数据类型>
举个栗子:
修改test_user_two这个表中的money字段的数据类型,由float改为int类型。
首先看下这个表中的数据结构;
mysql> desc test_user_two;
+--------+-------------+------+-----+---------+-------+
| Field | Type | Null | Key | Default | Extra |
+--------+-------------+------+-----+---------+-------+
| id | int | NO | PRI | NULL | |
| name | varchar(25) | YES | | NULL | |
| deptid | int | YES | | NULL | |
| money | float | YES | | NULL | |
+--------+-------------+------+-----+---------+-------+
4 rows in set (0.01 sec)
mysql>
然后执行如下语句进行修改;
mysql> alter table test_user_two modify money int;
Query OK, 0 rows affected (0.09 sec)
Records: 0 Duplicates: 0 Warnings: 0
mysql>
修改完毕之后我们再看下test_user_db这个表中的数据结构是否发生变化了。
mysql> desc test_user_two;
+--------+-------------+------+-----+---------+-------+
| Field | Type | Null | Key | Default | Extra |
+--------+-------------+------+-----+---------+-------+
| id | int | NO | PRI | NULL | |
| name | varchar(25) | YES | | NULL | |
| deptid | int | YES | | NULL | |
| money | int | YES | | NULL | |
+--------+-------------+------+-----+---------+-------+
4 rows in set (0.00 sec)
mysql>
三、mysql修改字段名
语法:
alter table <表名> change <旧字段名> <新字段名> <新数据类型>;
举个栗子:
将test_user_two表中的money字段改成howmoney,数据类型为int;
执行语句:
mysql> alter table test_user_two change money howmoney int;
Query OK, 0 rows affected (0.03 sec)
Records: 0 Duplicates: 0 Warnings: 0
mysql>
然后我们再看下此时这个test_user_two数据库的数据结构是什么;
mysql> desc test_user_two;
+----------+-------------+------+-----+---------+-------+
| Field | Type | Null | Key | Default | Extra |
+----------+-------------+------+-----+---------+-------+
| id | int | NO | PRI | NULL | |
| name | varchar(25) | YES | | NULL | |
| deptid | int | YES | | NULL | |
| howmoney | int | YES | | NULL | |
+----------+-------------+------+-----+---------+-------+
4 rows in set (0.00 sec)
mysql>
可以看到,已经将字段修改完毕了。
四、mysql添加字段
语法:
alter table <表名> add <新字段名> <数据类型> [约束条件] [first | after 已存在字段名]
新字段名为需要添加的字段的名称;first为可选参数,其作用是将新添加的字段设置为表的第一个字段;after为可选参数,其作用是将新添加的字段添加到指定的“已存在字段名” 的后面。
first或after 已存在字段名用于指定新增字段在表中的位置,如果SQL语句中没有这两个参数,则默认将新添加的字段设置为数据表的最后一列。
举几个栗子:
1、添加没有约束性的字段
在数据表test_user_two中添加一个没有完整性约束的int类型的字段year(入职几年);
执行sql如下:
mysql> alter table test_user_two add year int;
Query OK, 0 rows affected (0.04 sec)
Records: 0 Duplicates: 0 Warnings: 0
mysql>
然后我们查看下修改之后的表结构;
mysql> desc test_user_two;
+----------+-------------+------+-----+---------+-------+
| Field | Type | Null | Key | Default | Extra |
+----------+-------------+------+-----+---------+-------+
| id | int | NO | PRI | NULL | |
| name | varchar(25) | YES | | NULL | |
| deptid | int | YES | | NULL | |
| howmoney | int | YES | | NULL | |
| year | int | YES | | NULL | |
+----------+-------------+------+-----+---------+-------+
5 rows in set (0.00 sec)
mysql>
2、添加一个有约束性的字段
在test_user_two表中添加一个名为year1,数据类型是int,且不可为空的字段;
执行相关sql语句;
mysql> alter table test_user_two add year1 int not null;
Query OK, 0 rows affected (0.02 sec)
Records: 0 Duplicates: 0 Warnings: 0
mysql>
看下表结构;
mysql> desc test_user_two;
+----------+-------------+------+-----+---------+-------+
| Field | Type | Null | Key | Default | Extra |
+----------+-------------+------+-----+---------+-------+
| id | int | NO | PRI | NULL | |
| name | varchar(25) | YES | | NULL | |
| deptid | int | YES | | NULL | |
| howmoney | int | YES | | NULL | |
| year | int | YES | | NULL | |
| year1 | int | NO | | NULL | |
+----------+-------------+------+-----+---------+-------+
6 rows in set (0.00 sec)
mysql>
3、在表的第一列添加一个字段
在test_user_two数据表第一列添加一个名字year2,数据类型是int的字段。
执行相关sql语句;
mysql> alter table test_user_two add year2 int first;
Query OK, 0 rows affected (0.02 sec)
Records: 0 Duplicates: 0 Warnings: 0
mysql>
看下修改之后的表结构是什么;
mysql> desc test_user_two;
+----------+-------------+------+-----+---------+-------+
| Field | Type | Null | Key | Default | Extra |
+----------+-------------+------+-----+---------+-------+
| year2 | int | YES | | NULL | |
| id | int | NO | PRI | NULL | |
| name | varchar(25) | YES | | NULL | |
| deptid | int | YES | | NULL | |
| howmoney | int | YES | | NULL | |
| year | int | YES | | NULL | |
| year1 | int | NO | | NULL | |
+----------+-------------+------+-----+---------+-------+
7 rows in set (0.00 sec)
mysql>
4、在数据表中指定列之后添加一个字段
在test_user_two这个数据库中name字段之后,添加一个名为name2,数据类型是varchar(50)的字段;
执行sql语句;
mysql> alter table test_user_two add name2 varchar(50) after name;
Query OK, 0 rows affected (0.02 sec)
Records: 0 Duplicates: 0 Warnings: 0
mysql>
可以再次看下表结构;
mysql> desc test_user_two;
+----------+-------------+------+-----+---------+-------+
| Field | Type | Null | Key | Default | Extra |
+----------+-------------+------+-----+---------+-------+
| year2 | int | YES | | NULL | |
| id | int | NO | PRI | NULL | |
| name | varchar(25) | YES | | NULL | |
| name2 | varchar(50) | YES | | NULL | |
| deptid | int | YES | | NULL | |
| howmoney | int | YES | | NULL | |
| year | int | YES | | NULL | |
| year1 | int | NO | | NULL | |
+----------+-------------+------+-----+---------+-------+
8 rows in set (0.00 sec)
mysql>
可以看到,每次我们都会对这个表结构进行相关的修改。
五、mysql删除字段
语法:
alter table <表名> drop <字段名>;
举个栗子;
比如我们想将test_user_two数据库中刚添加的year2字段删除;
剩余内容请转至VX公众号 “运维家” ,回复 “194” 查看。
------ “运维家” ,回复 “194” ------
------ “运维家” ,回复 “194” ------
------ “运维家” ,回复 “194” ------
linux入门pdf,linux对硬件要求,配置linux时间服务器,linux系统盘满了,linux多次执行,linuxcrc命令;
linux拷出文件命令,linux可以下载游戏嘛,linux修改书时间,linux导入表csv文件,如何切换linux的图形界面;
linux查看是否安装db2,linux找不到进程目 录,linux怎样看端口号,linuxgcc4.8,linux系统主机布局,linux传文件除了ssh,配置桥接linux;
1030

被折叠的 条评论
为什么被折叠?



