H2数据库CREATE TABLE有约束

时间:2012-03-24 13:23:21

标签: sql h2

我有两个SQL语句:

CREATE TABLE legs(legid INT PRIMARY KEY AUTO_INCREMENT NOT NULL,
                  playerid1 INT NOT NULL REFERENCES players(playerid),
                  playerid2 INT NOT NULL REFERENCES players(playerid),
                  added TIMESTAMP AS CURRENT_TIMESTAMP NOT NULL);

ALTER TABLE legs ADD CONSTRAINT distinct_players CHECK(playerid1 <> playerid2);

我99%肯定我应该能够将它们压缩成一个,即:

CREATE TABLE table(...
                   playerid2 INT NOT NULL REFERENCES players(playerid) CHECK(playerid1 <> playerid2),
                   ...);

但是,我一直都会遇到语法错误。 AFAIK,这是约束的所在。

1 个答案:

答案 0 :(得分:16)

CREATE TABLE legs(legid INT PRIMARY KEY AUTO_INCREMENT NOT NULL,
                  playerid1 INT NOT NULL REFERENCES players(playerid),
                  playerid2 INT NOT NULL REFERENCES players(playerid),
                  added TIMESTAMP AS CURRENT_TIMESTAMP NOT NULL,
                  CHECK (playerid1 <> playerid2));