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

  • SEARCH
  • Home
  • 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 513871
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 13, 20262026-05-13T07:27:59+00:00 2026-05-13T07:27:59+00:00

I’m involved is a project to migrate a project from Oracle to MySQL. I

  • 0

I’m involved is a project to migrate a project from Oracle to MySQL. I Have a script that i’m running from the MySQL shell command, called CreateTables.sql that looks like this internally:

source table\DropForeignKeys.sql
source tables\Site.sql
source tables\Language.sql
source tables\Country.sql
source tables\Locale.sql
source tables\Tag.sql

mysql --user=root --password --database=junkdb -vv < CreateTables.sql

What I’m after is a way to make the execution for the first script DropForeignKeys.sql conditional based on if the db has any tables of not. Alternatively it would be nice if there were a way to drop constraint if not exists but such a construct does not exists in MySQL to my knowledge.

So my question is how do I make the dropping of foreign key constraints conditional at script level or constraint level, so that I can have a reliable re-playable script?

  • 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-13T07:28:00+00:00Added an answer on May 13, 2026 at 7:28 am

    What I’m after is a way to make the execution for the first script DropForeignKeys.sql conditional based on if the db has any tables of not.

    Conditional logic (IF/ELSE) is only supported in functions and stored procedures – you’d have to use something that resembles:

    DELIMITER $$
    
    DROP PROCEDURE IF EXISTS upgrade_database $$
    CREATE PROCEDURE upgrade_database()
    BEGIN
    
     -- INSERT NEW RECORD IF PREEXISTING RECORD DOESNT EXIST
     IF((SELECT COUNT(*) AS column_exists 
           FROM information_schema.columns 
          WHERE table_name = 'test' 
            AND column_name = 'test7') = 0) THEN
       ALTER TABLE test ADD COLUMN `test7` int(10) NOT NULL;
       UPDATE test SET test7 = test;
       SELECT 'Altered!';
     ELSE
       SELECT 'Not altered!';
     END IF;
    END $$
    
    DELIMITER ;
    CALL upgrade_database();
    

    Rather than reference INFORMATION_SCHEMA.COLUMNS, you could reference INFORMATION_SCHEMA.KEY_COLUMN_USAGE.

    Depending on your needs:

    ALTER TABLE [table name] DISABLE KEYS 
    ALTER TABLE [table name] ENABLE KEYS 
    

    …will disable and re-enable the keys attached to that table without needing to know each one. You can disable and enable keys on a database level using SET foreign_key_checks = 0; to disable, and SET foreign_key_checks = 1; to enable them.

    It surprises me that MySQL doesn’t seem to have a better way of dealing with this common scripting problem.

    Oracle doesn’t either, but constraints aren’t really something you want to alter blindly without knowing details.

    The reason I need the drop foreign keys script is because drop table yields an error when their are FK attachments. Will disabling FK checks allow for me to drop the tables?

    Yes, dropping or disabling the constraints will allow you to drop the table but be aware – in order to re-enable the fk check you’ll need the data in the parent to match the existing data in the child tables.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a French site that I want to parse, but am running into
I have a string like this: La Torre Eiffel paragonata all&#8217;Everest What PHP function
I'm parsing an RSS feed that has an &#8217; in it. SimpleXML turns this
I have a text area in my form which accepts all possible characters from
I am currently running into a problem where an element is coming back from
I have a bunch of posts stored in text files formatted in yaml/textile (from
link Im having trouble converting the html entites into html characters, (&# 8217;) i
That's pretty much it. I'm using Nokogiri to scrape a web page what has
I have just tried to save a simple *.rtf file with some websites and
For some reason, after submitting a string like this Jack’s Spindle from a text

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.