尝试创建存储过程,如果表中已存在用户名或电子邮件,则会通知我。 这是我表格的结构
CREATE TABLE IF NOT EXISTS `user` (
`user_id` smallint(5) unsigned NOT NULL,
`username` varchar(40) CHARACTER SET utf8 COLLATE utf8_bin NOT NULL,
`email` varchar(60) CHARACTER SET utf8 COLLATE utf8_bin NOT NULL,
`password` varchar(128) NOT NULL,
`active` tinyint(4) NOT NULL DEFAULT '1',
`date_joined` datetime NOT NULL DEFAULT CURRENT_TIMESTAMP,
`user_category_id` tinyint(3) unsigned NOT NULL
) ENGINE=InnoDB AUTO_INCREMENT=13 DEFAULT CHARSET=utf8;
这是存储过程的代码
DELIMITER $$
CREATE PROCEDURE `sp_create_account`( username_param VARCHAR(40), email_param VARCHAR(60), pass VARCHAR(30), category_id TINYINT )
BEGIN
DECLARE salt VARCHAR(60);
DECLARE username_var VARCHAR(40);
DECLARE email_var VARCHAR(60);
SELECT username_var INTO username_var FROM user WHERE username = username_param;
SELECT email_var INTO email_var FROM user WHERE email = email_param;
IF username_var = username_param THEN
SELECT 'username' AS message;
ELSEIF email_var = email_param THEN
SELECT 'email' AS message;
ELSE
SET salt = '@4$^7EC%?';
SET salt = CONCAT( username_param, salt );
INSERT INTO user VALUES
( DEFAULT, username_param, email_param, AES_ENCRYPT( pass, salt ), DEFAULT, DEFAULT, category_id );
SELECT 'created' AS message;
END IF;
END$$
DELIMITER ;
两个问题: 问题1: 插入一个没有用户名或电子邮件的唯一条目时,一切正常,但当用户名或电子邮件确实存在时,我在下面的屏幕截图中得到这些错误但我希望存储过程返回一个简单的选择,指示问题出在哪里可以是或表示成功,如果它返回'创建'
问题2 如果它是一个唯一的条目并且它被插入到表中,则该特定行中的密码列单元格将插入一个空字符串。
上述所有原因可能是什么?谢谢你。
答案 0 :(得分:1)
也许这些变化正是您所寻找的。对架构和if
块的更改以及返回值。
返回值是用户ID的AUTO_INCREMENT
。请注意,我几乎遵循您的架构。该表中的主键可能会折叠一点。有些人可能没有用户ID或用户名,只有电子邮件地址作为PK。这些是要考虑的事情。我还为电子邮件地址添加了一个唯一的密钥。
<强>架构:强>
CREATE TABLE IF NOT EXISTS `user` (
`user_id` int auto_increment primary key,
`username` varchar(40) CHARACTER SET utf8 COLLATE utf8_bin NOT NULL,
`email` varchar(60) CHARACTER SET utf8 COLLATE utf8_bin NOT NULL,
`password` varbinary(128) NOT NULL,
`active` tinyint(4) NOT NULL DEFAULT '1',
`date_joined` datetime NOT NULL DEFAULT CURRENT_TIMESTAMP,
`user_category_id` tinyint(3) unsigned NOT NULL,
unique key (`email`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8;
-- truncate table user;
存储过程:
drop procedure if exists sp_create_account;
DELIMITER $$
CREATE PROCEDURE `sp_create_account`
( username_param VARCHAR(40),
email_param VARCHAR(60),
pass VARCHAR(30),
category_id TINYINT
)
BEGIN
DECLARE salt VARCHAR(60);
DECLARE username_var VARCHAR(40);
DECLARE email_var VARCHAR(60);
DECLARE recFound int;
DECLARE foundStatus int DEFAULT 0;
SELECT user_id INTO recFound FROM user WHERE username = username_param;
IF recFound is null THEN
SELECT user_id INTO recFound FROM user WHERE email = email_param;
IF recFound is not null THEN
SET foundStatus=1;
END IF;
ELSE
SET foundStatus=1;
END IF;
IF foundStatus=0 THEN
SET salt = '@4$^7EC%?';
SET salt = CONCAT( username_param, salt );
INSERT INTO user (username,email,password,active,date_joined,user_category_id) VALUES
( username_param, email_param, AES_ENCRYPT( pass, salt ), DEFAULT, DEFAULT, category_id );
set recFound=LAST_INSERT_ID();
END IF;
SELECT recFound;
END$$
DELIMITER ;
<强>测试强>
call sp_create_account('Katherine','ksmith@hotmail.com','thepass01',101);
call sp_create_account('Katherine','ksmith@hotmail.com','thepass01',101);
call sp_create_account('Katherine','ksmith@hotmail.com','thepass01',101);
call sp_create_account('caspar','caspar001@gmail.com','thepass02',77);
select * from user;
+---------+-----------+---------------------+------------------+--------+---------------------+------------------+
| user_id | username | email | password | active | date_joined | user_category_id |
+---------+-----------+---------------------+------------------+--------+---------------------+------------------+
| 1 | Katherine | ksmith@hotmail.com | _╦*Fó▄GàB╔┌O►²§' | 1 | 2016-07-13 17:56:54 | 101 |
| 2 | caspar | caspar001@gmail.com | ♀½B§5U├↨I♀#*├ ∟L | 1 | 2016-07-13 17:57:09 | 77 |
+---------+-----------+---------------------+------------------+--------+---------------------+------------------+
2 rows in set (0.00 sec)
答案 1 :(得分:0)
您的上一个ELSE需要在BEGIN
... END
中包含您希望在其中执行的所有命令。也许? 我最近在MSSQL上工作得更多。