数据库:如何建模这些SQL关系?

时间:2017-09-29 11:19:12

标签: sql sql-server entity-relationship

实体

  1. AutoShop
  2. 供应商
  3. SupplierType
  4. 汽车
  5. 为了这个例子:

    1. autoshop有多个类型的多个供应商;
    2. 特定供应商可以是多种类型;
    3. 特定车型可由多个特定类型的供应商提供;
    4. 我的想法:

      CREATE TABLE [AutoShop] (
          [Id]                INT            IDENTITY (1, 1) NOT NULL,
          [Name]              NVARCHAR (200) NOT NULL,
          CONSTRAINT [PK_AutoShop] PRIMARY KEY CLUSTERED ([Id] ASC)
      );
      
      CREATE TABLE [Supplier] (
          [Id]                INT            IDENTITY (1, 1) NOT NULL,
          [Name]              NVARCHAR (200) NOT NULL,
          CONSTRAINT [PK_Supplier] PRIMARY KEY CLUSTERED ([Id] ASC)
      );
      
      CREATE TABLE [SupplierType] (
          [Id]                INT            IDENTITY (1, 1) NOT NULL,
          [Name]              NVARCHAR (200) NOT NULL,
          CONSTRAINT [PK_SupplierType] PRIMARY KEY CLUSTERED ([Id] ASC)
      );
      
      CREATE TABLE [Car] (
          [Id]                INT            IDENTITY (1, 1) NOT NULL,
          [Name]              NVARCHAR (200) NOT NULL,
          CONSTRAINT [PK_Car] PRIMARY KEY CLUSTERED ([Id] ASC)
      );
      
      -- an autoshop can have multiple suppliers of different types, 
      -- providing multiple products
      CREATE TABLE [dbo].[AutoShopSupplier] (    
          [AutoShopId]  INT NOT NULL,
          [SupplierId] INT NOT NULL,
          [SupplierTypeId] INT NOT NULL,
          CONSTRAINT [PK_AutoShopSupplier] PRIMARY KEY CLUSTERED ([AutoShopId] ASC, [SupplierId] ASC, [SupplierTypeId] ASC),
          CONSTRAINT [FK_AutoShopSupplier_ToAutoShop] FOREIGN KEY ([AutoShopId]) REFERENCES [dbo].[AutoShop] ([Id]),
          CONSTRAINT [FK_AutoShopSupplier_ToSupplier] FOREIGN KEY ([SupplierId]) REFERENCES [dbo].[Supplier] ([Id]),
          CONSTRAINT [FK_AutoShopSupplierType_ToSupplierType] FOREIGN KEY ([SupplierTypeId]) REFERENCES [dbo].[SupplierType] ([Id])
      );
      
      -- multiple cars can be supplied by multiple suppliers, but just from a specific type of supplier
      CREATE TABLE [dbo].[AutoShopSupplierCar] (    
          [AutoShopId]  INT NOT NULL,
          [SupplierId] INT NOT NULL,
          [SupplierTypeId] INT NOT NULL,
          [CarId] INT NOT NULL,
          CONSTRAINT [PK_AutoShopSupplierCar] PRIMARY KEY CLUSTERED ([AutoShopId] ASC, [SupplierId] ASC, [SupplierTypeId] ASC),
          CONSTRAINT [FK_AutoShopSupplierCar_ToAutoShop] FOREIGN KEY ([AutoShopId]) REFERENCES [dbo].[AutoShop] ([Id]),
          CONSTRAINT [FK_AutoShopSupplierCar_ToSupplier] FOREIGN KEY ([SupplierId]) REFERENCES [dbo].[Supplier] ([Id]),
          CONSTRAINT [FK_AutoShopSupplierCar_ToSupplierType] FOREIGN KEY ([SupplierTypeId]) REFERENCES [dbo].[SupplierType] ([Id]),
          CONSTRAINT [FK_AutoShopSupplierCar_ToCar] FOREIGN KEY ([SupplierTypeId]) REFERENCES [dbo].[SupplierType] ([Id])
      );
      

      这是正确的方法吗?

      提前致谢, 干杯!

2 个答案:

答案 0 :(得分:0)

我稍微调整了你的脚本并添加了一些表

CREATE TABLE [AutoShop] (
    [Id]      INT   PRIMARY KEY  IDENTITY (1, 1),
    [Name]    NVARCHAR (200) NOT NULL);
go
CREATE TABLE [Supplier] (
    [Id]     INT    PRIMARY KEY IDENTITY (1, 1),
    [Name]   NVARCHAR (200) NOT NULL);
go
CREATE TABLE [SupplierType] (
    [Id]     INT    PRIMARY KEY IDENTITY (1, 1),
    [Name]   NVARCHAR (200) NOT NULL);
go
CREATE TABLE [Car] (
    [Id]     INT    PRIMARY KEY IDENTITY (1, 1),
    [Name]   NVARCHAR (200) NOT NULL);
go
CREATE TABLE [SupplierTypeSupplier] (
    [SupplierId]     INT,
    [SupplierTypeID]   INT,
    CONSTRAINT [PK_SupplierTypeSupplier] PRIMARY KEY CLUSTERED ([SupplierId] , [SupplierTypeID]),
    CONSTRAINT [FK_Supplier] FOREIGN KEY ([SupplierId]) REFERENCES [dbo].[Supplier] ([Id]),
    CONSTRAINT [FK_SupplierType] FOREIGN KEY ([SupplierTypeId]) REFERENCES [dbo].[SupplierType] ([Id]));
go
CREATE TABLE [dbo].[AutoShopSupplier] (    
    [AutoShopId]  INT,
    [SupplierId] INT,
    CONSTRAINT [PK_AutoShopSupplier] PRIMARY KEY CLUSTERED ([AutoShopId] , [SupplierId]),
    CONSTRAINT [FK_AutoShopSupplierAutoShop] FOREIGN KEY ([AutoShopId]) REFERENCES [dbo].[AutoShop] ([Id]),
    CONSTRAINT [FK_AutoShopSupplierSupplier] FOREIGN KEY ([SupplierId]) REFERENCES [dbo].[Supplier] ([Id]));
go
CREATE TABLE [dbo].[CarSupplier] ( 
    [SupplierId] INT,  
    [CarId]  INT,
    CONSTRAINT [PK_CarSupplier] PRIMARY KEY CLUSTERED ([CarId] , [SupplierId]),
    CONSTRAINT [FK_CarSupplierCar] FOREIGN KEY ([CarId]) REFERENCES [dbo].[Car] ([Id]),
    CONSTRAINT [FK_CarSupplierSupplier] FOREIGN KEY ([SupplierId]) REFERENCES [dbo].[Supplier] ([Id]));

使用主要工作PRIMARY KEY时,您不需要使用NOT NULL 如果您只使用一个主键,则不必将PRIMEY KEY写为CONSTRAINT

答案 1 :(得分:0)

@Jonny

CREATE TABLE [Product] (
[Id]     INT    PRIMARY KEY IDENTITY (1, 1),
[Name]   NVARCHAR (200) NOT NULL);


CREATE TABLE [dbo].[AutoShopSupplierSuppliment] (    
[AutoShopId]  INT,
[SupplierId] INT,
[ProductId] INT,
[Date] DATE,
[Quantity] INT,

CONSTRAINT [PK_AutoShopSupplier] PRIMARY KEY CLUSTERED ([AutoShopId] , [SupplierId],[ProductId]),
CONSTRAINT [FK_AutoShopSupplierAutoShop] FOREIGN KEY ([AutoShopId]) REFERENCES [dbo].[AutoShop] ([Id]),
CONSTRAINT [FK_AutoShopSupplierSupplier] FOREIGN KEY ([SupplierId]) REFERENCES [dbo].[Supplier] ([Id])),
CONSTRAINT [FK_AutoShopSupplierProduct] FOREIGN KEY ([ProductId]) REFERENCES [dbo].[Product] ([Id]));

您可以使用日期列来分隔记录等。