实体
为了这个例子:
我的想法:
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])
);
这是正确的方法吗?
提前致谢, 干杯!
答案 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]));
您可以使用日期列来分隔记录等。