-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathDay02_Advanced_SQL.sql
More file actions
255 lines (168 loc) · 4.68 KB
/
Day02_Advanced_SQL.sql
File metadata and controls
255 lines (168 loc) · 4.68 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
--*******************GUN--2*************************
--film tablosundan id' si 1 olan aktorun tum bilgilerini aliniz.
do $$
declare
selected_actor actor%rowtype; -- rowtype secili row'un tum bilgilerini depolayabilmek icin kullanilir.
begin
select *
from actor
into selected_actor
where id=1;
raise notice '% %', selected_actor.first_name,selected_actor.last_name;
end$$
--****************** RECORD TYPE ****************************** (record bazi programlarda row anlaminda kullaniliyor)
-- Film tablosundan id'si 2 olan datanin yalnizca id, title ve type bilgisini al.
do $$
declare
rec record ; -- Record Type : Row Type gibi tüm degerleri bastan sona almaz, tablodaki belirli datalarin bilgilerini verir.
begin
select id, title, type
from film
into rec
where id=2;
raise notice 'Id: %, Film Ismi: %, Tur: %', rec.id, rec.title, rec.type;
end $$;
-- ************************* IC ICE BLOK YAPILARI *************************
do $$
<<outerblock>>
declare -- Tepedeki blok, outer block olarak geciyor.
counter integer := 0;
begin
counter := counter+1;
raise notice 'Outer block Counter Degeri: %', counter;
declare -- Inner block baslangici
counter integer :=0;
begin
counter := counter+10;
raise notice ' Inner block counter degeri : %', counter;
raise notice 'Dis bloktaki Counter degeri: %', outerblock.counter;
end; -- Inner Block sonu
end $$ ;-- Outer block sonu
--*******************CONSTANT DEGERLER *********************
--KDV degerini sabit deger yap.
do $$
declare
--selling_price := net_price *0.1;
vat constant numeric :=0.1; -- constant deger, sabir deger. Begin icerisinde tekrar degistirilemez.
net_price numeric := 20.5;
begin
raise notice 'Satis Fiyati: %', net_price*vat;
end $$;
-- CONSTANT'larin degismediginin kanit ornegi
do $$
declare
olusturulma constant integer := 17;
begin
raise notice 'Olusturulma zamani: %', olusturulma;
--olusturulma := 18;
--raise notice 'Yeni Olusturulma zamani: %', olusturulma;
end $$;
-- ******************************** CONTROL YAPILARI ******************************
-- *********************** IF STATEMENT ***************************
--sytax
/*
IF condition THEN
islemler
END IF;
*/
--Task 1: 1 Id'li filmi bulmaya calis, bulunursa bulundu yazisini print et.
--if found
do $$
declare
selected_film film%rowtype; -- Film objesi kullanılacak
input_film_id film.id%type := 1; -- Kullanıcının girdiği id numarası
begin
SELECT * FROM film
INTO selected_film
WHERE id=input_film_id;
-- Bulduğumuz filmin title'ını getir
--raise notice '%', selected_film.title;
if found then
raise notice 'Bulundu: %', selected_film.title;
end if;
end $$;
--if not found
do $$
declare
selected_film film%rowtype; -- Film objesi kullanılacak
input_film_id film.id%type := 10; -- Kullanıcının girdiği id numarası
begin
SELECT * FROM film
INTO selected_film
WHERE id=input_film_id;
-- Bulduğumuz filmin title'ını getir
--raise notice '%', selected_film.title;
if not found then
raise notice 'Bulunamadı! ----> %', input_film_id;
end if;
end $$;
-- ******************** IF - ELSE *******************************
/*
If condition THEN
islemler;
ELSE
alternatif islemler;
END IF;
*/
-- Task -1 1 Id'li film buunabulurse, title bilgisini yazdir, yoksa BULUNAMADI yazdir.
do $$
declare
selected_film film%rowtype; -- Film objesi kullanılacak
input_film_id film.id%type := 10; -- Kullanıcının girdiği id numarası
begin
SELECT * FROM film
INTO selected_film
WHERE id=input_film_id;
-- Bulduğumuz filmin title'ını getir
--raise notice '%', selected_film.title;
if found then
raise notice 'Bulundu: %', selected_film.title;
else
raise notice 'Bulunamadı: %', input_film_id;
end if;
end $$;
-- ******** IF - ELSE IF - ELSE
-- syntax:
/*
IF condition THEN
işlemler;
ELSEIF condition_2 THEN
işlemler;
ELSEIF condition_3 THEN
işlemler;
ELSE
işlemler;
END IF;
*/
/*
Task:
1 ID'li film bulunursa:
Süresi 50 dakikanın altında ise "Kısa",
50 ile 120 arasında ise "Ortalama",
120 dakikadan fazla ise "Uzun"
print edelim.
*/
do $$
declare
oFilm film%rowtype;
lenDescription varchar(50);
film_id film.id%type;
begin
SELECT * FROM film
INTO oFilm
WHERE id=film_id;
if not found then
raise notice 'Film bulunamadı!';
else
if oFilm.length>0 and oFilm.length<50 then
lenDescription := 'Kısa';
elseif oFilm.length>50 and oFilm.length<120 then
lenDescription := 'Ortalama';
elseif oFilm.length>120 then
lenDescription := 'Uzun';
else
lenDescription := 'Tanımlanamıyor.';
end if;
raise notice '% filminin uzunlugu: %', oFilm.title, lenDescription;
end if;
end $$;