Win a new Mac Book Air laptop

Archive for the ‘MySQL’ Category

UK Towns and Postcode Areas

Tuesday, November 18th, 2008

The other day I needed a list of UK post towns matched up to postcode areas, which I found on wikipedia.

Anyway I have tidied it up and put it into two database tables postcode and town

You can download the SQL file here towns.zip

Hope it comes in useful.

Finding overlapping dates and times in MySQL

Thursday, May 1st, 2008

Quite often you end up in situations where you are required to check and see if one time period overlaps another time period. Probably the most common situation this occurs in is when you are building a booking system - be it for tables or cars or rooms.

You have a series of entries in a database with a start (date)time and an end (date)time and before adding a new record you need to check that it doesn’t overlap with another booking.

The solution is fairly simple but I always end up making a quick diagram on a piece of paper to check.

Time overlapping diagram

As you can see two time periods can either be sequential (i.e. there is no overlap at all) or they can overlap in one of four ways. Put this into a diagram and the solution becomes pretty obvious.

There is an overlap if end_time_1 > start_time_2 AND start_time_1 < end_time_2

Time start_1 start_2 end_1 end_2 end_1 > start_2 start_1 < end_2
1 09:00 08:00 11:00 10:00 true true
2 09:00 10:00 11:00 12:00 true true
3 09:00 08:00 11:00 12:00 true true
4 09:00 09:30 11:00 10:30 true true
5 09:00 07:00 11:00 08:00 false true
6 09:00 12:00 11:00 13:00 true false

An example SQL query would be something like the one below but make sure your datetime formats are correct - in MySQL the default format is YYYY-MM-DD hh:mm:ss

 
SELECT * FROM bookings WHERE room_id = '" . $room_id . "' AND ((date_end > '" . $start_date . "') AND (date_start < '" . $end_date . "')) ";

The above query will pull back the records where the times overlap.

MySQL import / export at the Command Line

Wednesday, March 19th, 2008

Like many (most?) developers when I work with a database I tend to manage them using another programme - usually either phpMyAdmin or Navicat. However there are times when you have to get basic again and dig down to the Command Line (usually because of an access issue like a firewall setting you can’t change or because you need to import a file too big for phpMyAdmin). The trouble is if you only use these commands once in a blue moon you soon forget them.

Dump (export your database)

  1. Login to the shell or Command Prompt, navigate to where you would like your file saved.

  2. mysqldump --user=your_username --password=your_password your_database > your_dump_file.sql

Where:
your_username is a mysql user with the correct rights to the database you want to dump.
your_password is the password for your_username.
your_database is the name of your database.
your_dump_file.sql is the name of the file that will be created in the export.

Import

  1. Create the database that you want to import into, but leave it empty or if you are using an existing database remove the tables (make sure you have a backup first).
  2. Login to the shell or Command Prompt

  3. mysql --user=your_username --password=your_password your_database < your_file_to_import.sql
order clomid viagra online review cialis from canada cheap generic viagra compare cialis prices online buy cheap acomplia buy viagra no rx find discount viagra online cheap accutane online lasix pills drug cialis online purchase order discount viagra order viagra from canada cheap lasix online online propecia viagra information accutane prices cheap generic acomplia levitra generic cialis cost levitra without prescription propecia prescription buy acomplia cheap acomplia prices acomplia cheap discount viagra purchase cialis no rx buy cialis us synthroid buy cheap cialis cheap zithromax cialis drug lowest price lasix clomid online cheap order acomplia online viagra online cheapest soma prices order cialis no rx cialis without a prescription cheap clomid buy viagra without prescription cheap generic accutane buy propecia without prescription cialis free sample propecia pharmacy buy viagra from us zithromax cheap buy levitra generic propecia no rx viagra cialis tablets cialis without prescription generic viagra cheap generic accutane discount cialis online clomid cheap buy discount viagra cheap viagra on internet overnight viagra viagra uk buy viagra from canada buy generic accutane viagra no prescription zithromax pills cheapest synthroid prices discount synthroid where to buy zithromax cheapest zithromax prices buy soma without prescription accutane no prescription cheap levitra tablets find cheap cialis online levitra pills