在MySQL社区服务器中创建唯一序列

时间:2012-01-29 15:08:36

标签: mysql

我试图在MySQL社区服务器5.5.16中创建存储过程:

CREATE UNIQUE SEQUENCE seq_num_accountnumber;

Create Procedure usp_createCustomerAccount(IN pCustomer INTEGER,pCredit INTEGER)
NOT DETERMINISTIC
MODIFIES SQL DATA
BEGIN
DECLARE Z INTEGER;
set Z = NEXT_VALUE OF seq_num_accountnumber;
if pCredit is null then
Insert into Account(Number,Customer,Credit) Values(Z,pCustomer,0);
else
Insert into Account(Number,Customer,Credit) Values(Z,pCustomer,pCredit);
End if;
End

但我不知道如何在MySQL服务器中使用UNIQUE SEQUENCE。我试过这样但我认为这是不正确的,因为价值不是唯一的:

Create Procedure my_createCustomerAccount(IN pCustomer INTEGER,pCredit INTEGER)
NOT DETERMINISTIC
MODIFIES SQL DATA
BEGIN
DECLARE Z INTEGER;
set Z = 1;
if pCredit is null then
Insert into Account(Number,Customer,Credit) Values(Z,pCustomer,0);
else
Insert into Account(Number,Customer,Credit) Values(Z,pCustomer,pCredit);
set Z = Z+1;
End if;
End

1 个答案:

答案 0 :(得分:0)

我刚设置了字段Number as NOT NULL AUTO_INCREMENT并在插入sql INSERT INTO Account(Customer, Credit) VALUES(pCustomer,0);