Hi,
I am trying to create subtables 'router' and 'switch' under my supertable 'Products'. Could anyone tell me how to go about doing this either in phpMyAdmin or using sql. Thanks.
Azaar
Update: I'm using MySQL
| SitePoint Sponsor |


Hi,
I am trying to create subtables 'router' and 'switch' under my supertable 'Products'. Could anyone tell me how to go about doing this either in phpMyAdmin or using sql. Thanks.
Azaar
Update: I'm using MySQL


Code:create table products ( id integer not null primary key auto_increment , name varchar(99) not null , type varchar(9) not null ); insert into products ( name, type ) values ( 'foo', 'router' ); insert into products ( name, type ) values ( 'bar', 'router' ); insert into products ( name, type ) values ( 'qux', 'switch' ); create table routers ( productid integer not null primary key , foreign key ( productid ) references products ( id ) , sockets tinyint , evaluation varchar(24) ); insert into routers values ( 1, 11, 'fabulous' ); insert into routers values ( 2, 15, 'fantastic' ); create table switches ( productid integer not null primary key , foreign key ( productid ) references products ( id ) , wires tinyint , speed integer ); insert into switches values ( 3, 5, 1000 );


Thank you very much r937.
Am i right in thinking that if I INSERT a product into table 'routers' the relevant fields will be added to the 'products' table ?
I was lead to believe I need to use UNDER when creating the subtables.
Thanks
Azaar


no, you have to add it to products table first, and then add it to routersOriginally Posted by Azaar
i've never heard of this -- do you have a reference?I was lead to believe I need to use UNDER when creating the subtables.


Is there anyway of making the addition to the products table automatic?no, you have to add it to products table first, and then add it to routers
Couple of places, including herei've never heard of this -- do you have a reference?


only in a stored procedureOriginally Posted by Azaar
remember, the parent table row must be inserted first, before the child table row can refer to it
as for the UNDER thingie, that won't work in mysql


I'm afraid I don't know what a stored procedure is.only in a stored procedure


OK, I've answered my own question... http://dev.mysql.com/doc/mysql/en/st...rocedures.html
Thanks very much for your help and explanation.
Azaar


rudy, would you recommend I use a different type of database if I want to do this? PostgreSQL ?


i'm sorry, i don't understand, i thought you said you were using mysql
do you mean, you would actually switch databases so that you could have stored procedures so that you could generate a parent row insert ahead of a child row insert?
why not just insert the parent row first and then the child row?
maybe i don't undertstand


I was intending to use MySQL but as development is yet to begin I am able to consider other options. I was wondering if you could recommend the best course of action.
Thanks
Azaar


sure, i'd be happy toOriginally Posted by Azaar
use mysql
insert the products row first, then insert the row in whichever product type subtable second
these will be two consecutive INSERT statements issued by your application code
![]()


*chuckle*
Thanks for your assistance.
Azaar
Bookmarks