Relational Database Constraints KS141304 PBD Institut Teknologi Sepuluh Nopember (ITS) 1 Review Informal Definitions Please identify the component of the Student table below. KS141304 PBD Institut Teknologi Sepuluh Nopember (ITS) 2 Key Key of a Relation: Unique Identifier Mahasiswa: NRP Dosen: NIP Rumah Tangga: Nomor KK Warganegara: Nomor KTP Wajib Pajak: ??? Pelanggan: ??? Nasabah Bank: ??? Kendaraan Bermotor: ??? KS141304 PBD Institut Teknologi Sepuluh Nopember (ITS) 3 Key If there is no given ID, sequential number can be assigned as Key. This is called artificial key or surrogate key. What is the key of table Student below. KS141304 PBD Institut Teknologi Sepuluh Nopember (ITS) 4 Schema The Schema of a Relation is denoted by Rand attributes are A. R(A1, A2, An) means that a relation named R have n attributes. Students(Name, SSN, Home_Phone, Address, Office_Phone, Age, GPA) KS141304 PBD Institut Teknologi Sepuluh Nopember (ITS) 5 Tuple A tuple is an ordered set of values enclosed in brackets <>. Denoted by t. Set of tuples is known as r(R) r(Students) ={t1, t2, t3, t4, t5} ti =<v1, v2, v3, v4, v5, v6, v7> A row in Student relation is a 7-tuple, for example: <"Benjamin Bayer", "305-61-2435", "373-1616", "2918 Bluebonnet Lane", NULL, 19, 3.21> KS141304 PBD Institut Teknologi Sepuluh Nopember (ITS) 6 Domain Domain adalah himpunan pembicaraan (all possible column values). Contoh: domain atribut Age dari Student adalah semua bilangan asli antara 17 sampai 26 tahun. Domain dari telepon adalah (nnn)nnn-nnnn dimana n adalah bilangan asli. Domain dinotasikan dengan v. Domain dari tanggal transaksi memiliki domain yang berbeda-beda dd/mm/yyyy yyyy-mm-dd KS141304 PBD Institut Teknologi Sepuluh Nopember (ITS) 7 Domain Domain of Name, SSN, Home_phone, Address, Office_phone attribute is varchar(50) Domain of Age attribute is integer Domain of GPA attribute is float/double KS141304 PBD Institut Teknologi Sepuluh Nopember (ITS) 8 Formal Definitions Summary KS141304 PBD Institut Teknologi Sepuluh Nopember (ITS) 9 R =STUDENT t1 t2 t3 t4 t5 A1 A2 A3 A4 A5 A6 A7 Formal Definitions: Latihan Gambarlah schema dan state-nya dengan notasi formal berikut di papan tulis. FASHION(Nama, UkuranBaju, UkuranSepatu) dom(Nama) ={Astrid, Azis, Alam} dom(UkuranBaju) ={S, M, L, XL} dom(UkuranSepatu) ={38, 40, 41} r(FASHION) dom(Nama) X dom(UkuranBaju) X dom(UkuranSepatu) KS141304 PBD Institut Teknologi Sepuluh Nopember (ITS) 10 Relational Database Schema Is a set S of relation schema S is the name of the whole database schema S means himpunan semesta S ={R1, R2, , Rn} KS141304 PBD Institut Teknologi Sepuluh Nopember (ITS) 11 Definition Summary KS141304 PBD Institut Teknologi Sepuluh Nopember (ITS) 12 Relational Integrity Constraints Constraints are conditionsthat must be held by all valid relation states. There are three main types of constraints Key constraints Entity integrity constraints Referential integrity constraints Another implicit constraint is in the domain constraint. Every value in a tuple must be from the domain of its attribute (or null, if it is allowed) KS141304 PBD Institut Teknologi Sepuluh Nopember (ITS) 13 Key Constraints NRP Nama Kota Kelahiran No. KTP NPWP 521210007 Anna Berg 1231321 as4555543 521210008 Elsa Arandelle 2342324 qw123222 521210009 Sven Berg 3453453 pp544437 521210010 Rudolph Arandelle 4654564 ks332123 KS141304 PBD Institut Teknologi Sepuluh Nopember (ITS) 14 Superkey: A set of attribute which have no two tuples same. {NRP, Nama, No.KTP, NPWP}is a superkey Key: A minimal superkey. {NRP}is a key Key Constraint If a relation has several candidate keys, one is chosen to be a primary key. The primary key is underlined Choose the smallest size from candidate keys KS141304 PBD Institut Teknologi Sepuluh Nopember (ITS) 15 Entity Integrity Constraint Entity Integrity Each of entity should have at least one primary key, although it is weak or strong. The primary key must not null Function of primary key is to identify the individual tuples KS141304 PBD Institut Teknologi Sepuluh Nopember (ITS) 16 Referential Integrity Constraint A constraint involving two relations, displayed as directed arc R1.FK to R2. KS141304 PBD Institut Teknologi Sepuluh Nopember (ITS) 17 Singer Album Referential Integrity Constraint On update/delete cascade On update/delete reject On update/delete set null All actions related to foreign key should be consistent with the primary key and vice versa. KS141304 PBD Institut Teknologi Sepuluh Nopember (ITS) 18 Other types of Constraint Semantic Integrity Constraint Maximum overtime hours is 4 hours Maximum total working hours per week is 56 hours Minimum wage of all employees is 24K A certain language may have to be used to express these. KS141304 PBD Institut Teknologi Sepuluh Nopember (ITS) 19 Populated Database State Basic operation for changing database INSERT MODIFY/UPDATE DELETE All integrity constraint should be obeyed in each of operation. KS141304 PBD Institut Teknologi Sepuluh Nopember (ITS) 20 Mini World Artist_ID Artist_Name 1 Bono 2 Cher 3 Nuno 4 The Corrs KS141304 PBD Institut Teknologi Sepuluh Nopember (ITS) 21 Artist_ID Album_ID Album_Name 1 1 Schizophrenic 3 2 Best of the best 4 3 Radio 3 4 Love Possible violations for each operation Domain constraint: if one of the attribute values provided for the new tuple is not of the specified domain Insert <a2, Demi Lovato>into Singer Insert <3, 22-08-2014, I heart you>into Album KS141304 PBD Institut Teknologi Sepuluh Nopember (ITS) 22 Artist_ID Artist_Name 1 Bono 2 Cher 3 Nuno 4 The Corrs Artist_ID Album_ID Album_Name 1 1 Schizophrenic 3 2 Best of the best 4 3 Radio 3 4 Love Possible violations for each operation Key constraint: if the value of a key attribute in the new tuple already exist in another tuple in the relation Insert <3, Selena Gomez>into Singer Insert <2, 3, Dark Sky>into Album KS141304 PBD Institut Teknologi Sepuluh Nopember (ITS) 23 Artist_ID Artist_Name 1 Bono 2 Cher 3 Nuno 4 The Corrs Artist_ID Album_ID Album_Name 1 1 Schizophrenic 3 2 Best of the best 4 3 Radio 3 4 Love Possible violations for each operation Referential integrity: if a foreign key value in the new tuple references a primary key which doesn't exist insert <7, 5, Cherish>into Album KS141304 PBD Institut Teknologi Sepuluh Nopember (ITS) 24 Artist_ID Artist_Name 1 Bono 2 Cher 3 Nuno 4 The Corrs Artist_ID Album_ID Album_Name 1 1 Schizophrenic 3 2 Best of the best 4 3 Radio 3 4 Love Possible violations for each operation Entity integrity: if the primary key value is null in the new tuple Insert <null, One Direction>into Singer Insert <4, null, Open Air>into Album KS141304 PBD Institut Teknologi Sepuluh Nopember (ITS) 25 Artist_ID Artist_Name 1 Bono 2 Cher 3 Nuno 4 The Corrs Artist_ID Album_ID Album_Name 1 1 Schizophrenic 3 2 Best of the best 4 3 Radio 3 4 Love Possible violations for each operation DELETE may violate only referential integrity: if the primary key value of the tuple being deleted is referenced from other tuples in the database. KS141304 PBD Institut Teknologi Sepuluh Nopember (ITS) 26 Singer Album Possible violations for each operation Delete Singer tuple with Artist_ID =2 (wont violate) Delete Singer tuple with Artist_ID =3 (referenced by Album two times) KS141304 PBD Institut Teknologi Sepuluh Nopember (ITS) 27 Artist_ID Artist_Name 1 Bono 2 Cher 3 Nuno 4 The Corrs Artist_ID Album_ID Album_Name 1 1 Schizophrenic 3 2 Best of the best 4 3 Radio 3 4 Love Possible violations for each operation Can be remedied by several actions RESTRICT: reject the deletion CASCADE: propagate the new primary key value into the foreign keys of the referencing tuples SET NULL: set the foreign keys of the referencing tuples to NULL KS141304 PBD Institut Teknologi Sepuluh Nopember (ITS) 28 Singer Album Possible violations for each operation UPDATE/MODIFY: inherit all possible violations from INSERT and DELETE It can be remedied by RESTRICT, CASCADE, SET NULL KS141304 PBD Institut Teknologi Sepuluh Nopember (ITS) 29 Possible violations for each operation Modify the Artist_Name of the Singer tuple with Artist_ID =3 to Neno. (wont violate) Modify the Artist_ID of the Album tuple with Album_ID =3 to 5. (violate key constraint) KS141304 PBD Institut Teknologi Sepuluh Nopember (ITS) 30 Artist_ID Artist_Name 1 Bono 2 Cher 3 Nuno 4 The Corrs Artist_ID Album_ID Album_Name 1 1 Schizophrenic 3 2 Best of the best 4 3 Radio 3 4 Love Possible violations for each operation Modify the Artist_ID of the Album tuple with Album_ID =3 to Neno. (violate 2 constraints: domain dan inferential) What are they? How to remedy? KS141304 PBD Institut Teknologi Sepuluh Nopember (ITS) 31 Artist_ID Artist_Name 1 Bono 2 Cher 3 Nuno 4 The Corrs Artist_ID Album_ID Album_Name 1 1 Schizophrenic 3 2 Best of the best 4 3 Radio 3 4 Love TuliskanNotasi Aljabar KS141304 PBD Institut Teknologi Sepuluh Nopember (ITS) 32 Pertanyaan Apakah boleh menambahkan karakter pada penamaan field/attribute? Mengapa boleh? Atau Mengapa tidak boleh? KS141304 PBD Institut Teknologi Sepuluh Nopember (ITS) 33 Review Time! Formal and Informal Definition Type of Constraints Type of Violations KS141304 PBD Institut Teknologi Sepuluh Nopember (ITS) 34