Home > Programming > #PHPUK2010 Part 2 (MySQL stuff)

#PHPUK2010 Part 2 (MySQL stuff)

February 26th, 2010 Wade Leave a comment Go to comments

Just picked up a nice tid-bit on creating a unique index on a two column table where the values in each column may be either way around but you only ever want one instance of the value in that row. So what this means is, inserting 2,1 and 1,2 for example would result in only the first of the two inserts succeeding.

CREATE UNIQUE INDEX ON tablename (LEAST(col1,col2), GREATEST(col1,col2));

Also, WITH, I’ll be honest, never thought about using it to create temporary views. This is a bad example but shows the structure rather well:

WITH tempView (a,b) AS (
SELECT table1.col1, table2.col2
FROM table1
LEFT JOIN table2
ON table1.id=table2.id
)
SELECT a,b FROM tempView;

Better yet is changing this to WITH RECURSIVE tempView and then adding in a select inside the WITH that recalls tempView. The great example he gave is for getting flights from A to B with a varying  amount of stops, it would be possible to get all routes from A to B with one MySQL query, as long as the data stored all connecting routes.

Incidentally, while there is some great stuff coming out of this RDBMS talk, I think the queries are really hurting a lot of people’s heads. Good stuff though.


Related posts:

  1. MySQL – Binary(16) and scalability
  2. MySQL and Binary(16) – The Reasons/Benefits/Drawbacks (#mysql)
  3. #PHPUK2010 Part 1
  4. Regex-fu #PHPUK2010
  5. Reset MySQL root password if you forgot it #mysql

Categories: Programming Tags:
  1. No comments yet.
  1. No trackbacks yet.