SELECT
sætning PÅ informationsskema.TABLE_CONSTRAINTS
for at afgøre, om den fremmede nøgle findes:
IF NOT EXISTS (
SELECT NULL
FROM information_schema.TABLE_CONSTRAINTS
WHERE
CONSTRAINT_SCHEMA = DATABASE() AND
CONSTRAINT_NAME = 'fk_rabbits_main_page' AND
CONSTRAINT_TYPE = 'FOREIGN KEY'
)
THEN
ALTER TABLE `rabbits`
ADD CONSTRAINT `fk_rabbits_main_page`
FOREIGN KEY (`main_page_id`)
REFERENCES `rabbit_pages` (`id`);
END IF