SQL:使用触发器的无身份功能解决方法
我对我的触发器有点生疏,我试图解决一个类的这个问题:
在数据库测试中,表没有 IDENTITY 功能的选项。 换句话说,当我们向“Users”表中插入一行时,我们希望主键“UserID”自动递增。 请建议一种解决方法,以在没有此类内置功能的情况下实现此功能。 (提示:您仍然可以使用函数、存储过程、序列、触发器等)
I'm a little rusty with my triggers and what not and am trying to figure out this problem for a class:
In a database TEST, tables do not have the option of the IDENTITY feature. In other words, when we insert a row into the table “Users”, we would like the primary key “UserID” to auto-increment. Please suggest a workaround to implement this feature without such a built-in functionality.
(Hint: You may still use functions, stored procedures, sequences, triggers, etc)
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
使用名为 ID 的 Int 列作为表主键。
然后,您可以使用插入触发器来填充/计算要插入的 ID 值,而不是使用插入触发器。
触发器将确定相关表的最大现有 ID(使用 select MAX ID from TableA),然后为要插入的每条记录将其递增 1。
如果表中没有记录,则 ID 值为 1。
Use an Int column for the table Primary Key called ID.
You can then use an instead of Insert Trigger, to populate/calculate the value to be inserted for ID.
The trigger will determine what the maximum existing ID is for the table in question (using select MAX ID from TableA) and then increment it by 1 for each record to be inserted.
If there are no records in the table then the ID value is 1.
您使用序列,这在 Oracle 中很常见,它没有(或一次没有,可能已经更改)有标识列。 因为这是家庭作业,所以我会让你从这里弄清楚剩下的事情。
You use a sequence, and it's very common with Oracle, which does not (or did not once, it may have changed) have identity columns. Since this is homework I'll let you figure out the rest from here.