Useful SQL Commands

I recently needed to take a database with separate date and time columns, convert that combined date-time data into Unix time format (i.e. seconds since the epoch [1st Jan 1970]), and add a new column to the database with that data. It took me a fair bit of googling to get it all working, so here’s my notes.

Sorry this isn’t really in an article format – but I need this written down and available for the future… Also, I wrote a small Java app to read the separate date and time text files and spit out a timezone-correct unixtime text file, which I then added to a spreadsheet containing just the the primary key rows and exported it as tab-separated values suitable for MySQL (or really, MariaDB in this case) import.

Show the table structure for the ‘accidents’ table

Count unique values in the UNIX_TIME column in the accidents table

Find duplicate values in a column (cannot make column a primary key if there are dupes!)

Make a column be the primary key for a table

Drop the UNIX_TIME column

Add a new column UNIX_TIME to the accidents table after the last column (STAT_DIV_NAME)

Add an empty column with no default value

Create a temporary table in memory with a single column called UNIX_TIME which is of type BIGINT

Import from local CSV file

I created a tab-separated CSV file which had just the ACCIDENT_NO as primary key and the UNIX_TIME field. We can then import that data into the temptable with:

Load all the single-line data values fromthe UNIX_TIME.txt file into the UNIX_TIME column of our temp table

Merge the temptable with just the ACCIDENT_NO and UNIX_TIME into the accidents table using ACCIDENT_NO as the primary key

So in this particular instance where I had a partial update, I was missing 13,375 UNIX_TIME values!

Make sure this worked by setting all the UNIX_TIME values in the accidents table to zero, and then re-importing

Now finally, we can drop the in-memory temptable!

Export the database

To export the database we can use the mysqldump utility (this is NOT within the MySQL command line!):

Then to import this dump, let’s say we don’t have a govhack2015 database

Followed by (in the MySQL command line):

It all works! =D

How to stop a MySQL action which is taking forever… (like when I bodge a merge) – in a different MySQL commandline interface

How To: Force Word 2003 to Repeat Table Headings Across Pages

Occasionally Word 2003 will refuse to play ball when you ask it to repeat table headings across pages, that is, each new page that a table’s on gets the table heading so the reader knows what’s what in the columns. But sometimes no matter what actions you perform that should do the trick – it just doesn’t happen. Here’s how to skull-fuck force it into doing what you’re damned well telling it to do:

  • Tried selecting the top row, then Right click | Table Properties | Row tab followed by checking the box labled Repeat as header row at the top of each page? Not having it?
  • Tried selecting Text Wrapping to None from the Table tab of Table Properties? No dice?
  • Tried un-marking the Properties | Repeat as header box then hitting [OK] then getting the table properties up again and re-selecting the box followed by [OK]? Still being a dick?
  • Then click somewhere in the table and go to Table | Heading Rows Repeat in the menu. Sorted!