Three customer addresses in one table or in separate tables?

前端 未结 7 2342
慢半拍i
慢半拍i 2021-02-07 17:43

In my application I have a Customer class and an Address class. The Customer class has three instances of the Address class:

7条回答
  •  萌比男神i
    2021-02-07 18:25

    If you are 100% certain that a customer will only ever have the 3 addresses you described then this is OK:

    CREATE TABLE Customer
    (
        ID int not null IDENTITY(1,1) PRIMARY KEY,
        Name varchar(60) not null,
        customerAddress int not null
            CONSTRAINT FK_Address1_AddressID FOREIGN KEY References Address(ID),
        deliveryAddress int null
                CONSTRAINT FK_Address2_AddressID FOREIGN KEY References Address(ID),
        invoiceAddress int null
                CONSTRAINT FK_Address3_AddressID FOREIGN KEY References Address(ID),
        -- etc
    )
    
    CREATE TABLE Address
    (
        ID int not null IDENTITY(1,1) PRIMARY KEY,
        Street varchar(120) not null
        -- etc
    )
    

    Otherwise I would model like this:

    CREATE TABLE Customer
    (
        ID int not null IDENTITY(1,1) PRIMARY KEY,
        Name varchar(60) not null
        -- etc
    )
    
    CREATE TABLE Address
    (
        ID int not null IDENTITY(1,1) PRIMARY KEY,
        CustomerID int not null
            CONSTRAINT FK_Customer_CustomerID FOREIGN KEY References Customer(ID),
        Street varchar(120) not null,
        AddressType int not null 
        -- etc
    )
    

提交回复
热议问题