gpt4 book ai didi

sql - 多个表的替代插入触发器

转载 作者:行者123 更新时间:2023-12-02 23:05:06 25 4
gpt4 key购买 nike

对于日志系统,我希望拥有以下数据库架构:

CREATE TABLE [dbo].[Categories] (
[Id] INT IDENTITY (1, 1) NOT NULL,
[App] NVARCHAR (30) NULL,
[Source] NVARCHAR (30) NULL,
[LogLevel] NVARCHAR (5) NULL,
[Logger] NVARCHAR (120) NULL,
CONSTRAINT [PK_Categories] PRIMARY KEY NONCLUSTERED ([Id] ASC),
CONSTRAINT [UK_Categories] UNIQUE NONCLUSTERED ([App] ASC, [Source] ASC, [LogLevel] ASC, [Logger] ASC)
);

CREATE TABLE [dbo].[Occurences] (
[PointInTime] BIGINT NOT NULL,
[CategoryId] INT NOT NULL,
[Noise] INT NOT NULL,
CONSTRAINT [PK_Occurences] PRIMARY KEY CLUSTERED ([PointInTime] ASC, [CategoryId] ASC, [Noise] ASC),
CONSTRAINT [FK_Category] FOREIGN KEY ([CategoryId]) REFERENCES [Categories] ([Id])
);

设计目标是允许大量的日志记录数据,因为更昂贵的字符串被分解到单独的表中。

从语义上讲,两个表形成此 View 定义的单个逻辑表:

CREATE VIEW [dbo].[HistoricLogEntries]
AS SELECT o.PointInTime, o.Noise, c.App, c.[Source], c.LogLevel, c.Logger
FROM Occurences o
JOIN Categories c ON o.CategoryId = c.Id;

我现在想在 View 上定义一个替代插入触发器,这就是我的麻烦开始的地方。我有以下尝试:

CREATE TRIGGER InsteadTrigger on [dbo].[HistoricLogEntries]
INSTEAD OF INSERT
AS
BEGIN
INSERT INTO Categories
SELECT i.App, i.[Source], i.LogLevel, i.Logger
FROM INSERTED i;

INSERT INTO Occurences
SELECT i.PointInTime, i.Noise, c.Id AS CategoryId
FROM INSERTED i
JOIN Categories c ON i.App = c.App AND i.[Source] = c.[Source] AND i.LogLevel = c.LogLevel AND i.Logger = c.Logger;
END

明显的第一个问题是第一次插入没有检查元组是否已经在数据库中。我知道在单值插入的情况下如何做到这一点,但在这里我必须考虑多行插入。

我无法解释的另一件事是,如果第一次插入成功,触发器甚至不起作用。我遇到外键违规 - 就好像第一次插入实际上没有插入任何内容。

我认为这应该是一个常见的设置,所以也许有人有一些类似的示例代码?

最佳答案

对于 SQL2008+,我将使用 MERGE 语句插入新类别:

MERGE dbo.Categories WITH (HOLDLOCK) AS c
USING INSERTED AS i ON i.App = c.App
AND i.[Source] = c.[Source]
AND i.LogLevel = c.LogLevel
AND i.Logger = c.Logger
WHEN NOT MATCHED BY TARGET THEN
INSERT (App, [Source], LogLevel, Logger)
VALUES (i.App, i.[Source], i.LogLevel, i.Logger);

我使用了HOLDLOCK表提示to prevent race condition :

[...] To prevent concurrent sessions from inserting data with the same key, an incompatible lock must be acquired to ensure only one session can read the key (my note: UK_Categories unique index in this case) and that lock must be held until the transaction completes.[...]

并防止 FK 错误:

The INSERT statement conflicted with the FOREIGN KEY constraint "FK_Category". The conflict occurred in database "Test", table "dbo.Categories", column 'Id'.

我会在第二个INSERT中添加每列的名称,因此:

INSERT INTO Occurences (PointInTime, Noise, CategoryId)
SELECT i.PointInTime, i.Noise, c.Id AS CategoryId
FROM INSERTED i
JOIN Categories c ON i.App = c.App

这些 FK 错误的原因是列顺序不一致:

1) CREATE TABLE 中列的顺序是

   [PointInTime] BIGINT NOT NULL,
[CategoryId] INT NOT NULL,
[Noise] INT NOT NULL,

但是

2) 在第二次插入中,列的顺序不同(请参阅 CategoryId 与 Noise):

INSERT INTO Occurences
-- or INSERT INTO Occurences (PointInTime, CategoryId, Noise)
SELECT i.PointInTime, i.Noise, c.Id AS CategoryId
FROM ...

这是我的解决方案:

ALTER TRIGGER InsteadTrigger on [dbo].[HistoricLogEntries]
INSTEAD OF INSERT
AS
BEGIN
MERGE dbo.Categories WITH (HOLDLOCK) AS c
USING INSERTED AS i ON i.App = c.App
WHEN NOT MATCHED BY TARGET THEN
INSERT (App)
VALUES (i.App);

INSERT INTO Occurences (PointInTime, Noise, CategoryId)
SELECT i.PointInTime, i.Noise, c.Id AS CategoryId
FROM INSERTED i
JOIN Categories c ON i.App = c.App
END
GO

关于sql - 多个表的替代插入触发器,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19470319/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com