I'm trying to test this solution of ADD CONSTRAINT IF NOT EXISTS, but I get an error with my code :
IF NOT EXISTS (SELECT NULL FROM information_schema.TABLE_CONSTRAINTS WHERE
CONSTRAINT_SCHEMA = 'mydatabase' AND
CONSTRAINT_NAME = 'tb_uq') THEN
ALTER TABLE `scrape_xpaths` ADD CONSTRAINT `tb_uq` UNIQUE (`LinkCaption`);
END IF
I get a check syntax error.
Also this doesn't work:
IF NOT EXISTS (SELECT NULL FROM information_schema.TABLE_CONSTRAINTS) THEN
END IF
Same error.
Constraint name 'tb_uq' exists because commands :
SELECT NULL FROM information_schema.TABLE_CONSTRAINTS
SELECT * FROM information_schema.TABLE_CONSTRAINTS
work and return all constraints and tb_uq is one of them. What am I doing wrong?
This is how I add the constraint the first time. Second time fails of course :
ALTER TABLE `mydatabase`.tablename ADD CONSTRAINT tb_uq UNIQUE (LinkCaption);
Also the code below fails with the same error :
if (true)
begin
end else
begin
end