如何在一个事实表中添加多个外部列

时间:2018-05-21 18:00:35

标签: sql database data-warehouse

这是我的sql代码,其中包含维度表以及事实表...我面临的问题是我想在我的事实表中添加多个维度的外键....请帮助我在哪里我在语法或代码中做错了

CREATE TABLE [dbo].[Bouquet_DT](
[Bouquet_ID] [char](10) NOT NULL primary key,
[Flowers] [varchar](50) NULL,
[Color] [varchar](50) NULL,
[Size] [varchar](50) NULL,
[Cost_per_Unit] [numeric](10, 2) NULL
)

CREATE TABLE [dbo].[City_DT](
[City_ID] [char](10) NOT NULL primary key,
[City_Name] [varchar](50) NULL
)

CREATE TABLE [dbo].[Customer_DT](
[Customer_ID] [char](10) NOT NULL primary key,
[Customer_Name] [varchar](50) NULL,
[Customer_Address] [varchar](50) NULL,
[Customer_City] [char](10) NULL,
[CNIC] [varchar](50) NULL,
[Gender] [varchar](50) NULL,
[Age] [numeric](5, 0) NULL,
[Customer_PhoneNo] [varchar](50) NULL
)

CREATE TABLE [dbo].[MembershipCard_DT](
[Card_ID] [char](10) NOT NULL primary key,
[Discount%] [numeric](10, 2) NULL,
[Duration_in_Years] [numeric](10, 0) NULL,
[Price] [numeric](10, 2) NULL
)

CREATE TABLE [dbo].[Occasion_DT](
[Occasion_ID] [char](10) NOT NULL primary key,
[Occasion_Name] [varchar](50) NULL
)

CREATE TABLE [dbo].[Sales_FT](

[Date] [datetime] NOT NULL,
[Unit_Quantity] [numeric](10, 0) NULL,
[SalePrice_Per_Unit] [numeric](10, 2) NULL,
[MembershiP_TyPe] [char](10) NULL,
[Discount] [numeric](10, 2) NULL,
[Delivery_CharGes] [numeric](10, 2) NULL,
[Store_ID] [char] FOREIGN KEY REFERENCES [dbo].[Store_DT]([Store_ID])
[Customer_ID] [char] FOREIGN KEY REFERENCES [dbo].[Customer_DT] 
([Customer_ID])
[Bouquet_ID] [char] FOREIGN KEY REFERENCES [dbo].[Bouquet_DT]([Bouquet_ID])
[Occasion_ID] [char] FOREIGN KEY REFERENCES [dbo].[OccasionDT] 
([Occasion_ID])
)

CREATE TABLE [dbo].[Store_DT](
[Store_ID] [char](10) NOT NULL primary key,
[Store_Manager] [varchar](50) NULL,
[Store_Address] [varchar](50) NULL,
[Store_Area] [varchar](50) NULL,
[Store_City] [char](10) NULL
)

1 个答案:

答案 0 :(得分:0)

对多个外键没有任何限制。您可以根据需要添加任意数量。

在SQL Server中,我通常使用不同的语法。例如,列Store_ID

CREATE TABLE [dbo].[Sales_FT](
  ...
  [Store_ID] [char],
  constraint fk1 foreign key (Store_ID)
    references Store_DT (Store_ID),
  ...
);

这种语法总是适用于我。