Skip to content
This repository has been archived by the owner on Aug 21, 2024. It is now read-only.

Commit

Permalink
Submit HW1.10 and update deliverable 3
Browse files Browse the repository at this point in the history
  • Loading branch information
hanggrian committed Apr 16, 2023
1 parent 7673f2e commit 2c09e7f
Show file tree
Hide file tree
Showing 23 changed files with 959 additions and 333 deletions.
3 changes: 2 additions & 1 deletion .vscode/settings.json
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,8 @@
"markdown-pdf.styles": [
"~/Documents/GitHub/markdown-pdf-styles/styles/font-sans.css",
"~/Documents/GitHub/markdown-pdf-styles/styles/text-normal.css",
"~/Documents/GitHub/markdown-pdf-styles/styles/layout-half.css",
],
"markdown-pdf.headerTemplate": "<div style='font-size: 9px; margin-left: 1cm;'> <span>CS425 - Hendra Wijaya (A20529195)</span> </div> <div style='margin-left: auto; margin-right: 1cm;'> <img src='data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAAEQAAAAKCAYAAAANQfiuAAAACXBIWXMAAAsSAAALEgHS3X78AAABzUlEQVRIie1V0W3CMBB9VP0vE1hMYNgAvEGYoGQDmAAyAXSC0AmaDZxOUJoJokyQTtDq0HN7WBD4Sb94UhT7crbvnt9dcMcpBjLzxm4ArAFkAKYAZq6pBtrTG/sNoHRN5ZRtJi+uA/dYuabaxWu8sVsAS/q19NsHHwBzAG9ytvJ5cU21UXvFKDmfRXaJpwCQA5jQdgDgGIPE6VxTlTp3OeuhB4GsvbEjbfDGLhhIDWBHcx75LZhYwYRa+uskA/E1x69nvmckaksyCj4TEtSJxx4IGTKYubKN+RZVFN5YkKBJtA5UhSS00Zsqpcht1mr+rL8HeGM9/eb4q4Kr6IMQQeKNTdQ8JN7y/RXZodTgvbE1JS4yPtxyIAk4QpV1rWyBwEDM1hsr8ZyouY+SCbK+Kk8N6ScAUtUXEvanf0UfCpGEnlQDhbqpYeQb335BYuQmPyIFdUI3e4Xf886UzCpqqkdcJCTaICQ0umCPkfGGgxzf2TRFplOOTwjxxuYstX04S5XYVURxlXxm3tg32hI218+uvboUslbjIONRZM9wBq6pWm9sGiTP3+uYqlky0dQ1Vc0GCwaaKGUJ2atbCYniAtfm3DPkkEbKvaMTAH4AFEq7mpO1Vd8AAAAASUVORK5CYII='> </div>",
"markdown-pdf.footerTemplate": "<div style='font-size: 9px; margin-left: 1cm;'> <span>Project Deliverable 3</span> </div> <div style='font-size: 9px; margin-left: auto; margin-right: 1cm;'> <span class='pageNumber'></span> / <span class='totalPages'></span> </div>",
"markdown-pdf.footerTemplate": "<div style='font-size: 9px; margin-left: 1cm;'> <span>Homework 1.10</span> </div> <div style='font-size: 9px; margin-left: auto; margin-right: 1cm;'> <span class='pageNumber'></span> / <span class='totalPages'></span> </div>",
}
65 changes: 65 additions & 0 deletions acid.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
# [ACID](https://en.wikipedia.org/wiki/ACID)

In computer science, ACID (atomicity, consistency, isolation, durability) is a
set of properties of database transactions intended to guarantee data validity
despite errors, power failures, and other mishaps. In the context of databases,
a sequence of database operations that satisfies the ACID properties (which can
be perceived as a single logical operation on the data) is called a transaction.
For example, a transfer of funds from one bank account to another, even
involving multiple changes such as debiting one account and crediting another,
is a single transaction.

In 1983, Andreas Reuter and Theo Härder coined the acronym ACID, building on
earlier work by Jim Gray who named atomicity, consistency, and durability, but
not isolation, when characterizing the transaction concept. These four
properties are the major guarantees of the transaction paradigm, which has
influenced many aspects of development in database systems.

According to Gray and Reuter, the IBM Information Management System supported
ACID transactions as early as 1973 (although the acronym was created later).

## Characteristics

The characteristics of these four properties as defined by Reuter and Härder are
as follows:

### [Atomicity](https://en.wikipedia.org/wiki/Atomicity_(database_systems))

Transactions are often composed of multiple statements. Atomicity guarantees
that each transaction is treated as a single "unit", which either succeeds
completely or fails completely: if any of the statements constituting a
transaction fails to complete, the entire transaction fails and the database is
left unchanged. An atomic system must guarantee atomicity in each and every
situation, including power failures, errors, and crashes. A guarantee of
atomicity prevents updates to the database from occurring only partially, which
can cause greater problems than rejecting the whole series outright. As a
consequence, the transaction cannot be observed to be in progress by another
database client. At one moment in time, it has not yet happened, and at the
next, it has already occurred in whole (or nothing happened if the transaction
was canceled in progress).

### [Consistency](https://en.wikipedia.org/wiki/Consistency_(database_systems))

Consistency ensures that a transaction can only bring the database from one
consistent state to another, preserving database invariants: any data written to
the database must be valid according to all defined rules, including
constraints, cascades, triggers, and any combination thereof. This prevents
database corruption by an illegal transaction. Referential integrity guarantees
the primary key–foreign key relationship.

### [Isolation](https://en.wikipedia.org/wiki/Isolation_(database_systems))

Transactions are often executed concurrently (e.g., multiple transactions
reading and writing to a table at the same time). Isolation ensures that
concurrent execution of transactions leaves the database in the same state that
would have been obtained if the transactions were executed sequentially.
Isolation is the main goal of concurrency control; depending on the isolation
level used, the effects of an incomplete transaction might not be visible to
other transactions.

### [Durability](https://en.wikipedia.org/wiki/Durability_(database_systems))

Durability guarantees that once a transaction has been committed, it will
remain committed even in the case of a system failure (e.g., power outage or
crash). This usually means that completed transactions (or their effects) are
recorded in non-volatile memory.
188 changes: 184 additions & 4 deletions assignments/exam1.md
Original file line number Diff line number Diff line change
Expand Up @@ -16,14 +16,14 @@
> Consider the following database schema (__*RateMyProfDB*__) to answer all the
questions below.
>
> | <small>Table: Professor<br>PK: PID</small><br>PID | <br>Pname | <br>Papers | <br>Topic |
> | <small>Table: Professor<br>PK: PID</small><br>PID | <br><br>Pname | <br><br>Papers | <br><br>Topic |
> | --- | --- | --- | --- |
> | **109** | Steven | 10 | Java |
> | **110** | Francis | 50 | Databases |
> | **111** | Daniel | 40 | Java |
> | **112** | Joy | 20 | Java |
>
> | <small>Table: Rating<br>PK: SID + PID<br>FK: SID, PID</small><br>SID | <br>PID | <br>Score | <br>Attended |
> | <small>Table: Rating<br>PK: SID + PID<br>FK: SID, PID</small><br>SID | <br><br><br>PID | <br><br><br>Score | <br><br><br>Attended |
> | --- | --- | --- | --- |
> | **23** | **109** | 6 | 60 |
> | **23** | **110** | 10 | 70 |
Expand All @@ -33,7 +33,7 @@
> | **33** | **109** | 5 | 80 |
> | **33** | **112** | 1 | 4 |
>
> | <small>Table: Student<br>PK: SID</small><br>SID | <br>Sname | <br>Uni | <br>GPA |
> | <small>Table: Student<br>PK: SID</small><br>SID | <br><br>Sname | <br><br>Uni | <br><br>GPA |
> | --- | --- | --- | --- |
> | **23** | Michelle | Illinois Tech | 50.52 |
> | **25** | Tomas | UChi | 20.71 |
Expand All @@ -60,14 +60,194 @@
> Write a relational algebra statement to select all professors whose specialty
is not Java.

$$\sigma_\text{Topic = 'Java'} (\text{Professor})$$

## Relation algebra problem 1

> Write a relational-algebra expression that returns the name and GPA of
students with a GPA greater than 30 and studies at Illinois Tech.

$$\pi_\text{Sname,GPA} \sigma_\text{GPA > 30} \cap_\text{Uni = 'Illinois Tech'} (\text{Student})$$

## Relation algebra problem 1

> Write a relational-algebra expression to find all information about students
and ratings, and the students who gave a score less than five.

## SQL Problem 1
$$
\sigma_\text{Score > 5} (\text{Student}\bowtie_\text{Student.SID = Rating.SID}\text{Rating}) \\
\text{or} \\
\sigma_\text{Score > 5} \cap_\text{Student.SID = Rating.SID} (\text{Student}\times\text{Rating})
$$

## SQL problem 1

> Write the SQL commands to create the database and the relations shown in the
diagram above.

```sql
CREATE DATABASE RateMyProfDB;
USE RateMyProfDB;

CREATE TABLE Professor(
PID INT AUTO_INCREMENT,
Pname VARCHAR(25) NOT NULL,
Papers INT,
Topic VARCHAR(25),
PRIMARY KEY(PID)
);

CREATE TABLE Student1(
SID INT,
Sname VARCHAR(25) NOT NULL,
Uni VARCHAR(25) NOT NULL,
GPA DOUBLE,
PRIMARY KEY(SID)
);

CREATE TABLE Rating1(
SID INT,
PID INT,
Score INT NOT NULL,
Attended INT NOT NULL,
PRIMARY KEY(SID, PID),
FOREIGN KEY(SID) REFERENCES Student1(SID),
FOREIGN KEY(PID) REFERENCES Professor(PID),
CHECK(Score BETWEEN 0 AND 10)
);
```

## SQL problem 2

> Return the names of students attending a university other than `Illinois tech`
or `UChi`.

```sql
SELECT `Sname` FROM Student1
WHERE `Uni` NOT IN('Illinois Tech', 'UChi');
-- or
SELECT `Sname` FROM Student1
WHERE `Uni` <> 'Illinois Tech' AND `Uni` <> 'UChi';
-- or
SELECT `Sname` FROM Student1
WHERE `Sname` NOT IN(SELECT `Sname` FROM Student1
WHERE `Uni` = 'Illinois Tech' OR `Uni` = 'UChi');
```

## SQL problem 3

> Find the average score given to each professor who has been rated more than
once.

```sql
SELECT P.`PID`, P.`Pname`, AVG(`Score`)
FROM Rating1 AS R , Professor AS P
WHERE R.`PID` = P.`PID`
GROUP BY `PID`
HAVING COUNT(*) > 1;
-- or
SELECT `Pname`, AVG(`Score`) FROM Rating1
NATURAL JOIN Professor GROUP BY `PID` HAVING COUNT(*) > 1;
```

## SQL problem 4

> Get the names of professors along with each score they got and the student
who gave them that score.

```sql
SELECT P.`Pname`, R.`Score`, S`.Sname`
FROM Professor AS P, Rating1 AS R, Student1 AS S
WHERE P.`PID` = R.`PID` and S.`SID` = R.`SID`;
-- or
SELECT P.`Pname`, R.`Score`, S.`Sname`
FROM Professor AS P
JOIN Rating1 AS R ON P.`PID` = R.`PID`
JOIN Student1 AS S ON R.`SID` = S.`SID`;
-- or
SELECT `Pname`, `Score`, `Sname`
FROM Professor
NATURAL JOIN Rating1
NATURAL JOIN Student1;
```

## SQL problem 5

> Update the professor table by increasing the number of papers by one for
either `steven` or `joy`.

```sql
UPDATE Professor SET `Papers` = `Papers` + 1
WHERE `Pname` IN('Steven', 'Joy');
-- or
UPDATE Professor SET `Papers` = `Papers` + 1
WHERE `Pname` = 'Steven' OR `Pname` = 'Joy';
-- or
UPDATE Professor SET `Papers` = (
CASE
WHEN `Pname` = 'Steven' OR `Pname` = 'Joy' THEN `Papers` + 1
ELSE `Papers` + 0
END);
```

## SQL problem 6

> Which professors have ever scored less than one of prof 111's scores?
```sql
SELECT DISTINCT P.`PID`, P.`Pname` FROM Professor AS P, Rating1 AS R
WHERE P.`PID` = R.`PID`
AND `Score` < SOME(SELECT MIN(`Score`) FROM Rating1 WHERE `PID` = 111);
-- or
SELECT DISTINCT P.`PID`, P.`Pname`
FROM Professor AS P, Rating1 AS R
WHERE P.`PID` = R.`PID`
AND `Score` < SOME(SELECT `Score` FROM Rating1 where `PID` = 111);
```

## SQL problem 7

> Write an SQL statement that inserts new rows shown in the table below.
>
> | PID | Pname | Papers | Topic |
> | --- | --- | --- | --- |
> | 113 | George | 40 | Networks |
> | 114 | Ethan | 10 | Python |
> | 115 | Markus | 15 | ICT40 |
```sql
INSERT INTO Professor(`Pname`, `Papers`, `Topic`) VALUES
('George', 40, 'Networks'),
('Ethan', 10, 'Python'),
('Markus', 15, 'ICT4D');
```

## SQL problem 8

> Using the SQL command below, answer the following questions.
>
> ```sql
> SELECT PID, sum(Score)/count(*)
> FROM Rating
> WHERE Attended > 50
> GROUP BY PID
> HAVING count(*) > 1;
> ```
### Subproblem 8A
> Give the expected output/result in form of a table.
| PID | SUM(`Score`) / COUNT(*) |
| --- | --- |
| 109 | 5.5 |
### Subproblem 8B
> Provide an explanation of your output.
Considering only majority rating (i.e, `Attended > 50` percent), the query
returns the average score given to each professor (as a effect
of `GROUP BY PID`) who has been rated more than once (filtered by the `HAVING`
Clause). Thus, results in a single row showing `PID` = `109` and `Average`
rating score = `5.5`.
38 changes: 26 additions & 12 deletions assignments/hw1_1.md → assignments/hw1.md
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
# [Homework 1.1](https://github.com/hendraanggrian/IIT-CS425/blob/assets/assignments/hw1_1.docx): University DB
# [Homework 1.1](https://github.com/hendraanggrian/IIT-CS425/blob/assets/assignments/hw1.docx): University DB

## Problem 1

Expand All @@ -17,48 +17,62 @@
> Remember to include the metadata to self-describe the data stored in the
database.

### SQL commands
### SQL initialization

```sql
CREATE SCHEMA IF NOT EXISTS UniversityDB;
USE UniversityDB;

DROP TABLE IF EXISTS Registrations;
DROP TABLE IF EXISTS Schedules;
DROP TABLE IF EXISTS Classes;
DROP TABLE IF EXISTS Courses;
DROP TABLE IF EXISTS Schedules;
DROP TABLE IF EXISTS Lecturers;
DROP TABLE IF EXISTS Students;

CREATE TABLE Courses(
`id` VARCHAR(10) PRIMARY KEY,
`name` VARCHAR(50) NOT NULL
);
CREATE INDEX Courses_name ON Courses(`name`);

CREATE TABLE Lecturers(
`id` INT AUTO_INCREMENT PRIMARY KEY,
`name` VARCHAR(50) NOT NULL,
`date_join` DATE NOT NULL
);
CREATE INDEX Lecturers_name ON Lecturers(`name`);

CREATE TABLE Students(
`id` INT AUTO_INCREMENT PRIMARY KEY,
`name` VARCHAR(50) NOT NULL,
`date_join` DATE NOT NULL,
`date_graduate` DATE
);
CREATE INDEX Students_name ON Students(`name`);

CREATE TABLE Classes(
`id` INT AUTO_INCREMENT PRIMARY KEY,
`course_id` VARCHAR(10) NOT NULL,
`lecturer_id` INT NOT NULL,
`date_initial` DATE NOT NULL,
`date_final` DATE NOT NULL,
CONSTRAINT Classes_course_id FOREIGN KEY(`course_id`)
REFERENCES Courses(`id`) ON DELETE RESTRICT ON UPDATE RESTRICT,
CONSTRAINT Classes_lecturer_id FOREIGN KEY(`lecturer_id`)
REFERENCES Lecturers(`id`) ON DELETE RESTRICT ON UPDATE RESTRICT
CONSTRAINT Classes_course_id FOREIGN KEY(`course_id`) REFERENCES Courses(`id`)
ON DELETE RESTRICT ON UPDATE RESTRICT,
CONSTRAINT Classes_lecturer_id FOREIGN KEY(`lecturer_id`) REFERENCES Lecturers(`id`)
ON DELETE RESTRICT ON UPDATE RESTRICT
);

CREATE TABLE Registrations(
`class_id` INT NOT NULL,
`student_id` INT NOT NULL,
`grade` CHAR(1),
PRIMARY KEY(`class_id`, `student_id`),
CONSTRAINT Registrations_class_id FOREIGN KEY(`class_id`)
REFERENCES Classes(`id`) ON DELETE RESTRICT ON UPDATE RESTRICT,
CONSTRAINT Registrations_student_id FOREIGN KEY(`student_id`)
REFERENCES Students(`id`) ON DELETE RESTRICT ON UPDATE RESTRICT
CONSTRAINT Registrations_class_id FOREIGN KEY(`class_id`) REFERENCES Classes(`id`)
ON DELETE RESTRICT ON UPDATE RESTRICT,
CONSTRAINT Registrations_student_id FOREIGN KEY(`student_id`) REFERENCES Students(`id`)
ON DELETE RESTRICT ON UPDATE RESTRICT
);

CREATE TABLE Schedules(
Expand All @@ -67,8 +81,8 @@ CREATE TABLE Schedules(
`day` INT NOT NULL,
`time_start` TIME NOT NULL,
`time_end` TIME NOT NULL,
CONSTRAINT fk_Schedules_class_id__id FOREIGN KEY(`class_id`)
REFERENCES Classes(`id`) ON DELETE RESTRICT ON UPDATE RESTRICT
CONSTRAINT fk_Schedules_class_id__id FOREIGN KEY(`class_id`) REFERENCES Classes(`id`)
ON DELETE RESTRICT ON UPDATE RESTRICT
);
```

Expand Down
Loading

0 comments on commit 2c09e7f

Please sign in to comment.