如何防止 MySQL 中出现空格、空值和无效日期?
How can I prevent white spaces, empty values, and invalid dates in MySQL?
用户table
CREATE TABLE `USERS` (
`ID` char(255) COLLATE utf8_unicode_ci NOT NULL,
`NAME` char(255) COLLATE utf8_unicode_ci NOT NULL,
`EMAIL` char(255) COLLATE utf8_unicode_ci DEFAULT NULL,
`CREATED_IN` datetime NOT NULL,
`SIGNED_IN` datetime NOT NULL,
PRIMARY KEY (`ID`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8 COLLATE=utf8_unicode_ci
触发器
CREATE TRIGGER `ABC` BEFORE INSERT ON `USERS` FOR EACH ROW
IF NEW.ID = "" OR NEW.NAME = "" OR
NEW.CREATED_IN = "" OR NEW.CREATED_IN = "0000-00-00 00:00:00" OR
NEW.SIGNED_IN = "" OR NEW.SIGNED_IN = "0000-00-00 00:00:00"
THEN SIGNAL SQLSTATE "45000"; END IF
ID列
- 如何让它不接受空值?
("", " ", " ", " ", etc...) -> Should return an error
- 如果已经有一个值,我怎样才能从中删除所有空格?
("bf9 d 34 c9 08" = "bf9d34c908")
NAME 列
- 如何让它不接受空值?
("", " ", " ", " ", etc...) -> Should return an error
EMAIL 列
- 如何让它不接受空值?
("", " ", " ", " ", etc...) -> Should return an error
- 如果已经有一个值,我怎样才能从中删除所有空格?
("nkr owks lpehqp jmgdb @ gm ail. com" = "nkrowkslpehqpjmgdb@gmail.com")
CREATED_IN 和 SIGNED_IN 列
- 如何让它不接受无效值?当插入无效值(如字母、数字或无效格式)时,应该 return 出错而不是输入 0000-00-00 00:00:00.
MySQL version is 5.7
处理用户 table 的这些要求的最佳方法是什么?
触发器可能如下所示:
CREATE TRIGGER process_input
BEFORE INSERT ON USERS
FOR EACH ROW
BEGIN
SET NEW.id = REPLACE(NEW.id, ' ', '');
IF NEW.id = '' THEN
SIGNAL SQLSTATE '45000' SET MESSAGE_TEXT = '`id` cannot be empty.';
END IF;
SET NEW.name = TRIM(NEW.name);
IF NEW.name = '' THEN
SIGNAL SQLSTATE '45000' SET MESSAGE_TEXT = '`name` cannot be empty.';
END IF;
SET NEW.email = REPLACE(NEW.email, ' ', '');
IF NEW.email = '' THEN
SIGNAL SQLSTATE '45000' SET MESSAGE_TEXT = '`email` cannot be empty.';
END IF;
IF STR_TO_DATE(created_in, '%Y-%m-%d %H:%i:%s') IS NULL THEN
SIGNAL SQLSTATE '45000' SET MESSAGE_TEXT = 'Incorrect `created_in` datetime value.';
END IF;
IF STR_TO_DATE(signed_in, '%Y-%m-%d %H:%i:%s') IS NULL THEN
SIGNAL SQLSTATE '45000' SET MESSAGE_TEXT = 'Incorrect `signed_in` datetime value.';
END IF;
END
注意 - 此触发器允许任何值中的 NULL(但只有电子邮件可以为 NULL,所有其他列都定义为 NOT NULL)。
你可以对块进行排序,把概率最大的条件放在最前面。 SIGNAL执行后触发器不执行下面的代码。
用户table
CREATE TABLE `USERS` (
`ID` char(255) COLLATE utf8_unicode_ci NOT NULL,
`NAME` char(255) COLLATE utf8_unicode_ci NOT NULL,
`EMAIL` char(255) COLLATE utf8_unicode_ci DEFAULT NULL,
`CREATED_IN` datetime NOT NULL,
`SIGNED_IN` datetime NOT NULL,
PRIMARY KEY (`ID`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8 COLLATE=utf8_unicode_ci
触发器
CREATE TRIGGER `ABC` BEFORE INSERT ON `USERS` FOR EACH ROW
IF NEW.ID = "" OR NEW.NAME = "" OR
NEW.CREATED_IN = "" OR NEW.CREATED_IN = "0000-00-00 00:00:00" OR
NEW.SIGNED_IN = "" OR NEW.SIGNED_IN = "0000-00-00 00:00:00"
THEN SIGNAL SQLSTATE "45000"; END IF
ID列
- 如何让它不接受空值?
("", " ", " ", " ", etc...) -> Should return an error
- 如果已经有一个值,我怎样才能从中删除所有空格?
("bf9 d 34 c9 08" = "bf9d34c908")
NAME 列
- 如何让它不接受空值?
("", " ", " ", " ", etc...) -> Should return an error
EMAIL 列
- 如何让它不接受空值?
("", " ", " ", " ", etc...) -> Should return an error
- 如果已经有一个值,我怎样才能从中删除所有空格?
("nkr owks lpehqp jmgdb @ gm ail. com" = "nkrowkslpehqpjmgdb@gmail.com")
CREATED_IN 和 SIGNED_IN 列
- 如何让它不接受无效值?当插入无效值(如字母、数字或无效格式)时,应该 return 出错而不是输入 0000-00-00 00:00:00.
MySQL version is 5.7
处理用户 table 的这些要求的最佳方法是什么?
触发器可能如下所示:
CREATE TRIGGER process_input
BEFORE INSERT ON USERS
FOR EACH ROW
BEGIN
SET NEW.id = REPLACE(NEW.id, ' ', '');
IF NEW.id = '' THEN
SIGNAL SQLSTATE '45000' SET MESSAGE_TEXT = '`id` cannot be empty.';
END IF;
SET NEW.name = TRIM(NEW.name);
IF NEW.name = '' THEN
SIGNAL SQLSTATE '45000' SET MESSAGE_TEXT = '`name` cannot be empty.';
END IF;
SET NEW.email = REPLACE(NEW.email, ' ', '');
IF NEW.email = '' THEN
SIGNAL SQLSTATE '45000' SET MESSAGE_TEXT = '`email` cannot be empty.';
END IF;
IF STR_TO_DATE(created_in, '%Y-%m-%d %H:%i:%s') IS NULL THEN
SIGNAL SQLSTATE '45000' SET MESSAGE_TEXT = 'Incorrect `created_in` datetime value.';
END IF;
IF STR_TO_DATE(signed_in, '%Y-%m-%d %H:%i:%s') IS NULL THEN
SIGNAL SQLSTATE '45000' SET MESSAGE_TEXT = 'Incorrect `signed_in` datetime value.';
END IF;
END
注意 - 此触发器允许任何值中的 NULL(但只有电子邮件可以为 NULL,所有其他列都定义为 NOT NULL)。
你可以对块进行排序,把概率最大的条件放在最前面。 SIGNAL执行后触发器不执行下面的代码。