Guest User

Untitled

a guest
Dec 7th, 2018
108
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 11.49 KB | None | 0 0
  1. -- create table visitor (
  2. -- id number constraint user_pk primary key,
  3. -- nickname varchar2(50) not null,
  4. -- email varchar2(50) not null,
  5. -- address varchar2(100),
  6. -- country varchar2(3)
  7. -- );
  8. -- /
  9.  
  10. -- alter table visitor add constraint user_nickname_uq unique (nickname);
  11. -- alter table visitor add constraint user_email_uq unique (email);
  12.  
  13. -- insert into visitor (id, nickname, email,address, country) values (1, 'jozin1', 'hehe@a.pl', 'Jozefitwo', 'PL') ;
  14. -- insert into visitor (id, nickname, email,address, country) values (2, 'heniek2', 'gmail@a.pl', 'Krakow', 'EN') ;
  15. -- insert into visitor (id, nickname, email,address, country) values (3, 'grzymaal', 'grzyma@a.pl', 'Jaslo', 'GB') ;
  16.  
  17.  
  18. -- select * from visitor;
  19.  
  20. -- create table customer (
  21. -- id number constraint customer_pk primary key,
  22. -- firstname varchar2(50) not null,
  23. -- lastname varchar2(50) not null,
  24. -- email varchar2(100) not null,
  25. -- password_hash varchar2(255) not null,
  26. -- address varchar2(255),
  27. -- city varchar2(50),
  28. -- country varchar2(3),
  29. -- zip_code varchar2(10),
  30. -- bio varchar2(500),
  31. -- created_at timestamp not null
  32. -- );
  33. -- /
  34.  
  35. -- alter table customer add constraint customer_email_uq unique (email);
  36.  
  37. -- insert into customer (id, firstname, lastname, email, password_hash, address, city, country, zip_code, bio, created_at) values (1, 'Pat', 'Świerszcz', 'pat@gmail.com', 'h3jk12h3k12h3', 'Zakliczyn 15', 'Wieś', 'PL', '31-222', 'lorem ipsum', TIMESTAMP '1997-01-31 09:26:50.12') ;
  38. -- insert into customer (id, firstname, lastname, email, password_hash, address, city, country, zip_code, bio, created_at) values (2, 'Dom', 'Zreb', 'dom@gmail.com', 'h3213jk12h3k12h3', '3 Maja', 'Jaslo', 'EN', '31-555', 'lorem ipsum 1', TIMESTAMP '1996-01-31 09:26:50.12') ;
  39. -- insert into customer (id, firstname, lastname, email, password_hash, address, city, country, zip_code, bio, created_at) values (3, 'Mar', 'Kam', 'kam@gmail.com', 'h3jk12123h3k12h3', 'Dluga', 'Tymbark', 'GB', '31-999', 'lorem ipsum 2', TIMESTAMP '1998-01-31 09:26:50.12') ;
  40. -- insert into customer (id, firstname, lastname, email, password_hash, address, city, country, zip_code, bio, created_at) values (4, 'Other', 'Other', 'grzyma@a.pl', 'h3jk12123h33213k12h3', 'Otherska', 'Othercity', 'PL', '31-000', 'lorem ipsum other', TIMESTAMP '1999-01-31 09:26:50.12') ;
  41.  
  42.  
  43. -- select * from customer;
  44.  
  45. -- CREATE TABLE stage_customer
  46. -- (
  47. -- id INTEGER GENERATED ALWAYS AS IDENTITY (START WITH 1 INCREMENT BY 1) primary key,
  48. -- name VARCHAR(50) NOT NULL,
  49. -- email varchar2(100) not null,
  50. -- country varchar2(3)
  51. -- );
  52.  
  53. -- drop table stage_customer;
  54.  
  55. -- create table stage_customer (
  56. -- id number constraint customer_pk primary key,
  57. -- firstname varchar2(50) not null,
  58. -- lastname varchar2(50) not null,
  59. -- email varchar2(100) not null,
  60. -- password_hash varchar2(255) not null,
  61. -- address varchar2(255),
  62. -- city varchar2(50),
  63. -- country varchar2(3),
  64. -- zip_code varchar2(10),
  65. -- bio varchar2(500),
  66. -- created_at timestamp not null
  67. -- );
  68. -- /
  69.  
  70. -- alter table customer add constraint customer_email_uq unique (email);
  71.  
  72. -- SELECT nickname, email, country from visitor
  73. -- union
  74. -- SELECT firstname, email, country from customer
  75.  
  76. -- select * from customer;
  77.  
  78. -- create table film (
  79. -- id number constraint film_pk primary key,
  80. -- name varchar2(50) not null
  81. -- );
  82.  
  83. -- create table actor (
  84. -- id number constraint actor_pk primary key,
  85. -- name varchar2(50) not null
  86. -- );
  87.  
  88. -- insert into actor (id, name) values (1, 'DiCaprio');
  89. -- insert into actor (id, name) values (2, 'Hopkins');
  90. -- insert into actor (id, name) values (3, 'Snipes');
  91.  
  92. -- select * from actor;
  93.  
  94. -- create table actor2 (
  95. -- id number constraint actor2_pk primary key,
  96. -- name varchar2(50) not null
  97. -- );
  98.  
  99. -- insert into actor2 (id, name) values (1, 'Angelina Jolie');
  100. -- insert into actor2 (id, name) values (2, 'Bradd Pitt');
  101. -- insert into actor2 (id, name) values (3, 'Jackie Chan');
  102.  
  103. -- select * from actor2;
  104.  
  105. -- create table film_actor (
  106. -- film_id number,
  107. -- actor_id number
  108. -- );
  109.  
  110. -- alter table film_actor
  111. -- add constraint fk_film_film_actor
  112. -- foreign key (film_id) references film(id);
  113.  
  114. -- alter table film_actor
  115. -- add constraint fk_actor_film_actor
  116. -- foreign key (actor_id) references actor(id);
  117.  
  118. -- insert into film_actor (film_id, actor_id) values(1, 2);
  119. -- insert into film_actor (film_id, actor_id) values(2, 1);
  120. -- insert into film_actor (film_id, actor_id) values(3, 1);
  121. -- insert into film_actor (film_id, actor_id) values(1, 3);
  122. -- insert into film_actor (film_id, actor_id) values(2, 2);
  123. -- insert into film_actor (film_id, actor_id) values(3, 1);
  124.  
  125. -- select * from film_actor;
  126.  
  127. -- create table movie_cast (
  128. -- movie_id number not null,
  129. -- actor_id number not null
  130. -- );
  131.  
  132. -- alter table movie_cast
  133. -- add constraint fk_movie_movie_cast
  134. -- foreign key (movie_id) references movie(id);
  135.  
  136. -- alter table movie_cast
  137. -- add constraint fk_actor_movie_cast
  138. -- foreign key (actor_id) references actor2(id);
  139.  
  140. -- insert into movie_cast (movie_id, actor_id) values(1, 2);
  141. -- insert into movie_cast (movie_id, actor_id) values(2, 3);
  142. -- insert into movie_cast (movie_id, actor_id) values(3, 1);
  143. -- insert into movie_cast (movie_id, actor_id) values(3, 2);
  144. -- insert into movie_cast (movie_id, actor_id) values(2, 1);
  145. -- insert into movie_cast (movie_id, actor_id) values(4, 3);
  146.  
  147.  
  148. -- create table movie (
  149. -- id number constraint movie_pk primary key,
  150. -- title varchar2(50) not null
  151. -- );
  152.  
  153. -- insert into movie (id, title) values(1, 'Skazani na shawshank');
  154. -- insert into movie (id, title) values(2, 'Nietykalni');
  155. -- insert into movie (id, title) values(4, 'Harry Potter');
  156.  
  157.  
  158. -- insert into film (id, name) values(1, 'Harry Potter');
  159. -- insert into film (id, name) values(2, 'Incepcja');
  160. -- insert into film (id, name) values(3, 'Pasiasta pizama');
  161.  
  162. -- select * from movie;
  163.  
  164. -- select id one_id, NULL second_id, name FROM film f
  165. -- where not exists (select 1 from movie m
  166. -- where f.name = m.title)
  167. -- union
  168. -- select NULL, id, title from movie;
  169.  
  170. -- select id one_id, NULL second_id, name FROM actor a
  171. -- where not exists (select 1 from actor2 a2
  172. -- where a.name = a2.name)
  173. -- union
  174. -- select NULL, id, name from actor2;
  175.  
  176. -- select * from film_actor;
  177.  
  178. -- select email, (firstname || ' ' || lastname) name, country FROM customer c
  179. -- where not exists (select 1 from visitor v
  180. -- where c.email = v.email)
  181. -- union
  182. -- select email, nickname, country from visitor
  183.  
  184. -- create table stage_film
  185. -- (
  186. -- id INTEGER GENERATED ALWAYS AS IDENTITY (START WITH 1 INCREMENT BY 1) primary key,
  187. -- one_id integer,
  188. -- second_id integer,
  189. -- name VARCHAR(50) NOT NULL
  190. -- );
  191.  
  192. -- CREATE SEQUENCE stage_film_seq
  193. -- START WITH 1
  194. -- INCREMENT BY 1
  195. -- CACHE 100;
  196.  
  197. -- insert into stage_film (one_id, second_id, name)
  198. -- select id one_id, NULL second_id, name FROM film f
  199. -- where not exists (select 1 from movie m
  200. -- where f.name = m.title)
  201. -- union
  202. -- select NULL, id, title from movie;
  203.  
  204. -- select * from stage_film;
  205.  
  206. -- create table stage_actor
  207. -- (
  208. -- id INTEGER GENERATED ALWAYS AS IDENTITY (START WITH 1 INCREMENT BY 1) primary key,
  209. -- one_id integer,
  210. -- second_id integer,
  211. -- name VARCHAR(50) NOT NULL
  212. -- );
  213.  
  214. -- insert into stage_actor(one_id, second_id, name)
  215. -- select id one_id, NULL second_id, name FROM actor a
  216. -- where not exists (select 1 from actor2 a2
  217. -- where a.name = a2.name)
  218. -- union
  219. -- select NULL, id, name from actor2;
  220.  
  221. -- select * from stage_actor;
  222.  
  223. -- select * from actor;
  224. -- select * from actor2;
  225.  
  226. -- select * from film;
  227. -- select * from movie;
  228.  
  229. -- create table category (
  230. -- ID number constraint category_pk primary key,
  231. -- NAME varchar2(50) not null
  232. -- );
  233.  
  234. -- alter table CATEGORY add constraint category_name_uq unique (NAME);
  235.  
  236. -- create table film_category (
  237. -- film_id number,
  238. -- category_id number
  239. -- );
  240.  
  241.  
  242. -- alter table film_category
  243. -- add constraint fk_film_film_category
  244. -- foreign key (film_id) references film(id);
  245.  
  246. -- alter table film_category
  247. -- add constraint fk_category_film_category
  248. -- foreign key (category_id) references category(id);
  249.  
  250. -- insert into category (id, name) values (1, 'Psychologiczny');
  251. -- insert into category (id, name) values (2, 'Thriller');
  252. -- insert into category (id, name) values (3, 'Wojenny');
  253. -- insert into category (id, name) values (4, 'Obyczajowy');
  254.  
  255. -- select * from category;
  256.  
  257. -- create table genre (
  258. -- id number constraint genre_pk primary key,
  259. -- name varchar2(50) not null
  260. -- );
  261.  
  262. -- create table movie_genres (
  263. -- movie_id number not null,
  264. -- genre_id number not null
  265. -- );
  266.  
  267. -- alter table movie_genres
  268. -- add constraint fk_movie_movie_genres
  269. -- foreign key (movie_id) references movie(id);
  270.  
  271. -- alter table movie_genres
  272. -- add constraint fk_genre_movie_genres
  273. -- foreign key (genre_id) references genre(id);
  274.  
  275. -- insert into genre (id, name) values (1, 'Dla dzieci');
  276. -- insert into genre (id, name) values (2, 'Sci-Fi');
  277. -- insert into genre (id, name) values (3, 'Psychologiczny');
  278. -- insert into genre (id, name) values (4, 'Romans');
  279.  
  280. -- select * from genre;
  281.  
  282. -- select * from stage_film;
  283. -- select * from stage_actor;
  284. -- select * from film_actor;
  285. -- select * from movie_cast;
  286.  
  287. -- insert into film_category (film_id, category_id) values (1, 4);
  288. -- insert into film_category (film_id, category_id) values (2, 1);
  289. -- insert into film_category (film_id, category_id) values (3, 3);
  290. -- insert into film_category (film_id, category_id) values (1, 1);
  291. -- insert into film_category (film_id, category_id) values (2, 2);
  292.  
  293. -- select * from film_category;
  294.  
  295. -- insert into movie_genres (movie_id, genre_id) values (1, 2);
  296. -- insert into movie_genres (movie_id, genre_id) values (2, 3);
  297. -- insert into movie_genres (movie_id, genre_id) values (3, 3);
  298. -- insert into movie_genres (movie_id, genre_id) values (4, 1);
  299. -- insert into movie_genres (movie_id, genre_id) values (2, 1);
  300. -- insert into movie_genres (movie_id, genre_id) values (3, 2);
  301.  
  302.  
  303. -- select * from movie_genres;
  304.  
  305. -- create table stage_category
  306. -- (
  307. -- id INTEGER GENERATED ALWAYS AS IDENTITY (START WITH 1 INCREMENT BY 1) primary key,
  308. -- cat_id integer,
  309. -- gen_id integer,
  310. -- name VARCHAR(50) NOT NULL
  311. -- );
  312.  
  313. -- alter table stage_category add constraint stage_category_name_uq unique (name);
  314.  
  315. -- insert into stage_category(cat_id, gen_id, name)
  316. -- select id cat_id, NULL gen_id, name FROM category c
  317. -- where not exists (select 1 from genre g
  318. -- where c.name = g.name)
  319. -- union
  320. -- select NULL, id, name from genre;
  321.  
  322. select * from stage_film;
  323. select * from stage_category;
  324. select * from film_category;
  325. select * from movie_genres;
  326.  
  327. select stage_film.id film_id, stage_category.id category_id, stage_film.name, stage_category.name from film_category
  328. join stage_film on stage_film.one_id = film_category.film_id
  329. join stage_category on stage_category.cat_id = film_category.category_id
  330. union all
  331. select stage_film.id, stage_category.id, stage_film.name, stage_category.name from movie_genres
  332. join stage_film on stage_film.second_id = movie_genres.movie_id
  333. join stage_category on stage_category.gen_id = movie_genres.genre_id;
  334.  
  335.  
  336.  
  337. -- select stage_film.id film_id, stage_actor.id actor_id, stage_film.name,stage_actor.name from film_actor
  338. -- join stage_film on stage_film.one_id = film_actor.film_id
  339. -- join stage_actor on stage_actor.one_id = film_actor.actor_id
  340. -- union all
  341. -- select stage_film.id film_id, stage_actor.id actor_id, stage_film.name,stage_actor.name from movie_cast
  342. -- join stage_film on stage_film.second_id = movie_cast.movie_id
  343. -- join stage_actor on stage_actor.second_id = movie_cast.actor_id;
Add Comment
Please, Sign In to add comment