怎么修改mysql数据库表结构

这篇文章主要介绍了怎么修改mysql数据库表结构,具有一定借鉴价值,需要的朋友可以参考下。希望大家阅读完这篇文章后大有收获。下面让小编带着大家一起了解一下。

修改mysql数据库表结构的方法:1、查看表结构;2、添加列;3、删除列,代码为【mysql> alter table student drop column birthday】;4、修改列,代码为【mysql> alter table】。

修改mysql数据库表结构的方法:

1、查看表结构

mysql> show create table student;
+---------+-------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+
| Table   | Create Table                                                                                                                                                                                          |
+---------+-------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+
| student | CREATE TABLE `student` (
  `ID` int(11) NOT NULL AUTO_INCREMENT,
  `NAME` varchar(20) NOT NULL,
  `AGE` int(11) NOT NULL,
  PRIMARY KEY (`ID`)
) ENGINE=InnoDB AUTO_INCREMENT=13 DEFAULT CHARSET=utf8 |
+---------+-------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+
1 row in set (0.09 sec)

2、添加列

mysql> alter table student add column birthday datetime;
Query OK, 0 rows affected (2.06 sec)
Records: 0  Duplicates: 0  Warnings: 0

3、删除列

mysql> alter table student drop column birthday;
Query OK, 0 rows affected (0.80 sec)
Records: 0  Duplicates: 0  Warnings: 0

4、修改列

--1.修改列的类型
mysql> alter table student modify age int not null;
Query OK, 0 rows affected (0.09 sec)
Records: 0  Duplicates: 0  Warnings: 0
 
--2.修改列的名称
mysql> alter table student change column name sname varchar(20);
Query OK, 0 rows affected (1.31 sec)
Records: 0  Duplicates: 0  Warnings: 0

感谢你能够认真阅读完这篇文章,希望小编分享怎么修改mysql数据库表结构内容对大家有帮助,同时也希望大家多多支持亿速云,关注行业资讯频道,遇到问题就找亿速云,详细的解决方法等着你来学习!