如何通过pl/sql存储过程向表插入数据时生成主键值
我需要通过 pl/sql 存储过程将数据插入到特定表中。我的要求是:
- 插入时应该为特定列生成主键值;
- 它应该将该主键值返回到输出变量;对于
- 另一列,它应该验证我的字符串,使其只包含字符,而不包含整数。
I need to insert data into particular table through pl/sql stored procedure. My requirements are:
- while inserting it should generate PRIMARY KEY values for a particular column;
- it should return that PRIMARY KEY value to an output variable; and
- for another column it should validate my string such that it should contain only characters, not integers.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
您可以使用 Oracle
SEQUENCE
生成主键值作为代理键。您可以在使用TRANSLATE
的列上创建约束来检查新插入/更新的数据中不存在数字。一些适用于 SQL*Plus 的示例代码:
You can generate primary key values as a surrogate key using an Oracle
SEQUENCE
. You can create a constraint on a column that usesTRANSLATE
to check that no numeric digits exist in newly inserted/updated data.Some example code, suitable for SQL*Plus:
在oracle中,我相信“身份”列最好通过序列和插入触发器来实现,该触发器检查主键列是否为空,如果是则获取下一个序列并插入它。
然后,您可以使用“返回”子句来获取新创建的主键:
在进入数据库之前我会亲自在代码中处理字符串字段的过滤(如果可能的话)。众所周知,数据库在处理字符串操作方面效率低下。
In oracle I believe the "identity" column is best achieved with a sequence and an insert trigger that checks if the primary key columns is null and if so gets the next sequence and inserts it.
you can then use the "returning" clause to get the newly created primary key:
the filtering of the string field I would personally handle in code before going to the database (if that is a possibility). Databases are notoriously inefficient at handling string operations.