MySql将列中的所有字段更新为默认值

问题描述 投票:1回答:2

我希望能够使用update语句为home_country字段设置默认值。

这是按数据库表:

CREATE TABLE `countries` (
  `id` smallint(6) NOT NULL AUTO_INCREMENT,
  `name` varchar(70) COLLATE utf8_unicode_ci NOT NULL,
  `home_country` tinyint(1) unsigned NOT NULL DEFAULT '0',
  PRIMARY KEY (`id`),
) ENGINE=MyISAM;

我正在使用:

$sql = "UPDATE countries SET home_country = DEFAULT WHERE id = 1"

但是由于某种原因,它对我不起作用。在上述国家/地区中,ID为1的home_country值应设置为1,而其他所有值均应重置为0

mysql default-value
2个回答
3
投票

您最好直接运行该值

UPDATE countries SET home_country = 0 WHERE id = 1;

如果要动态设置默认值,则必须执行类似这样的疯狂操作]​​>

UPDATE countries SET home_country =
(SELECT column_default FROM information_schema.columns
WHERE table_schema=DATABASE()
AND table_name ='countries'
AND column_name = 'home_country') WHERE id = 1;

0
投票

您可以改用DEFAULT()函数。

© www.soinside.com 2019 - 2024. All rights reserved.