简体   繁体   中英

sql server relationships between 3 tables

I have these tables I want to create for a Grooming Shop:

CREATE TABLE Pets(

    Id int NOT NULL PRIMARY KEY IDENTITY
    ,Name varchar(20)
    ,Breed varchar(35)
    ,[Weight] decimal (10,2) NOT NULL
    ,Cat bit NOT NULL
    ,Dog bit NOT NULL
)

CREATE TABLE UserInfo(

    Id int NOT NULL PRIMARY KEY IDENTITY
    ,FirstName varchar(15) NOT NULL
    ,LastName varchar(30) NOT NULL
    ,PhoneNumber varchar(10) NOT NULL
    ,EmailAddress varchar(30) NOT NULL
    ,AddressId int NOT NULL FOREIGN KEY REFERENCES [Address](Id)--Address Table already created
    ,PetId int NOT NULL FOREIGN KEY REFERENCES Pets(Id)
)

CREATE TABLE Appointments(

    Id int NOT NULL PRIMARY KEY IDENTITY
    ,[Date] date NOT NULL
    ,UserInfoId int NOT NULL FOREIGN KEY REFERENCES UserInfo(Id)
    ,PetId int NOT NULL FOREIGN KEY REFERENCES Pets(Id)--?
)

My Appointments table will have the UserInfo but should it also have the Pets info? If there can be more than 1 pet for each appointment, and more than on appointment for each pet... When I wanted the UserInfo to point to the Pets table, because each user will have at least one pet.

不,您不需要在约会表中包含宠物信息,因为可以从UserInfo表本身中检索与宠物有关的信息。

You can have petID in appoinment table but it's not needed since you can link appoinment -> userinfo using UserInfoId relationship and then you can link userinfo -> pets using petid relationship.

This scenario is very well known as Transitive relationship . Ie, If A -> B and B -> C exist then A -> C can be formed.

Since each user will have one or more pets and a pet presumably has only one owner, I suggest you add UserInfoId to the Pets table and remove PetId from the UserInfo table. I also suggest you remove PetId from the Appointment table and create a separate table for the pets included in the appointment. So the appointment is actually with the owner, who may bring one or more pets.

CREATE TABLE UserInfo(
     Id int NOT NULL PRIMARY KEY IDENTITY
    ,FirstName varchar(15) NOT NULL
    ,LastName varchar(30) NOT NULL
    ,PhoneNumber varchar(10) NOT NULL
    ,EmailAddress varchar(30) NOT NULL
    ,AddressId int NOT NULL FOREIGN KEY REFERENCES [Address](Id)
);

CREATE TABLE Pets(
     Id int NOT NULL PRIMARY KEY IDENTITY
    ,UserInfoId int NOT NULL FOREIGN KEY REFERENCES UserInfo(Id)
    ,Name varchar(20)
    ,Breed varchar(35)
    ,[Weight] decimal (10,2) NOT NULL
    ,Cat bit NOT NULL
    ,Dog bit NOT NULL
);

CREATE TABLE Appointments(
     Id int NOT NULL PRIMARY KEY IDENTITY
    ,[Date] date NOT NULL
    ,UserInfoId int NOT NULL FOREIGN KEY REFERENCES UserInfo(Id)
);

CREATE TABLE AppointmentPets(
     AppointmentId int NOT NULL
    ,PetId int NOT NULL
    CONSTRAINT PK_AppointmentPets PRIMARY KEY(AppointmentId, PetId)
    );

You should know how you intend to use these tables, that is, what are your business rules . Then, you will have a better design for your table. If you are not sure, then you might have to redesign your tables again.

Here are some points to think about:

  1. You don't want to repeat user info for each pet they have, right ? (Normalization)

  2. You can map users to pets in a separate table or in appointments table. Its possible that you have a many to many relationship between users and pets, that is multiple users have one pet and multiple pets are owned by a user. You could link users to pets in a separate table.

  3. You could split user info into two tables - userinfo and useraddress table and link them with user id, since family in the same home can share a pet. (Normalization)

  4. Replace cat bit, dog bit columns by animal column. (Minor suggestion)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM