我是SQL和Oracle的新手,所以为了练习,我创建了一个虚拟表来跟踪我的打字学习会话(因为我从未学会打字,所以我现在正在为它补偿),并设置了一个序列在Oracle中使用此查询:
CREATE SEQUENCE seq_keyboard_learning
MINVALUE 1
START WITH 1
INCREMENT BY 1
CACHE 10
我的意图是我的id列增加,但每次添加新值时它会从1跳到5等。为了完整起见,以下是我在设置此表时使用的一些查询。
CREATE TABLE keyboard_learning
(
emplid NUMBER CONSTRAINT emplid_pk PRIMARY KEY
,WPM NUMBER
,date_completed DATE
)
CREATE SEQUENCE seq_keyboard_learning
MINVALUE 1
START WITH 1
INCREMENT BY 1
CACHE 10
INSERT INTO keyboard_learning (emplid,wpm,date_completed)
VALUES (seq_keyboard_learning.nextval,15,'12-JUN-2012')
UPDATE keyboard_learning
SET emplid = 1
WHERE emplid = 4
ALTER TABLE keyboard_learning
ADD attempt VARCHAR2(45)
INSERT INTO keyboard_learning
VALUES (seq_keyboard_learning.nextval,26,'6-JUN-2012','ASDFJKL:',2)
而不是每增加4个术语,我该如何调整?感谢
答案 0 :(得分:2)
确保你没有无间隙序列基本上是不可能的。请记住,序列中的get是一个原子操作,因此如果您要插入记录并遇到错误,序列仍会递增。见下面的例子。
拥有缓存也会导致“丢失”序列。如果我在缓存中指定值10,则数据库将从序列中缓存10。如果只插入2行并关闭数据库,则丢弃其他8行。注:由Alex Poole编辑修正。
我希望这有助于理解序列的一些行为。
create table test
(id number,
my_date date);
select seq.currval from dual;
insert into test
(id, my_date)
values (seq.nextval, 'foo'); -- will throw an exception
select seq.currval from dual;
结果是:
table TEST created.
CURRVAL
-------
1
Error starting at line 31 in command: insert into test (id, my_date) values (seq.nextval, 'foo') Error report: SQL Error: ORA-01858: a non-numeric character was found where a numeric was expected
01858. 00000 - "a non-numeric character was found where a numeric was expected"
*Cause: The input data to be converted using a date format model was
incorrect. The input data did not contain a number where a number was
required by the format model.
*Action: Fix the input data or the date format model to make sure the
elements match in number and type. Then retry the operation.
CURRVAL
-------
2