触发生成ID

时间:2016-04-25 14:55:28

标签: sql oracle

我想在表格中创建生成ID的触发器:

    CREATE TABLE client (
    clientID INT PRIMARY KEY NOT NULL,
    name VARCHAR(16) NOT NULL,
    surname VARCHAR(16) NOT NULL,
    personalID VARCHAR(10) NOT NULL,
    CONSTRAINT verifyPersonalID CHECK ((personalID BETWEEN 1000000000 and 9999999999) and (MOD(personalID, 11) = 0))
);

我试着像这样写它但它不断返回错误,我不知道为什么。请你能告诉我我做错了什么吗?

CREATE OR REPLACE TRIGGER clientID
AFTER INSERT
    ON client
    FOR EACH ROW
BEGIN
    UPDATE client
    SET client.clientID = klientSeq.nextval
    WHERE :new.personalID = client.personalID;
END;
/

2 个答案:

答案 0 :(得分:1)

你想要之前插入触发器:

CREATE OR REPLACE TRIGGER klientID
BEFORE INSERT
    ON klient
    FOR EACH ROW
BEGIN
    SELECT klientSeq.nextval INTO :new.cisloKlienta FROM dual;
END;

答案 1 :(得分:0)

也许不是创建触发器,而是可以将主键的默认值作为序列的下一个值。