Solved

How to not create the MySQL table if it exists

Posted on 2013-11-19
9
407 Views
Last Modified: 2013-11-21
Dear all,

Right now if I generate the MysQL table create statement it don't give me the statement that to check the existence of table before it creates.

how to do this, let have an example, how to ignore the following create table statement if it exists already.

[/CREATE TABLE `QRTZ_CALENDARS` (
  `SCHED_NAME` varchar(120) NOT NULL,
  `CALENDAR_NAME` varchar(200) NOT NULL,
  `CALENDAR` blob NOT NULL,
  PRIMARY KEY (`SCHED_NAME`,`CALENDAR_NAME`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8;

Open in new window

0
Comment
Question by:marrowyung
  • 5
  • 4
9 Comments
 
LVL 48

Accepted Solution

by:
PortletPaul earned 500 total points
ID: 39661506
0
 
LVL 48

Expert Comment

by:PortletPaul
ID: 39661509
Example
CREATE TABLE IF NOT EXISTS `QRTZ_CALENDARS` (
  `SCHED_NAME` varchar(120) NOT NULL,
  `CALENDAR_NAME` varchar(200) NOT NULL,
  `CALENDAR` blob NOT NULL,
  PRIMARY KEY (`SCHED_NAME`,`CALENDAR_NAME`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8
;

Open in new window

0
 
LVL 1

Author Comment

by:marrowyung
ID: 39661616
as I will do this too:

CONCAT( 'ALTER TABLE Audit_info.DBAudit_', tablelist,' ADD TriggerAction VARCHAR( 10 ) NULL,
ADD Action CHAR( 10 ) NULL AFTER TriggerAction ,
ADD ActionDate DATETIME NULL AFTER Action , 
ADD ActionBy VARCHAR( 50 ) NULL AFTER ActionDate ;'); 

Open in new window


any way to ignore field if it is there or just replace it as we might change this type later on ?
0
 
LVL 1

Author Comment

by:marrowyung
ID: 39661619
we need this as it will complain:


MySQL Database Error: Duplicate column name 'Action'. Add a differentiating column alias.

Open in new window


as it found the same column with the same name.
0
6 Surprising Benefits of Threat Intelligence

All sorts of threat intelligence is available on the web. Intelligence you can learn from, and use to anticipate and prepare for future attacks.

 
LVL 48

Expert Comment

by:PortletPaul
ID: 39661672
>>any way to ignore field if it is there or just replace it ...?
the grand purpose of this is to audit the source tables.

If you drop columns then you lose the audit data of that column.

>>" as we might change this type later on "
for this you would have to compare meta-data of each existing column (to determine if it has changed or not).


These are your decisions to make.

-----------
I think quite some time ago I suggested finding something that was tested and proven in this field rather than building from scratch. That advice still stands in my opinion.
0
 
LVL 1

Author Comment

by:marrowyung
ID: 39661726
"I think quite some time ago I suggested finding something that was tested and proven in this field rather than building from scratch. That advice still stands in my opinion. "

where is it ? please show me.

"If you drop columns then you lose the audit data of that column."

yes, we will tell developer about this. so we have a good reason to NOT removing the audit column.

But how about field type change? still lost all data?

"for this you would have to compare meta-data of each existing column (to determine if it has changed or not)."

so can't do any alter/replace statement directly on the schema ?
0
 
LVL 48

Expert Comment

by:PortletPaul
ID: 39661746
>>But how about field type change? still lost all data?
well this is really hard to answer

Most likely, no loss.
e.g. change column from decimal(12,4) to decimal(18,5)

But, on occasions, big problems!
e.g. change column from nvarchar(10) to date

in this example you may have started keeping dates in YYYY-MM-DD as a string but decided that was silly and to go for date instead. Trying to automate such a dramatic shift of data type would be "hard" and data loss is likely.
0
 
LVL 1

Author Comment

by:marrowyung
ID: 39665472
0
 
LVL 1

Author Comment

by:marrowyung
ID: 39668092
0

Featured Post

Complete Microsoft Windows PC® & Mac Backup

Backup and recovery solutions to protect all your PCs & Mac– on-premises or in remote locations. Acronis backs up entire PC or Mac with patented reliable disk imaging technology and you will be able to restore workstations to a new, dissimilar hardware in minutes.

Join & Write a Comment

Foreword This is an old article.  Instead of using the MySQL extension that was used in the original code examples, please choose one of the currently supported database extensions instead.  More information is available here: MySQLi / PDO (http://…
Introduction Since I wrote the original article about Handling Date and Time in PHP and MySQL (http://www.experts-exchange.com/articles/201/Handling-Date-and-Time-in-PHP-and-MySQL.html) several years ago, it seemed like now was a good time to updat…
In this tutorial you'll learn about bandwidth monitoring with flows and packet sniffing with our network monitoring solution PRTG Network Monitor (https://www.paessler.com/prtg). If you're interested in additional methods for monitoring bandwidt…
This demo shows you how to set up the containerized NetScaler CPX with NetScaler Management and Analytics System in a non-routable Mesos/Marathon environment for use with Micro-Services applications.

747 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

10 Experts available now in Live!

Get 1:1 Help Now