Enforce integrity in PHP and MySQL

Enforce integrity in PHP and MySQL

am 07.05.2006 14:10:13 von Rik

Is it possible to enforce intergrity in linked tables in MySQL?
I'm not sure I've got the english wording right:

What is mean:
I've got:

table1.
id
field1
field2
etc...

table2
id
ref = table1.id
field1
field2
etc...

Now if I want to add a row to table2, ref must exists in table1.id. Is there
a MySQL setting for this I cannot see, or do have to create custom code to
check this?

Similarly, if I delete a row from table1, I want all rows from table2
deleted where ref = table1.id

I could offcourse create a table 'relations', containing:
id (autoincrement int)
table1 (varchar)
field1 (varchar)
relationship (enum('one-to-one','one-to-many'))
table2 (varchar)
field2 (varchar)
delete (int(1) (boolean, wether to delete related records))

And route all queries through a custom made database-object that checks the
table "relations" on every query and acts accordingly.

A MySQL setting that is set in the database (doesn't need to be called in
PHP script) would be preferred, but I can't find one.

Grtz,
--
Rik Wasmus

Re: Enforce integrity in PHP and MySQL

am 07.05.2006 15:37:52 von Cruella DeVille

You should use the InnoDB storage engine,
http://dev.mysql.com/doc/refman/5.0/en/innodb-foreign-key-co nstraints.html

Re: Enforce integrity in PHP and MySQL

am 07.05.2006 15:49:49 von Rik

Cruella DeVille wrote:
> You should use the InnoDB storage engine,
> http://dev.mysql.com/doc/refman/5.0/en/innodb-foreign-key-co nstraints.html

Thanks, exactly what I was searching, I must confess I have never looked
into the exact differenced between storage engines in MySQL

Grtz,
--
Rik Wasmus