MySQL join for identical values on insert -


given these tables:

create table country (     country_id integer     primary key auto_increment,     name       varchar(16) unique not null ); insert country(name) values     ('usa'),     ('canada');  create table network (     network_id integer primary key auto_increment,     name       varchar(32) not null,     country_id integer references country(country_id)         on cascade update         on delete restrict ); 

i want perform insert network(country_id, name) values name list of values, country_id same each row, result of subquery that's select country_id country name = 'canada'. want in 1 insert, not insert update afterwards. think calls join i'm not sure.

ideas?

insert network     (country_id, name)  select     c.country_id, n.network_name       ( select country_id       country       name = 'canada'     ) c   cross join     ( select 'name1' network_name union       select 'name2' union       select 'name3' union       ...       select 'namen'     ) n ; 

sidekick: not define foreign keys inline in mysql, ignored:

create table network (     network_id integer primary key auto_increment,      --- pk fine, not ignored     name       varchar(32) not null,     country_id integer references country(country_id)   --- fk ignored         on cascade update         on delete restrict ); 

define them after list of columns, separately. prefer define primary key constraints after columns:

create table network (     network_id  integer      not null  auto_increment,     name        varchar(32)  not null,     country_id  integer      null,      primary key (network_id),      foreign key (country_id)                --- fk not ignored       references country (country_id)          on cascade update         on delete restrict ); 

Comments

Popular posts from this blog

java - Play! framework 2.0: How to display multiple image? -

gmail - Is there any documentation for read-only access to the Google Contacts API? -

php - Controller/JToolBar not working in Joomla 2.5 -