1º) Criando as tabelas da base de dados:
create table odi_run.departamentos
(
cd_depto integer,
nm_depto varchar2(300)
)
create table odi_run.funcionarios
(
cd_func integer,
nm_func varchar2(300),
dt_nasc date,
cd_depto integer
)
2º) Criando as chaves das tabelas:
alter table "DEPARTAMENTOS" add constraint pk_depto primary key("CD_DEPTO")
alter table "FUNCIONARIOS" add constraint pk_func primary key("CD_FUNC")
alter table "FUNCIONARIOS" add constraint FK_DEPTO foreign key("CD_DEPTO") references "DEPARTAMENTOS" ("CD_DEPTO")
3º)Criando os blocos PL/SQL para popular as tabelas criadas:
/*POPULA DEPARTAMENTOS*/
declare
v_codigo number;
v_loop number;
v_cont number;
begin
v_codigo := 1;
v_loop := 0;
v_cont := 0;
LOOP
insert into odi_run.departamentos
(
cd_depto,
nm_depto
)
values
(
v_codigo,
'Departamento'||(v_loop+v_codigo)
);
if (v_cont = 1000) then
commit;
v_cont := 0;
else
v_cont:=v_cont+1;
end if;
v_loop := v_loop + 1;
v_codigo := v_codigo +1;
EXIT WHEN v_loop > 3000; --nº de linhas na tabela
END LOOP;
end;
/*POPULA FUNCIONARIOS*/
declare
v_codigo number;
v_loop number;
v_cont number;
v_depto number;
v_aux number;
begin
v_codigo := 1;
v_loop := 0;
v_cont := 0;
v_depto := 1;
v_aux := 0;
LOOP
insert into odi_run.funcionarios
(
cd_func,
nm_func,
dt_nasc,
cd_depto
)
values
(
v_codigo,
'Fulano de Tal '||(v_loop+v_codigo),
SYSDATE-(10000-(v_loop+v_codigo)),
v_depto
);
if (v_cont = 1000) then
commit;
v_cont := 0;
else
v_cont:=v_cont+1;
end if;
v_loop := v_loop + 1;
v_codigo := v_codigo +1;
select max(cd_depto) into v_aux from odi_run.departamentos;
if v_depto >= v_aux then
v_depto := 1;
else
v_depto := v_depto +1;
end if;
EXIT WHEN v_loop > 500000; --nº de linhas
END LOOP;
end;
Nenhum comentário:
Postar um comentário