NotesWhat is notes.io?

Notes brand slogan

Notes - notes.io

Oracle Ders 4 – Constraints (Kisitlamalar) – Part 1


Bu dersimizde Oracle Database, Constraints(kisitlamalar) konusunu isleyecegiz.
Bu kisitlamalar neler once bunlara bakalim.

Primary Key pk
Foreign Key fk
Unique uk
Check ck
Not Null nn

columnname [Constraint constraintname] constrainttype,
Table Level Kisit olusturma genel ifadesi;

[Constraint [constraintname] constrainttype ( columnname, ...) ,
Table Level kisit olusturdugumuzda , kisit tanimi column tanimlamadan ayri olarak tanimlanir. Primary Key Constraint ( Kisitlama ) Primary Key , tablomuzdaki her bir kaydi birbirinden ayirmamizi saglar. 1000 lerce Levent Erguderi ayirmanin tek yolu TC Numarasindan yararlanmak mesela 🙂

ALTER TABLE tablename ADD [ Constraint constraintname] Primary Key (columname)
Her tablo icin yalnizca bir adet Primary Key tanimlanabilir !! not: bir primary key birden fazla column icerebilir.

Alter table customers add constraint customers_customer#_pk primary key (customer#);
not : alternatif olarak su sekilde de eklenebilir alter table musteri add primary key(musteri_no);

oracle ders 4 primary key 01

Sorgumuzu calistirdigimizda , bize hata veriyor. SQL Error: ORA-02260: table can have only one primary key 02260. 00000 – “table can have only one primary key” Her tablo yalnizca bir Primary Key alanina sahip olabilir ! Biz yeni bir tablo olusturalim ve bu tablomuz uzerinden incelemeye devam edelim..

create table musteri (
musteri_no number,
musteri_adi varchar2(25),
musteri_soyadi varchar2(25),
adres varchar2(50)
)
Tablomuzu basariyla olusturduktan sonra primary key’imizi ekleyelim.Primary Key alanimiz musteri_no olacak. not: constraint isimlendirme kuralina bakacak olursak once tablo ismi sonra constraint alani olarak kullanilacak kolon adi, sonra constraint turunu belirten kisa ek.

alter table musteri add constraint musteri_musteri_no_pk primary key (musteri_no);
Primary Key alanimizi olusturduktan sonra , tablomuza bir kayit ekleyelim..

insert into musteri values(1,'Levent' ,'Erguder', 'Istanbul' );
Kayit basariyla eklendikten sonra, tekrar ayni kaydi eklemeye calistigimizda hata ile karsilasiriz, bunun nedeni Primary Key alanimiz olan musteri_no , 1 olarak zaten tabloda kayitli, tekrar var olan bir deger ile tabloya eklememize izin vermez. Primary Key alanindaki her veri essizdir. Foreign Key Kisitlamasi Simdi de Siparis tablomuzu olusturalim..

create table siparis(
siparis_no number,
siparis_adi varchar(25),
fiyat number,
musteri_id number
);
Siparis tablomuzda musteri_id diye bir alan ekledik. Dusunelim elimizde siparislerin kayitlari var ama siparisleri kime verecegiz, hangi siparis kime ait bunun bilgisini nasil tutacagiz ? Iste foreign key yapisi bu noktada ortaya cikiyor. Boylece iki tabloyu birbirine baglayabiliriz. Bir musterinin birden fazla siparisi olabilir, bu nedenle Musteri ve Siparis arasinda ki iliski 1 e N dir , bu nedenle foreign key alani N tarafina yani Siparis tarafina eklenir. Foreign Key alani, diger tabloda mutlaka primary key olmalidir. Genel formatimiz :

ALTER TABLE tablename
ADD[ CONSTRAINT constraintname ] FOREIGN KEY (columname)
REFERENCES referencedtablename (referencedcolumname);
Siparis tablomuzdaki musteri_id ile , Musteri tablomuzdaki musteri_no alanlarini birbirine baglayalim..

alter table siparis
add constraint siparis_musteri_id_fk foreign key(musteri_id)
references musteri(musteri_no);
Simdi Siparis tablomuza bir alan ekleyelim, dusundugumuzde bir siparisi veren mutlaa birisi olmali oyle degil mi ? Bakalim Oracle bize ne diyecek bu konuda..

insert into siparis (siparis_no,siparis_adi,fiyat,musteri_id) values(10,'Bilgisayar','2000',5);


oracle ders 4 foreing key 02

resimde goruldugu gibi hata ile karsilastik. Neden peki ? Cunku musteri_id si 5 olan bir musterimiz yok . ( Musteri tablomuzda sadece 1 kayit var ve onunda musteri_id si 1)

insert into siparis (siparis_no,siparis_adi,fiyat,musteri_id) values(10,'Bilgisayar','2000',1);
Yukaridaki sorguyu calistirdigimizda kayidi basariyla ekleyecektir. Musteri tablomuzdaki kayit , Siparis tablomuzda eslesiyorsa ( yani siparisi olan bir musteri varsa ) , Oracle bu kaydi silmenize izin vermez. Once Siparis tablosundaki veriyi silip sonra Musteri tablosundan silmemiz gerekmektedir.

delete from musteri where musteri_no=1;

oracle ders 4 foreignkey delete 03

SQL Error: ORA-02292: integrity constraint (LEVENT.SIPARIS_MUSTERI_ID_FK) violated – child record found 02292. 00000 – “integrity constraint (%s.%s) violated – child record found” *Cause: attempted to delete a parent key value that had a foreign dependency. *Action: delete dependencies first then parent or disable constraint Goruldugu gibi, siparis tablosundan silmeden musteri tablosundan silmeye kalktigimizda hata aliyoruz. ( Eger siparis tablosunda foreign key alaninda kayitli olmayan bir kayit olsa, yani siparisi olmayan kisi varsa bu kaydi sorunsuzca silebiliriz ) Alternatif bir yol olarak ; Foreign key eklerken, ON DELETE CASCADE cumlecigini belirtseydik bu durumda diledigimiz gibi kaydi silebilirdik.

alter table siparis
add constraint siparis_musteri_id_fk foreign key(musteri_id)
references musteri(musteri_no) ON DELETE CASCADE;
(not: alter table siparis drop constraint siparis_musteri_id_fk; sorgusuyla once mevcut foreign key i dusurelim. daha sonra foreign keyi ekleyelim ilerleyen kisimlarda drop constraint konusuna tekrar gelecegiz.) Simdi tekrar, delete from musteri where musteri_no=1; sorgusunu calistirdigimizda bu sefer hata ile karsilasmayacagiz ve kaydimiz basariyla silinecektir. inanmiyorsaniz select cekin 🙂 Not: Siparis tablomuzdaki ilgili kayidin da uctugunu goreceksiniz 🙂 Simdi foreign key i tekrar dusurelim ve ON DELETE CASCADE olmadan tekrar ekleyelim. Once Musteri tablomuza sonra Siparis tablomuza biraz onceki kayitlarimizi tekrar ekleyelim.

oracle ders 4 foreignkey 04

Simdi Musteri tablomuzu drop edelim,.. Oracle buna izin verir mi ?

Drop table musteri;
SQL Error: ORA-02449: unique/primary keys in table referenced by foreign keys Hatamizi aldik hemen.. Once Siparis tablomuzu , sonrasinda ise Musteri tablomuzu drop etmemiz gerekmektedir. Yani foreign key olan tablo hangisiyse once o tabloyu dusurmek gerekir.( foreign key olan tabloya child table, diger tabloya ise parent table denir ) Bu duruma alternatif olarak,

DROP TABLE TABLENAME CASCADE CONSTRAINTS;
ile de silmemiz mumkun.

drop table musteri cascade constraints;
oracle ders 4 foreignkey cascadedrop 05

Gordugunuz gibi basariyla musteri tablomuz silindi. Oracle Constraints (Kisitlamalar) ile ilgili dersimizin ilk kisminin sonuna geldik.
     
 
what is notes.io
 

Notes.io is a web-based application for taking notes. You can take your notes and share with others people. If you like taking long notes, notes.io is designed for you. To date, over 8,000,000,000 notes created and continuing...

With notes.io;

  • * You can take a note from anywhere and any device with internet connection.
  • * You can share the notes in social platforms (YouTube, Facebook, Twitter, instagram etc.).
  • * You can quickly share your contents without website, blog and e-mail.
  • * You don't need to create any Account to share a note. As you wish you can use quick, easy and best shortened notes with sms, websites, e-mail, or messaging services (WhatsApp, iMessage, Telegram, Signal).
  • * Notes.io has fabulous infrastructure design for a short link and allows you to share the note as an easy and understandable link.

Fast: Notes.io is built for speed and performance. You can take a notes quickly and browse your archive.

Easy: Notes.io doesn’t require installation. Just write and share note!

Short: Notes.io’s url just 8 character. You’ll get shorten link of your note when you want to share. (Ex: notes.io/q )

Free: Notes.io works for 12 years and has been free since the day it was started.


You immediately create your first note and start sharing with the ones you wish. If you want to contact us, you can use the following communication channels;


Email: [email protected]

Twitter: http://twitter.com/notesio

Instagram: http://instagram.com/notes.io

Facebook: http://facebook.com/notesio



Regards;
Notes.io Team

     
 
Shortened Note Link
 
 
Looding Image
 
     
 
Long File
 
 

For written notes was greater than 18KB Unable to shorten.

To be smaller than 18KB, please organize your notes, or sign in.