如何限制数据库中的数据量... [英] how to limit amount of data in database...

查看:192
本文介绍了如何限制数据库中的数据量...的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想知道如何限制数据库中数据的插入...

i want to know ho to limit insertion of data in database...

推荐答案

在我看来,您正在寻找代替插入"触发器.基本上,它的作用是使您可以在SQL中查看数据,然后再插入(或根据需要进行更新或删除),并确定是否要允许插入.这样,您可以限制进入数据库的数据.

例如,您可能有一个包含人们对调查的投票的表.您可以设置一个规则,将每次调查的人数限制为每天只能投票一次.由于该人每天被允许投票一次,因此您不能使用UNIQUE约束,除非日期"字段是仅存储日期而不是时间的新日期"类型.代替触发可以检查该人是否已经有今天的条目.如果是这样,更改可能会被拒绝.

请注意:触发器占用数据库中的时间.您写的越多,数据库运行的速度就越慢.明智地使用它们,并且仅在没有更有效的方法时才使用.例如,上面示例中的UNIQUE约束在很多情况下都可以使用,应该首先尝试.

要了解有关触发器的更多信息,请阅读本文:

It sounds to me like you are looking for an Instead Of INSERT Trigger. Basically what it does is it allows you, in SQL, to look at the data before it is inserted (or update or deleted if you want) and determine if you want to allow the insertion to happen. In this way, you could limit the data going into your database.

For example, you might have a table that contained people''s votes on surveys. You could set up a rule that limited people to onl one vote per day per survey. Since the person would be allowed to vote once per day, you couldn''t use a UNIQUE constraint unless the Date field was the new Date type that just stored the date and not the time. The Instead Of trigger could check to see if that person already had an entry for today. If so, the change could be rejected.

A word of caution: triggers take up time in your database. The more you write, the slower your database will run. Use them wisely and only when a more efficient method is not available. For instance, the UNIQUE constraint in the above example would work for a lot of circumstances and should be tried first.

To learn more about triggers, read this article:

http://www.google.com/url?sa=t&rct=j&q=&esrc=s&frm=1&source=web&cd=6&ved=0CG4QFjAF&url=http%3A%2F%2Fwww.codeproject.com%2FArticles%2F25600%2FTriggers-Sql-Server&ei=nHfbT72_Kqf96gGbgLW8Cw&usg=AFQjCNE2-wxIYQDD3xvXqkSGPlvJ-HSAhw[^]

Just make sure you do an Instead Of trigger instead of an After trigger. After is too late.


这篇关于如何限制数据库中的数据量...的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆