Solved

How to not create the MySQL table if it exists

Posted on 2013-11-19
9
412 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
PRTG Network Monitor: Intuitive Network Monitoring

Network Monitoring is essential to ensure that computer systems and network devices are running. Use PRTG to monitor LANs, servers, websites, applications and devices, bandwidth, virtual environments, remote systems, IoT, and many more. PRTG is easy to set up & use.

 
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
 
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

PRTG Network Monitor: Intuitive Network Monitoring

Network Monitoring is essential to ensure that computer systems and network devices are running. Use PRTG to monitor LANs, servers, websites, applications and devices, bandwidth, virtual environments, remote systems, IoT, and many more. PRTG is easy to set up & use.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Introduction In this article, I will by showing a nice little trick for MySQL similar to that of my previous EE Article for SQLite (http://www.sqlite.org/), A SQLite Tidbit: Quick Numbers Table Generation (http://www.experts-exchange.com/A_3570.htm…
Introduction This article is intended for those who are new to PHP error handling (https://www.experts-exchange.com/articles/11769/And-by-the-way-I-am-New-to-PHP.html).  It addresses one of the most common problems that plague beginning PHP develop…
This Micro Tutorial will give you a basic overview how to record your screen with Microsoft Expression Encoder. This program is still free and open for the public to download. This will be demonstrated using Microsoft Expression Encoder 4.
Established in 1997, Technology Architects has become one of the most reputable technology solutions companies in the country. TA have been providing businesses with cost effective state-of-the-art solutions and unparalleled service that is designed…

770 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