将新表中的数据分配给for循环中的现有外键

时间:2009-08-23 00:15:21

标签: oracle loops insert foreign-keys

我想知道是否有办法将表中的新数据分配给现有的外键。 例如,如果我使用以下循环将随机生成的数字分配给customer表中的列,我如何能够将我在Sales表中指定为外键的cust_id与每次新创建的新数据相关联出售了吗?

CUSTOMER:
DECLARE
        v_cust_id          NUMBER(4) NOT NULL := 0000;
        v_cust_name        VARCHAR2(30);
        v_cust_add         VARCHAR2(30);
        v_phone            VARCHAR2(10);
BEGIN
        FOR v IN 1 .. 2000 --Loop 2000 times to create data for the 2000 customers in the database.
        LOOP
                v_cust_id := v_cust_id + 1;
                v_cust_name := dbms_random.string('U',5);
                v_cust_add := dbms_random.string('A',15);
                v_phone := dbms_random.value(1000000,9999999);
                        INSERT INTO customer (cust_id, cust_name, cust_add, phone)
                                VALUES (v_cust_id, v_cust_name, v_cust_add, v_phone);
        END LOOP;
END;
/

SALES:
DECLARE
        v_sale_id                 NUMBER(4) NOT NULL := ;
        v_sale_price              NUMBER(8,2);
        v_sale_date               DATE;
        v_no_of_prods             NUMBER(4);
        v_prod_id                 NUMBER(4);
        v_desp_id                 NUMBER(4);
        v_cust_id                 NUMBER(4);
BEGIN
        FOR v IN 1 .. 10
        LOOP
                v_sale_id :=
                v_sale_price
                v_sale_date :=
                v_no_of_products :=
                v_prod_id :=
                v_desp_id :=
                v_cust_id :=
                        INSERT INTO sales (sale_id, sale_price, sale_date, no_of_prods, prod_id, desp_id, cust_id)
                                VALUES (v_sale_id, v_sale_price, v_sale_date, v_no_of_prods, v_prod_id, v_desp_id, v_cust_id);
        END LOOP;
END;
\

1 个答案:

答案 0 :(得分:2)

您是否正在生成测试数据以进行某种性能测试?

让我们首先生成2000个客户。

(未测试的)

insert into customer 
(cust_id, cust_name, cust_add, phone)
select 
level l, 
dbms_random.string('U',5), 
dbms_random.string('A',15), 
dbms_random.value(1000000,9999999) 
from dual
connect by level <= 2000;

现在,您可以通过从客户表中选择来生成销售数据:

insert into sales 
(sale_id, sale_price, sale_date, no_of_prods, prod_id, desp_id, cust_id)
select sale_id_sequence.nextval , dbms_random. ...., cust_id
from customer;

insert into sales 
(sale_id, sale_price, sale_date, no_of_prods, prod_id, desp_id, cust_id)
select sale_id_sequence.nextval , dbms_random. ...., cust_id
from customer
where mod(cust_id,2) =0;

insert into sales 
(sale_id, sale_price, sale_date, no_of_prods, prod_id, desp_id, cust_id)
select sale_id_sequence.nextval , dbms_radom. ...., cust_id
from customer
where mod(cust_id,7) =0;

insert into sales 
(sale_id, sale_price, sale_date, no_of_prods, prod_id, desp_id, cust_id)
select sale_id_sequence.nextval , dbms_random. ...., cust_id
from customer
where mod(cust_id,13) =0;

commit;

我假设有一个序列来创建销售ID。

edit1改进:

create table customer 
( cust_id number(10)
, cust_name varchar2(50)
, cust_add  varchar2(30)
, cust_phone varchar2(10)
);

create sequence cust_id_seq;

create table sales
( sale_id number(10)
, prod_no number(10)
, cust_id number(10)
);

create sequence sale_id_seq;

begin

  insert into customer 
  select cust_id_seq.nextval 
  ,      dbms_random.string('U',5)
  ,      dbms_random.string('A',15) 
  ,      trunc(dbms_random.value(1000000,9999999))
  from   dual
  connect by level < 2000;

  for i in 1..10 loop

    insert into sales 
    select sale_id_seq.nextval
    ,      trunc(dbms_random.value(1,100))
    ,      cust_id
    from   customer;

    insert into sales 
    select sale_id_seq.nextval
    ,      trunc(dbms_random.value(1,100))
    ,      cust_id
    from   customer
    where  mod(cust_id+i,2)=0;

    insert into sales 
    select sale_id_seq.nextval
    ,      trunc(dbms_random.value(1,100))
    ,      cust_id
    from   customer
    where  mod(cust_id+i,7)=0; 

  end loop;

end;
/

commit;

select count(*) from customer;

select count(*) from sales;