Home >Database >Mysql Tutorial >How to modify the mysql database table structure

How to modify the mysql database table structure

coldplay.xixi
coldplay.xixiOriginal
2020-12-15 14:56:112659browse

Methods to modify the table structure of mysql database: 1. View the table structure; 2. Add columns; 3. Delete columns, the code is [mysql> alter table student drop column birthday]; 4. Modify columns, the code is 【mysql> alter table】.

How to modify the mysql database table structure

The operating environment of this tutorial: windows7 system, mysql version 8.0.22, thinkpad t480 computer.

Related free learning recommendations: mysql database(Video)

##Modify Mysql database table structure method:

1. View table structure

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. Add columns

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

3. Delete columns

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

4. Modify the column

--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

Related free learning recommendations:

Programming Video

The above is the detailed content of How to modify the mysql database table structure. For more information, please follow other related articles on the PHP Chinese website!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn