gpt4 book ai didi

mysql - 如何在mysql中设置文本类型的默认值

转载 作者:可可西里 更新时间:2023-11-01 07:02:03 25 4
gpt4 key购买 nike

CREATE TABLE IF NOT EXISTS `te` (
`id` int(30) NOT NULL,
`name` text NOT NULL,
`address` text NOT NULL,
`Aboutus` text NOT NULL
) ENGINE=InnoDB DEFAULT CHARSET=latin1;

这里的表 'te' 包含 4 个字段 id、name、address、Aboutus,Aboutus 是可选的意味着我如何通过 phpmyadmin sql 将默认文本更新到 db 中的配置文件

最佳答案

我已经将关于我们字段的 not null 更改为 null

CREATE TABLE IF NOT EXISTS `te` (
`id` int(30) NOT NULL,
`name` text NOT NULL,
`address` text NOT NULL,
`Aboutus` text NULL
) ENGINE=InnoDB DEFAULT CHARSET=latin1;

这是你的触发器BEFORE INSERT

CREATE TRIGGER new_insert
BEFORE INSERT ON `te`
FOR EACH ROW
SET NEW.`Aboutus` = CASE WHEN NEW.Aboutus IS NULL THEN 'Not Updated' ELSE NEW.Aboutus END
;

插入没有Aboutus

INSERT INTO `te` (`id`, `name`, `address`) 
VALUES (1, 'name', 'address') ;

插入 Aboutus

INSERT INTO `te` (`id`, `name`, `address`, `Aboutus`) 
VALUES (2, 'name', 'address', 'Aboutus') ;

通过传递空值插入 Aboutus

INSERT INTO `te` (`id`, `name`, `address`, `Aboutus`) 
VALUES (3, 'name', 'address', null) ;

Demo

编辑@garethD指出了更新场景的情况,您还需要在 BEFORE UPDATE 上触发另一个触发器,因此如果更新中出现 null 那么 aboutus 应该更新为 Not Updated

CREATE TRIGGER update_trigger
BEFORE UPDATE ON `te`
FOR EACH ROW
SET NEW.`Aboutus` = CASE WHEN NEW.Aboutus IS NULL THEN 'Not Updated' ELSE NEW.Aboutus END
;

UPDATE te
SET AboutUs = NULL;

Demo 2

关于mysql - 如何在mysql中设置文本类型的默认值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23886364/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com