Solved

How to not create the MySQL table if it exists

Posted on 2013-11-19
9
413 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

Networking for the Cloud Era

Join Microsoft and Riverbed for a discussion and demonstration of enhancements to SteelConnect:
-One-click orchestration and cloud connectivity in Azure environments
-Tight integration of SD-WAN and WAN optimization capabilities
-Scalability and resiliency equal to a data center

Question has a verified solution.

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

Suggested Solutions

I use MySQL for many of my development projects in a Windows environment. To manage my databases (and perform queries) for years I used a tool called MySQL administrator.  This tool has since been replaced by MySQL Workbench. So I decided to m…
Popularity Can Be Measured Sometimes we deal with questions of popularity, and we need a way to collect opinions from our clients.  This article shows a simple teaching example of how we might elect a favorite color by letting our clients vote for …
Although Jacob Bernoulli (1654-1705) has been credited as the creator of "Binomial Distribution Table", Gottfried Leibniz (1646-1716) did his dissertation on the subject in 1666; Leibniz you may recall is the co-inventor of "Calculus" and beat Isaac…
In an interesting question (https://www.experts-exchange.com/questions/29008360/) here at Experts Exchange, a member asked how to split a single image into multiple images. The primary usage for this is to place many photographs on a flatbed scanner…

838 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