Search code examples
sqldatabasestructurenormalizationstreet-address

How Bad is this Database for Address?


I'm still a novice to databases, normalization, etc, and I might need some help. Enclosed here is a portion of my database structure, and I'm thinking that my approach is a bad idea. Here, our country can be classified into different provinces, and all cities/towns are under a specific province, and a barangay (the closest Layman's term is District, I guess). So if all places, where ever you may be in our country, you must have that certain barangay, city/town, and province. What I did is I used Foreign Keys to refer to tables barangay, city/town, province. Is this a bad idea?

How different is it if I created a tblCustomer_Address to separate Country_ID: Int FK, Province_ID: Int FK, CityTown_ID: Int FK, Baranggay_ID: Int FK from tblCustomer?

Thanks!

tblCustomer(
  Customer_Id: Int PK
  Customer_FName: String
  Customer_MName: String
  Customer_LName: String
  Country_ID: Int FK
  Province_ID: Int FK
  CityTown_ID: Int FK
  Baranggay_ID: Int FK
  Additional_Address_Details: String
 )
tblCountry(
  Country_Id: Int PK
  Country_Name: String
)
tblProvince(
  Province_Id: Int PK
  Province_Name: String
)
tblCityTown(
  CityTown_Id: Int PK
  CityTown_Name: String
)
tblBarangay(
  Barangay_Id: Int PK
  Barangay_Name: String
)

* EDIT: By the way, I forgot to mention. A part of my project is report generation, so I what I had in mind is to keep track of the locations. So I thought of having separate tables for barangays, city/town, provinces, to make each and everyone unique.


Solution

  • Well it seems to me that a barangay will exist in a city or province, a city will exist in a province, and a province will exist in a country. How about a structure where you havea location table with a location type of Barangay, City, Province, Rural or country and a parentID pointing to the parent location in the hierarchy. Then your customer has a location id pointing to anywhere in the hierarchy. New locations can be added as existing in a city, province (for rural) district or country. Table would look like this:

    tblLocation(
    LocationID int PK,
    ParentID int FK references tblLocation LocationID,
    LocationType int FK references tbllocationTypes,
    LocationName
    )
    

    Couldn't add this as a comment, so here is a more full implementation:

    CREATE TABLE LocationType
    (
        LocationTypeID int not null primary key,
        LocationTypeName varchar(20) not null unique
    )
    GO
    CREATE TABLE Location (
        LocationID int not null primary key,
        ParentId int null references Location (LocationID),
        LocationName varchar(100),
        LocationTypeID int not null references LocationType (LocationTypeID)    
    )
    GO
    CREATE Table Customer (
        CustomerID int not null primary key,
        FirstName varchar(50),
        MiddleName varchar(50),
        LastName varchar(50),
        LocationID int references Location (LocationID)
    )
    GO
    CREATE TABLE City(  
        CityID int not null primary key references Location (LocationID),
        PostCode varchar(20) not null
    )
    GO
    CREATE VIEW DetailedLocation AS 
        SELECT L.*, C.PostCode FROM Location AS L
        LEFT OUTER JOIN City AS C
        ON C.CityID = L.LocationID