Sample Header Ad - 728x90

Column containing keys from columns of another table

1 vote
1 answer
189 views
I have two csv files, countries and birth_rate_stat with the following data: | ISO | ISO3 | ISO_CODE | FIPS | Display Name | Currency Name | Phone_Code | Region Code | |---------------|--------------|---------------|-------------|------------------|--------------------|------------------|------------------| | AF | AFG | 4 | AF | Afghanistan | Afghani | 93 | Asia | | AX | ALA | 248 | | Aland Islands | Euro | 340 | Europe | | country_code | country_name | year | birth_rate | |-------------------|-----------------|------------|-----------------| | SI | Slovenia | 2036 | 7.59 | | SI | Slovenia | 2022 | 7.52 | and i created two tables CREATE TABLE countries ( iso varchar(2) NOT NULL, iso3 varchar(3) DEFAULT NULL, iso_code int(11) NOT NULL DEFAULT 0, fips varchar(2) DEFAULT UNIQUE, display_name varchar(255) DEFAULT NULL, currency_name varchar(255) DEFAULT NULL, phone_code int(11) DEFAULT NULL, region_code int(11) DEFAULT NULL, PRIMARY KEY (iso) )ENGINE=InnoDB DEFAULT CHARSET=latin1; CREATE TABLE birth_rate_stat ( iso varchar(2) NOT NULL, year int(11) NOT NULL, crude_birth_rate float NOT NULL, PRIMARY KEY (iso,year), CONSTRAINT crude_birth_rate_ibfk_1 FOREIGN KEY (iso) REFERENCES countries (iso) )ENGINE=InnoDB DEFAULT CHARSET=latin1; The problem that i face while trying to connect the birth_rate_stat table with the countries one is that, in the birth_rate_stat table, the column country_code contains entries from both the ISO and the FIPS columns from the table countries. That means I can't set iso as a primary key nor fips as fips contains null values. I tried to create a country_id column which would distinguish the countries but then i get an error Cannot add or update a child row: a foreign key constraint fails which makes sense since it doesn't exist in birth_rate_stat. Do you have some suggestions as to how i could connect the two tables?
Asked by Red Tornado (11 rep)
Apr 17, 2023, 02:59 PM
Last activity: Jun 20, 2025, 06:01 PM