Category: Database

  • Mysql: Insert or Update in One SQL Statement

    I must say, I really wish I knew about this technique years ago – before I started using frameworks to do a lot of my development.  The effort that was wasted in building separate code and separate logic to deal with inserts vs updates, when all of this time, there is a nice and simple feature built-in to Mysql that does this for us…

    (more…)

  • Regular Expressions with Mysql

    I must admit, this is something that I’ve never done until just recently; creating a regular expression in Mysql.  I’ve typically been able to accomplish whatever I wanted to with a LIKE statement or some server-side code.

    However, just recently I updated the plugin I use to output sample code.  I was previously using a syntax of [codesyntax lang=”php”] and now I needed to change it simply be [code].  This wasn’t that simple since I’ve done hundreds of blogs with several different languages.  In searching I had over 15 distinct differences – hence, regular expressions to the rescue!

    (more…)

  • Mysql Uptime Check Script

    I’ve been pretty vocal recently over my move to Amazon EC2 and switching to LEMP stack and then switch to a LEPP stack.  This has certainly seen some great improvements in speed, traffic, and Google indexing.  Unfortunately as many of you may know who run their own dedicated server, it’s not all white sandy beaches and beautiful sunsets!  When something goes wrong, it’s up to you to fix it.

    So far the only issue that I see keep cropping up is my Mysql database seems to crash every several days.  From what I can see, it appears to be a lack of memory issue – it appears my EC2 server is just shy on a bit of memory.

    At this point, I don’t think it’s worth upgrading; instead I’ve built a simple Mysql uptime script that will ensure it’s always running for me.

    (more…)

  • Renaming a Database in Mysql

    Unfortunately – and probably for good reason – the RENAME DATABASE command had a short life in Mysql for safety precaution reasons.

    After doing some research, it appears that the best approach is as follows as it works well for both MyISAM and InnoDB table types:

    1. Create new database
    2. Perform RENAME TABLE command that includes the table and database from the old one to the newly created database
    3. Repeat once for each table

    In all of the examples I didn’t find an automated script – time to make one I thought!

    (more…)

  • The Best Way to Prevent SQL Injection

    If you use a framework of some sort, you probably haven’t thought about SQL injection for some time – in fact it almost seems dated to even discuss it.  However, security should never be overlooked and it’s important to not trust third party applications and people by default!  So what is the best way to prevent SQL injection?

    Have you noticed how I haven’t specified a specific language?  This is done purposely, because at the end of the day – all languages – should be able to follow this paradigm…

    (more…)