Uploaded by francescabrz31

Integrity constraints

advertisement
Integrity constraints
Tuesday, 16 March 2021
6:29 pm
Super key
→ A super key is different from a key
→ Can be used to uniquely identity a row
→ It doesn't have to be minimal
Key
→ A minimal key : no attributes for information
→ If we have more than one key we call the set= additive keys
→ We can have many keys. Each key is called a candidate key
○ One is selected as a primary key
Non primary key attribute
→ An attribute can potentially unique but is not the main identifier.
Entity Integrity restraints
→ No primary key can be null at any time
→ Additionally, if the primary key is also a compound key, no contributing
attributes can be null
Referential Integrity restraints
→ This is specified between two relations and based on foreign keys
→ It linking between two relations
Foreign keys
→ A minimal key : no attributes for information
Foreign keys
→ A minimal key : no attributes for information
→ Foreign keys allow us to relate two different schemas
→ The foreign key is what is expressed twice. It is giving additional information
and linking the attribute of the important schema
Anna less being a part of department 1 and she is also the manager.
There is no foreign key of human resources. Jack also is not associated with a
department yet
--Example--Referential integrity constraint violation
A referential integrity constraint can be utilised to guarantee that a department
with department number 2 exists before the "Grace Mills" tuple is stored.
The foreign. Key Employee.department reference Department.id. There is no
department 5 so it violates.
Another option is if we don't know where Grace works or starts a new department
we could write null in the department. So the constraint is still respected.
Self-referencing Relations
→ Its also possible for a table to reference itself
Another option is if we don't know where Grace works or starts a new department
we could write null in the department. So the constraint is still respected.
Self-referencing Relations
→ Its also possible for a table to reference itself
--Example--
In the example, managerID references ID
For example. Jack Smith has a manger who is 4671. The manager is Anna Lee.
Now the top three don't have mangers, we don’t need to specify which still satisfies
the constraint as null is admitted.
Relations with Composite Keys
→ Foreign keys to relations that can have multi-attribute primary key
→ Identified by curly brackets
--Example--
Constraints and…
Constraints and Operations
→ Enforces of the integrity constraints ensures that the database remains
consistent
Constraints and…
Constraints and Operations
→ Enforces of the integrity constraints ensures that the database remains
consistent
→ When we are making changes, the instances won't violate the integrity
constraints
→ If a database update is submitted to the DBMS that would violate the
integrity, it must be rejected.
→ Sometimes things won't work, I have understand the machine is not working.
I am doing wrong not the software.
--Example--
LOL is not a number
The key is already used
Null can not be used for unique ID
Lastly because employee department is referencing ID. There is no department 6
Constraints and Deletions
→ Referential integrity can be violated if the tuple being deleted is reference by
a foreign key from other tuples.
→ The deletion can be rejected, cascaded or referencing attribute values can be
modified.
If I had to delete the Development department but I still want to employee Paris
If I had to delete the Development department but I still want to employee Paris
Lane and Grace Mills. I can set the referencing values in department to NULL. This
would be modifying the referential department
Constraints and Modification
→ When we update we have to know the following
○ Non-key values: domain check
○ Primary key: is similar to performing a delete and insert
○ Foreign key: DBMS must ensure new value refers to existing tuple if
referenced relation
--Activity--
1. Superkey: stID, name, email in relation Student
2. Minimal Key: stID in relation Student ( we know this is minimal because it is
underlined- primary key)
3. Foreign Key: stID in Enrolment schema or cCode in Course is referenced by
enrolment
4. Domain Key: cCode, 4 letters followed by 4 num
--Activity--
The answer is A
The answer is A
--Activity--
The answer is B . The ID 2670 is already a unique key by Grace Mills.
--Activity--
The answer is B.
--Activity--
--Activity--
The answer is C. A is not correct because city name and province names, in order
to identity cities. City is a weak entity so it needs a reference to province. B partially
addresses that.
Semantic Integrity Constraint
→ These are constraints that cannot be directly expressed in the schemas data
model.
→ Also known as a business rules
○ They can be used to enforce organisation polices such as:
- "the salary of an employee should not exceed the employee's
supervisor's salary"
- "The maximum number of hours that an employee can work on a
project is 56"
○ These are implemented in a SQL using triffers and assertions
○ Semantic integrity constraints can be violated during insertion,deletion or
modification. They can have warnings.
--Example--
Transaction concept
→ Execution of a program that includes operations such as reading, insertions,
deletions or updates
→ At the end of the transaction, it must leave the database in a valid state that
satisfies all the constraints specified.
--Example-
Delete and its violations
→ You need to be concerned with semantics and referential constraints.
Download