Oracle 主键
您可以仅使用复合主键的一部分来检索 Oracle 中的数据记录吗?
示例 PK = Col1 + Col2 + Col3
SELECT *
FROM table
WHERE Col1 = 'SomeDate'
Can you retriev a data record in oracle using only a portion of a composite primary key?
example PK = Col1 + Col2 + Col3
SELECT *
FROM table
WHERE Col1 = 'SomeDate'
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
您可以提出该查询,但它可能不会为您提供单个记录,除非您对该列有唯一约束。但如果你这样做了,我不确定为什么你会有复合主键。
You can pose that query, but it may not give you a single record unless you have a unique constraint on that column. Though if you did, I'm not sure why you'd have the composite primary key.
当然可以,但由于它是复合主键,因此不能保证查询返回唯一或空结果。只保证Col1+Col2+Col3有一个唯一的组合;因此,可能有许多列具有相同的 Col1,除非如 Jody 所说,您已在一列上指定了附加的唯一约束。
Sure, but because it's a composite primary key, the query is not guaranteed to return a unique or empty result. There is only guaranteed to be one unique combination of Col1+Col2+Col3; there could thus be many columns with the same Col1, unless as Jody says you have specified an additional unique constraint on the one column.
是的,可以,而且这是完全正常的。您一直在使用多对多表执行此操作。
这是一个带有复合主键的表。
...使用一些测试数据:
选择部分键将是完全正常的。
但是,请注意,您可能永远依赖对键子集的查询来返回单个记录。该查询迟早会返回多行,并且依赖于它的任何逻辑都会中断。
Yes, you can, and it's perfectly normal. You do it all the time with many-to-many tables.
Here is a table with a composite primary key.
...with some test data:
Selecting on parts of the key would be completely normal.
However, note that you may never ever rely on a query on subset of a key to return a single record. Sooner or later that query will return more than one row, and any logic that depends on it will break.