0

I like to append a table to add column but without using alert table command

e.g.

This is the table which is missing some columns.

CREATE TABLE IF NOT EXISTS `admin` (
  `id` int(11) NOT NULL auto_increment,
  `username` varchar(20) NOT NULL,
  `passwd` varchar(40) NOT NULL,
  `isActive` tinyint(1) NOT NULL default '1',
  `lastVisit` datetime NOT NULL default '0000-00-00 00:00:00',
  `modifyAt` datetime NOT NULL,
  `createdAt` datetime NOT NULL,
  PRIMARY KEY  (`id`)
) ENGINE=MyISAM DEFAULT CHARSET=utf8;

So if i run this query then it should automatically add missing columns into my tables

CREATE TABLE IF NOT EXISTS `admin` (
  `id` int(11) NOT NULL auto_increment,
  `username` varchar(20) NOT NULL,
  `passwd` varchar(40) NOT NULL,
  `name` varchar(100) NOT NULL,
  `originalUser` tinyint(1) NOT NULL default '0',
  `isActive` tinyint(1) NOT NULL default '1',
  `lastVisit` datetime NOT NULL default '0000-00-00 00:00:00',
  `modifyAt` datetime NOT NULL,
  `createdAt` datetime NOT NULL,
  PRIMARY KEY  (`id`)
) ENGINE=MyISAM DEFAULT CHARSET=utf8;

Can this be possible to do without using alert table command ?

1 Answer 1

2

I understand your question as you want to add some columns to your table. Please be informed that the term row is usually related to the actual data in your table, not the columns itself. If my assumption is wrong, please clarify your question.

You cannot use CREATE TABLE for altering a table. It is there to create table, and if it cannot create it, it will in most cases throw an error like you described. Another command exists for that reason: ALTER TABLE.

You might do it something like this.

(1) Create your table with your CREATE TABLE syntax above:

CREATE TABLE IF NOT EXISTS `admin` (
  `id` int(11) NOT NULL auto_increment,
  `username` varchar(20) NOT NULL,
  `passwd` varchar(40) NOT NULL,
  `isActive` tinyint(1) NOT NULL default '1',
  `lastVisit` datetime NOT NULL default '0000-00-00 00:00:00',
  `modifyAt` datetime NOT NULL,
  `createdAt` datetime NOT NULL,
  PRIMARY KEY  (`id`)
) ENGINE=MyISAM DEFAULT CHARSET=utf8;

(2) Use ALTER TABLE like this to make the modifications I think you want to have in your second statement (two more columns):

ALTER TABLE
ADD COLUMN `name` varchar(100) NOT NULL AFTER `passwd`,
ADD COLUMN `originalUser` tinyint(1) NOT NULL default '0' AFTER `name`;

Not related to your question, but I'd avoid column names like name, because if you don't escape them properly it'll throw you other errors (see reserved words).

Sign up to request clarification or add additional context in comments.

4 Comments

I have a database with lot of tables where I add new columns but they are too many to go through each one of them to add it as alert table. i was hoping i can import the .sql file into database and it can think that these column are missing so i can add them in for you. what about somthing like replace table command
How many are too many? You can use dynamic SQL I guess, to go through all tables in a database and do some actions (like ALTER TABLE.)
If you're missing columns, you have to add them like I described. There is no replace table command.
thank you for all this information i was just wondering if this can be possible.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.