防止将不适当的行插入 SQL Server 数据库

发布于 2024-10-30 04:42:46 字数 454 浏览 5 评论 0原文

我有一个帐篷租金表,其中包含以下列:

RentID, DateStarted, DateEnded, CustomerID, etc.  

DateStarted 始终在租赁期开始时输入,DateEnded 最初为 NULL,仅在租赁期开始时填写过期。 CustomerID 是租用帐篷的客户的 ID。

我需要做的是,如果任何给定客户(由 CustomerID 表示)有任何带有 DateEnded=NULL 的租金,则阻止用户插入新行。

我不知道该怎么做。我正在考虑检查约束,但它们不允许我查看其他行。我正在考虑唯一约束,但不知道它们是否可以在这种情况下使用。我正在考虑插入触发器,但我不确定如果不满足条件是否可以取消插入。

感谢您的任何帮助。

I have a table for tent rents with the following columns:

RentID, DateStarted, DateEnded, CustomerID, etc.  

DateStarted is always entered at the beginning of the rental period, DateEnded is initally NULL and only gets filled when the rental period expires. CustomerID is the ID of the customer who rented the tent.

What I need to do is prevent users from inserting new rows for any given customer (denoted by CustomerID) if that customer has any rents with DateEnded=NULL.

I don't know how to do this. I was thinking of a Check constraint, but they don't allow me to see other rows. I was thinking of Unique constraints, but don't know if they could be used in this case. I was thinking of On-Insert trigger, but I'm not sure if I can cancel the insert if the condition is not met.

Thanks for any help.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

蓝眼睛不忧郁 2024-11-06 04:42:46

如果您使用的是 SQL Server 2008。

CREATE UNIQUE INDEX ix ON Rents(CustomerID) WHERE DateEnded IS NULL

早期版本,您可以使用索引视图(对于此选项,需要为更新基表的连接正确配置各种 SET 选项,但在 SQL Server 2005 中,它们可能会已经是了。)

CREATE VIEW dbo.ConstrainRents
WITH SCHEMABINDING
AS
SELECT CustomerID
FROM dbo.Rents 
WHERE DateEnded IS NULL

GO

CREATE UNIQUE CLUSTERED INDEX ix ON dbo.ConstrainRents(CustomerID)

If you are on SQL Server 2008.

CREATE UNIQUE INDEX ix ON Rents(CustomerID) WHERE DateEnded IS NULL

Earlier Versions you can use an indexed view (For this option various SET options need to be configured correctly for connections that update the base table but in SQL Server 2005 they probably will be already.)

CREATE VIEW dbo.ConstrainRents
WITH SCHEMABINDING
AS
SELECT CustomerID
FROM dbo.Rents 
WHERE DateEnded IS NULL

GO

CREATE UNIQUE CLUSTERED INDEX ix ON dbo.ConstrainRents(CustomerID)
各空 2024-11-06 04:42:46

您所描述的内容听起来更符合您的应用程序逻辑。我认为,当您考虑您的数据库以及您想要执行的“规则”时,请考虑什么会导致数据记录错误。根据您的描述,DateEnded 为空的记录是有效记录。

根据我的经验,如果您确实需要触发器,那么触发器非常有用,但非常危险,并且很快就会成倍增加问题的复杂性。

如果可以的话,我个人会避免触发,并在我的应用程序端执行该逻辑。

只是我的2分钱

What you describe sounds a bit more on the side of your application logic. I would think that when you think about your database, and what "rules" you want to enforce, think of what would make the data record wrong. From what you describe, a record with a null DateEnded is a valid record.

From my experience triggers are wonderful if you really need them, but are very dangerous, and very quickly increase the complexity of a problem exponentially.

I would personally avoid a trigger if I can, and do that logic on my application side.

Just my 2 cents

苍景流年 2024-11-06 04:42:46

我建议您看看 MS 的 而不是 触发器。文档在这里...
http://msdn.microsoft.com/en-us/library/ms175521.aspx

您还可以在传统插入触发器中使用回滚事务

I would suggest you take a look at MS's instead of triggers. Documentation here...
http://msdn.microsoft.com/en-us/library/ms175521.aspx

You could also use a rollback transaction in a traditional insert trigger.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文