Reply to Re: examples of INNODB & foreign keys

Your name:

Reply:


Posted by JMA on 09/28/50 11:22

Hi Matt,

Maybe the following sample could help you:

-- Create the 3 tables containing information
--
create table table_a (ida int not null primary key) engine=innodb;
create table table_b (idb int not null primary key) engine=innodb;
create table table_c (idc int not null primary key) engine=innodb;

-- Create a relationship table between the 3 'information' tables
--
create table relation_abc
(
ida int not null,
idb int not null,
idc int not null,
constraint pk_abc primary key (ida, idb, idc),
constraint fk_a foreign key (ida) references table_a(ida) on update no
action on delete cascade,
constraint fk_b foreign key (idb) references table_b(idb) on update no
action on delete cascade,
constraint fk_c foreign key (idc) references table_c(idc) on update no
action on delete cascade
)
engine=innodb;

-- Insert sample data
--
insert into table_a values (1);
insert into table_a values (2);
insert into table_a values (3);

insert into table_b values (10);
insert into table_b values (20);
insert into table_b values (30);

insert into table_c values (100);
insert into table_c values (200);
insert into table_c values (300);

insert into relation_abc values (1, 10, 100);
insert into relation_abc values (1, 20, 100);
insert into relation_abc values (2, 20, 200);
insert into relation_abc values (2, 20, 300);
insert into relation_abc values (3, 10, 300);
insert into relation_abc values (3, 20, 300);
insert into relation_abc values (3, 30, 300);

-- After executing the following delete clauses, the relation table is empty
--
delete from table_a where ida=1;
delete from table_b where idb=20;
delete from table_c where idc=300;

I hope this sample show you the advantages of using referential integrity...

Regards

JMA


"matt" <guest@guest.com> a ιcrit dans le message de news:
r6idnWy_VKqI_33fRVn-iQ@comcast.com...
> does anyone know of any good examples of using INNODB tables and foreign
> keys to manage references to other tables. I want to manage removing
> entries and teh references in other tables.
>
> thanks!
>

[Back to original message]


УдалСнная Ρ€Π°Π±ΠΎΡ‚Π° для программистов  •  Как Π·Π°Ρ€Π°Π±ΠΎΡ‚Π°Ρ‚ΡŒ Π½Π° Google AdSense  •  England, UK  •  ΡΡ‚Π°Ρ‚ΡŒΠΈ Π½Π° английском  •  PHP MySQL CMS Apache Oscommerce  •  Online Business Knowledge Base  •  DVD MP3 AVI MP4 players codecs conversion help
Home  •  Search  •  Site Map  •  Set as Homepage  •  Add to Favourites

Copyright © 2005-2006 Powered by Custom PHP Programming

Π‘Π°ΠΉΡ‚ ΠΈΠ·Π³ΠΎΡ‚ΠΎΠ²Π»Π΅Π½ Π² Π‘Ρ‚ΡƒΠ΄ΠΈΠΈ Π’Π°Π»Π΅Π½Ρ‚ΠΈΠ½Π° ΠŸΠ΅Ρ‚Ρ€ΡƒΡ‡Π΅ΠΊΠ°
ΠΈΠ·Π³ΠΎΡ‚ΠΎΠ²Π»Π΅Π½ΠΈΠ΅ ΠΈ ΠΏΠΎΠ΄Π΄Π΅Ρ€ΠΆΠΊΠ° Π²Π΅Π±-сайтов, Ρ€Π°Π·Ρ€Π°Π±ΠΎΡ‚ΠΊΠ° ΠΏΡ€ΠΎΠ³Ρ€Π°ΠΌΠΌΠ½ΠΎΠ³ΠΎ обСспСчСния, поисковая оптимизация