Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/mysql/71.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/eclipse/8.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
MySql将列中的所有字段更新为其默认值_Mysql_Default Value - Fatal编程技术网

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

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

我希望能够使用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,您最好直接运行该值

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"