约束多个表。 UDF?

时间:2015-06-02 13:29:14

标签: sql-server sql-server-2008-r2 constraints user-defined-functions

我有两个表:表A和表B.这两个表与A中的主键和B中的外键链接。

表A:

CREATE TABLE [BIO].[table_A](
    [table_A_id] [int] IDENTITY(1,1) NOT NULL,  
    [type_id] [nvarchar](2) NOT NULL    
CONSTRAINT [PK_table_A] PRIMARY KEY CLUSTERED 
(
    [table_A_id] ASC
))

表B:

CREATE TABLE [BIO].[table_B](
    [table_B_id] [int] IDENTITY(1,1) NOT NULL,
    [table_A_id] [int] NOT NULL,
    [analysis_id] [tinyint] NOT NULL
CONSTRAINT [PK_table_B] PRIMARY KEY CLUSTERED 
(
    [table_B_id] ASC
))

ALTER TABLE [BIO].[table_B] WITH CHECK 
ADD CONSTRAINT [FK_table_B_table_A] FOREIGN KEY([table_A_id])
REFERENCES [BIO].[table_A] ([table_A_id])
GO

ALTER TABLE [BIO].[table_B] CHECK CONSTRAINT [FK_table_B_table_A]
GO

表B必须根据表A中的值仅包含特定值。 例如,如果表A中有BL,则表B中只能有1或3;如果我在表A中有ST,我在表B中只能有2或4。

我已经设置了一个桥接表来定义这些组合:BL→1或3,ST→2或4。

桥牌表:

CREATE TABLE [QRY].[bridge_table](
    [type_id] [nvarchar](2) NOT NULL,
    [analysis_id] [tinyint] NOT NULL,
CONSTRAINT [PK_bridge_table] PRIMARY KEY CLUSTERED 
(
    [type_id] ASC,
    [analysis_id] ASC
))

我目前正在为每个表使用一个约束,以确保根据桥接表中定义的组合,任何插入或更新都是正确的。这两个约束基于UDF。

表B上的约束:

ALTER TABLE [BIO].[Table_B] WITH CHECK 
ADD CONSTRAINT [CK_chkAnalysisType] 
CHECK (([QRY].[TypeAnalysisMatch_table_B]([table_A_id])>(0)))
GO

UDF:

CREATE FUNCTION [QRY].[TypeAnalysisMatch_table_B] (@table_A_id int)
RETURNS int
AS
BEGIN
    RETURN
    (
    SELECT
        Count(BIO.table_A.table_A_id) AS cnt_rec
    FROM
        QRY.bridge_table
        INNER JOIN BIO.table_A ON QRY.bridge_table.type_id = BIO.table_A.type_id
        INNER JOIN BIO.table_B ON 
                QRY.bridge_table.analysis_id = BIO.table_B.analysis_id
            AND BIO.table_A.table_A_id = BIO.table_B.table_A_id
    WHERE
        BIO.table_A.table_A_id = @table_A_id
    )
END

它适用于INSERT,但不适用于UPDATE。此外,正如我读到应该避免约束中的UDF,我正在寻找更好的解决方案。

这些限制的有效替代方案是什么?

1 个答案:

答案 0 :(得分:1)

你是对的,在UDF约束中CHECK可能会很棘手,而且某些UPDATE语句可能会绕过检查:

http://sqlblog.com/blogs/tibor_karaszi/archive/2009/12/17/be-careful-with-constraints-calling-udfs.aspx

MSSQL: Update statement avoiding the CHECK constraint

正如您在SO问题中所看到的那样,建议使用触发器进行检查。编写正确有效的触发器也不是一件容易的事。

我认为您的bridge_table包含以下数据:

type_id    analysis_id
BL         1
BL         3
ST         2
ST         4

我只使用外键设置这些约束,而不使用UDF。但是,这需要一些(最小的)数据重复。我假设真实table_Atable_B的列数多于此示例。

1。在主要密钥中table_A包括type_id

CREATE TABLE [table_A](
    [table_A_id] [int] IDENTITY(1,1) NOT NULL,
    [type_id] [nvarchar](2) NOT NULL,
CONSTRAINT [PK_table_A] PRIMARY KEY CLUSTERED
(
    [table_A_id] ASC,
    [type_id] ASC
))

2. type_id列添加到table_B。是的,与您在table_A中已有的列相同。这就是我上面提到的重复数据:

CREATE TABLE [table_B](
    [table_B_id] [int] IDENTITY(1,1) NOT NULL,
    [table_A_id] [int] NOT NULL,
    [type_id] [nvarchar](2) NOT NULL,
    [analysis_id] [tinyint] NOT NULL
CONSTRAINT [PK_table_B] PRIMARY KEY CLUSTERED 
(
    [table_B_id] ASC
))

3。在两列table_B上创建将table_A(table_A_id, type_id)相关联的外键:

ALTER TABLE [table_B] WITH CHECK 
ADD CONSTRAINT [FK_table_B_table_A] FOREIGN KEY([table_A_id], [type_id])
REFERENCES [table_A] ([table_A_id], [type_id])

此约束可确保type_id中重复的table_B值与table_A中的原始值保持一致。

4。制作外键,将table_Bbridge_table再次链接到两列(type_id, analysis_id)上:

ALTER TABLE [table_B] WITH CHECK 
ADD CONSTRAINT [FK_table_B_bridge_table] FOREIGN KEY([type_id], [analysis_id])
REFERENCES [bridge_table] ([type_id], [analysis_id])

5. 现在您可以测试一切是否按预期运行。

table_A添加几行:

INSERT INTO [table_A] ([type_id]) 
VALUES ('BL'), ('BL'), ('ST'), ('ZZ');

table_A_id    type_id
1             BL
2             BL
3             ST
4             ZZ

尝试将有效数据插入table_B

INSERT INTO [dbo].[table_B] ([table_A_id],[type_id],[analysis_id])
VALUES (1,'BL',1)

INSERT INTO [dbo].[table_B] ([table_A_id],[type_id],[analysis_id])
VALUES (1,'BL',3)

INSERT INTO [dbo].[table_B] ([table_A_id],[type_id],[analysis_id])
VALUES (2,'BL',3)

INSERT INTO [dbo].[table_B] ([table_A_id],[type_id],[analysis_id])
VALUES (3,'ST',2)

INSERT INTO [dbo].[table_B] ([table_A_id],[type_id],[analysis_id])
VALUES (3,'ST',2)

table_B_id    table_A_id    type_id    analysis_id
1             1             BL         1
2             1             BL         3
3             2             BL         3
4             3             ST         2
5             3             ST         2

尝试插入无效数据:

INSERT INTO [dbo].[table_B] ([table_A_id],[type_id],[analysis_id])
VALUES (3,'ST',1)

The INSERT statement conflicted with the FOREIGN KEY constraint "FK_table_B_bridge_table". 
The conflict occurred in database "tempdb", table "dbo.bridge_table".
The statement has been terminated.

这意味着,ST不能拥有analysis_id=1

INSERT INTO [dbo].[table_B] ([table_A_id],[type_id],[analysis_id])
VALUES (3,'BL',1)

The INSERT statement conflicted with the FOREIGN KEY constraint "FK_table_B_table_A". 
The conflict occurred in database "tempdb", table "dbo.table_A".
The statement has been terminated.

这意味着,table_Atable_A_id=3BL中的行type_id中没有UPDATE

外键还会继续检查所有UPDATE [dbo].[table_B] SET [type_id] = 'ST' WHERE [table_B_id] = 1 The UPDATE statement conflicted with the FOREIGN KEY constraint "FK_table_B_table_A". The conflict occurred in database "tempdb", table "dbo.table_A". The statement has been terminated. UPDATE [dbo].[table_B] SET [analysis_id] = 2 WHERE [table_B_id] = 1 The UPDATE statement conflicted with the FOREIGN KEY constraint "FK_table_B_bridge_table". The conflict occurred in database "tempdb", table "dbo.bridge_table". The statement has been terminated. 语句的数据一致性:

UPDATE [dbo].[table_B]
SET [analysis_id] = 3
WHERE [table_B_id] = 1

(1 row(s) affected)

但是这个有效:

def main
 @variable = "People are awesome!"
end