| 3 | | == Overview |
| 4 | | |
| 5 | | In Phase P2, the conceptual ER model from Phase P1 was transformed into a logical and physical relational database design. |
| 6 | | The goal of this phase is to ensure data integrity, correct relational structure, and implementation of business logic rules |
| 7 | | through appropriate constraints at the database level. |
| 8 | | |
| 9 | | The database schema was implemented in PostgreSQL and verified using DBeaver. |
| 10 | | |
| 11 | | == Relational Design |
| 12 | | |
| 13 | | The ER model from Phase P1 was transformed into a relational schema using standard ER-to-Relational mapping rules. |
| 14 | | All entities were mapped into relational tables with primary keys, while relationships were implemented using foreign keys |
| 15 | | and associative (booking) tables for N:M relationships. |
| 16 | | |
| 17 | | Data integrity and business rules are enforced using: |
| 18 | | * PRIMARY KEY and FOREIGN KEY constraints |
| 19 | | * UNIQUE constraints |
| 20 | | * CHECK constraints (business logic validation) |
| 21 | | * Referential actions (ON UPDATE / ON DELETE) |
| 22 | | |
| 23 | | A 1:1 relationship between Wedding and Church is implemented using a UNIQUE foreign key in the Church table. |
| 24 | | |
| 25 | | == Relational Schema |
| 26 | | |
| 27 | | User(user_id PK, first_name, last_name, email UNIQUE, phone_number, gender, birthday) |
| 28 | | Wedding(wedding_id PK, date, budget, notes, user_id FK) |
| 29 | | Church(church_id PK, name, location, contact, wedding_id FK UNIQUE) |
| 30 | | Priest(priest_id PK, name, contact, church_id FK) |
| 31 | | |
| 32 | | Venue_Type(type_id PK, type_name UNIQUE) |
| 33 | | Venue(venue_id PK, name, location, city, address, capacity, menu, phone_number, price_per_guest, type_id FK) |
| 34 | | Venue_Booking(booking_id PK, date, start_time, end_time, status, price, venue_id FK, wedding_id FK) |
| 35 | | |
| 36 | | Photographer(photographer_id PK, name, email UNIQUE, phone_number, price_per_hour) |
| 37 | | Photographer_Booking(booking_id PK, date, start_time, end_time, status, photographer_id FK, wedding_id FK) |
| 38 | | |
| 39 | | Band(band_id PK, band_name, genre, equipment, phone_number, price_per_hour) |
| 40 | | Band_Booking(booking_id PK, date, start_time, end_time, status, band_id FK, wedding_id FK) |
| 41 | | |
| 42 | | Registrar(registrar_id PK, name, contact, location, working_hours) |
| 43 | | Registrar_Booking(booking_id PK, date, start_time, end_time, status, registrar_id FK) |
| 44 | | |
| 45 | | Event(event_id PK, event_type, date, start_time, end_time, status, wedding_id FK) |
| 46 | | Guest(guest_id PK, first_name, last_name, email, wedding_id FK) |
| 47 | | Event_RSVP(response_id PK, status, response_date, guest_id FK, event_id FK) |
| 48 | | Attendance(attendance_id PK, status, table_number, role, guest_id FK, event_id FK) |
| 49 | | |
| 50 | | == Data Integrity and Business Logic Constraints |
| 51 | | |
| 52 | | Data integrity is enforced directly at the database level using constraints defined in the DDL script: |
| 53 | | |
| 54 | | * Referential integrity is enforced through FOREIGN KEY constraints between related tables |
| 55 | | * Uniqueness constraints ensure correct cardinalities (e.g. one Church per Wedding) |
| 56 | | * CHECK constraints enforce business rules such as: |
| 57 | | - end_time must be greater than start_time for events and bookings |
| 58 | | - numeric values (prices, capacity) must be positive |
| 59 | | - table_number must be greater than zero if provided |
| 60 | | * Cascading actions ensure consistency when related records are updated or deleted |
| 61 | | |
| 62 | | These constraints ensure that invalid or inconsistent data cannot be inserted into the database. |
| | 3 | == Purpose and Scope of Phase P2 |
| | 4 | |
| | 5 | The objective of Phase P2 is to formally transform the conceptual ER model defined in Phase P1 |
| | 6 | into a logical and physical relational database design. |
| | 7 | |
| | 8 | This transformation is required because an ER model is descriptive and conceptual, |
| | 9 | while a relational database requires a precise formal structure. |
| | 10 | Therefore, every conceptual element identified in Phase P1 must be translated |
| | 11 | into relational constructs that preserve its meaning and constraints. |
| | 12 | |
| | 13 | All decisions in this phase are strictly derived from: |
| | 14 | 1) the ER model structure, |
| | 15 | 2) the cardinalities defined in Phase P1, |
| | 16 | 3) the real-world semantics of the wedding planning domain, |
| | 17 | and not from arbitrary or intuitive assumptions. |
| | 18 | |
| | 19 | --- |
| | 20 | |
| | 21 | == Basis for the ER-to-Relational Transformation |
| | 22 | |
| | 23 | The transformation process follows established ER-to-relational mapping rules. |
| | 24 | These rules are necessary because relational databases do not support ER constructs directly. |
| | 25 | |
| | 26 | The following principles were applied, each based on a specific limitation or requirement |
| | 27 | of the relational model: |
| | 28 | |
| | 29 | * Principle 1: Strong entities must be mapped to separate tables. |
| | 30 | This follows from the definition of a strong entity, which has an independent existence |
| | 31 | and must therefore be stored independently in the database. |
| | 32 | |
| | 33 | * Principle 2: Each entity must have a primary key. |
| | 34 | This requirement follows from entity integrity rules in relational theory, |
| | 35 | which state that each tuple must be uniquely identifiable. |
| | 36 | |
| | 37 | * Principle 3: Relationships must be mapped according to their cardinality. |
| | 38 | Since relational databases represent relationships through references, |
| | 39 | the placement of foreign keys is determined by the cardinality of the relationship. |
| | 40 | |
| | 41 | --- |
| | 42 | |
| | 43 | == Mapping of Entities |
| | 44 | |
| | 45 | Each entity identified in the ER model was mapped directly to a relational table. |
| | 46 | This decision is justified because each entity represents a real-world object |
| | 47 | with its own attributes and independent lifecycle. |
| | 48 | |
| | 49 | For example: |
| | 50 | The entity User represents a real system user and therefore requires its own table. |
| | 51 | Its identifier (user_id) is mapped as the primary key to ensure uniqueness. |
| | 52 | |
| | 53 | This reasoning applies equally to Wedding, Venue, Photographer, Band, Registrar, Event, Guest, and other entities. |
| | 54 | |
| | 55 | --- |
| | 56 | |
| | 57 | == Mapping of Relationships Based on Cardinality |
| | 58 | |
| | 59 | === One-to-Many (1:N) Relationships |
| | 60 | |
| | 61 | In a one-to-many relationship, one instance of an entity can be associated |
| | 62 | with multiple instances of another entity. |
| | 63 | |
| | 64 | According to relational mapping rules, such relationships are implemented |
| | 65 | by placing a foreign key in the table on the many-side of the relationship. |
| | 66 | This is necessary because the many-side depends on the existence of the one-side. |
| | 67 | |
| | 68 | Example: |
| | 69 | A Wedding can have multiple Events, but each Event belongs to exactly one Wedding. |
| | 70 | Therefore, wedding_id is stored as a foreign key in the Event table. |
| | 71 | This directly enforces the cardinality defined in the ER model. |
| | 72 | |
| | 73 | --- |
| | 74 | |
| | 75 | === Many-to-Many (N:M) Relationships |
| | 76 | |
| | 77 | Relational databases cannot represent N:M relationships directly. |
| | 78 | If such a relationship were implemented without an intermediate table, |
| | 79 | it would violate first normal form. |
| | 80 | |
| | 81 | Therefore, every N:M relationship in the ER model was transformed |
| | 82 | into an associative table containing foreign keys referencing the participating entities. |
| | 83 | |
| | 84 | Example: |
| | 85 | A Wedding can involve multiple Bands, and a Band can perform at multiple Weddings. |
| | 86 | This relationship was therefore transformed into the Band_Booking table, |
| | 87 | which references both Wedding and Band. |
| | 88 | The presence of additional attributes (date, time, status) further confirms |
| | 89 | that the relationship itself has properties and must be modeled as a separate entity. |
| | 90 | |
| | 91 | --- |
| | 92 | |
| | 93 | === One-to-One (1:1) Relationships |
| | 94 | |
| | 95 | One-to-one relationships require special handling because both entities |
| | 96 | have equal cardinality. |
| | 97 | |
| | 98 | In such cases, placing a UNIQUE foreign key in one of the participating tables |
| | 99 | is sufficient to preserve the relationship without redundancy. |
| | 100 | |
| | 101 | Example: |
| | 102 | The ER model specifies a 1:1 relationship between Wedding and Church. |
| | 103 | This reflects the real-world rule that a wedding can be held in only one church, |
| | 104 | and a church instance in this context is associated with only one wedding. |
| | 105 | |
| | 106 | This relationship is implemented by placing a UNIQUE wedding_id foreign key |
| | 107 | in the Church table. |
| | 108 | The UNIQUE constraint enforces the 1:1 cardinality explicitly at the database level. |
| | 109 | |
| | 110 | --- |
| | 111 | |
| | 112 | == Derivation of the Relational Schema |
| | 113 | |
| | 114 | Based on the transformation rules described above, |
| | 115 | the following relational schema was derived. |
| | 116 | |
| | 117 | Each relation corresponds either to: |
| | 118 | - a strong entity, or |
| | 119 | - an associative entity derived from an N:M relationship. |
| | 120 | |
| | 121 | (User, Wedding, Church, Priest, Venue_Type, Venue, Venue_Booking, |
| | 122 | Photographer, Photographer_Booking, Band, Band_Booking, |
| | 123 | Registrar, Registrar_Booking, Event, Guest, Event_RSVP, Attendance) |
| | 124 | |
| | 125 | No table exists without a direct conceptual origin in the ER model. |
| | 126 | |
| | 127 | --- |
| | 128 | |
| | 129 | == Data Integrity as a Consequence of the Model |
| | 130 | |
| | 131 | Data integrity constraints were not added arbitrarily, |
| | 132 | but derived directly from the structure and semantics of the model. |
| | 133 | |
| | 134 | === Entity Integrity |
| | 135 | |
| | 136 | Primary keys ensure that each entity instance is uniquely identifiable. |
| | 137 | Without primary keys, entities could not be referenced reliably, |
| | 138 | which would contradict the definition of an entity in the ER model. |
| | 139 | |
| | 140 | --- |
| | 141 | |
| | 142 | === Referential Integrity |
| | 143 | |
| | 144 | Foreign key constraints ensure that relationships between entities remain valid. |
| | 145 | A record that references another entity cannot exist unless that entity exists. |
| | 146 | |
| | 147 | For example: |
| | 148 | A booking without a corresponding wedding would have no real-world meaning. |
| | 149 | Therefore, foreign keys are mandatory to prevent invalid references. |
| | 150 | |
| | 151 | Cascading actions are used to maintain consistency when parent records are updated or removed. |
| | 152 | This reflects the dependency relationships already present in the ER model. |
| | 153 | |
| | 154 | --- |
| | 155 | |
| | 156 | === Business Logic Constraints |
| | 157 | |
| | 158 | Certain rules cannot be captured through keys alone. |
| | 159 | These rules are derived from real-world constraints and are enforced using CHECK and UNIQUE constraints. |
| | 160 | |
| | 161 | Examples: |
| | 162 | |
| | 163 | * Time constraints: |
| | 164 | An event or booking cannot end before it starts. |
| | 165 | This rule is enforced using CHECK (end_time > start_time), |
| | 166 | which directly reflects temporal logic in the real world. |
| | 167 | |
| | 168 | * Domain constraints: |
| | 169 | Attributes such as price and capacity must be positive. |
| | 170 | Negative values would contradict the meaning of these attributes. |
| | 171 | |
| | 172 | * Cardinality constraints: |
| | 173 | UNIQUE constraints enforce restrictions such as one church per wedding. |
| | 174 | This is a direct implementation of ER cardinality constraints. |
| | 175 | |
| | 176 | These rules are enforced at the database level to ensure correctness |
| | 177 | independently of application logic. |
| | 178 | |
| | 179 | --- |