Change Column Name or Set Default Value in MySql

来源:互联网 发布:php中删除数组元素 编辑:程序博客网 时间:2024/06/06 02:53

In this blog’s exmple, two scenes are included:

  • Change column’s name
  • Set default value to the specified column

Example:

MariaDB [testdate]> DESCRIBE ttt;+--------------+-------------+------+-----+-------------------+-------+| Field        | Type        | Null | Key | Default           | Extra |+--------------+-------------+------+-----+-------------------+-------+| others       | varchar(20) | YES  |     | NULL              |       || the_datetime | datetime    | NO   |     | CURRENT_TIMESTAMP |       |+--------------+-------------+------+-----+-------------------+-------+2 rows in set (0.00 sec)MariaDB [testdate]> ALTER TABLE ttt CHANGE the_datetime the_datetime2 DATETIME;Query OK, 0 rows affected (0.42 sec)Records: 0  Duplicates: 0  Warnings: 0MariaDB [testdate]> DESCRIBE ttt;+---------------+-------------+------+-----+---------+-------+| Field         | Type        | Null | Key | Default | Extra |+---------------+-------------+------+-----+---------+-------+| others        | varchar(20) | YES  |     | NULL    |       || the_datetime2 | datetime    | YES  |     | NULL    |       |+---------------+-------------+------+-----+---------+-------+2 rows in set (0.00 sec)MariaDB [testdate]> ALTER TABLE ttt MODIFY the_datetime2 DATETIME DEFAULT CURRENT_TIMESTAMP;Query OK, 0 rows affected (0.00 sec)Records: 0  Duplicates: 0  Warnings: 0MariaDB [testdate]> DESCRIBE ttt;+---------------+-------------+------+-----+-------------------+-------+| Field         | Type        | Null | Key | Default           | Extra |+---------------+-------------+------+-----+-------------------+-------+| others        | varchar(20) | YES  |     | NULL              |       || the_datetime2 | datetime    | YES  |     | CURRENT_TIMESTAMP |       |+---------------+-------------+------+-----+-------------------+-------+2 rows in set (0.00 sec)MariaDB [testdate]>
0 0