如何在Oracle中使用序列

时间:2013-07-06 22:02:32

标签: sql oracle oracle11g

我还是SQL新手,我正处于学习阶段。我有一个家庭作业,我已经工作了很长一段时间,我无法弄清楚我的错误在序列中的位置。我正在使用Oracle SQL。

INSERT INTO ORDERS
VALUES (SEQ_ORDER_ID.NEXTVAL, '200', 'MOVIE FOR RENT', '30', '322.61', '15.36', 'CP',          '10-MAR-13', '15-MAR-13');
INSERT INTO ORDERS
VALUES (SEQ_ORDER_ID.NEXTVAL, '200', 'MOVIE FOR RENT', '30', '419.74', '19.99', 'CP',         '12-MAR-13', '17-MAR-13');

INSERT INTO ORDER_ITEMS
VALUES (SEQ_ITEM_ID.NEXTVAL, SEQ_ORDER_ID.CURRVAL, '40', '10', '25', '12.29', '307.25');
INSERT INTO ORDER_ITEMS
VALUES (SEQ_ITEM_ID.NEXTVAL, SEQ_ORDER_ID.CURRVAL, '40', '11', '25', '15.99',     '399.75');

Order_id和Item_id我想用NEXTVAL和CURRVAL进行排序。我得到的错误是:

VALUES (SEQ_ITEM_ID.NEXTVAL, SEQ_ORDER_ID.CURRVAL, '40', '10', '25', '12.29', '307.25')
    *
ERROR at line 2:
ORA-02289: sequence does not exist 


VALUES (SEQ_ITEM_ID.NEXTVAL, SEQ_ORDER_ID.CURRVAL, '40', '11', '25', '15.99', '399.75')
    *
ERROR at line 2:
ORA-02289: sequence does not exist

感谢您的帮助。

以下是更多信息......

create table orders(
order_id                number(8),
Store_id                number(4) not null,  
description             varchar2(30),
total_items             number(3),
total_payment           number(8,2),
tax                     number(6,2),
order_status            varchar2(2),
ordering_date           date,
order_completed_date    date
);

create table order_items(
item_id                   number(10),
order_id                  number(8) not null,
distribution_id           number(8) not null,
movie_id                  number(5) not null, 
number_of_items           number(3),
item_unit_price           number(5,2),
item_sub_total            number(10,2)
);

alter table orders 
add constraint order_pk primary key (order_id);

alter table orders   
add constraint store_order_fk foreign key (store_id) references        movie_rental_stores(store_id);

alter table order_items 
add constraint order_item_pk primary key (item_id);

alter table order_items
add constraint order_item_fk foreign key (order_id) references orders(order_id);

alter table order_items   
add constraint movie_item_fk foreign key (movie_id) references movies(movie_id);

alter table order_items
add constraint distributor_order_item_fk foreign key (distribution_id) references   distributed_movie_list(distribution_id);

DROP sequence seq_order_id;
CREATE sequence seq_order_id 
increment BY 1 START WITH 1 minvalue 1;

DROP sequence seq_order_item_id;
CREATE sequence seq_order_item_id 
increment BY 1 START WITH 1 minvalue 1;

2 个答案:

答案 0 :(得分:3)

ORA-02289是正确的。序列不存在。更仔细地查看您的代码。 您的DDL脚本创建一个具有此名称的序列...

DROP sequence seq_order_item_id;
CREATE sequence seq_order_item_id 

...但您的代码引用了此名称:

VALUES (SEQ_ITEM_ID.NEXTVAL

只需更改INSERT语句,以便它们引用正确的名称,代码就会运行。

答案 1 :(得分:0)

是否在表中命令的相同模式中创建了序列SEQ_ITEM_ID?

已经通过SEQ_ITEM_ID?

创建了公共同义词