如何修改 MySQL 表格栏目的默认值?
要修改 MySQL 表格栏目的默认值,可使用 CHANGE 命令。语法如下:−
alter table yourTableName change yourCoumnName youColumnName datatype not null default Value;
为了理解上述语法,让我们创建一个表格。查询如下:−
mysql> create table DefaultDemo −> ( −> ArrivalTime timestamp −> ); Query OK, 0 rows affected (0.65 sec)
以下是描述带有默认栏目的表格的查询:−
mysql> desc DefaultDemo;
输出如下:−
+-------------+-----------+------+-----+---------+-------+ | Field | Type | Null | Key | Default | Extra | +-------------+-----------+------+-----+---------+-------+ | ArrivalTime | timestamp | YES | | NULL | | +-------------+-----------+------+-----+---------+-------+ 1 row in set (0.11 sec)
现在可以修改 MySQL 表格栏目为默认值。查询如下:−
mysql> ALTER TABLE DefaultDemo CHANGE ArrivalTime ArrivalTime TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP; Query OK, 0 rows affected (1.71 sec) Records: 0 Duplicates: 0 Warnings: 0
利用 insert 命令查看是否已经插入了默认值。查询如下:−
mysql> insert into DefaultDemo values(); Query OK, 1 row affected (0.15 sec)
显示记录:−
mysql> select *from DefaultDemo;
输出如下:−
+---------------------+ | ArrivalTime | +---------------------+ | 2018-12-07 11:31:00 | +---------------------+ 1 row in set (0.00 sec)
广告