[英]how to set default value for text type in mysql
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中的Profile
關於我們字段,我已將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') ;
通過傳遞null Aboutus
插入
INSERT INTO `te` (`id`, `name`, `address`, `Aboutus`)
VALUES (3, 'name', 'address', null) ;
編輯為@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;
對於Ubuntu 16.04:
如何在MySQL 5.7中禁用嚴格模式:
編輯文件/etc/mysql/mysql.conf.d/mysqld.cnf
如果mysql.cnf中存在以下行
sql-mode="STRICT_TRANS_TABLES,NO_AUTO_CREATE_USER,NO_ENGINE_SUBSTITUTION"
然后用它替換它
sql_mode='MYSQL40'
除此以外
只需在mysqld.cnf中添加以下行
sql_mode='MYSQL40'
這解決了問題。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.