The MySQL command line deletes a field from a table

  • 2020-12-21 18:13:03
  • OfStack

Take a look at the table structure before deleting:

mysql > select * from test;
+------+--------+----------------------------------+------------+------------+------------+------------+
| t_id | t_name | t_password | t_birth | birth | birth1 | birth2 |
+------+--------+----------------------------------+------------+------------+------------+------------+
| 1 | name1 | 12345678901234567890123456789012 | NULL | 1990-01-01 | 0000-00-00 | 2013-01-01 |
| 2 | name2 | 12345678901234567890123456789012 | 2013-01-01 | NULL | 0000-00-00 | 2013-01-01 |
+------+--------+----------------------------------+------------+------------+------------+------------+
2 rows in set (0.00 sec)

Execute the delete command, using the drop keyword.
The basic syntax is alter table < The name of the table > drop column < The field name > ;

The specific orders are as follows:

mysql > alter table test drop column birth1;
Query OK, 0 rows affected (0.13 sec)
Records: 0 Duplicates: 0 Warnings: 0

Look at the results after the deletion, is there no birth1 field?

mysql > select * from test;
+------+--------+----------------------------------+------------+------------+------------+
| t_id | t_name | t_password | t_birth | birth | birth2 |
+------+--------+----------------------------------+------------+------------+------------+
| 1 | name1 | 12345678901234567890123456789012 | NULL | 1990-01-01 | 2013-01-01 |
| 2 | name2 | 12345678901234567890123456789012 | 2013-01-01 | NULL | 2013-01-01 |
+------+--------+----------------------------------+------------+------------+------------+
2 rows in set (0.00 sec)

About MySQL SQL statement date function that calculates age according to birthday, this article introduces so much, hope to be helpful to you, thank you!


Related articles: