создать триггер для новой таблицы, который генерируется из выбора с учетом трех таблиц - PullRequest
0 голосов
/ 10 февраля 2020

это моя таблица стран

create table country(id int auto_increment primary key, 
country_name varchar(100),
country_code varchar(10), 
provider_country_id varchar(50), 
provider_id int);

это моя таблица состояний

create table state(id int primary key auto_increment, 
state_name varchar(100), 
provider_state_id varchar(50), 
provider_id int, 
country_id int, 
foreign key(country_id) references country(id) on delete set null);

это моя таблица городов

create table city(id int primary key auto_increment, 
city_name varchar(100), 
provider_city_id varchar(50), 
state_id int, 
foreign key(state_id) references state(id) on delete set null, 
country_id int, 
foreign key(country_id) references country(id) on delete set null, 
provider_id int);

я создал новый table как

create table new_table 
select city.id as cityId, city.provider_city_id as providerCityId, city.city_name as cityName, state.state_name as stateName, country.country_name as countryName, country.country_code as countryCode, state.id as stateId, city.state_id as stateIdFromCity, country.id as countryId, state.country_id as countryIdFromState from city city,state state, country country 
where city.state_id = state.id and state.country_id = country.id;

, тогда я также создал индекс как

create index multi_idx on new_table(stateId, stateIdFromCity, countryId, countryIdFromState);

, теперь мне нужно создать триггер для new_table при вставке, удалении, обновлении этих трех таблиц. как это сделать?

1 Ответ

0 голосов
/ 10 февраля 2020

"Чрезмерная нормализация".

Просто есть одна таблица с одной строкой на город. В ней есть страна, штат / провинция, город. Вызовите таблицу Locations или Cities.

. Между тем, country_code должно быть CHAR(2) CHARACTER SET ascii и использовать международный двухбуквенный стандарт.

Или, в некоторых связанных ситуациях, базовый это на почтовый индекс.

Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...