I am trying to understand this concept. For example: I have two tables City and Country.
Country
-------
id
abbreviation
name
City
-----
id
name
Country (name or id, or both? - This is the question)
To reference and keep a particular city in sync with the country it belongs to I guess this will be reference to country.id as a FK. This means an example of the city table will be: (200, New York, 19) - where 19 = USA in country table. But this doesn't help a person viewing the table because he wont know what 19 is without looking up in country table what 19 is.
So I want to add the country name also to city table so it reads: (200, New York, USA). I don't need the 19 to display because 19 is of no use to the reader but is only used in back to connect the tables.
So what should my tables colunms / FK look like to i can store in city table rows like this (200, New York, USA), yet ensure New york will always reference to USA in the USA lookup and keep the 19 which is the primary key for USA out of the city table so the tables look clean and easy to understand? And I assume if these are referenced, tomorrow if i update USA to be 20, it will update in the city table on its own, and same way if I rename USA to US it will update on city table on its own?
My DB is in MySQL
Why not use ISO 3166 country codes (2 character or 3 character) as the country ID? This leaves you with recognizable codes in the city table; you can map to the full name in the country table.
As for viewing the data, use a VIEW to create a good looking table:
CREATE VIEW CityInfo(CityID, CityName, CountryID, CountryName) AS
SELECT ci.id, ci.name, ci.country, co.name
FROM City AS ci JOIN Country AS co ON ci.Country = co.id;
You don't ... if you need to have "usable" tables in the DB, so someone can easily view something useful with select * etc. (or to make programing the SQL by hand easier). Then you create the tables as above, in normal form, and then create a VIEW which combines the tables.
Tables aren't supposed to be viewed by people: some application should be accessing the tables to present data to people in a way they can parse it. The reason you want to have country.id as a FK in your table is so that you don't have a million rows whose country name is "USA", because then all kinds of problems can occur, like what happens when you mistype and "US A" lands in one of the fields? Or what if you want to change what the user sees from "USA" to "United States"?
The right way to handle it is to use the country.id as you initially suggest, and then use a JOIN statement to present the data, like this:
SELECT city.name, country.name
FROM city JOIN country ON country.id = city.country
My syntax could be off, but that's in essence what you want.
Related
I need to create database which will contain the following entities in country:
Regions
Municipalities
Now comes the hard part for me:
Municipality can also be city, where city can (but not always) have its own municipalities.
Municipality can have several cities.
Municipality in both cases contains several settlements
I wanted to create several tables, for example:
regions (region_id, region_name)
municipalities (municipality_id, municipality_name, region_id).
cities (city_id, city_name, region_id)
city_municipalities (city_municipality_id, city_municipality_name, city_id)
settlements (settlement_id, settlement_name, municipality_id, city_municipality_id)
i think that here i make problem. There would be a lot of NULLs for municipality_id or city_municipality_id, since i cant have both field filled.
But right now it comes to my mind to have two tables organized like this:
geo_entity(geo_id, geo_name, geo_type_id, parent)
geo_entities_type (geo_type_id, geo_type_name)
this table will contain definitions of what geo_entity entry is, like:
1 Region
2 Municipality
3 City
4 City Municipality
5 Settlement
What should i stick to? Do you have a better approach and which one?
I have a strange problem. I got some data for cities, regions and countries in CSV format and imported them into MySQL tables.
I have 3 tables and their fields
1. City : id, name, country_code, region_number
2. Region : region_number, country_code, name
3. Country : country_code, name
Now things get a little complicated, as I added an auto-generated id column to the region table, so the region x for country y would be unique.
The thing is: Now i am trying to update city field region_number to hold this unique value (the new id column in region) so I can have relations city->region.
The relation region->country or country->region is OK.
Is it possible to write an update query that would update city region_code (or fill some new column, eg. region_id) with correct values?
If not an query, what could I use to get the correct values into the cities table?
I have arround 3 million records!
If I understant correctly, I think you are looking for something like this:
UPDATE
City inner join Region
on City.country_code = Region.country_code
and City.region_number = Region.region_number
SET
City.new_column = Region.id
However, since there's a relation already between City and Region, I am not sure this is the right thing to do, since it will make the table not normalized.
Now i am trying to update city field region_number to not hold this unique value
The only way you can do this is if the region_number uniquely identifies each region - and if that's already the case then you are wasting your time by creating redundant references. Although frankly, if these really are your table structures, there's no reason for using surrogate keys. And if there's no reason for using surrogate keys then the region and country table are redundant.
I am working on a little searching stuffs, the problem is that I they can choose multiple options and I want my query to be created depending of them.
Please if anybody could help me I will be so glad.
My main table of search is this:
id
name
card_id
phone_numbre
country_id
futurecountry_id
The ID is unique, the name a text, the card_id and phone_number are just ok, I can find them on my table, but the problem is when I need to go to pick up the data to the other tables, specially the country_id goes to a table named "country" where I comprare the id's to check if are the same I bring the name of the country, is the same table where I consult for get the country_id and futurecountry_id and I really have no idea what to do.
Does anybody have any idea?
p.s. Well, maybe I couldn't explain myself very good, my problem is this:
I have a form where you filled it with some data and brings you the personal information and instead the ids of the field origencountry and destinycountry I need the names, for example, if I send origencountry=10 and destinycountry=8 I will do this:
select *
from infotable,countrytable
where infotable.iddestinycountry = countrytable.id and infotable = 10
AND infotable.origencountry = countrytable.id and infotable = 8
The query seems to work fine, but it doesn't output any data.
At my table countries I have for example:
id name
1 usa
2 uk
3 spain
and at info
id name origencountry destinycountry
1 john 1 2
You can user LEFT JOIN or RIGHT JOIN.
For example :
SELECT c.country_id from country as c
LEFT JOIN futurecountry as fc ON c.country_id = fc.futurecountry_id
In this example, you return only results on country with country and futurecountry_id keys
For more informations, you can see here : http://dev.mysql.com/doc/refman/5.0/fr/join.html
i have 2 tables. The city tables is not normalized because the country information is in plain text. I have added the id_country to the 'city' table (that column is empty).
I need to check for matches between city>country and country>country and then update the city records that matched with the id_country from the country table. At the end i will be able to delete the 'country' column from the city table.
City table
id_city (1, 2, 3...)
city (Washington, Guayaquil, Bonn...)
country (Germany, Ecuador, USA...)
id_country (currently empty)
Country table
id_country (1, 2, 3...)
code (GE, EC, US...)
country (Germany, Ecuador, USA...)
I have no idea on where to start and if it can be done with a SQL query. My original idea was to search for matches in a php loop but that seems to be a really harder implementation.
You can do this with a JOIN on an UPDATE statement.
UPDATE city c1 INNER JOIN country c2 ON c1.country=c2.country
SET c1.id_country=c2.id_country;
Using an INNER JOIN will make sure that updates only occur for cities that have a matching country value.
Once you've run it, you'll be able to select all those cities that still have a null id_country just in case some of them didn't match. Conversely, once you've determined that all your cities have an id_country, you can delete that column from the city table.
The city tables is not normalized because the country information is
in plain text.
Nonsense. Normalization doesn't mean "replace plain text with id numbers". Find whoever taught you that and poke him in the eye with a sharp stick.
Your real problem is that "city" plus "country" isn't sufficient to identify cities, at least in the USA. I think there are at least a dozen different cities named "Washington" in the USA.
Instead of replacing the country name with an id number, you'd be far better off replacing it with the two-letter country code. The codes are human-readable; the id numbers will require an additional JOIN in every query that uses your table of cities.
Something like this should work:
UPDATE city set id_country = (SELECT country.id_country from country WHERE country.country = city.country)
I have a database of U.S. Congressional information whereby I have FirstName, MiddleName, LastName, District, State, Party, etc. of each Congressman in the 112th Session of the U.S. House of Representatives and I want to add Committee information for each (i.e. what committee(s) they are on). I have a large table (called StateCommittee) of District | State | Committee as such:
Financial Services 5 NY
Foreign Affairs 5 NY
Judiciary 24 FL
Science, Space, and Technology 24 FL
Appropriations 4 AL
that marks which committee is assigned to which district. I also made a field in my Representatives table called Committees, which is a multiple-value lookup field to another table I have called List of Committees. Ideally, I would like to create an Update query that generally "fills in" [Representatives].Committees.Value (the aforementioned multiple value field) with the appropriate Committees (ideally from the List of Committees table) when the two have identical district and state. So, ideally, it would look like this in pseudocode:
Update [Representatives].Committee.Value
SET [Representatives].Committee.Value = [StateCommittee].Committee
WHERE [Representatives].District = [StateCommittee].District And [Representatives].State = [StateCommittee].State
However, when I put into the Update query exactly the above code, Access produces the following error:
syntax operation error ".
Does anyone have any ideas as to what I am doing wrong or if what I want to do is not actually possible? Thanks so much!
It looks to me like you are you using a 1-to-many join between the Representatives table and the State Committee table (as many representatives will be on multiple committees). You can not update the any of the 1-table records on a 1-to-many join. That is pretty standard SQL.