Paper For Above instruction
Identification of Foreign Keys and Integrity Rules
In relational database design, foreign keys are used to establish and enforce referential integrity between tables. Within the described schema, the foreign keys are as follows: the 'hotelNo' in the Room table references the 'hotelNo' in the Hotel table, ensuring that every room is associated with an existing hotel; similarly, in the Booking table, 'hotelNo' references the Hotel table, 'guestNo' references the Guest table, and 'roomNo' references the Room table; though not explicitly stated, typically, 'roomNo' in Booking should reference 'roomNo' in the Room table, combined with 'hotelNo' to uniquely identify the room.
Referential integrity ensures that references between tables remain consistent. For instance, a booking cannot be made for a non-existent room, guest, or hotel. If an attempt is made to delete a hotel or guest that has related records, the system must prevent the deletion or cascade the changes based on defined rules. Also, entity integrity stipulates that primary keys are unique and not null, ensuring each record can be precisely identified.
Sample Tables Observing Integrity Rules
**Hotel Table:** | hotelNo | hotelName | city | |---------|---------------|------------|
| H001 | Grand Plaza | New York |
| H002 | Sea View Inn | Miami |
**Room Table:**
| roomNo | hotelNo | type | price |
| R101 | H001 | Single | 120 |
| R102 | H001 | Double | 200 |
| R201 | H002 | Suite | 350 |
**Guest Table:**
| guestNo | guestName | guestAddress | |---------|-------------|---------------------|
| G001 | John Smith | 123 Maple Street |
| G002 | Alice Johnson | 456 Oak Avenue |
**Booking Table:**
| hotelNo | guestNo | dateFrom | dateTo | roomNo | |---------|---------|----------|----------|---------|
| H001 | G001 | 2024-05-01 | 2024-05-05 | R101 |
| H002 | G002 | 2024-06-15 | 2024-06-20 | R201 |
These examples maintain referential integrity as all foreign key values exist in their respective primary key tables.
General Constraints for the Schema
Beyond primary and foreign keys, several constraints should be enforced:
- **Unique constraints** on fields such as hotelNo, guestNo, roomNo, ensuring each entity is uniquely identifiable.
- **Not Null constraints** on all key fields to prevent null entries.
- **Check constraints** on data values, for example, ensuring prices are non-negative, dates are valid, and room types conform to predefined categories.
- **Range constraints** on date fields (e.g., dateTo should be after dateFrom).
- **Capacity constraints** (if applicable), limiting the number of bookings per room.
- **Default values** for certain fields, such as defaulting booking status to 'confirmed'.
Analysis of RDBMS Support
Modern RDBMSs such as MySQL, PostgreSQL, Oracle, and SQL Server provide extensive support:
- **Primary Keys:** Explicitly supported with PRIMARY KEY clause; enforced automatically.
- **Alternate Keys:** Unique constraints can be defined on fields like hotelName or guestAddress to enforce alternate key constraints.
- **Foreign Keys:** Supported and enforce referential integrity with ON DELETE and ON UPDATE actions.
- **Relational Integrity:** RDBMSs enforce entity integrity via primary keys and referential integrity via foreign keys.
- **Views:** Support created views, which can be updatable or read-only based on their complexity and the underlying SELECT statement.
Implementation in an RDBMS
Using PostgreSQL for implementation:
```sql
-- Create Hotel table
CREATE TABLE Hotel (
hotelNo VARCHAR(10) PRIMARY KEY, hotelName VARCHAR(100) NOT NULL, city VARCHAR(50) NOT NULL );
-- Create Guest table
CREATE TABLE Guest ( guestNo VARCHAR(10) PRIMARY KEY, guestName VARCHAR(100) NOT NULL, guestAddress VARCHAR(255)
);
-- Create Room table
CREATE TABLE Room ( roomNo VARCHAR(10), hotelNo VARCHAR(10), type VARCHAR(50), price NUMERIC(8, 2),
PRIMARY KEY (roomNo, hotelNo),
FOREIGN KEY (hotelNo) REFERENCES Hotel(hotelNo) ON DELETE CASCADE
);
-- Create Booking table
CREATE TABLE Booking ( hotelNo VARCHAR(10), guestNo VARCHAR(10), dateFrom DATE, dateTo DATE, roomNo VARCHAR(10),
PRIMARY KEY (hotelNo, guestNo, dateFrom),
FOREIGN KEY (hotelNo) REFERENCES Hotel(hotelNo) ON DELETE CASCADE,
FOREIGN KEY (guestNo) REFERENCES Guest(guestNo) ON DELETE CASCADE, FOREIGN KEY (roomNo, hotelNo) REFERENCES Room(roomNo, hotelNo) ON DELETE CASCADE );
**User Views:**
-- Updatable view of hotel details
CREATE VIEW HotelDetails AS
SELECT hotelNo, hotelName, city FROM Hotel WITH CHECK OPTION;
-- Updatable view of guest bookings
CREATE VIEW GuestBookings AS
SELECT g.guestNo, g.guestName, b.hotelNo, b.roomNo, b.dateFrom, b.dateTo
FROM Guest g JOIN Booking b ON g.guestNo = b.guestNo
WITH CHECK OPTION;
-- Read-only view of hotel names and locations
CREATE VIEW HotelLocations AS SELECT hotelName, city FROM Hotel;
-- Read-only view of all bookings with guest and hotel info
CREATE VIEW AllBookings AS
SELECT g.guestName, h.hotelName, b.roomNo, b.dateFrom, b.dateTo FROM Booking b
JOIN Guest g ON b.guestNo = g.guestNo
Hotel h ON b.hotelNo = h.hotelNo;
Elmasri, R., & Navathe, S. B. (2015). Fundamentals of Database Systems (7th ed.). Pearson.