Home > Database > Mysql Tutorial > Can we change the order of columns in MySQL?

Can we change the order of columns in MySQL?

WBOY
Release: 2023-09-09 09:37:02
forward
1447 people have browsed it

我们可以改变 MySQL 中列的顺序吗?

Yes, we can change the order of the columns. This can be done using the ALTER command and the AFTER command to set a new order for individual columns. Let us first create a table -

mysql> create table DemoTable
   -> (
   -> `Student_Key_Age` int,
   -> `Student_Key_Name` varchar(20),
   -> `Student_Key_CountryName` varchar(20)
   -> );
Query OK, 0 rows affected (0.64 sec)
Copy after login

The following is the query to change the column order -

mysql> alter table DemoTable modify column `Student_Key_Age` int after `Student_Key_Name`;
Query OK, 0 rows affected (1.15 sec)
Records: 0 Duplicates: 0 Warnings: 0
Copy after login

Let us check the table description again -

mysql> desc DemoTable;
Copy after login

This will produce the following output. As you can see, the order of the columns has changed -

+-------------------------+-------------+------+-----+---------+-------+
| Field                   | Type        | Null | Key | Default | Extra |
+-------------------------+-------------+------+-----+---------+-------+
| Student_Key_Name        | varchar(20) | YES  |      | NULL   |       |
| Student_Key_Age         | int(11)     | YES  |      | NULL   |       |
| Student_Key_CountryName | varchar(20) | YES  |      | NULL   |       |
+-------------------------+-------------+------+-----+---------+-------+
3 rows in set (0.11 sec)
Copy after login

The above is the detailed content of Can we change the order of columns in MySQL?. For more information, please follow other related articles on the PHP Chinese website!

source:tutorialspoint.com
Statement of this Website
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
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template