Changes between Version 1 and Version 2 of RelationalDesign


Ignore:
Timestamp:
06/15/26 21:58:38 (6 days ago)
Author:
223091
Comment:

--

Legend:

Unmodified
Added
Removed
Modified
  • RelationalDesign

    v1 v2  
    1 RelationalDesign
     1```wiki
     2= Relational Design =
     3
     4This page documents Phase P2: Logical and Physical Design and Database Creation. The entity-relationship model from Phase P1 is transformed into a relational database schema and implemented in PostgreSQL in the assigned project database. The official schema used for the project is named ''project''.
     5
     6== Relational schema ==
     7
     8The following relational schema is derived from the ER model in Phase P1. Primary keys are marked with '''PK''', foreign keys are marked with '''FK''', and unique keys are explicitly noted.
     9
     10'''BUILDINGS'''(
     11 building_id '''PK''',
     12 name,
     13 address,
     14 '''UNIQUE'''(name, address)
     15)
     16
     17'''ROOMS'''(
     18 room_id '''PK''',
     19 building_id '''FK''' → BUILDINGS(building_id),
     20 room_code '''UNIQUE''',
     21 capacity,
     22 type
     23)
     24
     25'''EQUIPMENT'''(
     26 equipment_id '''PK''',
     27 name '''UNIQUE'''
     28)
     29
     30'''ROOM_EQUIPMENT'''(
     31 room_id '''PK, FK''' → ROOMS(room_id),
     32 equipment_id '''PK, FK''' → EQUIPMENT(equipment_id),
     33 quantity
     34)
     35
     36'''USERS'''(
     37 user_id '''PK''',
     38 username '''UNIQUE''',
     39 email '''UNIQUE''',
     40 full_name,
     41 role
     42)
     43
     44'''RESERVATIONS'''(
     45 reservation_id '''PK''',
     46 room_id '''FK''' → ROOMS(room_id),
     47 user_id '''FK''' → USERS(user_id),
     48 reservation_date,
     49 start_time,
     50 end_time,
     51 status,
     52 '''UNIQUE'''(room_id, reservation_date, start_time, end_time)
     53)
     54
     55'''APPROVALS'''(
     56 approval_id '''PK''',
     57 reservation_id '''FK, UNIQUE''' → RESERVATIONS(reservation_id),
     58 approver_id '''FK''' → USERS(user_id),
     59 decision,
     60 decision_time,
     61 note
     62)
     63
     64== Transformation notes ==
     65
     66The ER model was transformed into a relational model using partial transformation. Each strong entity set from the ER diagram is transformed into a separate relation. The entities Buildings, Rooms, Equipment, Users, Reservations, and Approvals are represented as separate tables.
     67
     68The many-to-many relationship between Rooms and Equipment is represented through the associative relation ROOM_EQUIPMENT. This relation has a composite primary key consisting of room_id and equipment_id. It also contains the additional attribute quantity, which stores how many pieces of a specific equipment type are available in a specific room.
     69
     70The one-to-many relationships are implemented using foreign keys:
     71 * each room references exactly one building;
     72 * each reservation references exactly one room;
     73 * each reservation references exactly one user;
     74 * each approval references exactly one reservation;
     75 * each approval references exactly one authorized user.
     76
     77The optional one-to-one relationship between Reservations and Approvals is implemented by allowing reservations to exist without an approval record, while each approval must reference exactly one reservation. The UNIQUE constraint on approvals.reservation_id ensures that one reservation can have at most one approval record.
     78
     79== Constraints ==
     80
     81The database schema includes the following constraints:
     82
     83 * Primary keys for all tables.
     84 * Foreign keys for all relationships derived from the ER model.
     85 * Composite primary key on ROOM_EQUIPMENT(room_id, equipment_id).
     86 * UNIQUE constraint on BUILDINGS(name, address).
     87 * UNIQUE constraint on ROOMS(room_code).
     88 * UNIQUE constraint on EQUIPMENT(name).
     89 * UNIQUE constraints on USERS(username) and USERS(email).
     90 * UNIQUE constraint on RESERVATIONS(room_id, reservation_date, start_time, end_time).
     91 * UNIQUE constraint on APPROVALS(reservation_id).
     92 * CHECK constraint that room capacity must be greater than 0.
     93 * CHECK constraint that room equipment quantity must be greater than 0.
     94 * CHECK constraint that reservation end_time must be greater than start_time.
     95 * CHECK constraints for controlled values of room types, user roles, reservation statuses, and approval decisions.
     96
     97== DDL script for creating the database schema and objects ==
     98
     99The DDL script recreates the project schema and all database objects. It first drops the existing schema if it already exists, then creates the schema named ''project''. After that, it creates all tables and defines all primary keys, foreign keys, unique constraints, and check constraints.
     100
     101The script is designed to work both in an empty database and in a database where the project schema and tables already exist, because it drops the existing project schema and recreates it.
     102
     103Attached file: ''schema_creation.sql''
     104
     105== DML script for filling tables with data ==
     106
     107The DML script fills all created tables with realistic sample data. The sample data includes buildings, rooms, equipment, equipment quantities per room, users, reservations, and approval decisions. The data is logically related and simulates realistic room reservation situations.
     108
     109The script can be executed multiple times because it first truncates the tables, restarts identity values, and then reloads the sample data.
     110
     111Attached file: ''data_load.sql''
     112
     113== Relational diagram ==
     114
     115The relational schema diagram is generated in DBeaver from the PostgreSQL schema named ''project''. The diagram uses crow-foot notation and visually represents the tables, primary keys, foreign keys, and relationships created by the DDL script.
     116
     117[[Image(relational_schema.jpg, width=100%)]]
     118
     119Attached file: ''relational_schema.jpg''