Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

H2 database CREATE TABLE with constraint

Tags:

sql

h2

I have two SQL statements:

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);

I am 99% sure I should be able to condense them into one, i.e:

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

However, I am consistently getting a syntax error. AFAIK, this is where the constraint should be.

like image 313
c24w Avatar asked Mar 24 '12 13:03

c24w


1 Answers

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));
like image 72
a_horse_with_no_name Avatar answered Oct 14 '22 15:10

a_horse_with_no_name