Mavzu: sql da funksiyalar va triggerlar yaratish



Download 1,14 Mb.
bet2/2
Sana17.01.2022
Hajmi1,14 Mb.
#381575
1   2
Bog'liq
Omonov Abbosxon 612

FUNCTIONS


  1. Minus

mysql> create function minus(a int, b int)

-> returns int

-> deterministic

-> begin


-> declare result int;

-> set result = a - b;

-> return (result);

-> end//


Query OK, 0 rows affected (0.00 sec)
mysql> delimiter ;

mysql> select minus(100, 25);

+----------------+

| minus(100, 25) |

+----------------+

| 75 |


+----------------+

1 row in set (0.00 sec)


mysql>




  1. Bo`lish

mysql> create function bolish(a float, b float)

-> returns float

-> deterministic

-> begin


-> declare result float;

-> set result = a / b;

-> return (result);

-> end//


Query OK, 0 rows affected (0.00 sec)
mysql> select bolish(10, 3)//

+------------------+

| bolish(10, 3) |

+------------------+

| 3.33333325386047 |

+------------------+

1 row in set (0.06 sec)
mysql>



  1. Ildiz

mysql> create function ildiz(a float)

-> returns float

-> deterministic

-> begin


-> declare result float;

-> set result = sqrt(a);

-> return (result);

-> end//


Query OK, 0 rows affected (0.07 sec)
mysql> select ildiz(16)//

+-----------+

| ildiz(16) |

+-----------+

| 4 |

+-----------+



1 row in set (0.00 sec)
mysql>



  1. Modul

mysql> create function modul(a int)

-> returns int

-> deterministic

-> begin


-> declare result int;

-> set result = abs(a);

-> return (result);

-> end//


Query OK, 0 rows affected (0.00 sec)
mysql> select modul(-1)//

+-----------+

| modul(-1) |

+-----------+

| 1 |

+-----------+



1 row in set (0.00 sec)
mysql>



  1. Ustunlarni birlashtirish

mysql> create function birlashtirish(

-> ism text charset utf8,

-> fam text charset utf8,

-> ota text charset utf8)

-> returns text charset utf8

-> reads sql data

-> begin

-> return concat(

-> ism, ' ', fam, ' ', ota);

-> end//


Query OK, 0 rows affected (0.09 sec)
mysql> select birlashtirish(ismi, familyasi, otasining_ismi) from bemorlar_2;

-> //


+------------------------------------------------+

| birlashtirish(ismi, familyasi, otasining_ismi) |

+------------------------------------------------+

| Botir Ahrorov Ziyatovich |

| Dilobar Saidova Davron qizi |

| Qayyum Ahad Jobirovich |

| Davron Qodirov Otaxonovich |

| Farzona Mo`minova Ahmadjon qizi |

| Vladimir Belyakov Gennadevich |

| Andrey ╨Рndreev Ivanovich |

| Liliya Muxamadullina Nailevna |

| Elvira ╨Рbdullina Rashidovna |

| Viktoriya Gayfullina ╨Рleksandrovna |

| Bekzod Asadov Valiyevich |

| Temur Ahrorov Aliyevich |

+------------------------------------------------+

12 rows in set (0.00 sec)



  1. Mavjud standart funksiyalar(10-lab)dan foydalanib yangi funksiya yaratish.

mysql> create function uzunligi(s varchar(30))

-> returns int

-> deterministic

-> begin


-> declare result int;

-> set result = length(s);

-> return (result);

-> end//


Query OK, 0 rows affected (0.11 sec)
mysql> select uzunligi('Abdurauf')//

+----------------------+

| uzunligi('Abdurauf') |

+----------------------+

| 8 |

+----------------------+



1 row in set (0.00 sec)
mysql>

TRIGGERS

After delete

mysql> create trigger tuzalgan_bemorlar

-> after delete

-> on bemorlar_2

-> for each row

-> set @del = @del + 1;

-> //

Query OK, 0 rows affected (0.25 sec)






Before delete

mysql> create trigger tuzalganlar

-> before delete

-> on bemorlar_2 for each row

-> begin

-> insert into tuzalganlar

-> values(old.familyasi, old.ismi, old.manzili);

-> end//


Query OK, 0 rows affected (0.13 sec)

Before update

mysql> create trigger check_yoshi before update on bemorlar_2

-> for each row

-> begin


-> if new.yoshi < 0 then

-> set new.yoshi = 0;

-> elseif new.yoshi > 100 then

-> set new.yoshi = 100;

-> end if;

-> end //

Query OK, 0 rows affected (0.16 sec)


After update

mysql> create table update_bemorlar like bemorlar_2;

Query OK, 0 rows affected (0.13 sec)
mysql> select * from update_bemorlar;

Empty set (0.00 sec)


mysql> create trigger after_update

-> after update

-> on bemorlar_2 for each row

-> begin


-> insert into update_bemorlar values

-> (old.bemor_id, old.familyasi, old.ismi, old.otasining_ismi, old.manzili, old.yoshi, old.jinsi);

-> end//

Query OK, 0 rows affected (0.10 sec)



Before Insert

mysql> create trigger before_insert before insert

-> on shifokorlar_2 for each row

-> begin


-> if new.lavozimi = 'Shifokor' then

-> set new.lavozimi = 'Yordamchi';

-> end if;

-> end//


Query OK, 0 rows affected (0.09 sec)
mysql> insert into shifokorlar_2 values

-> (13, 'Abrorov', 'Ali', 'Asadovich', 'Shifokor', 9, 'Magistr', 'Namangan'),

-> (14, 'Asrorov', 'Vali', 'Ahadovich', 'Shifokor', 6, 'Bakalavr', 'Andijon')//

Query OK, 2 rows affected (0.08 sec)

Records: 2 Duplicates: 0 Warnings: 0
mysql> select * from shifokorlar_2//

After Insert

mysql> create table ogohlantirish

-> (id int auto_increment,

-> bemor_id int(4),

-> message varchar(255) not null,

-> primary key(id, bemor_id))//

Query OK, 0 rows affected (0.11 sec)
mysql> create trigger after_insert after insert

-> on bemorlar_2 for each row

-> begin

-> if new.ismi is null then

-> insert into ogohlantirish (bemor_id, message)

-> values(new.bemor_id, concat(new.familyasi, ' ismingizni yangilang.'));

-> end if;

-> end//


Query OK, 0 rows affected (0.13 sec)
mysql> insert into bemorlar_2(bemor_id, familyasi) values

-> (9, 'Ahmadjonov')//



Query OK, 1 row affected (0.11 sec)
mysql> select * from ogohlantirish//

Xulosa:

Men ushbu laboratoriya ishida MySQL da triggerlarr va funksiyalar yaratish bo`yicha ko`nikmaga ega bo`ldim.
Download 1,14 Mb.

Do'stlaringiz bilan baham:
1   2




Ma'lumotlar bazasi mualliflik huquqi bilan himoyalangan ©hozir.org 2024
ma'muriyatiga murojaat qiling

kiriting | ro'yxatdan o'tish
    Bosh sahifa
юртда тантана
Боғда битган
Бугун юртда
Эшитганлар жилманглар
Эшитмадим деманглар
битган бодомлар
Yangiariq tumani
qitish marakazi
Raqamli texnologiyalar
ilishida muhokamadan
tasdiqqa tavsiya
tavsiya etilgan
iqtisodiyot kafedrasi
steiermarkischen landesregierung
asarlaringizni yuboring
o'zingizning asarlaringizni
Iltimos faqat
faqat o'zingizning
steierm rkischen
landesregierung fachabteilung
rkischen landesregierung
hamshira loyihasi
loyihasi mavsum
faolyatining oqibatlari
asosiy adabiyotlar
fakulteti ahborot
ahborot havfsizligi
havfsizligi kafedrasi
fanidan bo’yicha
fakulteti iqtisodiyot
boshqaruv fakulteti
chiqarishda boshqaruv
ishlab chiqarishda
iqtisodiyot fakultet
multiservis tarmoqlari
fanidan asosiy
Uzbek fanidan
mavzulari potok
asosidagi multiservis
'aliyyil a'ziym
billahil 'aliyyil
illaa billahil
quvvata illaa
falah' deganida
Kompyuter savodxonligi
bo’yicha mustaqil
'alal falah'
Hayya 'alal
'alas soloh
Hayya 'alas
mavsum boyicha


yuklab olish