Archive for November, 2006

SQL Magic – Automatically Creating Lookup Tables

Tuesday, November 28th, 2006

I recently had to write a script that would import large amounts of data (~50MB) into a PostgreSQL database every night. I thought long and hard about how this could be accomplished best, I read a lot about it on the usenet and so I decided that the best way would be to use PostgreSQL’s COPY function. The data is available in a .CSV format so everything worked very smoothly.

However after the data is imported it also shall be searchable very easily. Therefor I let the import script create indexes on the data. And as I like to organize and normalize data I thought about a way to automatically creat lookup tables. The following picture describes what I did:

create_lookup_table

This describes the steps necessary to create the lookup table:

  1. We try to delete an probably already existing lookup table.

    DROP TABLE #LOOKUP_TABLE#;

    Delete the table #LOOKUP_TABLE# if it exists.

  2. We create a new lookup table.

    CREATE TABLE #LOOKUP_TABLE#
    ( id SERIAL NOT NULL PRIMARY KEY,
    #LOOKUP_COLUMN# #LOOKUP_COLUMN_TYPE#);

    Create the table #LOOKUP_TABLE# with one column called “id” which is the primary key and a second column called #LOOKUP_COLUMN# which is of the type #LOOKUP_COLUMN_TYPE#.

  3. We fill the lookup table with the values from the large data table.

    INSERT INTO #LOOKUP_TABLE# #LOOKUP_COLUMN#
    (SELECT DISTINCT #DATA_TABLE_COLUMN# FROM #DATA_TABLE#);

    Insert the following data into the table #LOOKUP_TABLE#: every distinct piece of information in the column #DATA_TABLE_COLUMN# from the table #DATA_TABLE#.

  4. We add a column to the large data table that will hold the reference key to the lookup table.

    ALTER TABLE #DATA_TABLE# ADD COLUMN #LOOKUP_COLUMN#_id INTEGER;

    Add an INTEGER column to the #DATA_TABLE# and call it #LOOKUP_TABLE#_id.

  5. We fill that column in the large data table with the appropriate key.

    UPDATE ONLY #DATA_TABLE#
    SET #LOOKUP_COLUMN#_id = #TARGET_TABLE#.id
    FROM #TARGET_TABLE#
    WHERE #DATA_TABLE#.#DATA_TABLE_COLUMN# = #LOOKUP_TABLE#.#LOOKUP_COLUMN”;

    Now comes the cool part: We update the #LOOKUP_COLUMN#_id column in the #DATA_TABLE# table. We write the the id into that colum whereever the #DATA_TABLE_COLUMN# from the #DATA_TABLE# matches the #LOOKUP_TABLE_COLUMN# from the #LOOKUP_TABLE#.

  6. We create a foreign key on that referencing column.

    ADD CONSTRAINT #DATA_TABLE# #LOOKUP_COLUMN#_id_fk FOREIGN KEY (#LOOKUP_COLUMN#_id) REFERENCES #LOOKUP_TABLE#(id) MATCH FULL;

    Here we simply add the foreign key #LOOKUP_COLUMN#_id_fk to the #DATA_TABLE# on the #LOOKUP_COLUMN#_id column.

  7. We delete the old colum that holds the original values.

    ALTER TABLE #DATA_TABLE# DROP COLUMN #DATA_TABLE_COLUMN# CASCADE;

    Here we drop the #DATA_TABLE_COLUMN# in the #DATA_TABLE#.

I will probably post a PHP function that does all the above on phpclasses.org in a few days.

Break up songs

Saturday, November 25th, 2006

This is probably the worst post to this blog so far so stand ready for this. It’s going to be very personal (obviously) and very pathetic: a list of break up songs. No, I don’t intend to copy the stupid “High Fidelity” flick (actually it’s not that bad – I just don’t like it). But I want to get that list out in the world. And there’s something for you in it too. The first song can be downloaded for free. (more…)

Fusion – Inertial Electrostatic Confinement

Tuesday, November 21st, 2006

“What’s that?” you might ask. Well I did not know either a few days ago. Basically it’s a method to produce fusion.
And what exactly is fusion anyway? Well it’s the oposite of fission: “Nuclear fusion is the process by which multiple nuclei join together to form a heavier nucleus.” And why is this so interesting? “It is accompanied by the release or absorption of energy depending on the masses of the nuclei involved.” (from Wikipedia: Nuclear Fusion) That’s why! (more…)

Old and homebrew CPUs

Wednesday, November 15th, 2006

Slashdot recently had an item about Intel’s release of schematics for its 4004 microprocessor. From the article:

“Intel is celebrating the 35th anniversary of the Intel 4004, their very first microprocessor, by releasing the chip’s schematics, maskworks, and users manual. This historic revelation was championed by Tim McNerney, who designed the Intel Museum’s newest interactive exhibit. Opening on November 15th, the exhibit will feature a fully functional, 130x scale replica of the 4004 microprocessor running the very first software written for the 4004.”

(more…)

First Browsergame Prototype

Friday, November 10th, 2006

My first browsergame prototype is out!

It’s a very very basic graphical user interface prototype that does not do much so far. It was created to start learning the technology and to determine what can be done and what can’t.
The JavaScript inside the prototype is probably very ugly and you can’t do anything more than scroll the map. There’s still lots of room for improvement both usability and featurewise. But as I am a little proud of it I’m showing it anyway, hoping somebody will leave comments or suggestions.

The map is taken from Starcraft (an ingenious realtime strategy game created by Blizzard Entertainment) and is called Lost Temple. It’s very famous among online players.

If you have any suggestions or comments feel free to send an email to chairman at soa-world.de. Well, there’s nothing more to say, just:

go ahead and start the prototype.

To scroll the map you may either touch one of the eight green arrows or move the mouse to an edge of the map.

Plotting measured data with gnuplot

Wednesday, November 8th, 2006

In conjunction with a physics lecture I am attending this semester a fellow student and I had to create a measurement report of an experiment we did. Working on that report I realized that I had never before felt the need for plotting measured data with a computer. Therefore I naturally did not know what freely available tool could help me with that task. Another fellow student then pointet out gnuplot to me and I gave it a try. And what can I say. I was able to create the desired plot very fast and it was pretty too.

About gnuplot:
Gnuplot is a portable command-line driven interactive data and function plotting utility for UNIX, IBM OS/2, MS Windows, DOS, Macintosh, VMS, Atari and many other platforms.”

One of our final results looked like this:
tiefpass
This illustrates of course only the very basic features of the tool. It’s much more powerfull than our example shows.

Some usefull commands I used to customize the plot:
As I wanted a double logarithmic scale I typed
set logscale xy
I also wanted to see the scale in my plot so I typed
set grid xtics mxtics ytics mytics
I set the lables of the axes with
set xlabel “x name” and set ylabel “y name”
(btw this labeling can also be enhanced with LaTeX style formula expressions)
and I defined the axes’ ranges with
set xrange [1:100] and set yrange [1:100]
the smoothing (interpolation) of the curve can be accomplished by specifying
smooth csplines or smooth bezier
within the plot command.

Med Videos

Thursday, November 2nd, 2006

I am tracking popular Med Blogs for some time now (my favourite being medgadget.com) and came accross several cool videos which I think everyone should take a look at.

First there’s a video that shows the da Vinci Robot Surgery System in action. Very impressive and interesting indeed (and also a bit disgusting).

The second and third videos are clips from a british documentary on autistic savants. (more…)