P a g e | 50
Activity 4D
User Name Age Gender OccupationId CityId
Id 1 3
1 John 25 Male 3 4
2 2 5
3 Sara 20 Female 1 3
4
Victor 31 Male
Jane 27 Female
Occupation OccupationName City CityName
OccupationId Software Engineer CityId Halifax
1 Calgary
1 Accountant 2 Boston
2 Pharmacist 3
3 Library Assistant 4 New York
4 5 Toronto
Figure 4D
As shown in Figure 4D, solve the requirements below.
i. Create tables for User, Occupation and City.
Answer :
P a g e | 51
ii. Insert all data regarding to the Figure 4D.
Answer:
iii. Display the number of users according to their genders.
Answer:
iv. Find the user that works as software engineer and age from 25 until 30 only.
Answer:
P a g e | 52
v. Display all the information of user named as Sara and Victor which consist of Name,
Age, Gender, OccupationName and CityName only.
Answer:
P a g e | 53
Activity 4E
Complete the database and answer all the questions below using MySQL.
Database: CAR
CUSTOMER
CUSTID CUSTNAME CAR_NUMBER PHONE_ REGISTER_DATE
NUMBER
C0001 David Teo WUV2318 2014-01-09
C0002 Sulaiman Ali JKL9782 81691 2011-05-07
C0003 Kang Gary MAM123 61291 2010-06-13
C0004 SAA888 41972 2012-09-26
C0005 Ji Hyo KUL511 11341 2003-07-18
Karigalan 21412
CAR_COMPONENT COMPNAME PRICE
COMPID ISWARA Rear Lamp 100
KK001 WIRA Front Bumper 450
KK002 ISWARA Front Mirror 300
KK003 WAJA Break Lamp 250
KK004 WIRA Front Lamp 600
KK005
CLAIM
CLAIMID CLAIM_DATE CUSTID
CL001 2014-04-19 C0005
CL002 2014-01-28 C0001
CL003 2014-02-20 C0003
CL004 2014-09-30 C0002
CL005 2014-12-07 C0004
COMPONENT_CLAIM COMPID QUANTITY
CLAIMID KK001 1
CL001 KK003 2
CL003 KK002 1
CL002 KK005 1
CL005 KK004 2
CL004
P a g e | 54
STEP 1 : Create table CUSTOMER, CAR_COMPONENT, CLAIM and COMPONENT_CLAIM.
Answer:
P a g e | 55
STEP 2 : Insert all data into each table.
Answer:
P a g e | 56
STEP 3 : Type SQL command to find customer name from CUSTOMER table.
Answer:
P a g e | 57
STEP 4 : Type SQL command to display component ID, component name and price is
between 200 and 400.
Answer:
P a g e | 58
STEP 5 : Open the SQL Editor and type SQL command to display car number begin with J.
Answer:
P a g e | 59
STEP 6 : Open the SQL Editor and type SQL command to display claimID and quantity where
claim number is CL001 and CL004.
Answer:
P a g e | 60
STEP 7 : Open the SQL Editor and type SQL command to find the minimum value for price in
the CAR_COMPONENT table.
Answer:
P a g e | 61
STEP 8 : Open the SQL Editor and type SQL command to find the maximum value for price in
the CAR_COMPONENT table.
Answer:
P a g e | 62
STEP 9 : Open the SQL Editor and type SQL command to find the average value for price in
the CAR_COMPONENT table.
Answer:
P a g e | 63
STEP 10 : Open the SQL Editor and type SQL command to find the sum of price in the
CAR_COMPONENT table.
Answer:
P a g e | 64
STEP 11 : Open the SQL Editor and type SQL command to count the number of customers.
Answer:
P a g e | 65
DATABASE TRANSACTION MANAGEMENT
A database transaction is a logical unit of database operations, which is executed as
a whole to process user requests for retrieving data or updating the database.
Transaction Processing Systems (TPS)
A type of information system that collects, stores, Also attempt to provide
modifies and retrieves the data transactions of an predictable response
times to requests,
enterprise. although this is not as
critical as for real-time
Transaction processing Each transaction is usually systems.
allows only predefined, short duration and the
structured transactions.
processing activity for each
transaction is programmed
in advance.
VALUE OF TPS
Performance • Fast performance with a rapid response time
• Measured by the number of transactions they can process in a given
period of time.
Continuous • The system must be available during the time period when the users
availability are entering transactions.
• A breakdown will disrupt operations or even stop the business.
Data integrity • The system must be able to handle hardware or software problems
without corrupting data.
Ease of use
• Multiple users must be protected from attempting to change the
Modular same piece of data at the same time
Growth
• Often users of transaction processing systems are casual users
• The system should be simple for them to understand, protect them
from data-entry errors as much as possible, and allow them to easily
correct their errors.
• The system should be capable of growth at incremental costs, rather
than requiring a complete replacement.
• It should be possible to add, replace, or update hardware and
software components without shutting down the system.
P a g e | 66
Batch Transaction Processing
Relies on accumulating transaction data over a period of time and
then processing the entire batch at once.
Batch processing is usually cyclic: daily, weekly, or monthly run
cycle is established depending on the nature of the transactions
Cheaper than on-line processing
Easier to control than on-line processing
Being captured using disk files
Database is constantly out of date
On-Line Transaction Processing System (OLTP)
Each transaction is completely processed immediately upon
entry.
the most common mode of used today
More costly than batch processing
Database is always up to date
Require the use of fast secondary storage such as magnetic disks
Properties Of Database Transaction P a g e | 67
A C I D
ATOMIC CONSISTENT ISOLATED DURABLE
Atomic Consistent Isolated Durable
• All or nothing • The database • Data used • Database
must be in a during the changes are
• All of the tasks consistent execution of a permanent after
of a database before and after database trrhe
transaction must the database transactionmust transaction
be completed transaction. It not be used by completes
means that a another
• If incomplete database database
due to any transaction must transaction until
possible not break the the execution is
reasons, the database completed
database integrity
transaction must constraints.
be aborted.
SYNTAX START TRANSACTION: begins a new
transaction.
COMMIT: commits the current
transaction, making its changes
permanent.
ROLLBACK: rolls back the current
transaction, canceling its changes.
P a g e | 68
CONCURRENCY CONTROL PURPOSE
• To prevent two different users (or two different connections by the same user)
from trying to update the same data at the same time.
Lost
Update
The INTERFERENCE PROBLEM
Scheduler ARISE FROM
SIMULTANEOUS ACCESS
TO DATABASE
Uncomitted
Dependency
Inconsistent
Retrieval
LOCK
GRANULARITY
DEADLOCKS CONCURRENCY TWO-PHASE
CONTROL WITH LOCKING (2PL)
LOCKING PROTOCOLS
METHODS
LOCKS
P a g e | 69
TOOLS TO PREVENT THE INTERFERENCE PROBLEMS
Locks • A procedure used to control concurrent access to data.
When transaction is accessing the database, a lock may
deny access to other transaction to prevent incorrect results
Two-Phase • The protocol utilizes locks that block other transactions from
Locking accessing the same data during a transaction's life.
(2PL)
protocol • Protocol to prevent lost update problems
RECOVERY TOOLS
TRANSAC- •Transaction log records all transactions and the database modifications
TION LOG made by each transaction.
•History of database changes which include undo and redo.
DATABASE •Enables to back up and restore databases.
BACKUP
•Backup and restore component provides an important safeguard for
protecting critical data stored in database.
CHECKPOINT •Writes all dirty pages for the current database to disk.
•Dirty pages are data pages that have been entered into the buffer
cache and modified, but not yet written to disk.
P a g e | 70
Activity 5A
Describe the properties of database transaction according to situation below in Figure 5A.
1. start transaction
2. read (A)
3. A = A–700
4. write (A) Transaction to transfer money from account A to B
5. read(B)
6. B = B+700
7. write(B)
8. commit
Figure 5A
a. Atomicity
_________________________________________________________________
_________________________________________________________________
_________________________________________________________________
b. Consistent
_________________________________________________________________
_________________________________________________________________
_________________________________________________________________
c. Isolation
_________________________________________________________________
_________________________________________________________________
_________________________________________________________________
d. Durability
_________________________________________________________________
_________________________________________________________________
_________________________________________________________________
P a g e | 71
Activity 5B
Perform a transaction of a given database in Figure 5B, using SQL statements.
Figure 5B
Many real world scenarios require transaction mostly in banking, finance and trading domain.
Database transaction is implemented in SQL using three keywords START TRANSACTION, COMMIT
and ROLLBACK. Based on the solution of scenario shows in Figure 5B, write the SQL statement to
transfer RM900 from Account _Number 1001 to Account _Number 2002.
Answer:
P a g e | 72
Activity 5C
Use START TRANSACTION AND COMMIT statement.
Note: You have to activate Xampp in order to execute the command.
STUDENT
ICNum Name Email
941018052157 Suffian Iskandar [email protected]
950802016214 Sara Summayyah [email protected]
Table 5C (i)
Figure 5C (i)
Step 1: Create database DAFTAR
Step 2: Type USE
P a g e | 73
Step 3: Create table STUDENT
Step 4: Type the SQL command to insert all data into STUDENT table
Step 5: Type SQL command to display all values in STUDENT table
Step 6: Type the SQL command with START TRANSACTION
Step 7: Type SQL command with to insert new data into STUDENT table
ICNum: 921120034454
Name: Syed Auris
Email: [email protected]
P a g e | 74
Step 8: Use select statement again command to display data in STUDENT table. State your
observation.
Step 9: Type SQL command to ROLLBACK the transaction.
Step 10: Use select statement again command to display data in STUDENT table. State
your observation.
Step 11: Type SQL command to COMMIT the transaction.
Figure 5C (ii)
P a g e | 75
Activity 5D
Higher Order Thinking Skills (HOTS) Questions
1. Explain the statement of ‘a transaction is a logical unit of work’.
_________________________________________________________________
_________________________________________________________________
_________________________________________________________________
_________________________________________________________________
_________________________________________________________________
_________________________________________________________________
2. What is the consistent database state, and how it is achieved?
_________________________________________________________________
_________________________________________________________________
_________________________________________________________________
_________________________________________________________________
3. List and discuss the four transaction properties. (A.C.I.D)
_________________________________________________________________
_________________________________________________________________
_________________________________________________________________
_________________________________________________________________
_________________________________________________________________
_________________________________________________________________
_________________________________________________________________
_________________________________________________________________
_________________________________________________________________
_________________________________________________________________
_________________________________________________________________
_________________________________________________________________
_________________________________________________________________
_________________________________________________________________
_________________________________________________________________
_________________________________________________________________
_________________________________________________________________
P a g e | 76
4. What is a transaction log and its functions?
_________________________________________________________________
_________________________________________________________________
_________________________________________________________________
_________________________________________________________________
_________________________________________________________________
_________________________________________________________________
_________________________________________________________________
_________________________________________________________________
5. What is a scheduler and the important to concurrency control?
_________________________________________________________________
_________________________________________________________________
_________________________________________________________________
_________________________________________________________________
_________________________________________________________________
_________________________________________________________________
_________________________________________________________________
_________________________________________________________________
_________________________________________________________________
_________________________________________________________________
_________________________________________________________________
_________________________________________________________________
6. What is lock and how it works?
_________________________________________________________________
_________________________________________________________________
_________________________________________________________________
_________________________________________________________________
_________________________________________________________________
_________________________________________________________________
_________________________________________________________________
_________________________________________________________________
_________________________________________________________________
P a g e | 77
7. What is concurrency control and its objective?
_________________________________________________________________
_________________________________________________________________
_________________________________________________________________
_________________________________________________________________
_________________________________________________________________
_________________________________________________________________
_________________________________________________________________
_________________________________________________________________
8. What is an exclusive lock and binary lock?
_________________________________________________________________
_________________________________________________________________
_________________________________________________________________
_________________________________________________________________
_________________________________________________________________
_________________________________________________________________
_________________________________________________________________
_________________________________________________________________
_________________________________________________________________
_________________________________________________________________
_________________________________________________________________
9. What is a deadlock and how it can be avoided?
_________________________________________________________________
_________________________________________________________________
_________________________________________________________________
_________________________________________________________________
_________________________________________________________________
_________________________________________________________________
_________________________________________________________________
_________________________________________________________________
_________________________________________________________________
_________________________________________________________________
_________________________________________________________________
_________________________________________________________________
_________________________________________________________________
P a g e | 78
10. What is the recovery tools and its explanations?
_________________________________________________________________
_________________________________________________________________
_________________________________________________________________
_________________________________________________________________
_________________________________________________________________
_________________________________________________________________
_________________________________________________________________
_________________________________________________________________
_________________________________________________________________
_________________________________________________________________
_________________________________________________________________
_________________________________________________________________
_________________________________________________________________
_________________________________________________________________
_________________________________________________________________
_________________________________________________________________
_________________________________________________________________
_________________________________________________________________
_________________________________________________________________
P a g e | 79
Activity 5E
Explain the interference problem arise from simultaneous access to database as situated
in figures below.
Figure 5E (i)
Answer:
An Uncommitted Data Problem P a g e | 80
TIME TRANSACTION STEP STORED VALUE
Read PROD_QOH 35
1 T1 PROD_QOH = 35 + 100 135
Write PROD_QOH 135
2 T1 Read PROD_QOH
(Read uncommitted data) 35
3 T1 PROD_QOH = 135 – 30 105
***** ROLLBACK *****
4 T2 Write PROD_QOH
5 T2
6 T1
7 T2
Figure 5E (ii)
Answer:
P a g e | 81
Figure 5E (iii)
Answer :
P a g e | 82
BIBLIOGRAPHY
Coronel, C. & Morris, S. (2018). Database Systems : Design, Implementation & Management
13th Edition. Cengage Learning, Inc. (ISBN: 9781337627900)
Hogan, R. (2018). A Practical Guide to Database Design. Taylor & Francis. Ltd.
(ISBN 9781138578067)
Watt, A. (2014, October 24). Chapter 11 Functional Dependencies – Database Design – 2nd
Edition. Pressbooks. https://opentextbc.ca/dbdesign01/chapter/chapter-11-
functional-dependencies/
Rungta, K. (2021, May 29). Functional Dependency in DBMS: What is, Types and Examples.
Https://Www.Guru99.Com/. https://www.guru99.com/dbms-functional-
dependency.html
Data Anomalies. (n.d.). Jhigh.Co.Uk. Retrieved June 9, 2021, from
http://jhigh.co.uk/Higher/dbases/anomalies.html
Data Anomalies | Database Management | Fandom. (n.d.). Database Management.
Retrieved July 8, 2021, from
https://databasemanagement.fandom.com/wiki/Data_Anomalies
P a g e | 83
ANSWER
Activity 1A Activity 1C
i. A collection of information that is organized so that i.
it can be easily accessed, managed and updated. Data redundancy and inconsistency
Difficulty in accessing data
Collection of related data that is shared by the various Data isolation — multiple files and formats
categories of users to meet the requirements of an Integrity problems
organization’s information. Atomicity of updates
Concurrent access by multiple users
ii. e-commerce, social Gaming, Online Television Security problems
Streaming
ii.
a. Data Security
Example:
i. The Customer_Transaction file has details about the
total available balance of all customers.
ii. A customer wants information about his/her
account balance.
iii.In a file system it is difficult to give the customer
access to only his/her data in the file.
iii. iv.Thus enforcing security constraints for entire file or
for certain data items are difficult.
Activity 1B v. b. Data Redundancy
Example:
i. a. Ensures that users can access the data they want. i. At a university, information of a staff might be at all
b. Ensures that data is both consistent (no contradictory departments.
data) and correct (no invalid data), and ensures that users ii. It can cause inconsistent data when in update.
trust the database.
0 c. Data Isolation
c. Ensures that a database can evolve to satisfy changing user
requirements. Example:
i. A teller looking up a balance must be isolated from
d. Ensures that users do not have unduly long response times
when accessing data. a concurrent transaction involving a withdrawal
from the same account. Only when the withdrawal
e. Ensures that data can be accessed and manipulated in transaction commits successfully and the teller
ways which match user requirements. looks at the balance again will the new balance be
reported.
ii.
d. Program/ Data Dependence
Allow Concurrency Example:
Control Security i. Assume in a banking system there is need to find
Maintain Data Security
Provide for Backup and Recovery out the names of all customers who live within a
Control Redundancy particular postal-code area.
Allow Data Independence ii. But there is only a program to generate the list of
Provide Non-Procedural Query Language all customers.
Perform Automatic Query Optimization iii. The bank officer has now two choices: either
obtain the list of all customers and extract the
iii. needed information manually or ask a system
It helps to make data management more efficient and programmer to write the necessary application
effective. program.
A database management system stores, organizes and iv. Both the alternatives are obviously unsatisfactory.
manages a large amount of information within a single
software application. e. Concurrent Access Anomalies
Use of this system increases efficiency of business Example:
operations and reduces overall costs i. Bank account A containing RM 6000/-. If two
transactions of withdraw funds (RM 500/- and RM
1000/- respectively) from account about same
P a g e | 84
time, result of the concurrent executions may i. b. Record Based Logical Model are based on application
leave the account in an incorrect state. and user levels of data. They are modelled considering
ii. Program on the behalf of each withdrawal read the logical structure of the objects in the database. This
the old balance, reduce amount and write result data models defines the actual relationship between
back. the data in the entities. There are 3 types of record
based data models defined so far- Hierarchical,
iii. If both two programs are concurrent they both Network and Relational data models.
may read the value RM 6000/-.
Depending on which one writes the value last, the
account may contain either RM 5500/- or RM 5000/-, Advantages :
rather than the correct value of RM 4500/-
It helps to address the issues of flat file data storage.
iii. In flat files, data will be scattered and there will not
Centralize be any proper structuring of the data. This model
groups the related data into tables and defines the
relationship between the tables, which is not
addressed in flat files.
ii. Hierarchical Data Model
Network Data Model
Relational Data Model
Activity 1E
A single central database accessed by multiple
users.
Easier to organise, edit, query and backup.
Can be slower because of high usage / load.
Distribute
i.
Database split into multiple files. ii. a. Microsoft Access, FoxPro, FileMaker Pro, Paradox,
Data access and retrieval faster at nearest points Lotus
Need to be ensure data is consistent /
b. Oracle, Microsoft SQL Server, IBM, DB2
synchronised
Activity 1D ii. End User
Application Programmer
i. a. Object based Data Models is designed using the entities Database Administrator
in the real world, attributes of each entity and their
relationship. It picks up each thing/object in the real world Activity 2A
which is involved in the requirement.
Advantages : Tuple Row of relation
It makes the requirement simple and easily Attribute Also known as row or record
Domain
understandable by representing simple diagrams. Named column of relation
One can covert ER diagrams into record based data Also known as Table
Set of possible value of
model easily.
Easy to understand ER diagrams attributes
P a g e | 85
Degree Number of attributes of a c.
Cardinality relation
Relation Number of tuples of relation
Relation used to refer a table
in a relational database.
Also be known as
relationship
Activity 2B d.
Attribute StaffNum, ICNum, Name, Position, Department, PhoneNum
Domain StaffNum ICNum Name Position Department PhoneNum
IT001 Abdullah Pensyarah
IT002 78110 Maimunah Kejuruteraan 09- e.
IT003 3-03- Prof Perisian 8840023 Activity 2E
IT004 5333 Kamal Madya
IT005 68092 Jamilah Pensyarah Sains 09-
IT006 3-11- Hakim Komputer 8840024
4908 Prof
80120 Anis Madya Sistem 09-
4-06- Professor Maklumat 8840025
2323
60072 Pensyarah Multimedia 09-
0-06- 8840026
5380 Kejuruteraan
59010 Perisian 09-
1-01- 8840027
2425 Multimedia
81030 09-
3-19- 8840028
9923
Degree 6
Tuple 6
Cardinality 6
Activity 2C a.
i. Primary Key b.
c.
Branch_Info table: Branch_Id
Student_Information Table: Coll_Id
ii. Candidate Key
Branch_Info table: Branch_Id, Branch_Name,
Branch_Code
Student_Information table: Stu_Id, Coll_Id, Ic_Num
iii. Foreign Key
Branch_Id
iv. Alternate Key
Branch_Info table: Branch_Name, Branch_Code
Student_Information table: Student_Id, Ic_Num
Activity 2D
d.
a.
e.
b.
Activity 3A P a g e | 86
i.
ii. 5. Each maintenance record is generated by one and
only one vehicle.
6. Some vehicles have not (yet) generated a
maintenance procedure.
ii.
iii.
iv. Activity 3D
v. i. Course Credit
Code Hours
Activity 3B Attributes Course Name DFC123
4
i. Lect_ID, Lect_FName, Lect_LName System Analysis 4 4
Class_ID, Class_Name, Lect_ID And Design DFP234 4
Entity Course_ID, Course_Name 4
Room_ID, Room_Loc, Room_Cap Human Computer 5 3
LECTURER Interaction DFC345 3
CLASS
COURSE Database Design 6
ROOM DFC102
Programming 3
Fundamentals DFC312
Internet Publishing 3
DFM41
Multimedia
Technology 25
ii.
Entity Attributes
Student No, Student Name,
Student Address, Programme, Academic
Advisor
Course Course Code, Course Name, Credit
Hours
ii. Student Student No, Course Code, Grade
Grade
Activity 3C iii.
i.
Entity Attributes
Student
1. A client can own many vehicles. Student No, Student
2. Some clients do not own vehicles. Course
3. A vehicle is owned by one and only one client. Name, Address,
4. A vehicle may generate one or more maintenance
Programme
records.
Course Code, Course
Name, Credit Hours
P a g e | 87
Student Grade Student No, Course Code,
Programme Grade
Programme, Academic
Advisor
3NF
PROJECT
Activity 3E Project_No Project_name
1 DADD
1NF 2 Programming
STAFF
3 Multimedia
Staff_ Staff_n Charge PROJECT(Project_no, Project_name)
no ame _hour
Job_classification Hour
00689 Samat Elect Engineer RM 20 20 STAFF
00500 Ah Web Developer RM 22 18 Staff_no Staff_name Job_classification
00444 Azie Web Developer RM 22 15 00689 Samat Elect Engineer
00123 Kamal System Analyst RM 25 30 00500 Ah Web Developer
00999 Hazlim Technical Support RM 16 56 00444 Azie Web Developer
00689 Samat Elect Engineer RM 20 40 00123 Kamal System Analyst
00111 Anaz Programmer RM 18 35 00999 Hazlim Technical Support
STAFF (Project_No, Staff_no, Project_name, Staff_name, 00111 Anaz Programmer
Job_classification, Charge_hour, Hour) STAFF(Staff_no, Staff_name, Charge_hour, Job_classification)
PROJECT
Project_No Project_name HOUR
1 DADD Staff_no Project_no Hour
2 Programming 20
3 Multimedia 00689 1 18
PROJECT(Project_No, Project_name) 15
00500 1 30
56
00444 1 40
35
00123 2
00999 2
2NF 00689 3
STAFF 00111 3
Charge_ HOUR(Staff_no, Project_no, Hour)
hour
Staff_no Staff_name Job_classification JOB
00689 Samat Elect Engineer RM 20 Job_classification Charge_hour
00500 Ah Web Developer RM 22 Elect Engineer RM 20
00444 Azie Web Developer RM 22 Web Developer RM 22
00123 Kamal System Analyst RM 25 System Analyst RM 25
00999 Hazlim Technical Support RM 16 Technical Support RM 16
00111 Anaz Programmer RM 18 Programmer RM 18
STAFF(Staff_no, Staff_name, Job_classification, Charge_hour) JOB(Job_classification, Charge_hour)
PROJECT
Project_No Project_name Activity 4A
1. The WHERE clause is used to filter individual
1 DADD
rows using either a comparison to a literal, or a
2 Programming comparison to another column.
The HAVING clause is used to filter groups of rows
3 Multimedia using an aggregate expression. Often, the same
aggregate expression is in the SELECT list.
PROJECT(Project_No, Project_name) You can use WHERE and HAVING in the same SELECT
statement, if necessary.
HOUR When a SELECT statement is executed, the WHERE
clause is executed before the GROUP BY clause, if
Staff_no Project_no Hour one exists. The HAVING clause is executed after the
GROUP BY clause.
00689 1 20
00500 1 18
00444 1 15
00123 2 30
00999 2 56
00689 3 40
00111 3 35
HOUR(Staff_no, Project_No, Hour)
2. Sometimes a number sequence is pointless as a
number. for instance, you would never perform a
mathematical function on a phone number or social
security number, but you might want to use them as
P a g e | 88
string types - for instance to append or remove an Activity 4B
area code or something. i.
3. The ORDER BY clause’s purpose is to sort the query
result by specific columns.
The GROUP BY clause’s purpose is summarize
unique combinations of columns values.
4. COUNT returns the number of values without regard ii. CREATE DATABASE CONSULT_COMPANY;
to what the values are. CREATE TABLE JOB (
SUM adds the values together and can only be JOB_CODE VARCHAR (10) NOT NULL,
applied to numeric values. JOB_DESCRIPTION VARCHAR (100),
JOB_CHARGE_HOUR INT,
5. The DATE data type uses numeric values based on the JOB_LAST_DATE DATE,
Julian calendar to store dates. This makes date PRIMARY KEY (JOB_CODE));
arithmetic such as adding and subtracting days or
fractions of days possible. CREATE TABLE EMPLOYEE (
EMP_NO VARCHAR (5) NOT NULL,
6. DELETE remove some or all the tuples from a table. JOB_CODE VARCHAR (10),
DROP can remove entire schema, table, domain, or EMP_NAME VARCHAR (100),
constraints from the database. EMP_HIRE_DATE DATE,
EMP_YEARS INT,
7. The logical AND operator (a && b) creates PRIMARY KEY (EMP_NO),
logical expressions where both values must be true for
the overall expression to also be true.
The logical OR operator (a || b) is an infix operator
made from two adjacent pipe characters. You use it to
create logical expressions in which only one of the two
values has to be true for the overall expression to be
true.
8. The SELECT DISTINCT statement is used to return only
distinct (different) values. Inside a table, a column
often contains many duplicate values; and sometimes
you only want to list the different (distinct) values.
9. The DATE type is used for values with a date part but
no time part. MySQL retrieves and displays DATE
values in 'YYYY-MM-DD' format. The supported range
is '1000-01-01' to '9999-12-31'.
10. NULL - an unknown value or no value or a missing
value. Eg: When user leaves the field without entering
details.
NOT NULL - non empty value. Eg: When user fill the
field with data.
Zero or White Space - It is different from NULL.
Eg: When user gives field values as Zero or
White Spaces.
P a g e | 89
FOREIGN KEY (JOB_CODE) REFERENCES JOB staffNo varchar (10) NOT NULL,
(JOB_CODE)); fName varchar (100),
lName varchar (100),
CREATE TABLE PROJECT ( position varchar (100),
PROJ_NO VARCHAR (10) NOT NULL, gender varchar (10),
EMP_NO VARCHAR (5), DOB date,
PROJ_NAME VARCHAR (100), salary int,
PROJ_VALUE INT, Foreign Key (branchNo) REFERENCES Branch (branchNo),
PROJ_BALANCE INT, Primary Key (staffNo));
PRIMARY KEY (PROJ_NO),
FOREIGN KEY (EMP_NO) REFERENCES EMPLOYEE Create table Client (
(EMP_NO)); clientNo varchar (10) NOT NULL,
fName varchar (100),
CREATE TABLE ASSIGNMENT ( lName varchar (100),
ASSIGN_NO VARCHAR (8) NOT NULL, telNo int,
PROJ_NO VARCHAR (10), prefType varchar (100),
EMP_NO VARCHAR (5), maxRent int,
ASSIGN_DATE DATE, email varchar (100),
ASSIGN_CHARGE_HOUR INT, Primary Key (clientNo));
PRIMARY KEY (ASSIGN_NO),
FOREIGN KEY (PROJ_NO) REFERENCES PROJECT Create table PrivateOwner (
(PROJ_NO), ownerNo varchar (10) NOT NULL,
FOREIGN KEY (EMP_NO) REFERENCES EMPLOYEE fName varchar (100),
(EMP_NO)); lName varchar (100),
address varchar (100),
INSERT INTO JOB VALUES (‘J10001’,’SENIOR telNo int,
EXECUTIVE’, 3,’ 2018-10-07’); email varchar (100),
INSERT INTO EMPLOYEE VALUES (‘E1001’, password varchar (100),
‘AZKAYRA BINTI EL AZMAN’,’ 2019-02-08,’J1001’, Primary Key (ownerNo));
5);
INSERT INTO PROJECT VALUES (‘P10016’, ‘SIME Create table PropertyForRent (
DARBY LISTING PROPERTY’, 20000, 25000, ‘E1001’); propertyNo varchar (10) NOT NULL,
INSERT INTO ASSIGNMENT VALUES (‘A722912’, ownerNo varchar (10),
‘2019-05-16, ‘P10016’, ‘E1001’, 3); staffNo varchar (10),
branchNo varchar (10),
Activity 4C type varchar (50),
rooms varchar (50),
i. rent int,
a. Branch (branchNo, street, city, postcode); Foreign Key (ownerNo) REFERENCES PrivateOwner
b. Staff (staffNo, fName, lName, position, gender, (ownerNo),
DOB, salary, branchNo); Foreign Key (staffNo) REFERENCES Staff (staffNo),
c. PropertyForRent (propertyNo, type, rooms, rent, Foreign Key (branchNo) REFERENCES Branch (branchNo),
ownerNo, staffNo, branchNo); Primary Key (propertyNo));
d. Client (clientNo, fName, lName, telNo, prefType,
maxRent, email); Create table Viewing (
e. PrivateOwner (ownerNo, fName, lName, address, clientNo varchar (10),
telNo, email, password); propertyNo varchar (10),
f. Viewing (clientNo, propertyNo, viewDate, viewDate Date,
comments); comments varchar (150),
Foreign Key (clientNo) REFERENCES Client (clientNo),
ii.
Create table Branch ( Foreign Key (propertyNo) REFERENCES PropertyForRent
branchNo varchar (10) NOT NULL, (propertyNo));
street varchar (100),
city varchar (100), Activity 4D
postcode int,
Primary Key (branchNo)); i. Create table City (
CityId int NOT NULL,
Create table Staff ( cityName varchar (50),
Primary Key (CityId));
Create table Occupation ( P a g e | 90
OccupationId int NOT NULL,
occupationName varchar (50), FROM User U, Occupation O, City C
Primary Key (OccupationId)); WHERE U.occupationId = O.occupationId
AND U.cityId = C.cityId
Create table User ( AND Name IN ('Sara', ‘Victor’);
Id int NOT NULL,
Name varchar (50), Activity 4E
Age int, STEP 1:
Gender varchar(20),
OccupationId int,
CityId int,
Foreign Key (OccupationId) REFERENCES Occupation
(OccupationId),
Foreign Key (CityId) REFERENCES City (CityId),
Primary Key (Id));
ii.
INSERT INTO City VALUES (1,'Halifax');
INSERT INTO City VALUES (2,'Calgary');
INSERT INTO City VALUES (3,'Boston');
INSERT INTO City VALUES (4,'New York');
INSERT INTO City VALUES (5,'Toronto');
INSERT INTO Occupation VALUES (1,'Software Engineer');
INSERT INTO Occupation VALUES (2,'Accountant');
INSERT INTO Occupation VALUES (3,'Pharmacist');
INSERT INTO Occupation VALUES (4,'Library Assistant');
INSERT INTO User VALUES (1,’John’, 25,'Male', 1, 3);
INSERT INTO User VALUES (2,'Sara', 20,'Female', 3, 4);
INSERT INTO User VALUES (3,'Victor', 31,'Male', 2, 5);
INSERT INTO User VALUES (4,'Jane', 27,'Male', 1, 3);
iii.
SELECT Gender, COUNT (Id)
FROM User
Group by Gender;
iv. SELECT U.Name
FROM User U, Occupation O
WHERE O.occupationName ='Software Engineer'
AND U.Age BETWEEN 25 AND 30;
v. SELECT Name, Age, Gender,
O.occupationName, C.cityName
P a g e | 91
STEP 2: STEP 4 :
STEP 3 :
STEP 5 :
STEP 6 :
STEP 7 :
STEP 8 : P a g e | 92
STEP 9 :
STEP 10 : select balance from Account where
STEP 11 : Account_Number='2002';
update Account set balance = balance - 900 where
Account_Number='1001';
update Account set balance = balance + 900 where
Account_Number='2002';
commit; //if all sql queries succeed
Activity 5C
Step 1:
Step 2 :
Step 3 :
Step 4 :
Step 5 :
Activity 5A Step 6 :
a. If the transaction fails after step 4 but before step 8, Step 7 :
Step 8 :
the updates on A should not be reflected in the
database (rollback).
b. The sum of A and B should not be changed by the
transaction.
c. If another transaction is going to access the partially
updated database between step 4 and 7, it will see an
inconsistent database (with a sum of A and B is less
than it should be).
d. Once the money has been transferred from A to B
(commit), the effect of the transaction must persist.
Activity 5B
start transaction
select balance from Account where
Account_Number='1001';
Step 9 : P a g e | 93
Step 10 : database concurrency control and integrity. The
information stored in the log is used by the DBMS to
Step 11 : recover the database after a transaction is aborted
or after a system failure. The transaction log is
Activity 5D usually stored in a different hard disk or in a
different media (tape) to prevent the failure caused
1. A transaction is a logical unit of work that must by a media error.
be entirely completed of aborted; no 5. The scheduler is the DBMS component that
intermediate states are accepted. In other words, establishes the order in which concurrent
a transaction, composed of several database database operations are executed. The scheduler
requests, is treated by the DBMS as a unit of work interleaves the execution of the database
in which all transaction steps must be fully operations (belonging to several concurrent
completed if the transaction is to be accepted by transactions) to ensure the serializability of
the DBMS. transactions. In other words, the scheduler
guarantees that the execution of concurrent
2. A consistent database state is one in which all transactions will yield the same result as though
data integrity constraints are satisfied. To achieve the transactions were executed one after another.
a consistent database state, a transaction must The scheduler is important because it is the DBMS
take the database from one consistent state to component that will ensure transaction
another. serializability. In other words, the scheduler allows
the concurrent execution of transactions, giving
3. Atomicity end users the impression that they are the
requires that all parts of a transaction must be DBMS's only users.
completed or the transaction is aborted. This 6. A lock is a mechanism used in concurrency control
property ensures that the database will remain to guarantee the exclusive use of a data element to
in a consistent state. the transaction that owns the lock. For example, if
Consistency the data element X is currently locked by
Indicates the permanence of the database transaction T1, transaction T2 will not have access
consistent state to the data element X until T1 releases its lock.
Isolation Generally speaking, a data item can be in only two
means that the data required by an executing states: locked (being used by some transaction) or
transaction cannot be unlocked (not in use by any transaction). To access
accessed by any other transaction until the first a data element X, a transaction T1 first must
transaction finishes. This property ensures data request a lock to the DBMS. If the data element is
consistency for concurrently executing transactions. not in use, the DBMS will lock X to be used by T1
Durability exclusively. No other transaction will have access to
indicates that the database will be in a permanent X while T1 is executed.
consistent state after the execution of a 7. Concurrency control is the activity of coordinating
transaction. In other words, once a consistent the simultaneous execution of transactions in a
state is reached, it cannot be lost. multiprocessing or multi-user database
management system. The objective of concurrency
4. The transaction log is a special DBMS table that control is to ensure the serializability of
contains a description of all the database transactions in a multi-user database
transactions executed by the DBMS. The database management system. (The DBMS's scheduler is in
transaction log plays a crucial role in maintaining charge of maintaining concurrency control.)Because
it helps to guarantee data integrity and
consistency in a database system, concurrency
control is one of the most critical activities
performed by a DBMS. If concurrency control is
not maintained, three serious problems may be
caused by concurrent transaction execution: lost
updates, uncommitted data, and inconsistent
retrievals.
8. Binary Locks − A lock on a data item can be in two
states; it is either locked or unlocked.
Shared/exclusive − this type of locking mechanism
differentiates the locks based on their uses. If a lock
is acquired on a data item to perform a write P a g e | 94
operation, it is an exclusive lock. Allowing more than
one transaction to write on the same data item simultaneously and T1 is rolled back after the T2 has
would lead the database into an inconsistent state. already accessed the uncommitted data. Refers to
Read locks are shared because no data value is being figure above, the original units are 35 then it have
changed. an addition of 100 units. Now, the total units
9. Start by pointing out that, although locks prevent become 135. For any possibilities, it can occurs an
serious data inconsistencies, their use may lead to error due to any transaction when T1 does not
two major problems:1.The transaction schedule commit then T2 has read the uncommitted data.
dictated by the locking requirements may not be Now, T2 has made a new transaction which
serializable, thus causing data integrity and subtracts 30 units from the current value which is
consistency problems.2.The schedule may create 135 units. Suddenly, T1 has rolled back then the
deadlocks. Database deadlocks are the equivalent value now become 35 units back. The correct
of a traffic gridlock in a big city and are caused by execution happens when T1 has rolled back then he
two transactions waiting for each other to unlock or she made a commit, the stored value will hold 35
data. units then subtracts the 30 units, the final result will
In a real world DBMS, many more transactions can be supposed to 5 units only.
be executed simultaneously, thereby increasing the iii. Inconsistent retrievals occur when transaction
probability of generating deadlocks. Note that accesses data before and after another transaction
deadlocks are possible only if one of the finish working with such data. As shown in Figure 5E
transactions wants to obtain an exclusive lock on a (iii), the original value of a, b and c are 100, 200 and
data item; no deadlock condition can exist among 300. T1 had withdrawn the value of 100 and now the
shared locks. balance become 0. Then, T1 has made an additional
10. of 100 as deposit which the current value is 300 for
now. At the same time, T2 reads the current value
Log File as 0 after the T1 has made a subtraction. Then, T2
The file contains information about all changes reads total of b is still 200 as the original value and
made to the database. It provides a history of not the value of transaction that have been made by
database changes made by the transactions. T1. So, it can occurs inconsistent reading of balance
Checkpoint in this account because T1 reads the value of a b and
The checkpoint is the time when all transactions c are 0, 300 and 300 but then T2 reads another
stop. At this point a checkpoint record is written balance amount as 0, 200 and 500
to the log and database buffers are written to
disk. Checkpoints are written at periodic -END-
intervals and involve the following operations:
Writing all log records in main memory to
secondary storage
Writing all modified blocks in database
buffers to secondary storage
Writing a checkpoint record to the log file
Activity 5E
i. Lost updates occur when two transactions which
made by Alice and Bob, they want to update the
quantity of product which is 7. Note that the first
transaction (Alice) has not yet been committed
when she update the product quantity from 7 to 6
while the second transaction (Bob) is also executed.
Bob had made quantity of product become 10. At
the same time Alice has done the commit which
quantity of product still hold value of 6,then Bob
also committed the transaction that promptly
overwrite the value for quantity of product which
supposed to hold the value of 6 become 10.
ii. Uncommitted data problem can arise when two
transactions (T1 and T2) are executed
P a g e | 95
A book related to DATABASE DESIGN basic knowledge, which has FIVE (5)
chapters. Each chapter have tutorial questions to enhance the
understanding of the chapter. It is suitable for all ages to learn Database
Design by using this book. This e-book also uses easy language to
understand and have examples of how to solve questions related to
databases.
AUTHORS
NORSHADILA BINTI AHMAD BADELA
Pensyarah
Jabatan Teknologi Maklumat & Komunikasi
Politeknik Mersing, Johor
MAS AYU BINTI MOHD ARIFF
Pensyarah
Jabatan Teknologi Maklumat & Komunikasi
Politeknik Mersing, Johor
PUZIAHHAIZA BINTI PAZUI
Pensyarah
Jabatan Teknologi Maklumat & Komunikasi
Politeknik Mersing, Johor
POLITEKNIK MERSING
Jalan Nitar,
86800 Mersing,
Johor
07-7980001 07-7980002
https://pmj.mypolycc.edu.my