所以这就是伙计们,
我有一个包含此内容的表:
CREATE TABLE IF NOT EXISTS `people` (
`Id_People` smallint(5) unsigned NOT NULL AUTO_INCREMENT,
`Firstname` varchar(20) NOT NULL,
`Lastname` varchar(20) NOT NULL,
PRIMARY KEY (`Id_People`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8 AUTO_INCREMENT=1002 ;
如何设置触发器以使我的名字和姓氏的第一个字母大写?
每当我添加某人时,
插入:
INSERT INTO people (Firstname, Lastname) VALUES ('doe', 'john')
每当我编辑某人时,
并在更新:
UPDATE people SET Lastname = 'smith', Firstname= 'jane' WHERE Id_People = 1;
这甚至有一个双管名称,例如:john smith(名字)doe(姓氏)?
答案 0 :(得分:3)
我可以建议您使用MySql RDBMS。
MySql不提供标准功能,用于大写每个单词中的第一个字母。
首先,
创建此功能:
DELIMITER $$
CREATE FUNCTION `capitalize`(s varchar(255)) RETURNS varchar(255)
BEGIN
declare c int;
declare x varchar(255);
declare y varchar(255);
declare z varchar(255);
set x = UPPER( SUBSTRING( s, 1, 1));
set y = SUBSTR( s, 2);
set c = instr( y, ' ');
while c > 0
do
set z = SUBSTR( y, 1, c);
set x = CONCAT( x, z);
set z = UPPER( SUBSTR( y, c+1, 1));
set x = CONCAT( x, z);
set y = SUBSTR( y, c+2);
set c = INSTR( y, ' ');
end while;
set x = CONCAT(x, y);
return x;
END$$
其次,
创建更新和插入触发器
CREATE TRIGGER frst_l_up_insert BEFORE INSERT ON people FOR EACH ROW
SET NEW.Firstname = capitalize(NEW.Firstname), NEW.Lastname = capitalize(NEW.Lastname);
CREATE TRIGGER frst_l_up_update BEFORE UPDATE ON people FOR EACH ROW
SET NEW.Firstname = capitalize(NEW.Firstname), NEW.Lastname = capitalize(NEW.Lastname)
最后,
更新表中的存在记录
UPDATE people SET Lastname = capitalize(Lastname), Firstname = capitalize(Firstname);