AskOverflow.Dev

AskOverflow.Dev Logo AskOverflow.Dev Logo

AskOverflow.Dev Navigation

  • 主页
  • 系统&网络
  • Ubuntu
  • Unix
  • DBA
  • Computer
  • Coding
  • LangChain

Mobile menu

Close
  • 主页
  • 系统&网络
    • 最新
    • 热门
    • 标签
  • Ubuntu
    • 最新
    • 热门
    • 标签
  • Unix
    • 最新
    • 标签
  • DBA
    • 最新
    • 标签
  • Computer
    • 最新
    • 标签
  • Coding
    • 最新
    • 标签
主页 / dba / 问题 / 131524
Accepted
Eric S
Eric S
Asked: 2016-03-08 10:19:46 +0800 CST2016-03-08 10:19:46 +0800 CST 2016-03-08 10:19:46 +0800 CST

如何设置引用多个表的外键约束

  • 772

我有一个简单的结构,我不确定如何处理弹出的一些问题。

有 4 个表:Reports, ReportParameters, ScheduledReports, ScheduledReportParameters。

ReportParameters列出报告所需的参数。

ScheduledReports是报表的预定实例。(以周四报 X 运行为例)。

ScheduledReportParameters是此实例运行时要使用的参数及其值的列表。

基本布局:

Reports:
ReportID (PK)

ReportParameters:
ReportParameterID (PK)
ReportID (FK to Reports)
Name

ScheduledReports:
ScheduledReportID (PK)
ReportID (FK to reports)

ScheduledReportParameters:
ScheduledReportID (FK to ScheduledReports)
ReportParameterID (FK to ReportParameters)
Value
(PK is formed by the ScheduledReportID and ReportParameterID)

这有一个问题,即与计划报告关联的参数不必与基础报告的实际预期一致,它可能是完全不同的基础报告,而不是计划报告所基于的报告。

所以,我想我可以将 the 添加ReportID到 the ScheduledReportParameters,但这无济于事,因为没有任何强制要求ReportIDthere 同意 there 的ScheduledReport基础。

我错过了一些简单的东西吗?或者,这比我想象的更棘手吗?

有一个计划的报告,其中的参数实际上并不与报告一起使用,这对我来说没什么大不了的,因为当我提取它们时,连接会自动过滤掉任何潜在的坏数据,但我想防止坏数据进入第一名。我知道我可以使用触发器来做到这一点,但这很快就会变得丑陋。

sql-server database-design
  • 1 1 个回答
  • 119 Views

1 个回答

  • Voted
  1. Best Answer
    Vladimir Baranov
    2016-03-08T20:28:02+08:002016-03-08T20:28:02+08:00

    看起来您不知道外键可以基于多个列。

    所以,是的,添加一列并将ReportID其ScheduledReportParameters包含在两个外键中以强制执行约束。

    两个字段之间ScheduledReportParameters和使用一个外键。ScheduledReports(ScheduledReportID, ReportID)

    ScheduledReportParameters两个字段之间和ReportParameters使用两个字段之间的第二个外键(ReportParameterID, ReportID)。

    完整脚本

    报告

    CREATE TABLE [dbo].[Reports](
        [ReportID] [int] NOT NULL,
        [ReportName] [nvarchar](50) NOT NULL,
     CONSTRAINT [PK_Reports] PRIMARY KEY CLUSTERED 
    (
        [ReportID] ASC
    ))
    

    报告参数

    CREATE TABLE [dbo].[ReportParameters](
        [ReportParameterID] [int] NOT NULL,
        [ReportID] [int] NOT NULL,
        [ParameterName] [nvarchar](50) NOT NULL,
     CONSTRAINT [PK_ReportParameters] PRIMARY KEY CLUSTERED 
    (
        [ReportParameterID] ASC
    ))
    GO
    
    CREATE UNIQUE NONCLUSTERED INDEX [IX_ReportParameterID_ReportID] 
    ON [dbo].[ReportParameters]
    (
        [ReportParameterID] ASC,
        [ReportID] ASC
    )
    GO
    
    ALTER TABLE [dbo].[ReportParameters]  WITH CHECK ADD  
    CONSTRAINT [FK_ReportParameters_Reports] FOREIGN KEY([ReportID])
    REFERENCES [dbo].[Reports] ([ReportID])
    GO
    
    ALTER TABLE [dbo].[ReportParameters] CHECK CONSTRAINT [FK_ReportParameters_Reports]
    GO
    

    预定报告

    CREATE TABLE [dbo].[ScheduledReports](
        [ScheduledReportID] [int] NOT NULL,
        [ReportID] [int] NOT NULL,
        [ScheduleName] [nvarchar](50) NOT NULL,
     CONSTRAINT [PK_ScheduledReports] PRIMARY KEY CLUSTERED 
    (
        [ScheduledReportID] ASC
    ))
    GO
    
    CREATE UNIQUE NONCLUSTERED INDEX [IX_ScheduledReportID_ReportID] 
    ON [dbo].[ScheduledReports]
    (
        [ScheduledReportID] ASC,
        [ReportID] ASC
    )
    GO
    
    ALTER TABLE [dbo].[ScheduledReports]  WITH CHECK ADD  
    CONSTRAINT [FK_ScheduledReports_Reports] FOREIGN KEY([ReportID])
    REFERENCES [dbo].[Reports] ([ReportID])
    GO
    
    ALTER TABLE [dbo].[ScheduledReports] CHECK CONSTRAINT [FK_ScheduledReports_Reports]
    GO
    

    预定报告参数

    CREATE TABLE [dbo].[ScheduledReportParameters](
        [ScheduledReportParametersID] [int] NOT NULL,
        [ScheduledReportID] [int] NOT NULL,
        [ReportID] [int] NOT NULL,
        [ReportParameterID] [int] NOT NULL,
        [ParameterValue] [nvarchar](50) NOT NULL,
     CONSTRAINT [PK_ScheduledReportParameters] PRIMARY KEY CLUSTERED 
    (
        [ScheduledReportParametersID] ASC
    ))
    GO
    
    ALTER TABLE [dbo].[ScheduledReportParameters]  WITH CHECK ADD  
    CONSTRAINT [FK_ScheduledReportParameters_ReportParameters] 
    FOREIGN KEY([ReportParameterID], [ReportID])
    REFERENCES [dbo].[ReportParameters] ([ReportParameterID], [ReportID])
    GO
    
    ALTER TABLE [dbo].[ScheduledReportParameters] 
    CHECK CONSTRAINT [FK_ScheduledReportParameters_ReportParameters]
    GO
    
    ALTER TABLE [dbo].[ScheduledReportParameters]  WITH CHECK ADD  
    CONSTRAINT [FK_ScheduledReportParameters_ScheduledReports] 
    FOREIGN KEY([ScheduledReportID], [ReportID])
    REFERENCES [dbo].[ScheduledReports] ([ScheduledReportID], [ReportID])
    GO
    
    ALTER TABLE [dbo].[ScheduledReportParameters] 
    CHECK CONSTRAINT [FK_ScheduledReportParameters_ScheduledReports]
    GO
    

    外键使用的两列必须有唯一索引。我在这个例子中添加了明确的索引(IX_ReportParameterID_ReportID和IX_ScheduledReportID_ReportID)。您还可以将第二列添加到相应表的主键中。

    样本数据

    INSERT INTO [dbo].[Reports] 
        ([ReportID]
        ,[ReportName]) 
    VALUES
        (1,'R1'),
        (2,'R2');
    
    INSERT INTO [dbo].[ReportParameters]
        ([ReportParameterID]
        ,[ReportID]
        ,[ParameterName])
    VALUES
        (1,1,'R1P1'),
        (2,1,'R1P2'),
        (3,2,'R2P3'),
        (4,2,'R2P4'),
        (5,2,'R2P5');
    
    INSERT INTO [dbo].[ScheduledReports]
        ([ScheduledReportID]
        ,[ReportID]
        ,[ScheduleName])
    VALUES
        (1,1,'S1');
    

    测试约束

    为报表 1 添加参数值:

    INSERT INTO [dbo].[ScheduledReportParameters]
        ([ScheduledReportParametersID]
        ,[ScheduledReportID]
        ,[ReportID]
        ,[ReportParameterID]
        ,[ParameterValue])
    VALUES
        (1,1,1,1,'R1 P1 V1'),
        (2,1,1,2,'R1 P2 V2');
    
    (2 row(s) affected)
    

    尝试为不属于报表的参数添加值(参数 3 属于报表 2,而不是 1:

    INSERT INTO [dbo].[ScheduledReportParameters]
        ([ScheduledReportParametersID]
        ,[ScheduledReportID]
        ,[ReportID]
        ,[ReportParameterID]
        ,[ParameterValue])
    VALUES
        (3,1,1,3,'V');
    
    The INSERT statement conflicted with the FOREIGN KEY constraint "FK_ScheduledReportParameters_ReportParameters". The conflict occurred in database "AdventureWorks2014", table "dbo.ReportParameters".
    The statement has been terminated.
    

    尝试为错误的计划报告添加参数值(参数 3 属于报告 2,计划 1 不用于报告 2):

    INSERT INTO [dbo].[ScheduledReportParameters]
        ([ScheduledReportParametersID]
        ,[ScheduledReportID]
        ,[ReportID]
        ,[ReportParameterID]
        ,[ParameterValue])
    VALUES
        (3,1,2,3,'V');
    
    The INSERT statement conflicted with the FOREIGN KEY constraint "FK_ScheduledReportParameters_ScheduledReports". The conflict occurred in database "AdventureWorks2014", table "dbo.ScheduledReports".
    The statement has been terminated.
    

    缺少参数值

    请注意,此模式不会阻止参数的缺失值。例如,您可以删除值V2:

    DELETE FROM [dbo].[ScheduledReportParameters]
    WHERE [ScheduledReportParametersID] = 2;
    
    (1 row(s) affected)
    

    因此,现在 scheduleS1仅具有 parameterP1的值,而没有 parameter 的值P2。

    • 3

相关问题

  • 我需要为每种类型的查询使用单独的索引,还是一个多列索引可以工作?

  • 什么时候应该使用唯一约束而不是唯一索引?

  • 死锁的主要原因是什么,可以预防吗?

  • 在数据仓库中实现多对多关系有哪些方法?

  • 如何确定是否需要或需要索引

Sidebar

Stats

  • 问题 205573
  • 回答 270741
  • 最佳答案 135370
  • 用户 68524
  • 热门
  • 回答
  • Marko Smith

    连接到 PostgreSQL 服务器:致命:主机没有 pg_hba.conf 条目

    • 12 个回答
  • Marko Smith

    如何让sqlplus的输出出现在一行中?

    • 3 个回答
  • Marko Smith

    选择具有最大日期或最晚日期的日期

    • 3 个回答
  • Marko Smith

    如何列出 PostgreSQL 中的所有模式?

    • 4 个回答
  • Marko Smith

    列出指定表的所有列

    • 5 个回答
  • Marko Smith

    如何在不修改我自己的 tnsnames.ora 的情况下使用 sqlplus 连接到位于另一台主机上的 Oracle 数据库

    • 4 个回答
  • Marko Smith

    你如何mysqldump特定的表?

    • 4 个回答
  • Marko Smith

    使用 psql 列出数据库权限

    • 10 个回答
  • Marko Smith

    如何从 PostgreSQL 中的选择查询中将值插入表中?

    • 4 个回答
  • Marko Smith

    如何使用 psql 列出所有数据库和表?

    • 7 个回答
  • Martin Hope
    Jin 连接到 PostgreSQL 服务器:致命:主机没有 pg_hba.conf 条目 2014-12-02 02:54:58 +0800 CST
  • Martin Hope
    Stéphane 如何列出 PostgreSQL 中的所有模式? 2013-04-16 11:19:16 +0800 CST
  • Martin Hope
    Mike Walsh 为什么事务日志不断增长或空间不足? 2012-12-05 18:11:22 +0800 CST
  • Martin Hope
    Stephane Rolland 列出指定表的所有列 2012-08-14 04:44:44 +0800 CST
  • Martin Hope
    haxney MySQL 能否合理地对数十亿行执行查询? 2012-07-03 11:36:13 +0800 CST
  • Martin Hope
    qazwsx 如何监控大型 .sql 文件的导入进度? 2012-05-03 08:54:41 +0800 CST
  • Martin Hope
    markdorison 你如何mysqldump特定的表? 2011-12-17 12:39:37 +0800 CST
  • Martin Hope
    Jonas 如何使用 psql 对 SQL 查询进行计时? 2011-06-04 02:22:54 +0800 CST
  • Martin Hope
    Jonas 如何从 PostgreSQL 中的选择查询中将值插入表中? 2011-05-28 00:33:05 +0800 CST
  • Martin Hope
    Jonas 如何使用 psql 列出所有数据库和表? 2011-02-18 00:45:49 +0800 CST

热门标签

sql-server mysql postgresql sql-server-2014 sql-server-2016 oracle sql-server-2008 database-design query-performance sql-server-2017

Explore

  • 主页
  • 问题
    • 最新
    • 热门
  • 标签
  • 帮助

Footer

AskOverflow.Dev

关于我们

  • 关于我们
  • 联系我们

Legal Stuff

  • Privacy Policy

Language

  • Pt
  • Server
  • Unix

© 2023 AskOverflow.DEV All Rights Reserve