修改表指的是修改数据库之后中已经存在的数据表的结构。
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
字段删除;
执行sql
语句为:
看下表结构;
mysql> desc test_user_two; +----------+-------------+------+-----+---------+-------+ | field | type | null | key | default | extra | +----------+-------------+------+-----+---------+-------+ | 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 | | +----------+-------------+------+-----+---------+-------+ 7 rows in set (0.00 sec) mysql>
可以看到已经将名为year2
的字段进行删除了。
六、mysql修改字段的排列位置
有时候我们会面临修改数据表中某些字段的排列位置,比如说将某一个字段排到第一个,或者是将某个字段后移,看下如何操作。
语法:
alter table <表名> modify <字段1> <数据类型> first | after <字段2>;
“字段1” 指要修改位置的字段;“数据类型” 指 “字段1” 的数据类型;“first
” 为可选参数,指将 “字段1” 修改为表的第一个字段;“after
字段2” 指将 “字段1” 插入到 “字段2” 后面。
举两个栗子;
1、修改字段为表的第一个字段
比如我们将test_user_two
这个数据表中名为name2
的字段,修改成这个对应数据表中第一个字段;
执行sql
语句;
mysql> alter table test_user_two modify name2 varchar(50) first; query ok, 0 rows affected (0.08 sec) records: 0 duplicates: 0 warnings: 0 mysql>
查看下表结构;
mysql> desc test_user_two; +----------+-------------+------+-----+---------+-------+ | field | type | null | key | default | extra | +----------+-------------+------+-----+---------+-------+ | name2 | varchar(50) | 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>
可以看到我们已经成功将name2
这个字段修改成第一个字段了;
2、修改字段为指定列后面
比如我们将test_user_two
这个表中的name2
字段,让他移动到year
这个字段后面;
执行sql
语句;
mysql> alter table test_user_two modify name2 varchar(50) after year; query ok, 0 rows affected (0.06 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 | | | name2 | varchar(50) | yes | | null | | | year1 | int | no | | null | | +----------+-------------+------+-----+---------+-------+ 7 rows in set (0.00 sec) mysql>
可以发现,已经将test_user_two
这个数据库中的name2
字段,移动到了year
字段之后。
七、mysql更改表的存储引擎
存储引擎是
mysql
中的数据存储在文件或者内存中时采用的不用技术实现。可以根据自己所需,选择不同的引擎,也可以为每一张表选择不用的存储引擎。可以使用show engines;
语句来查看系统支持的存储引擎。
mysql> show engines; +--------------------+---------+----------------------------------------------------------------+--------------+------+------------+ | engine | support | comment | transactions | xa | savepoints | +--------------------+---------+----------------------------------------------------------------+--------------+------+------------+ | memory | yes | hash based, stored in memory, useful for temporary tables | no | no | no | | mrg_myisam | yes | collection of identical myisam tables | no | no | no | | csv | yes | csv storage engine | no | no | no | | federated | no | federated mysql storage engine | null | null | null | | performance_schema | yes | performance schema | no | no | no | | myisam | yes | myisam storage engine | no | no | no | | innodb | default | supports transactions, row-level locking, and foreign keys | yes | yes | yes | | blackhole | yes | /dev/null storage engine (anything you write to it disappears) | no | no | no | | archive | yes | archive storage engine | no | no | no | +--------------------+---------+----------------------------------------------------------------+--------------+------+------------+ 9 rows in set (0.00 sec) mysql>
更改表的存储引擎的语法:
alter table <表名> engine=<更改后的存储引擎名>
到此这篇关于mysql数据库表增添字段,删除字段,修改字段的排列等操作的文章就介绍到这了,更多相关mysql字段操作内容请搜索以前的文章或继续浏览下面的相关文章希望大家以后多多支持!