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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 25, 20262026-05-25T15:15:19+00:00 2026-05-25T15:15:19+00:00

I want to rename columns in database programmatically and MySQL enforces me to specify

  • 0

I want to rename columns in database programmatically and MySQL enforces me to specify column definition in CHANGE statement.

ALTER TABLE table_name CHANGE old_name new_name column_definition 

So, I want to build column definition from INFORMATION_SCHEMA.COLUMNS table. I’ve read answers which adjust me to parse output of SHOW CREATE TABLE table_name, but I do not want to do that in some reasons.

I want to get column definition as result of single SQL script if it is possible.

  • 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-25T15:15:20+00:00Added an answer on May 25, 2026 at 3:15 pm

    Try this:

    select COLUMN_TYPE from INFORMATION_SCHEMA.COLUMNS WHERE TABLE_SCHEMA = '{database name}' AND TABLE_NAME = '{table name}' AND COLUMN_NAME = '{column name}';
    

    You’ll need to fill in the right values, but it should give you the precise string you’ll need to fill into the ALTER statement. You will need to do some additional work to deal with default statements and NULL settings.

    The complete sample:

    SELECT CONCAT(
          CAST(COLUMN_NAME AS CHAR),
          ' ',
          CAST(COLUMN_TYPE AS CHAR),
          IF(ISNULL(CHARACTER_SET_NAME),
             '',
             CONCAT(' CHARACTER SET ', CHARACTER_SET_NAME)),
          IF(ISNULL(COLLATION_NAME), '', CONCAT(' COLLATE ', COLLATION_NAME)),
          ' ',
          IF(IS_NULLABLE = 'NO', 'NOT NULL ', ''),
          IF(IS_NULLABLE = 'NO' AND ISNULL(COLUMN_DEFAULT),
             '',
             CONCAT('DEFAULT ', QUOTE(COLUMN_DEFAULT), ' ')),
          UPPER(extra))
          AS column_definition
     FROM INFORMATION_SCHEMA.COLUMNS
    WHERE  TABLE_SCHEMA = 'table_schema'
       AND TABLE_NAME = 'table_name'
       AND COLUMN_NAME = 'column_name';
    

    EDIT Added quoting of default value, collation and charset.

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

Sidebar

Related Questions

I want to rename my DataTable column names. I tried this: dt.Columns[8].ColumnName = "regnum";
How do I rename a column in table xyz ? The columns are: Manufacurerid,
I want to rename an existing table using SQL statement: I have already tried:
I want to rename a database, but keep getting the error that 'couldn't get
I want to rename the timestamp columns defined in timestamp.rb . Can the methods
I want to execute a query in which I rename one of the columns
I want to run the following rename EXECUTE sp_rename N'dbo.Semesters.IsPublic', N'Tmp_ShowNCs', 'COLUMN' I get
I want to rename a column at the end of a series of queries
I would like to rename a large number of columns (column headers) to have
How can I rename table column renaming using LINQ like in regular SQL SELECT

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.