化合物PK查询
使用复合pk时,插入的值可以相同吗?我向你们说明一个例子:
创建表:
Create table test
(
testno number(5) not null,
testpaper varchar(2) not null,
time date
CONSTRAINT Pa_Pks PRIMARY KEY (testno, testpaper)
)
然后这是我要插入的值:
Testno Testpaper Time
12345 22 14-JUL-2011
12345 23 15-JUL-2011
12345 22 16-JUL-2011
正如您所看到的,我的主键在插入过程中具有相同的值。我想这样做的原因是相同的测试号和试卷可能会在不同的日期发生。
如果我想添加相同的值但将其标记为主键,我该怎么做?
这应该是标准且正确的方法:
Create table TEST
(
Testid number(1) not null,
testno number(5) not null,
testpaper varchar(2) not null,
time date
CONSTRAINT Pa_Pks PRIMARY KEY (Testid)
)
感谢您的任何澄清。
when using compound pk, can insertion of values be the same ? I illustrate an example to you guys:
Creation of table:
Create table test
(
testno number(5) not null,
testpaper varchar(2) not null,
time date
CONSTRAINT Pa_Pks PRIMARY KEY (testno, testpaper)
)
Then this is the values that i would like insert:
Testno Testpaper Time
12345 22 14-JUL-2011
12345 23 15-JUL-2011
12345 22 16-JUL-2011
As you can see that my primary keys have the same values during insertion. The reason of why i would like to do that is the same testno and testpaper can happen on different dates.
How can i do that if i would like to add in same values but label it as primary key?
This should be the standard and correct way to do it:
Create table TEST
(
Testid number(1) not null,
testno number(5) not null,
testpaper varchar(2) not null,
time date
CONSTRAINT Pa_Pks PRIMARY KEY (Testid)
)
Thanks for any clarification.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
不,你不能这样做,主键必须是唯一的。但是,您可以做的是添加测试时间作为密钥的一部分。或者您可以使用另一个列名称作为 Id,这将是一个自动增量键。所以你的主键将是ID(自动增量)。
在oracle中,如果你想有一个自动增量键,那么你还必须编写序列和触发器。序列只是一系列数字。触发器的目的是在插入新行时增加数字,并将其存储在列 id 中。 ,您可能还想增加 Number 的大小。对于主键来说,Number(1) 的大小可能不够好。(如果您希望 Oracle 有一个自动增量 ID)或者简单,您可以将 Time 添加到主键。
标准取决于您的要求。这两种方法都被遵循。
No you cant do that, Primary Key has to be Unique. However what you can do is that you can add Test Time as part of the Key. Or you can have another column name as Id, this will be an Auto Increment Key. So your Primary Key will be ID (Auto Increment).
In oracle if you want to have an AutoIncrement Key, then you will have to write sequence and trigger also. A sequence is simply a series of number. The purpose of trigger will be to increment the number whenever new row is inserted, and store it in the column id. , And you may also wanna increase the size of Number. Number(1) might not be good enough size for primary key.(If you want Oracle to have an auto increment Id) OR Simple you can add Time to your primary Key.
The standard depends on your requirement. Both approaches are followed.
如果
time
可以为空,那么它不能形成PRIMARY KEY
的一部分,但它可以是UNIQUE
约束的一部分。请注意,密钥就是密钥,将密钥指定为“主要”是任意的而不是强制的。仅当您一开始就有自然键时,添加
Testid
作为代理键才会起作用。因此:仅自然键:
或自然键加代理键:
If
time
is nullable then it cannot form part of aPRIMARY KEY
but it can be part of eUNIQUE
constraint. Note that a key is a key and designating a key as 'primary' is arbitrary and not compulsory.Adding
Testid
as a surrogate key will only work when you have a natural key to begin with. Therefore:EITHER natural key only:
OR natural key plus surrogate key: