Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                

Database Systems 1: Lab Session 2

Download as docx, pdf, or txt
Download as docx, pdf, or txt
You are on page 1of 8

1

BAHRAIN POLYTECHNIC

Database Systems 1
Lab Session 2
2

Lab session 2

Data Types

Each value manipulated by database has a datatype.

The datatype of a value associates a fixed set of properties with the value.

When a table is created, a datatype for each of its column must be specified.

Datatypes define the domain of values that each column can contain.

Sl No. Datatype Description


1 VARCHAR2(size [BYTE | CHAR]) Variable-length character string having
maximum length size bytes or characters.
Maximum size is 4000 bytes or characters,
and minimum is 1 byte or 1 character. You
must specify size for VARCHAR2.
2 NUMBER[(precision [, scale]]) Number having precision p and scale s. The
precision p can range from 1 to 38. The
scale s can range from -84 to 127.
3 DATE Valid date range from January 1, 4712 BC to
December 31, 9999 AD. The default format
is determined explicitly by the
NLS_DATE_FORMAT parameter or implicitly
by the NLS_TERRITORY parameter. The size
is fixed at 7 bytes. This datatype contains
the datetime fields YEAR, MONTH, DAY,
HOUR, MINUTE, and SECOND. It does not
have fractional seconds or a time zone.
4 CHAR [(size [BYTE | CHAR])] Fixed-length character data of length size
bytes. Maximum size is 2000 bytes or
characters. Default and minimum size is 1
byte.

For more details about datatypes, refer Oracle Datatypes

Data Integrity Constraints

Data integrity requires the database to be an accurate reflection of the real world - data must be valid
and complete

Codd (1985) states that integrity constraints specific to a particular RDBMS must be definable in the
RDBMS sublanguage and stored in the system catalogue.

Integrity rules should be considered at design time.

Transactions must be monitored for integrity violations and appropriate actions taken.

Rules should be few, without overlap and should not impact performance significantly.
3
4

Codd defined four types of integrity constraints.


 Type E - Entity Integrity
 Type R - Referential Integrity
 Type D - Domain Integrity
 Type U - User Defined Integrity

Entity integrity (E-type)


 No primary key value of a base relation is allowed to be null or have a null component.
 The rationale for this rule was that entities in the real world are distinguishable, that is,
they are identifiable in some way.
 Provided by placing NOT NULL on the primary key.

CREATE TABLE emp


( empno NUMBER NOT NULL
, ename VARCHAR(10)
, job VARCHAR(9)
, mgr NUMBER
………
PRIMARY KEY (empno));

Referential integrity (R-type)


 For each foreign-key value in a relational database, there must exist in the database an
equal value of a primary key from the same domain.
 If the foreign key is composite, those components that are themselves foreign keys and
non-null must exist in the database as components of at least one primary-key value drawn
from the same domain.
 Referential Integrity constraints, through the use of foreign keys allows the representation
of the relationships between tables.
 Referential integrity means that if a foreign key exists and contains a value, that value must
refer to an existing tuple (row) in another relation. It must:
 either be equal to null (provided nulls are permitted), or
 be equal to the primary key attribute in the referenced relation
 for example, when updating the dept_no attribute in the EMPLOYEE relation the
value entered must match a dept_no attribute in the DEPARTMENT relation.
 If an FK error occurs the most suitable response would be either a failure to insert the new
tuple or update the existing foreign key attribute.

Domain integrity (D-type)

 Consists of those integrity constraints that are shared by all the attributes that draw their
values from that domain.
 Common constraints are regular data types, ranges of values permitted and whether or not
the ordering comparators, > and <, are applicable to these values.

User-defined integrity (U-type)

 Permits the DBA to define the “business rules” pertaining to the business.
 For example, the salary of an employee should not exceed the salary of the employee’s
supervisor.
5

Consider the below example to identify the possible integrity constraints in the below two figures:

Fig 1:

Fig 2:
6

Referential Integrity Violations

Delete Cascade

The delete of a referenced relation cascades to delete all the matching foreign key tuples

For example, deleting a department from the DEPARTMENT relation would cascade to delete
all matching foreign keys tuples in the EMPLOYEE relation.

Delete Set Null

 When a referenced tuple is deleted, then all the matching foreign key values are set to null
 For example, deleting a department from the DEPARTMENT relation would result in all
matching foreign key values in the EMPLOYEE relation being set to null
 Can only apply to foreign keys that can accept nulls

Delete Set Default

 When a referenced tuple is deleted, then all the matching foreign key values are set to a
default value
 For example, deleting a department from the DEPARTMENT relation would result in all
matching foreign key values in the EMPLOYEE relation being set to a default value

Update rule

 Applies to updating the Primary Key in the parent (referenced) relation


 For example, updating the department number attribute in the DEPARTMENT relation with
matching foreign keys in the EMPLOYEE relation would cause an integrity violation

Update Restrict

 The update operation is restricted if a referenced tuple exists with a foreign key value equal to
a primary key value in the referenced relation. For example, updating the department number
in the DEPARTMENT relation would be restricted if there were matching department numbers
in the EMPLOYEE relation
 Default in Oracle
7

Update Cascade

 The update of a primary key value cascades to update matching foreign key values in
dependent relations
 For example, updating the dept_no in the DEPARTMENT relation would cascade to
update all matching dept_nos in the EMPLOYEE relation.

Update Set Null

 When a primary key is updated, matching foreign key values in dependent relations are set to
null.
 Can only apply to foreign keys that can accept nulls.
 For example, updating the dept_no in the DEPARTMENT relation would result in all matching
dept_nos in the EMPLOYEE relation being set to null.

Update Set Default

 When a primary key is updated, matching foreign key values in dependent relations are set to a
default value
 for example, updating the dept_no in the DEPARTMENT relation would result in all matching
dept_nos in the EMPLOYEE relation being set to a default value.
8

Part 1 – Theory Questions


You are required to create a table to store information as shown below. Identify suitable data types for
each column and also specify the data integrity constraint as well

Customer_ID Customer_Name Customer_Gender Order_Date


T212336 Tom Cruise M 25-Jan-2000
A456665 Alice Jane F 4-Dec-2000

I. List the datatypes for each column


II. List the data intergrity constraints for each column

Student_ID Student_Name Grade


2013333 Emai Jack A+
2015456 Diana Rose C

I. List the datatypes for each column and specify the reason for the same
II. List the data intergrity constraints for each column and specify the reason s for the same

Multi-Valued Attributes
Consider the below scenario:

A student can enroll in many courses and a course can be taken by more than one student

Student Details

Student_ID Student_Name
201089555 Hayna Lee
201125656 Dee Rai Course Details
201489666 Liam R
201534343 Tim Jew
Course_ID Course_Name
CP1 Computer Programming 1
DB1 Database Systems 1

Enrollment Details

Student_ID Course_Name
201089555 Computer Programming 1
201489666 Computer Programming 1
201089555 Database Systems 1
201534343 Database Systems 1
201489666 Databse Systems 1

1. Can Student_ID be chosen as the primary key in the Enrollment table? If No, specify the reason.
2. How do we resolve this?

You might also like