Link to home
Start Free TrialLog in
Avatar of Delacourt
DelacourtFlag for South Africa

asked on

function / object to update and delete table entries

Short question:

Is there a way to build an object and or function / function list (OR ANY EASIER WAY) that takes the trouble out of writing update and delete scripts for all your different tables as this is especially time consuming when you have a relational data model and entries in other tables may rely on what you are about to change or delete !
Avatar of Delacourt
Delacourt
Flag of South Africa image

ASKER

database would be mysql !!!
i.e. having ten tables, where table 1 relates to table 2, table 2 relates to table 3, table 3 relates to table 4, and so forth (where the table with the lower numbers id is in the next table as a foreign key)

problem, how would i be able to get a message about table10 having something to do with one of the tables with a lower number, before i go ahead and update and delete it, thereby losing what was in table10 ?

is my explanation working here ... ? , pls let me know :)

table1
table2
table3
table4
table5
table6
table7
table8
table9
table10
Avatar of Gitcho
Gitcho

Are you talking about getting the ID of an SQL insert statement, so you can update the next table with the new ID ? ( mysql_insert(); )

http://ca3.php.net/manual/en/function.mysql-insert-id.php
The database can handle all those things if you have support for InnoDB-Tables in Mysql. This is the fastest and error-less way to do it

See http://dev.mysql.com/doc/mysql/en/CREATE_TABLE.html and there the part about referencial integrety

example:
CREATE TABLE tab1 (PK int not null auto_increment, Disabled char(1) default 'N' not null,Host varchar (20) null,Id varchar (20) not null,lastlogin int null,Password varchar (40) null, primary key( PK )) TYPE = InnoDB;
CREATE TABLE tab2 (PK int not null auto_increment, FK_tab1 int null,Type char(1) null, INDEX (FK_tab1) , FOREIGN KEY (FK_tab1) REFERENCES tab1(PK) ON DELETE cascade ON UPDATE cascade, primary key( PK )) TYPE = InnoDB;
Thanks Guys,

To answer your question Gitcho: nope, I think I would use isset() for that (methinks:)
Looking more at the edit / delete process

Hernst42: Thanks for that, I dont have innoDB tables, and not going to be using them, however, is there another way, other than the tedious coding of all edit delete statements per table, to keep referential integrity of my tables entries without using innoDB ?
tabletype: myisam
ASKER CERTIFIED SOLUTION
Avatar of hernst42
hernst42
Flag of Germany image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
thanks for that, i am going to try and write this using your above suggestions, but was also thinking along the lines of

choosing something to delete or edit, then this would be passed to a page before going ahead with the execution of the code: essentially something like this:

when deleting from table 4:
having a select table10_field_name from table10, table9, table8, table7, table6, table5, table4
where table4.table4id = table5.table4id
and table5.table5id = table6.table5id
and table7.table7id = table8.table7id
and table9.table9id = table10.table9id

1. then if there is a field name, say, you must delete result[table10_field_name] first
2. or say, you are free to delete this item

BUT
what happens one level up, say there is nothing in table10, what about table9 ???

thats what i am getting at: let me know if you think this is feasible at all !

i meant in second last line, what happens if there is something in table9 that requires referential integrity

also, how would i go about deleting something from table2 in the same way, would it need to be hardcoded, or could i write some type of function ??

Thanks again for all your help Hernst42

I've never seen a DB schema that required that much logic for deletes.   Are you sure your DB is in 3rd normal ? Unless this is a supremely complicated app, there's not normally that much logic required.  Hard to know though, as not much data provided.
Gitcho, just left a comment for you on another question of mine, I have some points for oyu, let me know where to put them up !

Basically, my tables are like this: each with a foreign key from the table above (product owned by suburb - thereby also by country)
also, each table before product also has their own images table for a pic gallery, so, how would i got about it !!! :)

country_table
province_table
city_table
suburb_table
product_table

another thing: each with a pic gallery table (i.e. city_pic_table) would it be better to have one, with Foreign Keys from each of the tables that would require images ?
Awesome ! Thanks Delacourt - I only need a few more points to get my membership !  

Regarding this database, which tables have 1 -> 1 relationships ? 1 -> many ? many -> 1  ?

Depending on your business rules, instead of tying all the foreign keys together in the same table, you may want to set up a lookup table with all your foreign keys that ties these tables together.

Regarding the images table :  " another thing: each with a pic gallery table (i.e. city_pic_table) would it be better to have one, with Foreign Keys from each of the tables that would require images "  ... I would agree.  
I think you are looking for triggers. They are not yet implemented in mysql but they will be:
http://dev.mysql.com/doc/mysql/en/ANSI_diff_Triggers.html

With triggers, you can define functions (triggers) that are stored somewhere in the database and they are automatically called. I don't know the syntax but it would be something like

TRIGGER mytrigger: ON DELETE FROM table1 DO DELETE FROM table2 WHERE table2.table1_id=table1.id
Then when you execute "DELETE FROM table1 where id=2" entries in the other tables will automatically be deleted too.

You'll still have to write 10 triggers (well maybe they can be put in 1 statement too), but then you can delete from everywhere while deleting only entries from table1.

In Oracle you can define triggers.