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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 27, 20262026-05-27T14:22:46+00:00 2026-05-27T14:22:46+00:00

I am trying to run a script to automate upgrading of some db tables

  • 0

I am trying to run a script to automate upgrading of some db tables but some systems won’t have the table exist at all. How to best write, in MySQL, something like this:

  1. I have an existing table with two columns, address and city. I want to add column version OR create a new table for systems that dont have the table at all, so:

  2. CREATE table ‘hello’ (
    address text NOT NULL,
    city tinytext NOT NULL,
    version tinytext NOT NULL,
    )

  3. However, IF ‘hello’ exists but does not have column version, preserve table but add new column?

  4. In ‘version’ column, add the value ‘old’ for all the preserved data, but add ‘new’ for all the new entries I will populate the db with.

  5. Futureproof this so I can always add columns, checking in iteration as I upgrade different versions of the software?

EDIT: Some PHP code on the actual upgrade script would be helpful; imagine I have one “activation” function that should check for table, create it or otherwise modify it…

  • 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-27T14:22:47+00:00Added an answer on May 27, 2026 at 2:22 pm

    Answering your comment for how to check if the table exists if you can’t or don’t want to use CREATE TABLE IF NOT EXISTS tablename:

    <?php
    
    $db_name    = 'mydb';
    $table_name = 'table';
    
    mysql_connect("localhost", "mysql_user", "mysql_password");
    $result = mysql_list_tables($db_name);
    $num_rows = mysql_num_rows($result);
    
    for ($i = 0; $i < $num_rows; $i++) {
      if (mysql_tablename($result, $i) == $table_name) {
        // the specified table exists, do what you need to
        break;
      }
    }
    
    ?>
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm trying to run a script that will fade out all the divs selected
I am trying to run lame from a php script. I have tried these,
I have some SQL scripts that I'm trying to automate. In the past I
Im trying to run a UNIX script that will automate the process of creating
I am trying to run a script in several machines I have at work,
I'm trying to automate data merge process. Here's what I have: First of all
I was trying to run a script to create a DB with all the
I've Google around and haven't found much... I'm trying to run script/server for my
I'm trying to run this script that basically reads/interprets text in from a textfile
I'm trying to run this script: import re, os def build_pool(cwd): global xtn_pool, file_pool

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.