I have the following tables:
create table countries (
country_id tinyint unsigned,
iso_alpha3_code varchar(4)
);
create table provinces (
province_id bigint unsigned,
province_name varchar(50),
country_id tinyint unsigned,
foreign key (country_id) references countries(country_id) on delete restrict on update cascade
);
For inserting records into provinces, the corresponding country_id is needed:
insert into provinces (province_name, country_id) VALUES
('Alabama', (select country_id from countries where iso_alpha3_code = 'USA')),
('California', (select country_id from countries where iso_alpha3_code = 'USA')),
('Alaska', (select country_id from countries where iso_alpha3_code = 'USA'));
How to insert these records without repeated-select statements to get the foreign key? Can something like CTE be used?
PS: I found this, but it's for postgreSQL.