Last active
May 26, 2019 20:59
-
-
Save nathiss/6d88a134cfe81d4cde21dd6709cffde7 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
-- @d:\data.sql | |
-- @D:\RusinKamil_01.sql | |
-- @/d | |
-- @/d.sql | |
-- (^ symlimk) --> @/home/r/r/nathiss/data.sql | |
set LINESIZE 32000; | |
set serveroutput on size 30000; | |
ALTER SESSION SET nls_date_format = 'DD-MON-YYYY HH24:MI'; | |
clear screen; | |
------------------------------------------------- | |
-- DROPS -- | |
------------------------------------------------- | |
delete from XODW_GRA; | |
delete from ODWIEDZAJACY; | |
delete from XOPC_GRA; | |
delete from OPCJA; | |
delete from ROZDZIELCZOSC; | |
delete from XTAG_GRA; | |
delete from TAG; | |
delete from XKOL_GRA; | |
delete from KOLEKCJA; | |
delete from POLUBIENIE; | |
delete from GRAFIKA; | |
delete from KATEGORIA; | |
delete from XUZY_GRU; | |
delete from GRUPA; | |
delete from SUBSKRYPCJA; | |
delete from UZYTKOWNIK; | |
drop table XODW_GRA; | |
drop table ODWIEDZAJACY; | |
drop table XOPC_GRA; | |
drop table OPCJA; | |
drop table ROZDZIELCZOSC; | |
drop table XTAG_GRA; | |
drop table TAG; | |
drop table XKOL_GRA; | |
drop table KOLEKCJA; | |
drop table POLUBIENIE; | |
drop table GRAFIKA; | |
drop table KATEGORIA; | |
drop table XUZY_GRU; | |
drop table GRUPA; | |
drop table SUBSKRYPCJA; | |
drop table UZYTKOWNIK; | |
------------------------------------------------- | |
-- UZYTKOWNIK -- | |
------------------------------------------------- | |
create table UZYTKOWNIK ( | |
UZY_ID number(4) not null, | |
UZY_NAZWA varchar2(20) not null, | |
UZY_PASSWD varchar2(30) , -- hasło null = zablokowane konto | |
UZY_EMAIL varchar2(30) not null, | |
UZY_AKTYWNY number(1) default 1 not null | |
); | |
alter table UZYTKOWNIK | |
add constraint PK_UZY_ID primary key (UZY_ID); | |
alter table UZYTKOWNIK | |
add constraint UNQ_UZY_NAZWA unique (UZY_NAZWA); | |
alter table UZYTKOWNIK | |
add constraint UNQ_UZY_EMAIL unique (UZY_EMAIL); | |
drop sequence SEQ_UZY_ID; | |
create sequence SEQ_UZY_ID start with 1; | |
create or replace trigger UZY_BI_ID | |
before insert on UZYTKOWNIK | |
for each row | |
begin | |
if :new.UZY_ID is null then | |
select SEQ_UZY_ID.NEXTVAL into :new.UZY_ID from dual; | |
end if; | |
end; | |
/ | |
-- create or replace trigger UZY_IO_DELETE | |
-- instead of delete on UZYTKOWNIK | |
-- for each row | |
-- begin | |
-- update UZYTKOWNIK set UZY_AKTYWNY = 0, UZY_PASSWD = null | |
-- where UZY_ID = :old.UZY_ID; | |
-- end; | |
-- / | |
------------------------------------------------- | |
-- SUBSKRYPCJA -- | |
------------------------------------------------- | |
create table SUBSKRYPCJA ( | |
SUB_ID number(4) not null, | |
UZY_KTO number(4) not null, | |
UZY_KOGO number(4) not null | |
); | |
alter table SUBSKRYPCJA | |
add constraint PK_SUB_ID primary key (SUB_ID); | |
alter table SUBSKRYPCJA | |
add constraint FK_SUB_UZY_KTO foreign key (UZY_KTO) | |
references UZYTKOWNIK (UZY_ID); | |
alter table SUBSKRYPCJA | |
add constraint FK_SUB_UZY_KOGO foreign key (UZY_KOGO) | |
references UZYTKOWNIK (UZY_ID); | |
drop sequence SEQ_SUB_ID; | |
create sequence SEQ_SUB_ID start with 1; | |
create or replace trigger SUB_BI_ID | |
before insert on SUBSKRYPCJA | |
for each row | |
begin | |
if :new.SUB_ID is null then | |
select SEQ_SUB_ID.NEXTVAL into :new.SUB_ID from dual; | |
end if; | |
end; | |
/ | |
create or replace trigger SUB_BU | |
before update on SUBSKRYPCJA | |
for each row | |
begin | |
:new.SUB_ID := :old.SUB_ID; | |
end; | |
/ | |
------------------------------------------------- | |
-- GRUPA -- | |
------------------------------------------------- | |
create table GRUPA ( | |
GRU_ID number(4) not null, | |
GRU_NAZWA varchar2(20) not null | |
); | |
alter table GRUPA | |
add constraint PK_GRU_ID primary key (GRU_ID); | |
alter table GRUPA | |
add constraint UQ_GRU_NAZWA unique (GRU_NAZWA); | |
drop sequence SEQ_GRU_ID; | |
create sequence SEQ_GRU_ID start with 1; | |
create or replace trigger GRU_BI_ID | |
before insert on GRUPA | |
for each row | |
begin | |
if :new.GRU_ID is null then | |
select SEQ_GRU_ID.NEXTVAL into :new.GRU_ID from dual; | |
end if; | |
end; | |
/ | |
create or replace trigger GRU_BU | |
before update on GRUPA | |
for each row | |
begin | |
:new.GRU_ID := :old.GRU_ID; | |
end; | |
/ | |
create table XUZY_GRU ( | |
XUZ_ID number(4) not null, | |
UZY_ID number(4) not null, | |
GRU_ID number(4) not null | |
); | |
alter table XUZY_GRU | |
add constraint FK_XUZ_UZY_ID foreign key (UZY_ID) | |
references UZYTKOWNIK (UZY_ID); | |
alter table XUZY_GRU | |
add constraint FK_XUZ_GRU_ID foreign key (GRU_ID) | |
references GRUPA (GRU_ID); | |
drop sequence SEQ_XUZ_ID; | |
create sequence SEQ_XUZ_ID start with 1; | |
create or replace trigger XUZ_BI_ID | |
before insert on XUZY_GRU | |
for each row | |
begin | |
if :new.XUZ_ID is null then | |
select SEQ_XUZ_ID.NEXTVAL into :new.XUZ_ID from dual; | |
end if; | |
end; | |
/ | |
create or replace trigger XUZ_BU | |
before update on XUZY_GRU | |
for each row | |
begin | |
:new.XUZ_ID := :old.XUZ_ID; | |
end; | |
/ | |
------------------------------------------------- | |
-- KATEGORIA -- | |
------------------------------------------------- | |
create table KATEGORIA ( | |
KAT_ID number(4) not null, | |
KAT_NAZWA varchar2(30) not null, | |
KAT_ID_NAD number(4) | |
); | |
alter table KATEGORIA | |
add constraint PK_KAT_ID primary key (KAT_ID); | |
alter table KATEGORIA | |
add constraint FK_KAT_KAT_ID_NAD foreign key (KAT_ID_NAD) | |
references KATEGORIA (KAT_ID); | |
alter table KATEGORIA | |
add constraint UNQ_KAT_NAZWA unique (KAT_NAZWA, KAT_ID_NAD); | |
-- KATEGORIA musi być unikalna na swoim poziomie | |
drop sequence SEQ_KAT_ID; | |
create sequence SEQ_KAT_ID start with 1; | |
create or replace trigger KAT_BI_ID | |
before insert on KATEGORIA | |
for each row | |
begin | |
if :new.KAT_ID is null then | |
select SEQ_KAT_ID.NEXTVAL into :new.KAT_ID from dual; | |
end if; | |
end; | |
/ | |
create or replace trigger KAT_BU | |
before update on KATEGORIA | |
for each row | |
begin | |
:new.KAT_ID := :old.KAT_ID; | |
end; | |
/ | |
------------------------------------------------- | |
-- GRAFIKA -- | |
------------------------------------------------- | |
create table GRAFIKA ( | |
GRA_ID number(4) not null, | |
KAT_ID number(4) , | |
UZY_ID number(4) not null, | |
GRA_NAZWA varchar2(60) not null | |
); | |
alter table GRAFIKA | |
add constraint PK_GRA_ID primary key (GRA_ID); | |
alter table GRAFIKA | |
add constraint FK_GRA_KAT_ID foreign key (KAT_ID) | |
references KATEGORIA (KAT_ID); | |
alter table GRAFIKA | |
add constraint FK_GRA_UZY_ID foreign key (UZY_ID) | |
references UZYTKOWNIK (UZY_ID); | |
--alter table GRAFIKA | |
--add constraint UNQ_GRA_NAZWA unique (GRA_NAZWA, UZY_ID); | |
drop sequence SEQ_GRA_ID; | |
create sequence SEQ_GRA_ID start with 1; | |
create or replace trigger GRA_BI_ID | |
before insert on GRAFIKA | |
for each row | |
begin | |
if :new.GRA_ID is null then | |
select SEQ_GRA_ID.NEXTVAL into :new.GRA_ID from dual; | |
end if; | |
end; | |
/ | |
create or replace trigger GRA_BU | |
before update on GRAFIKA | |
for each row | |
begin | |
:new.GRA_ID := :old.GRA_ID; | |
end; | |
/ | |
------------------------------------------------- | |
-- POLUBIENIE -- | |
------------------------------------------------- | |
create table POLUBIENIE ( | |
POL_ID number(4) not null, | |
UZY_ID number(4) not null, | |
GRA_ID number(4) not null | |
); | |
alter table POLUBIENIE | |
add constraint PK_POL_ID primary key (POL_ID); | |
alter table POLUBIENIE | |
add constraint FK_POL_UZY_ID foreign key (UZY_ID) | |
references UZYTKOWNIK (UZY_ID); | |
alter table POLUBIENIE | |
add constraint FK_POL_GRA_ID foreign key (GRA_ID) | |
references GRAFIKA (GRA_ID); | |
drop sequence SEQ_POL_ID; | |
create sequence SEQ_POL_ID start with 1; | |
create or replace trigger POL_BI_ID | |
before insert on POLUBIENIE | |
for each row | |
begin | |
if :new.POL_ID is null then | |
select SEQ_POL_ID.NEXTVAL into :new.POL_ID from dual; | |
end if; | |
end; | |
/ | |
create or replace trigger POL_BU | |
before update on POLUBIENIE | |
for each row | |
begin | |
:new.POL_ID := :old.POL_ID; | |
end; | |
/ | |
------------------------------------------------- | |
-- KOLEKCJA -- | |
------------------------------------------------- | |
create table KOLEKCJA ( | |
KOL_ID number(4) not null , | |
UZY_ID number(4) not null , | |
KOL_NAZWA varchar2(50) not null , | |
KOL_PUBLICZNA number(1) default 1 not null -- bool | |
); | |
alter table KOLEKCJA | |
add constraint PK_KOL_ID primary key (KOL_ID); | |
alter table KOLEKCJA | |
add constraint FK_UZY_KOL_ID foreign key (UZY_ID) | |
references UZYTKOWNIK (UZY_ID); | |
alter table KOLEKCJA | |
add constraint UNQ_KOL_NAZWA unique (UZY_ID, KOL_NAZWA); | |
-- kolekcja musi być unikalna dla każdego użytkownika | |
drop sequence SEQ_KOL_ID; | |
create sequence SEQ_KOL_ID start with 1; | |
create or replace trigger KOL_BI_ID | |
before insert on KOLEKCJA | |
for each row | |
begin | |
if :new.KOL_ID is null then | |
select SEQ_KOL_ID.NEXTVAL into :new.KOL_ID from dual; | |
end if; | |
end; | |
/ | |
create or replace trigger KOL_BU | |
before update on KOLEKCJA | |
for each row | |
begin | |
:new.KOL_ID := :old.KOL_ID; | |
end; | |
/ | |
create table XKOL_GRA ( | |
XKO_ID number(4) not null, | |
KOL_ID number(4) not null, | |
GRA_ID number(4) not null | |
); | |
alter table XKOL_GRA | |
add constraint PK_XKO_ID primary key (XKO_ID); | |
alter table XKOL_GRA | |
add constraint FK_XKO_KOL_ID foreign key (KOL_ID) | |
references KOLEKCJA (KOL_ID); | |
alter table XKOL_GRA | |
add constraint FK_XKO_GRA_ID foreign key (GRA_ID) | |
references GRAFIKA (GRA_ID); | |
alter table XKOL_GRA | |
add constraint UNQ_XKO_GRAFIKA | |
unique (KOL_ID, GRA_ID); | |
--obraz może należeć do kolekcji tylko raz | |
drop sequence SEQ_XKO_ID; | |
create sequence SEQ_XKO_ID start with 1; | |
create or replace trigger XKO_BI_ID | |
before insert on XKOL_GRA | |
for each row | |
begin | |
if :new.XKO_ID is null then | |
select SEQ_XKO_ID.NEXTVAL into :new.XKO_ID from dual; | |
end if; | |
end; | |
/ | |
create or replace trigger XKO_BU | |
before update on XKOL_GRA | |
for each row | |
begin | |
:new.XKO_ID := :old.XKO_ID; | |
end; | |
/ | |
------------------------------------------------- | |
-- TAG -- | |
------------------------------------------------- | |
create table TAG ( | |
TAG_ID number(4) not null, | |
TAG_NAZWA varchar2(20) not null | |
); | |
alter table TAG | |
add constraint PK_TAG_ID primary key (TAG_ID); | |
alter table TAG | |
add constraint UNQ_TAG_NAZWA unique (TAG_NAZWA); | |
drop sequence SEQ_TAG_ID; | |
create sequence SEQ_TAG_ID start with 1; | |
create or replace trigger TAG_BI_ID | |
before insert on TAG | |
for each row | |
begin | |
if :new.TAG_ID is null then | |
select SEQ_TAG_ID.NEXTVAL into :new.TAG_ID from dual; | |
end if; | |
end; | |
/ | |
create or replace trigger TAG_BU | |
before update on TAG | |
for each row | |
begin | |
:new.TAG_ID := :old.TAG_ID; | |
end; | |
/ | |
create table XTAG_GRA ( | |
XTA_ID number(4) not null, | |
TAG_ID number(4) not null, | |
GRA_ID number(4) not null | |
); | |
alter table XTAG_GRA | |
add constraint PK_XTA_ID primary key (XTA_ID); | |
alter table XTAG_GRA | |
add constraint FK_XTA_TAG_ID foreign key (TAG_ID) | |
references TAG (TAG_ID); | |
alter table XTAG_GRA | |
add constraint FK_XTA_GRA_ID foreign key (GRA_ID) | |
references GRAFIKA (GRA_ID); | |
drop sequence SEQ_XTA_ID; | |
create sequence SEQ_XTA_ID start with 1; | |
create or replace trigger XTA_BI_ID | |
before insert on XTAG_GRA | |
for each row | |
begin | |
if :new.XTA_ID is null then | |
select SEQ_XTA_ID.NEXTVAL into :new.XTA_ID from dual; | |
end if; | |
end; | |
/ | |
create or replace trigger XTA_BU | |
before update on XTAG_GRA | |
for each row | |
begin | |
:new.XTA_ID := :old.XTA_ID; | |
end; | |
/ | |
------------------------------------------------- | |
-- ROZDZIELCZOSC -- | |
------------------------------------------------- | |
create table ROZDZIELCZOSC ( | |
ROZ_ID number(4) not null, | |
GRA_ID number(4) not null, | |
ROZ_DATA blob not null, | |
ROZ_TYP varchar2(15) not null | |
); | |
alter table ROZDZIELCZOSC | |
add constraint PK_ROZ_ID primary key (ROZ_ID); | |
alter table ROZDZIELCZOSC | |
add constraint FK_ROZ_GRA_ID foreign key (GRA_ID) | |
references GRAFIKA (GRA_ID); | |
alter table ROZDZIELCZOSC | |
add constraint UNQ_ROZ_TYP unique (GRA_ID, ROZ_TYP); | |
drop sequence SEQ_ROZ_ID; | |
create sequence SEQ_ROZ_ID start with 1; | |
create or replace trigger ROZ_BI_ID | |
before insert on ROZDZIELCZOSC | |
for each row | |
begin | |
if :new.ROZ_ID is null then | |
select SEQ_ROZ_ID.NEXTVAL into :new.ROZ_ID from dual; | |
end if; | |
end; | |
/ | |
create or replace trigger ROZ_BU | |
before update on ROZDZIELCZOSC | |
for each row | |
begin | |
:new.ROZ_ID := :old.ROZ_ID; | |
end; | |
/ | |
------------------------------------------------- | |
-- OPCJA -- | |
------------------------------------------------- | |
create table OPCJA ( | |
OPC_ID number(4) not null, | |
OPC_NAZWA varchar2(20) not null, | |
OPC_WARTOSC varchar2(100) | |
); | |
alter table OPCJA | |
add constraint PK_OPC_ID primary key (OPC_ID); | |
drop sequence SEQ_OPC_ID; | |
create sequence SEQ_OPC_ID start with 1; | |
create or replace trigger OPC_BI_ID | |
before insert on OPCJA | |
for each row | |
begin | |
if :new.OPC_ID is null then | |
select SEQ_OPC_ID.NEXTVAL into :new.OPC_ID from dual; | |
end if; | |
end; | |
/ | |
create or replace trigger OPC_BU | |
before update on OPCJA | |
for each row | |
begin | |
:new.OPC_ID := :old.OPC_ID; | |
end; | |
/ | |
create table XOPC_GRA ( | |
XOP_ID number(4) not null, | |
OPC_ID number(4) not null, | |
GRA_ID number(4) not null | |
); | |
alter table XOPC_GRA | |
add constraint PK_XOP_ID primary key (XOP_ID); | |
alter table XOPC_GRA | |
add constraint FK_XOP_OPC_ID foreign key (OPC_ID) | |
references OPCJA (OPC_ID); | |
alter table XOPC_GRA | |
add constraint FK_XOP_GRA_ID foreign key (GRA_ID) | |
references GRAFIKA (GRA_ID); | |
drop sequence SEQ_XOP_ID; | |
create sequence SEQ_XOP_ID start with 1; | |
create or replace trigger XOP_BI_ID | |
before insert on XOPC_GRA | |
for each row | |
begin | |
if :new.XOP_ID is null then | |
select SEQ_XOP_ID.NEXTVAL into :new.XOP_ID from dual; | |
end if; | |
end; | |
/ | |
create or replace trigger XOP_BU | |
before update on XOPC_GRA | |
for each row | |
begin | |
:new.XOP_ID := :old.XOP_ID; | |
end; | |
/ | |
------------------------------------------------- | |
-- ODWIEDZAJACY -- | |
------------------------------------------------- | |
create table ODWIEDZAJACY ( | |
ODW_ID number(4) not null, | |
ODW_IP varchar2(15) not null -- IPv4 (IPv6?) | |
); | |
alter table ODWIEDZAJACY | |
add constraint PK_ODW_ID primary key (ODW_ID); | |
alter table ODWIEDZAJACY | |
add constraint UNQ_ODW_IP unique (ODW_IP); | |
drop sequence SEQ_ODW_ID; | |
create sequence SEQ_ODW_ID start with 1; | |
create or replace trigger ODW_BI_ID | |
before insert on ODWIEDZAJACY | |
for each row | |
begin | |
if :new.ODW_ID is null then | |
select SEQ_ODW_ID.NEXTVAL into :new.ODW_ID from dual; | |
end if; | |
end; | |
/ | |
create or replace trigger ODW_BU | |
before update on ODWIEDZAJACY | |
for each row | |
begin | |
:new.ODW_ID := :old.ODW_ID; | |
end; | |
/ | |
create table XODW_GRA ( | |
XOD_ID number(4) not null, | |
ODW_ID number(4) not null, | |
GRA_ID number(4) not null | |
); | |
alter table XODW_GRA | |
add constraint PK_XOD_ID primary key (XOD_ID); | |
alter table XODW_GRA | |
add constraint FK_XOD_ODW_ID foreign key (ODW_ID) | |
references ODWIEDZAJACY (ODW_ID); | |
alter table XODW_GRA | |
add constraint FK_XOD_GRA_ID foreign key (GRA_ID) | |
references GRAFIKA (GRA_ID); | |
drop sequence SEQ_XOD_ID; | |
create sequence SEQ_XOD_ID start with 1; | |
create or replace trigger XOD_BI_ID | |
before insert on XODW_GRA | |
for each row | |
begin | |
if :new.XOD_ID is null then | |
select SEQ_XOD_ID.NEXTVAL into :new.XOD_ID from dual; | |
end if; | |
end; | |
/ | |
create or replace trigger XOD_BU | |
before update on XODW_GRA | |
for each row | |
begin | |
:new.XOD_ID := :old.XOD_ID; | |
end; | |
/ | |
------------------------------------------------------------------ | |
-- Indexes -- | |
------------------------------------------------------------------ | |
create index FIX_UZYTKOWNIK | |
on UZYTKOWNIK (UZY_NAZWA, upper(UZY_EMAIL), UZY_PASSWD) | |
storage (initial 10k next 10k) | |
tablespace STUDENT_INDEX; | |
create index FIX_KATEGORIA | |
on KATEGORIA (KAT_NAZWA) | |
storage (initial 10k next 10k) | |
tablespace STUDENT_INDEX; | |
create index FIX_GRAFIKA | |
on GRAFIKA (GRA_NAZWA) | |
storage (initial 10k next 10k) | |
tablespace STUDENT_INDEX; | |
create index FIX_OPCJA | |
on OPCJA (upper(OPC_NAZWA)) | |
storage (initial 10k next 10k) | |
tablespace STUDENT_INDEX; | |
------------------------------------------------- | |
-- INSERTS -- | |
------------------------------------------------- | |
-- KATEGORIA -- | |
-- Natura --- | |
-- / \ \ | |
-- / \ \ | |
-- Ocean Las Pustynia | |
insert into KATEGORIA(KAT_NAZWA, KAT_ID_NAD) values ('Natura', null); | |
insert all | |
into KATEGORIA(KAT_NAZWA, KAT_ID_NAD) values | |
('Ocean', (select KAT_ID from KATEGORIA where KAT_NAZWA = 'Natura')) | |
into KATEGORIA(KAT_NAZWA, KAT_ID_NAD) values | |
('Las', (select KAT_ID from KATEGORIA where KAT_NAZWA = 'Natura')) | |
into KATEGORIA(KAT_NAZWA, KAT_ID_NAD) values | |
('Pustynia', (select KAT_ID from KATEGORIA where KAT_NAZWA = 'Pustynia')) | |
select * from dual; | |
-- Cywilizacja --- | |
-- / \ \ | |
-- / \ \ | |
-- Tech Miasto Światła | |
-- / \ | |
-- / \ | |
-- / \ | |
-- Metropolia Miasteczko | |
insert into KATEGORIA(KAT_NAZWA, KAT_ID_NAD) values ('Cywilizacja', null); | |
insert all | |
into KATEGORIA(KAT_NAZWA, KAT_ID_NAD) values | |
('Tech', (select KAT_ID from KATEGORIA where KAT_NAZWA = 'Cywilizacja')) | |
into KATEGORIA(KAT_NAZWA, KAT_ID_NAD) values | |
('Światła', (select KAT_ID from KATEGORIA where KAT_NAZWA = 'Cywilizacja')) | |
into KATEGORIA(KAT_NAZWA, KAT_ID_NAD) values | |
('Miasto', (select KAT_ID from KATEGORIA where KAT_NAZWA = 'Cywilizacja')) | |
into KATEGORIA(KAT_NAZWA, KAT_ID_NAD) values | |
('Metropolia', (select KAT_ID from KATEGORIA where KAT_NAZWA = 'Miasto')) | |
into KATEGORIA(KAT_NAZWA, KAT_ID_NAD) values | |
('Miasteczko', (select KAT_ID from KATEGORIA where KAT_NAZWA = 'Miasto')) | |
select * from dual; | |
-- GRUPA -- | |
insert all | |
into GRUPA(GRU_NAZWA) values | |
('admin') | |
into GRUPA(GRU_NAZWA) values | |
('uzytkownik') | |
select * from dual; | |
-- UZYTKOWNIK -- | |
insert all | |
into UZYTKOWNIK(UZY_NAZWA, UZY_PASSWD, UZY_EMAIL) values | |
('root', 'root1', '[email protected]') | |
into UZYTKOWNIK(UZY_NAZWA, UZY_PASSWD, UZY_EMAIL) values | |
('user1', 'user1', '[email protected]') | |
into UZYTKOWNIK(UZY_NAZWA, UZY_PASSWD, UZY_EMAIL) values | |
('user2', 'user2', '[email protected]') | |
into UZYTKOWNIK(UZY_NAZWA, UZY_PASSWD, UZY_EMAIL) values | |
('user3', 'user3', '[email protected]') | |
select * from dual; | |
-- KOLEKCJA -- | |
insert all | |
into KOLEKCJA(UZY_ID, KOL_NAZWA, KOL_PUBLICZNA) values | |
(1, 'Ulubione', 0) | |
into KOLEKCJA(UZY_ID, KOL_NAZWA, KOL_PUBLICZNA) values | |
(2, 'Ulubione', 1) | |
into KOLEKCJA(UZY_ID, KOL_NAZWA, KOL_PUBLICZNA) values | |
(1, 'Zwierzęta', 1) | |
into KOLEKCJA(UZY_ID, KOL_NAZWA) values | |
(1, 'Domyślna wartość') | |
select * from dual; | |
-- XUZY_GRU -- | |
insert into XUZY_GRU(UZY_ID, GRU_ID) values ( | |
(select UZY_ID from UZYTKOWNIK where UZY_NAZWA = 'user1'), | |
(select GRU_ID from GRUPA where GRU_NAZWA like '%dm%' | |
fetch first 1 rows only) | |
); | |
-- SUBSKRYPCJA -- | |
insert into SUBSKRYPCJA(UZY_KTO, UZY_KOGO) values ( | |
(select UZY_ID from UZYTKOWNIK where UZY_NAZWA = 'user1'), | |
(select UZY_ID from UZYTKOWNIK where UZY_NAZWA = 'user2') | |
); | |
-- ODWIEDZAJACY -- | |
insert all | |
into ODWIEDZAJACY(ODW_IP) values | |
('127.0.0.1') | |
into ODWIEDZAJACY(ODW_IP) values | |
('192.168.0.1') | |
into ODWIEDZAJACY(ODW_IP) values | |
('8.8.8.8') | |
into ODWIEDZAJACY(ODW_IP) values | |
('210.56.74.12') | |
into ODWIEDZAJACY(ODW_IP) values | |
('125.89.0.0') | |
into ODWIEDZAJACY(ODW_IP) values | |
('255.255.255.255') | |
select * from dual; | |
-- TAG -- | |
insert all | |
into TAG(TAG_NAZWA) values | |
('Modern') | |
into TAG(TAG_NAZWA) values | |
('Sky') | |
into TAG(TAG_NAZWA) values | |
('Kolor') | |
into TAG(TAG_NAZWA) values | |
('Technologia') | |
into TAG(TAG_NAZWA) values | |
('Pies') | |
into TAG(TAG_NAZWA) values | |
('Ruch') | |
select * from dual; | |
-- OPCJA -- | |
insert all | |
into OPCJA(OPC_NAZWA, OPC_WARTOSC) values | |
('Licencja', 'Własność publiczna') | |
into OPCJA(OPC_NAZWA, OPC_WARTOSC) values | |
('Licencja', 'CC') | |
into OPCJA(OPC_NAZWA, OPC_WARTOSC) values | |
('Licencja', null) | |
into OPCJA(OPC_NAZWA, OPC_WARTOSC) values | |
('Do pobrania', 'Tak') | |
into OPCJA(OPC_NAZWA, OPC_WARTOSC) values | |
('Do pobrania', 'Nie') | |
into OPCJA(OPC_NAZWA, OPC_WARTOSC) values | |
('Opis', 'Lorem ipsum dolor sid..........') | |
into OPCJA(OPC_NAZWA, OPC_WARTOSC) values | |
('Opis', 'DardzoDługiOpisX2') | |
into OPCJA(OPC_NAZWA, OPC_WARTOSC) values | |
('Opis', null) | |
select * from dual; | |
-- GRAFIKA -- | |
insert all | |
into GRAFIKA(KAT_ID, UZY_ID, GRA_NAZWA) values | |
( | |
(select KAT_ID from KATEGORIA where KAT_NAZWA = 'Las' | |
fetch first 1 rows only), | |
(select UZY_ID from UZYTKOWNIK where UZY_NAZWA = 'root'), | |
'Liść' | |
) | |
into GRAFIKA(KAT_ID, UZY_ID, GRA_NAZWA) values | |
( | |
(select KAT_ID from KATEGORIA where KAT_NAZWA = 'Ocean' | |
fetch first 1 rows only), | |
(select UZY_ID from UZYTKOWNIK where UZY_NAZWA = 'user1'), | |
'Ryba' | |
) | |
into GRAFIKA(KAT_ID, UZY_ID, GRA_NAZWA) values | |
( | |
(select KAT_ID from KATEGORIA where KAT_NAZWA = 'Pustynia' | |
fetch first 1 rows only), | |
(select UZY_ID from UZYTKOWNIK where UZY_NAZWA = 'user3'), | |
'Piasek' | |
) | |
select * from dual; | |
-- ROZDZIELCZOSC -- | |
insert all | |
into ROZDZIELCZOSC(GRA_ID, ROZ_DATA, ROZ_TYP) values | |
( | |
(select GRA_ID from GRAFIKA where GRA_NAZWA = 'Liść' and UZY_ID = 1), | |
RAWTOHEX('/s/leaf.jpg'), | |
'220x220' | |
) | |
select * from dual; | |
-- POLUBIENIE -- | |
insert into POLUBIENIE(UZY_ID, GRA_ID) values ( | |
(select UZY_ID from UZYTKOWNIK where UZY_NAZWA = 'root'), | |
(select GRA_ID from GRAFIKA where GRA_NAZWA = 'Liść') | |
); | |
-- XGRA_ODW -- | |
insert all | |
into XODW_GRA(GRA_ID, ODW_ID) values | |
((select GRA_ID from GRAFIKA where GRA_NAZWA = 'Liść'), 1) | |
into XODW_GRA(GRA_ID, ODW_ID) values | |
((select GRA_ID from GRAFIKA where GRA_NAZWA = 'Liść'), 3) | |
into XODW_GRA(GRA_ID, ODW_ID) values | |
((select GRA_ID from GRAFIKA where GRA_NAZWA = 'Liść'), 5) | |
select * from dual; | |
-- XTAG_GRA -- | |
insert all | |
into XTAG_GRA(TAG_ID, GRA_ID) values | |
((select TAG_ID from TAG where TAG_NAZWA = 'Modern'), | |
(select GRA_ID from GRAFIKA where GRA_NAZWA = 'Liść')) | |
into XTAG_GRA(TAG_ID, GRA_ID) values | |
((select TAG_ID from TAG where TAG_NAZWA = 'Kolor'), | |
(select GRA_ID from GRAFIKA where GRA_NAZWA = 'Liść')) | |
into XTAG_GRA(TAG_ID, GRA_ID) values | |
((select TAG_ID from TAG where TAG_NAZWA = 'Pies'), | |
(select GRA_ID from GRAFIKA where GRA_NAZWA = 'Liść')) | |
select * from dual; | |
-- XKOL_GRA -- | |
insert into XKOL_GRA(KOL_ID, GRA_ID) values ( | |
(select KOL_ID from KOLEKCJA where KOL_NAZWA = 'Zwierzęta'), | |
(select GRA_ID from GRAFIKA where GRA_NAZWA = 'Liść') | |
); | |
-- XOPC_GRA -- | |
insert all | |
into XOPC_GRA(GRA_ID, OPC_ID) values | |
((select GRA_ID from GRAFIKA where GRA_NAZWA = 'Liść'), | |
(select OPC_ID from OPCJA | |
where OPC_NAZWA = 'Licencja' and OPC_WARTOSC = 'CC')) | |
into XOPC_GRA(GRA_ID, OPC_ID) values | |
((select GRA_ID from GRAFIKA where GRA_NAZWA = 'Liść'), | |
(select OPC_ID from OPCJA | |
where OPC_NAZWA = 'Opis' fetch first 1 rows only)) | |
select * from dual; | |
------------------------- SELECTS ---------------------------------- | |
column UZY_NAZWA format A10; | |
column UZY_EMAIL format A10; | |
column GRA_NAZWA format A10; | |
column OPC_NAZWA format A10; | |
column OPC_WARTOSC format A10; | |
column KAT_NAZWA format A10; | |
-- JOIN / ON -- | |
select u.UZY_NAZWA, u.UZY_EMAIL, g.GRA_NAZWA from UZYTKOWNIK u | |
join GRAFIKA g on g.UZY_ID = u.UZY_ID | |
where g.UZY_ID = u.UZY_ID | |
and KAT_ID is not null; | |
select k.KAT_NAZWA, g.GRA_NAZWA from KATEGORIA k | |
join GRAFIKA g on UZY_ID = g.UZY_ID | |
where k.KAT_ID = g.KAT_ID; | |
select g.GRA_NAZWA, u.UZY_NAZWA, u.UZY_EMAIL from GRAFIKA g | |
join UZYTKOWNIK u on u.UZY_ID = g.UZY_ID | |
where UZY_NAZWA like 'user%'; | |
-- GROUP BY -- | |
select count(OPC_ID), OPC_NAZWA | |
from OPCJA | |
group by OPC_NAZWA | |
having count(OPC_ID) > 2; | |
select count(KAT_ID), KAT_NAZWA | |
from KATEGORIA | |
group by KAT_NAZWA; | |
select count(KOL_ID), KOL_NAZWA | |
from KOLEKCJA | |
group by KOL_NAZWA; | |
select count(OPC_ID), OPC_NAZWA from OPCJA group by OPC_NAZWA; | |
/* | |
COUNT(OPC_ID) OPC_NAZWA | |
------------- ------------------- | |
2 Do pobrania | |
3 Licencja | |
3 Opis | |
*/ | |
select OPC_ID, OPC_NAZWA from OPCJA where OPC_ID not in (1,3); | |
/* | |
OPC_ID OPC_NAZWA | |
---------- ------------------ | |
2 Licencja | |
4 Do pobrania | |
5 Do pobrania | |
*/ | |
select distinct OPC_NAZWA from OPCJA; | |
/* | |
OPC_NAZWA | |
------------------- | |
Do pobrania | |
Licencja | |
Opis | |
*/ | |
------------------------------------------------------------------------------------- | |
create or replace view V_UZY_DANE | |
(UZY_ID, UZY_NAZWA, UZY_EMAIL, UZY_PASSWD, GRA_ID, KAT_ID, GRA_NAZWA) as | |
select u.UZY_ID, u.UZY_NAZWA, u.UZY_EMAIL, u.UZY_PASSWD, | |
g.GRA_ID, g.KAT_ID, g.GRA_NAZWA | |
from UZYTKOWNIK u | |
join GRAFIKA g on u.UZY_ID = g.UZY_ID | |
where u.UZY_ID = g.UZY_ID | |
with check option; | |
create or replace trigger V_UZY_DANE_II | |
instead of insert on V_UZY_DANE | |
for each row | |
begin | |
insert into UZYTKOWNIK (UZY_NAZWA, UZY_EMAIL, UZY_PASSWD) | |
values (:new.UZY_NAZWA, :new.UZY_EMAIL, :new.UZY_PASSWD); | |
insert into GRAFIKA (UZY_ID, KAT_ID, GRA_NAZWA) values | |
( | |
(select UZY_ID from UZYTKOWNIK where UZY_NAZWA = :new.UZY_NAZWA), | |
:new.KAT_ID, :new.GRA_NAZWA | |
); | |
end; | |
/ | |
create or replace trigger V_UZY_DANE_IU | |
instead of update on V_UZY_DANE | |
for each row | |
begin | |
update UZYTKOWNIK | |
set UZY_NAZWA = :new.UZY_NAZWA, UZY_EMAIL = :new.UZY_EMAIL, | |
UZY_PASSWD = :new.UZY_PASSWD where UZY_ID = :new.UZY_ID; | |
update GRAFIKA | |
set KAT_ID = :new.KAT_ID, GRA_NAZWA = :new.GRA_NAZWA | |
where GRA_ID = :new.GRA_ID; | |
end; | |
/ | |
create or replace trigger V_UZY_DANE_ID | |
instead of delete on V_UZY_DANE | |
for each row | |
begin | |
delete from GRAFIKA where GRA_ID = :old.GRA_ID; | |
delete from UZYTKOWNIK where UZY_ID = :old.UZY_ID; | |
end; | |
/ | |
insert into V_UZY_DANE (UZY_NAZWA, UZY_EMAIL, UZY_PASSWD, KAT_ID, GRA_NAZWA) | |
values ('kot', '[email protected]', 'passwd', 1, 'obraz'); | |
update V_UZY_DANE set UZY_NAZWA = 'kot2' where UZY_NAZWA = 'kot'; | |
delete from V_UZY_DANE where UZY_NAZWA = 'kot2'; | |
--------------------------------------------------------------------------------- | |
create or replace view V_GRA_DANE | |
(GRA_ID, GRA_NAZWA, KAT_ID, UZY_ID, ROZ_ID, ROZ_DATA, ROZ_TYP) as | |
select g.GRA_ID, g.GRA_NAZWA, g.KAT_ID, g.UZY_ID, | |
r.ROZ_ID, r.ROZ_DATA, r.ROZ_TYP | |
from GRAFIKA g | |
join ROZDZIELCZOSC r on r.GRA_ID = g.GRA_ID | |
where r.GRA_ID = g.GRA_ID | |
with check option; | |
create or replace trigger V_GRA_DANE_II | |
instead of insert on V_GRA_DANE | |
for each row | |
begin | |
insert into GRAFIKA (GRA_NAZWA, KAT_ID, UZY_ID) | |
values (:new.GRA_NAZWA, :new.KAT_ID, :new.UZY_ID); | |
insert into ROZDZIELCZOSC (GRA_ID, ROZ_DATA, ROZ_TYP) | |
values ( | |
(select GRA_ID from GRAFIKA where GRA_NAZWA = :new.GRA_NAZWA), | |
:new.ROZ_DATA, :new.ROZ_TYP | |
); | |
end; | |
/ | |
create or replace trigger V_GRA_DANE_IU | |
instead of update on V_GRA_DANE | |
for each row | |
begin | |
update GRAFIKA | |
set GRA_NAZWA = :new.GRA_NAZWA, KAT_ID = :new.KAT_ID, | |
UZY_ID = :new.UZY_ID where GRA_ID = :new.GRA_ID; | |
update ROZDZIELCZOSC | |
set ROZ_DATA = :new.ROZ_DATA where ROZ_ID = :new.ROZ_ID; | |
end; | |
/ | |
create or replace trigger V_GRA_DANE_ID | |
instead of delete on V_GRA_DANE | |
for each row | |
begin | |
delete from ROZDZIELCZOSC where ROZ_ID = :old.ROZ_ID; | |
delete from GRAFIKA where GRA_ID = :old.GRA_ID; | |
end; | |
/ | |
insert into V_GRA_DANE (GRA_NAZWA, KAT_ID, UZY_ID, ROZ_DATA, ROZ_TYP) | |
values ('abra', null, 1, RAWTOHEX('data'), '720p'); | |
update V_GRA_DANE set GRA_NAZWA = 'abra' | |
where GRA_ID = (select GRA_ID from V_GRA_DANE where GRA_NAZWA = 'abra'); | |
delete from V_GRA_DANE where GRA_NAZWA = 'abra'; | |
create or replace view V_UZYTKOWNIK | |
(UZY_ID, UZY_NAZWA, UZY_EMAIL, UZY_PASSWD) as | |
select UZY_ID, UZY_NAZWA, UZY_EMAIL, UZY_PASSWD from UZYTKOWNIK; | |
create or replace trigger V_UZY_ID | |
instead of delete on V_UZYTKOWNIK | |
for each row | |
begin | |
update UZYTKOWNIK set UZY_AKTYWNY = 0 where UZY_ID = :old.UZY_ID; | |
end; | |
/ | |
create or replace trigger V_UZY_ID | |
instead of update on V_UZYTKOWNIK | |
for each row | |
begin | |
update UZYTKOWNIK | |
set UZY_NAZWA = :new.UZY_NAZWA, UZY_EMAIL = :new.UZY_NAZWA, | |
UZY_PASSWD = :new.UZY_PASSWD | |
where UZY_ID = :new.UZY_ID; | |
end; | |
/ | |
analyze index FIX_UZYTKOWNIK validate structure; | |
select btree_space, used_space, pct_used, LF_ROWS, BLOCKS | |
from index_stats where name = 'FIX_UZYTKOWNIK'; | |
/* | |
BTREE_SPACE USED_SPACE PCT_USED LF_ROWS BLOCKS | |
----------- ---------- ---------- ---------- ---------- | |
7996 217 3 6 8 | |
*/ | |
analyze index FIX_KATEGORIA validate structure; | |
select btree_space, used_space, pct_used, LF_ROWS, BLOCKS | |
from index_stats where name = 'FIX_KATEGORIA'; | |
/* | |
BTREE_SPACE USED_SPACE PCT_USED LF_ROWS BLOCKS | |
----------- ---------- ---------- ---------- ---------- | |
7996 197 3 10 8 | |
*/ | |
analyze index FIX_GRAFIKA validate structure; | |
select btree_space, used_space, pct_used, LF_ROWS, BLOCKS | |
from index_stats where name = 'FIX_GRAFIKA'; | |
/* | |
BTREE_SPACE USED_SPACE PCT_USED LF_ROWS BLOCKS | |
----------- ---------- ---------- ---------- ---------- | |
7996 74 1 4 8 | |
*/ | |
analyze index FIX_OPCJA validate structure; | |
select btree_space, used_space, pct_used, LF_ROWS, BLOCKS | |
from index_stats where name = 'FIX_OPCJA'; | |
/* | |
BTREE_SPACE USED_SPACE PCT_USED LF_ROWS BLOCKS | |
----------- ---------- ---------- ---------- ---------- | |
7996 154 2 8 8 | |
*/ | |
select UZY_NAZWA, UZY_EMAIL from UZYTKOWNIK where UPPER(UZY_NAZWA) = 'ROOT'; | |
/* | |
UZY_NAZWA UZY_EMAIL | |
---------- ----------------- | |
root [email protected] | |
*/ | |
select OPC_WARTOSC from OPCJA where UPPER(OPC_NAZWA) = 'DO POBRANIA'; | |
/* | |
OPC_WARTOSC | |
----------- | |
Tak | |
Nie | |
*/ | |
savepoint abc; | |
insert into GRUPA(GRU_NAZWA) values ('ABC'); | |
select * from GRUPA; | |
/* | |
GRU_ID GRU_NAZWA | |
---------- -------------------- | |
3 ABC | |
1 admin | |
2 uzytkownik | |
*/ | |
rollback to abc; | |
select * from GRUPA; | |
/* | |
GRU_ID GRU_NAZWA | |
---------- -------------------- | |
1 admin | |
2 uzytkownik | |
*/ | |
/* | |
create or replace procedure gen_uzy(maks number) is | |
idx number(10); | |
begin | |
idx := 1; | |
while maks => idx | |
loop | |
insert into UZYTKOWNIK(UZY_NAZWA, UZY_PASSWD, UZY_EMAIL) | |
values ('abc' || idx, 'passwd', 'abc' || idx || '@gmail.com'); | |
idx := idx + 1; | |
end loop; | |
end; | |
/ | |
begin | |
gen_uzy(5); | |
end; | |
/*/ | |
/* | |
UZY_ID UZY_NAZWA UZY_PASSWD UZY_EMAIL UZY_AKTYWNY | |
---------- ---------- ------------------------------ ---------- ----------- | |
7 abc2 passwd [email protected] 1 | |
6 abc1 passwd [email protected] 1 | |
8 abc3 passwd [email protected] 1 | |
9 abc4 passwd [email protected] 1 | |
10 abc4 passwd [email protected] 1 | |
*/ | |
/* | |
create or replace procedure wszyscy_do_grupy(nazwa GRUPA.GRU_NAZWA%TYPE) is | |
grupa_idx GRUPA.GRU_ID%TYPE; | |
begin | |
insert into GRUPA(GRU_NAZWA) values(nazwa); | |
select GRU_ID into grupa_idx from GRUPA where GRU_NAZWA = nazwa; | |
for uzytkownik_idx in (select UZY_ID from UZYTKOWNIK) | |
loop | |
insert into XUZY_GRU(UZY_ID, GRU_ID) values (uzytkownik_idx, grupa_idx); | |
end loop; | |
end; | |
/ | |
begin | |
wszyscy_do_grupy('admin'); | |
end; | |
/*/ | |
/* | |
UZY_ID GRU_ID | |
---------- ---------- | |
2 1 | |
1 1 | |
3 1 | |
4 1 | |
6 1 | |
7 1 | |
9 1 | |
10 1 | |
7 1 | |
*/ | |
create or replace procedure pokaz_date is | |
czas varchar(40); | |
begin | |
czas := to_char(sysdate, 'dd.mm.yyyy HH24:MI:SS'); | |
dbms_output.put_line('Teraz: ' || czas); | |
end; | |
/ | |
begin | |
pokaz_date(); | |
end; | |
/ | |
/* | |
Czas: 29.04.2019 08:37:23 | |
Procedura PL/SQL została zakończona pomyślnie. | |
*/ | |
/* | |
create or replace function admin_przed_nazwe(nazwa UZYTKOWNIK.UZY_NAZWA%TYPE) | |
return UZYTKOWNIK.UZY_ID%TYPE | |
is | |
idx_grupy GRUPA.GRU_ID%TYPE; | |
idx_uzytkownika UZYTKOWNIK.UZY_ID%TYPE; | |
begin | |
select GRU_ID into idx_grupy from GRUPA where GRU_NAZWA = 'admin'; | |
select UYZ_ID into idx_uzytkownika from UZYTKOWNIK where UZY_NAZWA = nazwa; | |
insert into XUZY_GRU(UZY_ID, GRU_ID) values (idx_uzytkownika, idx_grupy); | |
return idx_uzytkownika; | |
end; | |
/ | |
begin | |
admin_przed_nazwe('root') | |
end; | |
/*/ | |
create or replace procedure dodaj_uzy_gru( | |
nazwa UZYTKOWNIK.UZY_NAZWA%TYPE, | |
email UZYTKOWNIK.UZY_EMAIL%TYPE, | |
haslo UZYTKOWNIK.UZY_PASSWD%TYPE, | |
gru_nazwa GRUPA.GRU_NAZWA%TYPE | |
) | |
is | |
cursor uzy_cursor is select * from UZYTKOWNIK | |
where UZY_NAZWA = nazwa and | |
UZY_EMAIL = email and | |
UZY_PASSWD = HASLO; | |
cursor gru_cursor is select * from GRUPA | |
where GRU_NAZWA = gru_nazwa; | |
uzy UZYTKOWNIK%ROWTYPE; | |
gru GRUPA%ROWTYPE; | |
begin | |
savepoint BEFORE_PROCEDURE; | |
insert into UZYTKOWNIK(UZY_NAZWA, UZY_EMAIL, UZY_PASSWD) | |
values (nazwa, email, haslo); | |
insert into GRUPA(GRU_NAZWA) values (gru_nazwa); | |
open uzy_cursor; | |
open gru_cursor; | |
fetch uzy_cursor into uzy; | |
fetch gru_cursor into gru; | |
if uzy_cursor%FOUND and gru_cursor%FOUND then | |
dbms_output.put_line('SUCCESS'); | |
commit; | |
else | |
dbms_output.put_line('FAIL'); | |
rollback to BEFORE_PROCEDURE; | |
end if; | |
if uzy_cursor%ISOPEN then | |
close uzy_cursor; | |
end if; | |
if gru_cursor%ISOPEN then | |
close gru_cursor; | |
end if; | |
end; | |
/ | |
begin | |
dodaj_uzy_gru('foo_user', '[email protected]', 'foo_passwd', 'foo_group'); | |
end; | |
/ | |
-- SUCCESS -- | |
create or replace procedure gen_los_uzy | |
is | |
nazwa UZYTKOWNIK.UZY_NAZWA%TYPE; | |
email UZYTKOWNIK.UZY_EMAIL%TYPE; | |
haslo UZYTKOWNIK.UZY_PASSWD%TYPE; | |
begin | |
for idx in 1..10 | |
loop | |
nazwa := DBMS_RANDOM.STRING('A', 5); | |
email := DBMS_RANDOM.STRING('B', 7) || '@example.com'; | |
haslo := DBMS_RANDOM.STRING('C', 10); | |
insert into UZYTKOWNIK(UZY_NAZWA, UZY_EMAIL, UZY_PASSWD) | |
values (nazwa, email, haslo); | |
end loop; | |
end; | |
/ | |
begin | |
gen_los_uzy; | |
end; | |
/ | |
/* | |
NAZWA EMAIL HASLO | |
----------- ---------------------- ------------- | |
PMEBY [email protected] CUTLZLIXCO | |
ZASNE [email protected] NTFBGQWCYP | |
VIRVR [email protected] MMULXAUWYK | |
VMAQF [email protected] TKFEAXHJUD | |
WYYMI [email protected] FPDSRXJEIP | |
AORUR [email protected] JZJAXCNYEW | |
FTZGS [email protected] LHSXZZWCPA | |
JOXWS [email protected] YMSYRXNVPI | |
LEJEI [email protected] LLQTVEPEUZ | |
LHYIA [email protected] ZTEQUFQDNJ | |
*/ | |
create or replace procedure P_UZY_INS( | |
nazwa UZYTKOWNIK.UZY_NAZWA%TYPE, | |
email UZYTKOWNIK.UZY_NAZWA%TYPE, | |
haslo UZYTKOWNIK.UZY_PASSWD%TYPE | |
) | |
is | |
cursor uzy_cursor is select UZY_NAZWA, UZY_EMAIL, UZY_PASSWD from UZYTKOWNIK | |
where UZY_NAZWA = nazwa and UZY_EMAIL = email and UZY_PASSWD = haslo; | |
type uzy_rekord is record( | |
naz UZYTKOWNIK.UZY_NAZWA%TYPE default null, | |
ema UZYTKOWNIK.UZY_EMAIL%TYPE default null, | |
has UZYTKOWNIK.UZY_PASSWD%TYPE default null | |
); | |
rekord uzy_rekord; | |
NULL_RECORD_EXCEPTION EXCEPTION; | |
PRAGMA EXCEPTION_INIT(NULL_RECORD_EXCEPTION, -20001); | |
begin | |
open uzy_cursor; | |
fetch uzy_cursor into rekord; | |
rekord.naz := substr(rekord.naz, 1, 5); | |
rekord.ema := substr(rekord.ema, 1, 5); | |
rekord.has := substr(rekord.has, 1, 5); | |
if rekord.naz is null or rekord.ema is null or rekord.has is null then | |
RAISE_APPLICATION_ERROR(-20001, 'Rekord zawiara wartosci null'); | |
end if; | |
insert into UZYTKOWNIK(UZY_NAZWA, UZY_EMAIL, UZY_PASSWD) | |
values (rekord.naz, rekord.ema, rekord.has); | |
if uzy_cursor%ISOPEN then | |
close uzy_cursor; | |
end if; | |
exception | |
when NULL_RECORD_EXCEPTION then | |
dbms_output.put_line('Skladora rekordu to null'); | |
when INVALID_CURSOR then | |
dbms_output.put_line('Bledny kursor'); | |
when CURSOR_ALREADY_OPEN then | |
dbms_output.put_line('Kursor nie zostal zamkniety'); | |
when OTHERS then | |
dbms_output.put_line('Nieznany wyjatek'); | |
end; | |
/ | |
begin | |
P_UZY_INS('ERR_NAME', 'ERR_EMAIL', 'ERR_PASSWD'); | |
end; | |
/ | |
create or replace procedure P_UZY_UPD( | |
nazwa UZYTKOWNIK.UZY_NAZWA%TYPE, | |
email UZYTKOWNIK.UZY_NAZWA%TYPE, | |
haslo UZYTKOWNIK.UZY_PASSWD%TYPE, | |
id UZYTKOWNIK.UZY_ID%TYPE | |
) | |
is | |
cursor uzy_cursor is select UZY_NAZWA, UZY_EMAIL, UZY_PASSWD from UZYTKOWNIK | |
where UZY_NAZWA = nazwa and UZY_EMAIL = email and UZY_PASSWD = haslo; | |
type uzy_rekord is record( | |
naz UZYTKOWNIK.UZY_NAZWA%TYPE default null, | |
ema UZYTKOWNIK.UZY_EMAIL%TYPE default null, | |
has UZYTKOWNIK.UZY_PASSWD%TYPE default null | |
); | |
rekord uzy_rekord; | |
ROW_NOT_FOUND_EXCEPTION EXCEPTION; | |
PRAGMA EXCEPTION_INIT(ROW_NOT_FOUND_EXCEPTION, -20002); | |
begin | |
open uzy_cursor; | |
fetch uzy_cursor into rekord; | |
rekord.naz := 'Nowy: ' || rekord.naz; | |
rekord.ema := 'Nowy: ' || rekord.ema; | |
rekord.has := 'Nowy: ' || rekord.has; | |
if uzy_cursor%NOTFOUND then | |
RAISE_APPLICATION_ERROR(-20002, 'KURSOR%NOTFOUND'); | |
end if; | |
update UZYTKOWNIK set UZY_NAZWA = rekord.naz, | |
UZY_EMAIL = rekord.ema, | |
UZY_PASSWD = rekord.has | |
where UZY_ID = id; | |
if uzy_cursor%ISOPEN then | |
close uzy_cursor; | |
end if; | |
exception | |
when ROW_NOT_FOUND_EXCEPTION then | |
dbms_output.put_line('Nie znaleziono wiersza'); | |
when INVALID_CURSOR then | |
dbms_output.put_line('Bledny kursor'); | |
when CURSOR_ALREADY_OPEN then | |
dbms_output.put_line('Kursor nie zostal zamkniety'); | |
when OTHERS then | |
dbms_output.put_line('Nieznany wyjatek'); | |
end; | |
/ | |
begin | |
P_UZY_UPD('root', '[email protected]', 'passwd', 1); | |
end; | |
/ | |
create or replace procedure P_UZYTKOWNIK_Insert | |
is | |
type grupa_nazwa_t is table of GRUPA.GRU_NAZWA%TYPE | |
index by BINARY_INTEGER; | |
type grupa_id_t is table of GRUPA.GRU_ID%TYPE | |
index by BINARY_INTEGER; | |
type uzytkownik_nazwa_t is varray(3) of UZYTKOWNIK.UZY_NAZWA%TYPE; | |
type uzytkownik_id_t is table of UZYTKOWNIK.UZY_ID%TYPE | |
index by BINARY_INTEGER; | |
gru_naz grupa_nazwa_t; | |
gru_id grupa_id_t; | |
uzy_naz uzytkownik_nazwa_t; | |
uzy_id uzytkownik_id_t; | |
ilosc_blad exception; | |
PRAGMA EXCEPTION_INIT(ilosc_blad, -20020); | |
begin | |
for idx in 1..3 | |
loop | |
gru_naz(idx) := DBMS_RANDOM.STRING('A', 5); | |
insert into GRUPA(GRU_NAZWA) | |
values (gru_naz(idx)) returning GRU_ID into gru_id(idx); | |
end loop; | |
uzy_naz := uzytkownik_nazwa_t(DBMS_RANDOM.STRING('A', 6), | |
DBMS_RANDOM.STRING('B', 6), DBMS_RANDOM.STRING('C', 6)); | |
if (uzy_naz.COUNT != gru_naz.COUNT) then | |
RAISE_APPLICATION_ERROR(-20020, ''); | |
end if; | |
for idx in 1..uzy_naz.COUNT | |
loop | |
insert into UZYTKOWNIK(UZY_NAZWA, UZY_EMAIL, UZY_PASSWD) | |
values (uzy_naz(idx), uzy_naz(idx) || '@a.b', 'blank') | |
returning UZY_ID into uzy_id(idx); | |
end loop; | |
exception | |
when ilosc_blad then | |
dbms_output.put_line('Ilosc danych musi byc taka sama.'); | |
when collection_is_null then | |
dbms_output.put_line('Tabela jest pusta'); | |
when no_data_found then | |
dbms_output.put_line('Nie znaleziono podanej wartosci'); | |
when others then | |
dbms_output.put_line('Nieznany blad'); | |
end; | |
/ | |
begin | |
P_UZYTKOWNIK_Insert; | |
end; | |
/ | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment