Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

How to not create the MySQL table if it exists

Posted on 2013-11-19
9
Medium Priority
?
421 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 49

Accepted Solution

by:
PortletPaul earned 2000 total points
ID: 39661506
0
 
LVL 49

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
NFR key for Veeam Backup for Microsoft Office 365

Veeam is happy to provide a free NFR license (for 1 year, up to 10 users). This license allows for the non‑production use of Veeam Backup for Microsoft Office 365 in your home lab without any feature limitations.

 
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 49

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 49

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

Free Tool: IP Lookup

Get more info about an IP address or domain name, such as organization, abuse contacts and geolocation.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Introduction Since I wrote the original article about Handling Date and Time in PHP and MySQL several years ago, it seemed like now was a good time to update it for object-oriented PHP.  This article does that, replacing as much as possible the pr…
Recursive SQL is one of the most fascinating and powerful and yet dangerous feature offered in many modern databases today using a Common Table Expression (CTE) first introduced in the ANSI SQL 99 standard. The first implementations of CTE began ap…
In this video, Percona Solution Engineer Rick Golba discuss how (and why) you implement high availability in a database environment. To discuss how Percona Consulting can help with your design and architecture needs for your database and infrastr…
In this video, Percona Solutions Engineer Barrett Chambers discusses some of the basic syntax differences between MySQL and MongoDB. To learn more check out our webinar on MongoDB administration for MySQL DBA: https://www.percona.com/resources/we…
Suggested Courses

971 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