What is it?How useful?How does it relate to business rules?How is it implemented?
• They believe it is accurate, !!!!!! Is the information in the database accurate? How?• Data Consistency is one attribute of the database system, how is it realized?• How do we control the integrity of the data and information in the database?• How do we enforce the business rules in the database application?
StudID Name Dbirth SEX SubjID Courwo Fexam Grade rkMU009 Sophy 25th May female ICT 208 40% 3o A 99MU007 June M ICT 2o8 thirty forty A 2005MU009 Hamad 17/6/98 ICT 208 40/50 20/50 BMU008 Bahati Mke ICT 208 70 110 - Marks – what is the format? Coursework more than 50? 110? 0 or o? Student with no name??? Date of birth – null? Format? Gender – null? Format? Grade – null? What is -? StudentID MU009 – duplicate?
CUSTOMERCustI Fnam Lnam Sex AddreD e e ssC001 John Mgatta M 1 TAC002 Kassi Hamid M 12 Mk mC009 Baraka Njenje F ORDER OrdID CustI Date Desc Amou D nt Think!! 1.Delete customer coo1!! His order?? 001 C007 21/1/0 disks 12000 2.Where do we ship order 002? 9 3.Who pressed order 001 ? 002 c009 12/3/0 RAM 10000 9 0 008 c001 1/3/09 software 20000 0
Database is not consistent!! INTIGRITY OF THE INFORMATION IS QUESTIONABLE!!! WHAT DO WE DO THEN??
“A constraint is a restriction. Placedat either column or table level, aconstraint ensures that your datameets cer tain data integrity rulesbefore they are either inser t, updateor deleted from the relation .”
• Constraints allows database developers to enforce some restriction that should be satisfied before data are entered, updated, or deleted from a database.• Constraints can be used to enforce business rules. For example if the marks for student can not be more than 100% or less that 0% etc, and if one enters values out of that range the system won’t allow it. Error message will be displayed. If the rule allows student to borrow only four books, the constraint will stop student from borrowing fifth book
1. Entity constraints Entity constraints are all about individual rows. PRIMARY KEY , UNIQUE2. Domain constraints Domain constraints are column level constraints. Either these constraints are based on one or more columns. CHECK , RULES , DEFAULTS3. Referential integrity constraints Referential integrity constraints are created when a value in one column must match the value in another column — in either the same table or a different table. FOREIGN KEY
PRIMARY KEY A primary key constraint ensures uniqueness within the column(s) declared as being part of that primary key, and that unique value serves as an identifier for each row in that table. Primary key does not allow null values because their presence will not make possible to locate data items, and there might be duplicate null values When a person enters duplicate or null value to the primary key, error message will be displayed and record will not be updated.
UNIQUE KEY• UNIQUE Key constraints require a unique value throughout the named column or set of columns in the table.• Once you establish a UNIQUE Key constraint, every value in the named columns must be unique. If you go and update or insert a row with a value that already exists in a column with a unique constraint, SQL Server will raise an error and reject the record.• The major difference btn UNIQUE and Primary Key is that you can have more than one UNIQUE constraint but only one primary key per table.• You can have only one Null value on a column where Unique Key is defined
It ensures that the column that is listed in the constraint contains no null value. Columns without NOT NULL can contain null values except for primary key column. NOT NULL constrain is a domain constraint and specifically is defined at column level.
• Defines a condition that each row must satisfy• CHECK constraints is not restricted to a particular column. They can be defined on multiple columns as long as all the columns are within a single or same table.• Example if you want to make student marks in a range between 0 and 100 then while creating a relation set check constraint at mark column to accept that range.• This will stop insert or update of record if it will make marks to a value outside that range.
Sometimes it is used to highlight missing value orto avoid Null value- Defaults are only used when Inserting record —they are ignored for UPDATE and DELETEstatements.- If any value is supplied in the INSERT, then thedefault is not used.- If no value is supplied, the default will always beused.
Foreign key (referential integrity) constraint are both a method of ensuring data integrity and a manifestation of the relationships between tables. When you add a foreign key to a table, you are creating a dependency between the table for which you define the foreign key and the table your foreign key references. After adding a foreign key, any record you insert into the referencing table must either have a matching record in the referenced column(s) of the referenced table, or the value of the foreign key column(s) must be set to NULL. A foreign key value must match an existing value in a parent table or be null The foreign key is defined at child table ON DELETE CASCADE or ON DELETE SET NULL
Do we need constraints? Are the constraints enough to make database accurate? What do we do to have accurate database or is it possible to have 100% accurate system? Suggest how do we solve problems found in previous slides?