我还是数据库、规范化等方面的新手,可能需要一些帮助。这是我数据库结构的一部分,我认为我的方法是个坏主意。在这里,我们的国家可以分为不同的省份,所有的城市/城镇都在一个特定的省份和一个省(我猜最接近的外行术语是地区)。所以,如果你在我们国家的任何地方,你都必须拥有那一定的巴郎盖、市/镇和省。我所做的是使用外键来指代表barangay,市/镇,省。这是个坏主意吗?
如果我创建一个tblCustomer_Address来将Country_ID: Int FKProvince_ID: Int FKCityTown_ID: Int FKBaranggay_ID: Int FKtblCustomer分开,这有什么不同?
谢谢!

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
)

*
编辑:顺便说一下,我忘了提。我的项目的一部分是报告生成,所以我的想法是跟踪位置。所以我想为巴郎盖、市/镇、省设立单独的桌子,使每个人都与众不同。

最佳答案

在我看来,一个巴兰将存在于一个城市或一个省,一个城市将存在于一个省,一个省将存在于一个国家。如果结构中有一个位置表,其位置类型为barangay、city、province、rural或country,并且parentid指向层次结构中的父位置,则该结构如何?然后,您的客户有一个指向层次结构中任何位置的位置ID。可以在城市、省(农村)地区或乡村增加新的地点。桌子看起来像这样:

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

无法将此添加为注释,因此下面是一个更完整的实现:
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

关于sql - 这个地址数据库有多严重?,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/11134449/

10-13 02:30