Changes between Version 9 and Version 10 of P2


Ignore:
Timestamp:
01/26/26 23:30:58 (2 weeks ago)
Author:
193284
Comment:

--

Legend:

Unmodified
Added
Removed
Modified
  • P2

    v9 v10  
    11= Phase P2: Logical and Physical Design (DDL & DML)
    22
    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
     5The objective of Phase P2 is to formally transform the conceptual ER model defined in Phase P1
     6into a logical and physical relational database design.
     7
     8This transformation is required because an ER model is descriptive and conceptual,
     9while a relational database requires a precise formal structure.
     10Therefore, every conceptual element identified in Phase P1 must be translated
     11into relational constructs that preserve its meaning and constraints.
     12
     13All decisions in this phase are strictly derived from:
     141) the ER model structure,
     152) the cardinalities defined in Phase P1,
     163) the real-world semantics of the wedding planning domain,
     17and not from arbitrary or intuitive assumptions.
     18
     19---
     20
     21== Basis for the ER-to-Relational Transformation
     22
     23The transformation process follows established ER-to-relational mapping rules.
     24These rules are necessary because relational databases do not support ER constructs directly.
     25
     26The following principles were applied, each based on a specific limitation or requirement
     27of 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
     45Each entity identified in the ER model was mapped directly to a relational table.
     46This decision is justified because each entity represents a real-world object
     47with its own attributes and independent lifecycle.
     48
     49For example:
     50The entity User represents a real system user and therefore requires its own table.
     51Its identifier (user_id) is mapped as the primary key to ensure uniqueness.
     52
     53This 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
     61In a one-to-many relationship, one instance of an entity can be associated
     62with multiple instances of another entity.
     63
     64According to relational mapping rules, such relationships are implemented
     65by placing a foreign key in the table on the many-side of the relationship.
     66This is necessary because the many-side depends on the existence of the one-side.
     67
     68Example:
     69A Wedding can have multiple Events, but each Event belongs to exactly one Wedding.
     70Therefore, wedding_id is stored as a foreign key in the Event table.
     71This directly enforces the cardinality defined in the ER model.
     72
     73---
     74
     75=== Many-to-Many (N:M) Relationships
     76
     77Relational databases cannot represent N:M relationships directly.
     78If such a relationship were implemented without an intermediate table,
     79it would violate first normal form.
     80
     81Therefore, every N:M relationship in the ER model was transformed
     82into an associative table containing foreign keys referencing the participating entities.
     83
     84Example:
     85A Wedding can involve multiple Bands, and a Band can perform at multiple Weddings.
     86This relationship was therefore transformed into the Band_Booking table,
     87which references both Wedding and Band.
     88The presence of additional attributes (date, time, status) further confirms
     89that the relationship itself has properties and must be modeled as a separate entity.
     90
     91---
     92
     93=== One-to-One (1:1) Relationships
     94
     95One-to-one relationships require special handling because both entities
     96have equal cardinality.
     97
     98In such cases, placing a UNIQUE foreign key in one of the participating tables
     99is sufficient to preserve the relationship without redundancy.
     100
     101Example:
     102The ER model specifies a 1:1 relationship between Wedding and Church.
     103This reflects the real-world rule that a wedding can be held in only one church,
     104and a church instance in this context is associated with only one wedding.
     105
     106This relationship is implemented by placing a UNIQUE wedding_id foreign key
     107in the Church table.
     108The UNIQUE constraint enforces the 1:1 cardinality explicitly at the database level.
     109
     110---
     111
     112== Derivation of the Relational Schema
     113
     114Based on the transformation rules described above,
     115the following relational schema was derived.
     116
     117Each 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,
     122Photographer, Photographer_Booking, Band, Band_Booking,
     123Registrar, Registrar_Booking, Event, Guest, Event_RSVP, Attendance)
     124
     125No table exists without a direct conceptual origin in the ER model.
     126
     127---
     128
     129== Data Integrity as a Consequence of the Model
     130
     131Data integrity constraints were not added arbitrarily,
     132but derived directly from the structure and semantics of the model.
     133
     134=== Entity Integrity
     135
     136Primary keys ensure that each entity instance is uniquely identifiable.
     137Without primary keys, entities could not be referenced reliably,
     138which would contradict the definition of an entity in the ER model.
     139
     140---
     141
     142=== Referential Integrity
     143
     144Foreign key constraints ensure that relationships between entities remain valid.
     145A record that references another entity cannot exist unless that entity exists.
     146
     147For example:
     148A booking without a corresponding wedding would have no real-world meaning.
     149Therefore, foreign keys are mandatory to prevent invalid references.
     150
     151Cascading actions are used to maintain consistency when parent records are updated or removed.
     152This reflects the dependency relationships already present in the ER model.
     153
     154---
     155
     156=== Business Logic Constraints
     157
     158Certain rules cannot be captured through keys alone.
     159These rules are derived from real-world constraints and are enforced using CHECK and UNIQUE constraints.
     160
     161Examples:
     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
     176These rules are enforced at the database level to ensure correctness
     177independently of application logic.
     178
     179---
    63180
    64181== Relational Diagram
    65182
    66 The relational diagram was generated directly from the PostgreSQL schema using DBeaver.
    67 It presents all tables, primary keys, foreign keys, and relationships using crow-foot notation.
     183The relational diagram was generated directly from the implemented schema using DBeaver.
     184Because the diagram is generated from the actual database objects,
     185it serves as a formal verification of the relational model.
     186
     187The diagram visually confirms:
     188- correct primary and foreign key placement,
     189- correct cardinalities,
     190- consistency with the relational schema derived from the ER model.
    68191
    69192[[Image(db_202526z_va_prj_wedding_planner v.2 - project.png)]]
    70193
    71 == DDL Script
    72 
    73 The DDL script defines the complete database schema, including tables, constraints, and indexes.
    74 
    75 [[attachment:schema_creation.sql|DDL Script]]
    76 
    77 == DML Script
    78 
    79 The DML script inserts sample data into all tables, ensuring consistency with the defined constraints.
    80 
     194---
     195
     196== DDL and DML Implementation
     197
     198The logical and physical design is implemented using two SQL scripts:
     199
     200The DDL script defines the database structure and all integrity constraints.
     201The DML script inserts sample data that complies with these constraints.
     202
     203The successful execution of the DML script serves as additional evidence
     204that the relational design correctly enforces the intended rules.
     205
     206[[attachment:schema_creation.sql|DDL Script]] 
    81207[[attachment:data_load.sql|DML Script]]