lazy saunter

(personal learnings in computation)

Saturday, February 09, 2008

a note on databases

this past month made 5 years since i began learning about computers- that is, i first enrolled in undergraduate computer science classes. this means i have had time to learn and forget and relearn a lot of stuff already. redundant notes on issues that i wish were programmed into my brain.

in a graduate school database class we studied the normalization forms of relations- how to create DB tables that were sound and consistent with one another. for example, consider a "cars" table containing cars and owner IDs and an "owners" table containing owner IDs and owner names. each car must belong to an owner. in the "cars" table, the owner ID must refer to a real owner so that owner must be an entry in the "owners" table. therefore, inserting a car into the "cars" table with an owner ID that is not in the "owners" table should fail.

in grad school we used the open-source Postgresql database which handled this case fine. for some reason, MySQL databases are more popular and seem to be the default choice of database for most small programmers. so at my job, i began using mysql databases for all the applications i create only to realize somewhere along the way that it would allow insertion of a car with no owner despite the fact that when i created the table i had the references set up correctly.

not only did this baffle me for some time, it also made my believe that mysql was not suitable for some larger applications that i had planned.

however, it was not until the other day when an occassion arose which forced me to get to the bottom of this: one particularly large application that will soon be deployed contains test data that needs to be deleted. there are about 20 tables, 19 of which contains foreign keys that refer back to the primary key in the first table. i want to be able to delete the entry from the first table and have mysql delete all the records from all the other tables that refer back to this key. as it is, this is not going to work since an "ON DELETE CASCADE" in each of the referring tables are not actually referring back to the first table's key.

here is the problem and solution: a default mysql table creation uses the "MyISAM" engine which does not support referential integrity- why it does not i do not know. what kind of database would not support this? it doesnt do any of the great things that databases are supposed to do. when creating the database table, you must specify that it use the "InnoDB" engine which does support references and therefore would not allow entering a car with no owner and would allow the deletes to cascade. fortunately, the literature implies that it is easy to convert over so this is what i will be doing with all my DB tables from here on out most likely.

2 Comments:

  • At 5:24 PM , Blogger POPPA said...

    I really, really enjoy this blog! Keep up the good work. I especially enjoyed this one. I thought all true databases had referential integrity... this is what I was taught in the (very) short course I took once. Thanks for the exposition!

     
  • At 7:39 AM , Blogger huitzilopochtli said...

    thanks(!)

    i am glad that someone understands this one because it seems so misleading to beginning programmers to offer a database which does not allow referencing one table to another by default and instead only by explicitly setting up the tables to do so.

     

Post a Comment

Subscribe to Post Comments [Atom]

<< Home