Split table into One to many table

I need to create 4 tables from the below 2 tables.

Province
County(FK is Provicne table id)
District(FK is County table id)
City (FK is District table id)


CREATE TABLE
    country
    (
        id BIGINT NOT NULL IDENTITY,
        Province NVARCHAR(255) COLLATE Latin1_General_CI_AS,
        County NVARCHAR(255) COLLATE Latin1_General_CI_AS,
        District NVARCHAR(255) COLLATE Latin1_General_CI_AS,
        City NVARCHAR(255) COLLATE Latin1_General_CI_AS,
        ProvinceID BIGINT
    );

CREATE TABLE
    province
    (
        id INT NOT NULL IDENTITY,
        Province NVARCHAR(255) COLLATE Latin1_General_CI_AS,
        Capital NVARCHAR(255) COLLATE Latin1_General_CI_AS,
        ProvinceCode INT,
        PRIMARY KEY (id)
    ); 

Open in new window

zolfAsked:
Who is Participating?

Improve company productivity with a Business Account.Sign Up

x
 
David ToddConnect With a Mentor Senior DBACommented:
Hi,

From the original county table - rename county to county_old - try this

insert dbo.county( county, provinceID )
select distinct county, provinceID
from dbo.county_old

insert dbo.district( district, countyid )
select distinct co.district, cn.countyid
from dbo.county_old co
inner join dbo.county cn
    on cn.county = co.county

let me know the results ... extending the above for city would be a little too much guessing.

I suggest that instead of
dbo.county
id
county
...
provinceid


you make the table like this
dbo.county
countyid
countyname
...
provinceid

that way, wherever in your code you see provinceid it means provinceid. The way that you are using id in all your tables means that you aren't sure just by looking at the name what it refers to - you'll always have to check to see which table you are looking at/aliased to.

compare to this join with my suggested naming

from dbo.county c
inner join dbo.district d
    on d.countyid = c.countyid

immediately can be seen to make sense and be correct, whereas on this one you aren't sure

from dbo.county c
inner join dbo.province p
    on p.id = c.provinceid

which while correct, is not far from this nonsense

from dbo.county c
inner join dbo.district d
    on d.id = c.countyid

-- WRONG WAY AROUND

HTH
  David
0
 
chaauCommented:
What's your tables' relationship? E.g. county is part of province, city is part of district, etc
0
 
zolfAuthor Commented:
is this query correct. I am trying to compare the province name and county name as there can be duplicate county

UPDATE district
SET district.countyID = county.ID
FROM county INNER JOIN district ON county.county = district.county
INNER JOIN county on county.province = district.province;

Open in new window

0
What Kind of Coding Program is Right for You?

There are many ways to learn to code these days. From coding bootcamps like Flatiron School to online courses to totally free beginner resources. The best way to learn to code depends on many factors, but the most important one is you. See what course is best for you.

 
zolfAuthor Commented:
province is FK in county
county is FK in District
district is FK in city
city
0
 
zolfAuthor Commented:
I had this excel sheet which had all of them together. now I need to use this in my application and I need to separate them in tables
0
 
zolfAuthor Commented:
I created a new table District

CREATE TABLE
    District
    (
        id BIGINT NOT NULL IDENTITY,
        County NVARCHAR(255) COLLATE Latin1_General_CI_AS,
        Province NVARCHAR(255) COLLATE Latin1_General_CI_AS,
        District NVARCHAR(255) COLLATE Latin1_General_CI_AS,
        CountyID BIGINT
    );

Open in new window


and populated it with the id,county,province,district anem using SSIS. now I need to get the id of the county table in this district table.

I am using your reference to the previous question.but I am not sure

UPDATE district
SET district.countyID = county.ID
FROM county INNER JOIN district ON county.county = district.county
INNER JOIN county on county.province = district.province; 

Open in new window

0
 
zolfAuthor Commented:
please help...I have issue that in county I have duplicate rows...so we need to first delete the duplicate rows from the county and then use that for linking other table
0
 
zolfAuthor Commented:
thanks for your help. the county table is not perfect without duplicate.but the district query of your is wrong as I don't have district name in the county table. my main table is county which has all the cols and using that to split them in individual tables
0
 
zolfAuthor Commented:
basically now we have to compare the County.countyname and county.provincename with the district.countyname and district.provincename if they are same then put the countyid from the county table into the district.countyid which is fk
0
 
zolfAuthor Commented:
the county table is not perfect without duplicate

my apologies. I mean now perfect without duplicate
0
 
David ToddSenior DBACommented:
Hi

So where are you up to - what do you need help with next?

Can you post snippets of data that illustrate your problem - helps me tune query.

Regards
  David
0
 
zolfAuthor Commented:
thanks a lot, managed to get it to work with your original comments.
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.