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

ADVANCED DATABASE

ASSIGNMENT - 2
UNIT - 2

Question 1:

Consider the employee details such as employee identity, employee name, job position
code, job position name and annual salary. Write the SQL statements for the following task:

a. Create a non-temporal table for representing the above details.


b. Create a temporal table to describe the position of the employee with valid-time state
data List the name of the employees along with the period in the same position.
c. List the period in which the employee is in a particular position.
d. Construct an employee bitemporal table to maintain the designation and daily
attendance entry details. (With 5 instances)
e. Extract the employees with similar designation from the above created table.
f. Extract the employees who are on leave on a particular day.

Consider the employee details such as employee identity, employee name, job position
code, job position name and annual salary. Write the SQL statements for the following task:

Create a non-temporal table for representing the above details.

Assuming that the table should be named "employees", and the columns should be named
"employee_id", "employee_name", "job_position_code", "job_position_name", and "annual_salary",
the SQL statement for creating the non-temporal table would be:

SQL code:

CREATE TABLE employees (


employee_id INT PRIMARY KEY,
employee_name VARCHAR(50),
job_position_code VARCHAR(10),
job_position_name VARCHAR(50),
annual_salary DECIMAL(10,2)
);

This creates a table with the specified columns and data types. The employee_id column is
set as the primary key to ensure that each employee has a unique identifier. The VARCHAR data
type is used for columns that will store text values, while the DECIMAL data type is used for the
annual_salary column to store the annual salary as a decimal number with up to 10 digits and 2
decimal places.
Create a temporal table to describe the position of the employee with valid-time state
data.

To create a temporal table to describe the position of an employee with valid-time state data, we
need to add two additional columns to store the time interval during which each row of data is valid.
These columns are the valid_from and valid_to columns. The SQL statement for creating a
temporal table with this structure is as follows:

SQL code:

CREATE TABLE employee_positions (


employee_id INT,
employee_name VARCHAR(50),
job_position_code VARCHAR(10),
job_position_name VARCHAR(50),
annual_salary DECIMAL(10,2),
valid_from DATE,
valid_to DATE,
PRIMARY KEY (employee_id, valid_from),
PERIOD FOR valid_time (valid_from, valid_to)
) AS SYSTEM VERSIONING;

This creates a table named employee_positions with the specified columns and data types,
as well as the valid_from and valid_to columns for storing the time interval during which each row
of data is valid. The PRIMARY KEY constraint is set on the employee_id and valid_from columns
to ensure that each employee has a unique identifier and that each row of data is also unique in
terms of its valid-time period. The PERIOD FOR valid_time clause defines the valid-time period as
a time interval between the valid_from and valid_to columns.

Finally, the AS SYSTEM VERSIONING clause enables system-versioned temporal tables


for this table. This means that a history table will be automatically created to store old versions of
each row, and the valid_from and valid_to columns will be automatically updated as rows are
inserted, updated, or deleted from the table.
List the name of the employees along with the period in the same position.

To list the name of employees along with the period in the same position, we need to query
the employee_positions temporal table and join it with itself on the employee_id and
job_position_code columns to find rows with the same job position. We can then filter the results
to only show rows where the time intervals overlap, indicating that the employee was in the same
position for a period of time. The SQL statement to do this is as follows:

SQL code:

SELECT
e1.employee_name,
e1.valid_from,
e1.valid_to,
e2.valid_from,
e2.valid_to
FROM
employee_positions e1
JOIN employee_positions e2
ON e1.employee_id = e2.employee_id
AND e1.job_position_code = e2.job_position_code
WHERE
e1.valid_from <= e2.valid_to
AND e1.valid_to >= e2.valid_from
AND e1.employee_name <> e2.employee_name;

This query joins the employee_positions table with itself as e1 and e2, and filters the
results to only show rows where the time intervals overlap (e1.valid_from <= e2.valid_to and
e1.valid_to >= e2.valid_from) and the employee names are not the same (e1.employee_name
<> e2.employee_name), indicating that two different employees held the same job position for a
period of time. The result will show the name of the employees along with the start and end dates
of the period in which they held the same job position.

List the period in which the employee is in a particular position.

To list the period in which an employee is in a particular position, we can query the
employee_positions temporal table and filter the results based on the employee_id,
job_position_code, and the time interval during which the employee held the job position. The
SQL statement to do this is as follows:
SQL code:

SELECT
employee_name,
job_position_name,
valid_from,
valid_to
FROM
employee_positions
WHERE
employee_id = <employee_id>
AND job_position_code = <job_position_code>
AND valid_from <= <end_date>
AND valid_to >= <start_date>;

In this statement, we need to replace <employee_id> with the ID of the employee we're
interested in, <job_position_code> with the job position code we're interested in, <start_date>
with the start date of the period we want to check, and <end_date> with the end date of the period
we want to check. This query will return the name of the employee, the name of the job position,
and the start and end dates of the time interval during which the employee held the job position.
The result will show only the rows where the employee was in the particular job position during the
specified time interval.

Construct an employee bitemporal table to maintain the designation and daily attendance
entry details.(with 5 instances)

To construct an employee bitemporal table to maintain the designation and daily attendance
entry details with 5 instances, we need to add two additional columns to the employee_positions
table to track the system time when each row is inserted or updated. These columns are the
system_start_time and system_end_time columns. We can then use this table to store both the
historical and current state of employee position and daily attendance entry details. The SQL
statement to create this bitemporal table with 5 instances is as follows:

SQL code:

CREATE TABLE employee_bitemporal (


employee_id INT,
employee_name VARCHAR(50),
job_position_code VARCHAR(10),
job_position_name VARCHAR(50),
annual_salary DECIMAL(10,2),
valid_from DATE,
valid_to DATE,
system_start_time TIMESTAMP,
system_end_time TIMESTAMP,
PRIMARY KEY (employee_id, valid_from, system_start_time),
PERIOD FOR valid_time (valid_from, valid_to),
PERIOD FOR system_time (system_start_time, system_end_time)
) AS SYSTEM VERSIONING WITH 5 HISTORY TABLES;

This creates a table named employee_bitemporal with the specified columns and data
types, as well as the valid_from, valid_to, system_start_time, and system_end_time columns
for tracking the time intervals during which each row is valid and the system time when each row is
inserted or updated. The PRIMARY KEY constraint is set on the employee_id, valid_from, and
system_start_time columns to ensure that each row is unique in terms of its valid-time and
system-time periods. The PERIOD FOR valid_time and PERIOD FOR system_time clauses
define the valid-time period as a time interval between the valid_from and valid_to columns and
the system-time period as a time interval between the system_start_time and system_end_time
columns. Finally, the AS SYSTEM VERSIONING WITH 5 HISTORY TABLES clause enables
system-versioned bitemporal tables for this table with 5 history tables. This means that a history
table will be automatically created to store old versions of each row, and the valid_from, valid_to,
system_start_time, and system_end_time columns will be automatically updated as rows are
inserted, updated, or deleted from the table. With 5 history tables, we can store up to 5 previous
versions of each row, allowing us to track changes to the employee position and daily attendance
entry details over time.

Extract the employees with similar designation from the above created table.

To extract the employees with similar designations from the employee_bitemporal table,
we can use the LIKE operator to match the job position name against a pattern. The SQL
statement to do this is as follows:

SQL code:

SELECT
employee_id,
employee_name,
job_position_code,
job_position_name,
annual_salary,
valid_from,
valid_to,
system_start_time,
system_end_time
FROM
employee_bitemporal
WHERE
job_position_name LIKE '%<designation>%';
In this statement, we need to replace <designation> with the designation we want to match.
The % symbol is used as a wildcard to match any characters before or after the specified
designation. This query will return all rows from the employee_bitemporal table where the job
position name matches the specified pattern. The result will include the employee ID, employee
name, job position code, job position name, annual salary, valid-time and system-time period for
each row.

Extract the employees who are on leave on a particular day.

To extract the employees who are on leave from the employee_bitemporal table, we need to
join it with a daily_attendance table that stores the attendance details for each employee on each
day. We can then filter the result to include only those employees who have a leave entry for the
current date. The SQL statement to do this is as follows:

SQL code:

SELECT
e.employee_id,
e.employee_name,
e.job_position_code,
e.job_position_name,
e.annual_salary,
e.valid_from,
e.valid_to,
e.system_start_time,
e.system_end_time
FROM
employee_bitemporal e
JOIN daily_attendance d ON e.employee_id = d.employee_id
WHERE
d.attendance_date = CURRENT_DATE()
AND d.attendance_status = 'Leave';

In this statement, we assume that there is a daily_attendance table that stores the
attendance details for each employee on each day. We join this table with the
employee_bitemporal table using the employee_id column as the join condition. We then filter
the result to include only those rows where the attendance date is the current date and the
attendance status is 'Leave'. This will give us the employees who are on leave on the current day.
The result will include the employee ID, employee name, job position code, job position name,
annual salary, valid-time and system-time period for each row.
QUESTION 2:

Consider the entities such as owner, property, and property owner with the attributes such as
customer name, property number, property name, property location, property value.

a) Create a table to represent each entity as bitemporal tables using SQL

 A bitemporal table is a structure that records the history of an enterprise while also
capturing the sequence of changes to the record of that history.
 It allows for queries on the history as best known, the change history of stored data items, and
the interaction of valid time and transaction time.
 It is versatile and useful, but requires care in maintaining and requires longer sequences of
more complex SQL statements.

CREATE TABLE Prop_owner(


customer_number INT,
property_number INT,
VT_Begin DATE,
VT_End DATE,
TT_Start TIMESTAMP,
TT_Stop TIMESTAMP
);

CREATE TABLE Customer (


name CHAR,
VT_Begin DATE,
VT_End DATE,
TT_Start TIMESTAMP,
TT_Stop TIMESTAMP
);

CREATE TABLE Property (


property_number INT,
address CHAR,
property_type INT,
estimated_value INT,
VT_Begin DATE,
VT_End DATE,
TT_Start TIMESTAMP,
TT_Stop TIMESTAMP
);

b) Create an assertion to describe property number as valid-time and transction-time sequenced


primary key

 Assertion is a condition that must be true at all times.

 It is a statement that is executed to check the integrity of the database, ensuring that certain
constraints are met.

 If an assertion fails, an error is raised, indicating that the database is in an inconsistent state.

 Assertions are often used to enforce business rules and data constraints, and they can be
created using the CREATE ASSERTION statement.

Assertion for valid-time :

CREATE ASSERTION P_0_seq_primary_key


CHECK ( NOT EXISTS ( SELECT * FROM Prop_Owner AS P1
WHERE property_number IS NULL
OR 1 < (SELECT COUNT(customer_number) FROM Prop_Owner AS P2
WHERE P1 .property_number = P2.property_number
AND P1.VT_Begin < P2.VT_End AND P2.VT_Begin < P1.VT_End
AND P1.TT_Stop = DATE '9999-12-31 AND P2.TT_Stop = DATE '9999-12-31') ) );

Non sequenced valid-time assertion: no gaps in valid-time history

Assertion for transaction time :

CREATE ASSERTION P_0_Contiguous_History


CHECK ( NOT EXISTS ( SELECT * FROM Prop_Owner AS P,
Prop_Owner AS P2 WHERE P.VT_End < P2.VT_Begin
AND P.property_number = P2.property_number
AND P.TT_Stop = DATE '9999-12-31'
AND P2.TT_Stop = DATE '9999-12-31'
AND NOT EXISTS ( SELECT * FROM Prop_Owner AS P3
WHERE P3.property_number = P.property_number
AND (((P3.VT_Begin <= P.VT_End
AND (P.VT_End < P3.VT_End))
OR ((P3.VT_Begin < P2.VT_Begin)
AND (P2.VT_Begin <= P3.VT_End))
AND P3.TT_Stop = DATE '9999-12-31')) );

Applying the assertion at the current transaction time creates a sequenced transaction-time
constraint.
a) Illustrate the various data types used in SQL to represent the time and interval
DATE : Represents a date without time. The format is YYYY-MM- DD

TIME : Represents a time of day. The format is HH:MM:SS

TIMESTAMP : Represents a date and time. The format is YYYY-MM-DD


HH:MM:SS

TIMESTAMP (TIME ZONE) : Represents a date and time with a time zone.
The format is YYYY-MM-DD HH:MM:SS [TZ].

INTERVAL YEAR TO MONTH: Represents a period of time with a year and


month component. The format is INTERVAL 'X' YEAR[S] TO MONTH.

INTERVAL DAY TO SECOND: Represents a period of time with a day, hour,


minute, and second component.
The format is INTERVAL 'X' DAY[S] HH:MM:SS`.

 Datetimes and intervals are time-based data types that are useful for recording when a database
entry was created or modified, for logging when an event occurred, or for determining how much
time has elapsed since another datetime value was created.

 The SQL92 standard's descriptions of these time-tracking types include DATE, TIME, and
TIMESTAMP. Each datetime data type has its own means for determining the length of the value
and for knowing what information is stored, such as day, month, minutes, seconds, fraction of a
second, etc.

 DATE is the only datetime type that allows no parameters, such as precision, and the length of a
DATE value is ten characters: YYYY-MM-DD.

 Time is represented using Universal Coordinated Time (UTC), where 00:00:00 represents midnight
in Greenwich, and the server's local time zone is implied.

 Intervals can be positive or negative.

 There are two types of intervals:

year-month (YYYY-MM) and day-time (DD HH:MM:SS).

 The qualifier determines whether the interval is year-month or day-time, and the result takes on the
greatest precision, padding values with zeros if necessary.

 All INTERVAL components are integers, except seconds, which can contain fractions of a second.
These data types can be used to store and manipulate time and interval values in a database. The specific
data type you choose will depend on the level of detail and precision you require for your time and interval
values.
https://www.techrepublic.com/article/sql-basics-datetime-and-interval-data-types/

b) Modify the existing employee table with the attributes to represent the designation along with
the period.

Modifications:

Modifications refers to various changes or enhancements made to the system to improve its
performance, scalability, reliability, and security. Some common modifications in advanced DBMS
include:

 Adding new features and functionalities to the system to meet new requirements and user
needs.
 Improving the query optimization and indexing algorithms to enhance query performance and
reduce response time.
 Implementing advanced data compression techniques to reduce storage requirements and
improve data retrieval speed.
 Enhancing security features to protect against various security threats, such as hacking,
phishing, and data breaches.

Operations for tables:

 Valid-time state tables admit nine kinds of modifications: current, sequenced, and non
sequenced versions of INSERT, DELETE, and UPDATE.
 Transaction-time tables are much simpler, only current versions are relevant. Bitemporal
tables, however, only 9 kinds of modifications apply, all current in transaction time.
 Translating modifications on bitemporal tables into SQL parallels the translation on
valid-time tables, with a two-stage transformation.
 The first stage applies those transformations given for valid-time, and the second stage
applies a second set of transformations to render a final sequence of SQL statements that
respect the coupled semantics of valid time and transaction time.
 The mappings themselves are straightforward, but tedious. ALTER TABLE employee

ADD COLUMN designation varchar(255) NOT NULL,


ADD COLUMN period_start date NOT NULL,
ADD COLUMN period_end date NOT NULL;

We can also add a constraint to ensure that the "period_end" date is greater than or equal to the
"period_start" date:

ALTER TABLE employee ADD CONSTRAINT period_check


CHECK (period_end >= period_start);

You might also like