Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 3281318
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 17, 20262026-05-17T19:48:09+00:00 2026-05-17T19:48:09+00:00

In my create script for my database create script looking something like this: CREATE

  • 0

In my create script for my database create script looking something like this:

CREATE TABLE IF NOT EXISTS `rabbits`
(
    `id` INT UNSIGNED NOT NULL AUTO_INCREMENT,
    `name` VARCHAR(255) NOT NULL,
    `main_page_id` INT UNSIGNED COMMENT 'What page is the main one',
    PRIMARY KEY (`id`),
    KEY `main_page_id` (`main_page_id`)
)
ENGINE=InnoDB;

CREATE TABLE IF NOT EXISTS `rabbit_pages`
(
    `id` INT UNSIGNED NOT NULL AUTO_INCREMENT,
    `rabbit_id` INT UNSIGNED NOT NULL,
    `title` VARCHAR(255) NOT NULL,
    `content` TEXT NOT NULL,
    PRIMARY KEY (`id`),
    KEY `rabbit_id` (`rabbit_id`),
    CONSTRAINT `fk_rabbits_pages` FOREIGN KEY (`rabbit_id`) REFERENCES `rabbits` (`id`)
)
ENGINE=InnoDB;

ALTER TABLE `rabbits`
    ADD CONSTRAINT `fk_rabbits_main_page` FOREIGN KEY (`main_page_id`) REFERENCES `rabbit_pages` (`id`);

This runs fine the first time, but if I run it again it fails on the last line there with “Duplicate key on write or update”.

Is there a way I can do sort of a ADD CONSTRAINT IF NOT EXISTS or something like that? Like I can do with the CREATE TABLE query?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-17T19:48:10+00:00Added an answer on May 17, 2026 at 7:48 pm

    Interesting question. You may want to disable foreign keys before you call your CREATE TABLE statements and enable them afterwards. This will allow you to define the foreign keys directly in the CREATE TABLE DDL:

    Example:

    SET FOREIGN_KEY_CHECKS = 0;
    Query OK, 0 rows affected (0.00 sec)
    
    CREATE TABLE IF NOT EXISTS `rabbits` (
        `id` INT UNSIGNED NOT NULL AUTO_INCREMENT,
        `name` VARCHAR(255) NOT NULL,
        `main_page_id` INT UNSIGNED COMMENT 'What page is the main one',
        PRIMARY KEY (`id`),
        KEY `main_page_id` (`main_page_id`),
        CONSTRAINT `fk_rabbits_main_page` FOREIGN KEY (`main_page_id`) REFERENCES `rabbit_pages` (`id`)
    ) ENGINE=InnoDB;
    Query OK, 0 rows affected (0.04 sec)
    
    CREATE TABLE IF NOT EXISTS `rabbit_pages` (
        `id` INT UNSIGNED NOT NULL AUTO_INCREMENT,
        `rabbit_id` INT UNSIGNED NOT NULL,
        `title` VARCHAR(255) NOT NULL,
        `content` TEXT NOT NULL,
        PRIMARY KEY (`id`),
        KEY `rabbit_id` (`rabbit_id`),
        CONSTRAINT `fk_rabbits_pages` FOREIGN KEY (`rabbit_id`) REFERENCES `rabbits` (`id`)
    ) ENGINE=InnoDB;
    Query OK, 0 rows affected (0.16 sec)
    
    SET FOREIGN_KEY_CHECKS = 1;
    Query OK, 0 rows affected (0.00 sec)
    

    Test case:

    INSERT INTO rabbits (name, main_page_id) VALUES ('bobby', NULL);
    Query OK, 1 row affected (0.02 sec)
    
    INSERT INTO rabbit_pages (rabbit_id, title, content) VALUES (1, 'My Main Page', 'Hello');
    Query OK, 1 row affected (0.00 sec)
    
    SELECT * FROM rabbits;
    +----+-------+--------------+
    | id | name  | main_page_id |
    +----+-------+--------------+
    |  1 | bobby | NULL         |
    +----+-------+--------------+
    1 row in set (0.00 sec)
    
    SELECT * FROM rabbit_pages;
    +----+-----------+--------------+---------+
    | id | rabbit_id | title        | content |
    +----+-----------+--------------+---------+
    |  1 |         1 | My Main Page | Hello   |
    +----+-----------+--------------+---------+
    1 row in set (0.00 sec)
    
    UPDATE rabbits SET main_page_id = 2 WHERE id = 1;
    ERROR 1452 (23000): A foreign key constraint fails
    
    UPDATE rabbits SET main_page_id = 1 WHERE id = 1;
    Query OK, 1 row affected (0.00 sec)
    Rows matched: 1  Changed: 1  Warnings: 0
    
    UPDATE rabbit_pages SET rabbit_id = 2 WHERE id = 1;
    ERROR 1452 (23000): A foreign key constraint fails
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'd quite like to use ADO.NET to generate a CREATE TABLE script to create
How to create a database using T SQL script on a specified location? Let's
I'd like to create a script to manipulate Apache2 configuration directly, reading and writing
I have a column with a DEFAULT constraint. I'd like to create a script
I would like to create a batch script, to go through 20,000 links in
I would like to create a WLST script to create my Weblogic domain. However
I tried creating a DB migration in Rails that looked something like this: ruby
I want to create a script that parses or makes sense of apache's error
I need to create a script that automatically setup a ssh tunnel. I think
I'm trying to create a build script for my current project, which includes an

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.