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 8291379
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 8, 20262026-06-08T13:06:40+00:00 2026-06-08T13:06:40+00:00

Hello The following procedure will have to move all constraints from one table to

  • 0

Hello The following procedure will have to move all constraints from one table to the other however I am having some difficulties at the point where the constraint should be deleted.

The problem: how do I use variables in the following line

ALTER TABLE var_referenced_table_name DROP FOREIGN KEY var_constraint_name;

when I use as is, I receive the following error

Error Code: 1146. Table 'oaf_businesslink_dev.var_referenced_table_name' doesn't exist

MySQL does not recognise var_referenced_table_name and var_constraint_name as variables.

DELIMITER //
DROP PROCEDURE IF EXISTS AlterConstraints//
CREATE PROCEDURE AlterConstraints()
BEGIN

    DECLARE schema_name VARCHAR(60) DEFAULT 'oaf_businesslink_dev';
    DECLARE table_name VARCHAR(60) DEFAULT 'wp_systemuser';

    DECLARE finished INTEGER DEFAULT 0;
    DECLARE total INTEGER DEFAULT 0;    

    DECLARE var_constraint_name VARCHAR(60) DEFAULT '';
    DECLARE var_table_name VARCHAR(60) DEFAULT '';
    DECLARE var_column_name VARCHAR(60) DEFAULT '';
    DECLARE var_referenced_table_name VARCHAR(60) DEFAULT '';
    DECLARE var_referenced_column_name VARCHAR(60) DEFAULT '';

    DECLARE cur_constraints CURSOR FOR SELECT constraint_Name, table_name,column_name,referenced_table_name,referenced_column_name
    FROM information_schema.key_column_usage
    WHERE constraint_schema = schema_name
        AND referenced_table_name = table_name
        AND table_name IS NOT NULL;

    DECLARE CONTINUE HANDLER FOR NOT FOUND SET finished = 1;
    OPEN cur_constraints;

    get_constraint: 
    LOOP FETCH cur_constraints 
    INTO var_constraint_name
        ,var_table_name
        ,var_column_name
        ,var_referenced_table_name
        ,var_referenced_column_name;          

        IF finished THEN
            LEAVE get_constraint;
        END IF; 

        /* Get Constraint Count */
        SET total = total + 1;

        /* Remove Constraint */
        IF EXISTS(SELECT * FROM information_schema.TABLE_CONSTRAINTS
            WHERE CONSTRAINT_NAME = var_constraint_name AND TABLE_NAME = var_referenced_table_name AND TABLE_SCHEMA = schema_name)
            THEN
            /* 
             * Error Code: 1146. Table 'oaf_businesslink_dev.var_referenced_table_name' doesn't exist
             */
            ALTER TABLE var_referenced_table_name DROP FOREIGN KEY var_constraint_name;
        END IF;

        /* Change Datatype to BIGINT */

        /* Recreate Constraint to new table */
    END

    LOOP get_constraint;
    CLOSE cur_constraints;
    SELECT total;

END
//
DELIMITER ;

CALL AlterConstraints();

Thanks in advance.

  • 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-06-08T13:06:41+00:00Added an answer on June 8, 2026 at 1:06 pm

    With the use of variables as column names and tables, it would be best to DECLARE a query as a “string” and then execute that string via a Prepared Statement.

    This can be done in two ways, either by CONCAT() to build the full string or by using PREPARE with arguments:

    SET @query = CONCAT('ALTER TABLE ', var_referenced_table_name, ' DROP FOREIGN KEY ', var_constraint_name, ';');
    PREPARE stmt FROM @query; 
    EXECUTE stmt; 
    DEALLOCATE PREPARE stmt;
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Hello have following strange issue, can vertical center the menu text if it's one
Hello I have following columns in mysql table: rating1, rating2, price, cond, approved Is
Hello all, I want to execute the following procedure: EXECUTE MYDB.dbo.MYPROCEDURE @gender='male', @status='single' The
I have a file template.txt which contains the following: Hello ${something} I would like
Say I have the following string: hello my car is red and my shoe
Hello I have the following code: if(_deviceSegmentCntrl.selectedSegmentIndex == 0) { deviceOne = [[NSString alloc]
Hello i'm testing the following code it's suppose to list all apps installed on
Hello I have the following code which uses knockout.js, var allJobberDetailsArray = []; getAllJobberDetailsArray();
Hello I have the following code namespace ConsoleApplication2 { class Program { static void
Hello i have the following problem, I record a video using red5 like this:

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.