MySql将列中的所有字段更新为其默认值
我希望能够使用update语句为home_country字段设置默认值 这是按数据库表列出的:MySql将列中的所有字段更新为其默认值,mysql,default-value,Mysql,Default Value,我希望能够使用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 (`i
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,您最好直接运行该值
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;
您可以改为使用函数DEFAULT()
$sql = "UPDATE countries SET home_country = DEFAULT(home_country) WHERE id = 1"