SQL Statements for Olympic Venue and Event Management

Answered using GPT-4o mini by AI Answer Generator

Question:

Question 3: Further SQL (15 marks) You have been given the following specifications of a simple database for keeping track of venues and events at the Olympics (note that primary keys are shown underlined, foreign keys in bold).

You should run your SQL to demonstrate that it works correctly.

VENUE (VenueName, Location, Capacity)

EVENT (EventName, ScheduledStart, VenueName)

Based on the table specifications provided, answer the following questions. Each question is worth 3 marks.

  1. Give the SQL to create the VENUE table. Choose appropriate data types. None of the attributes should be allowed to be null. Include the primary key constraint.
  2. Give the SQL to create the EVENT table. Use appropriate data types and include the primary key and foreign key constraints. Referential integrity should be set such that a venue may not be deleted from the database if there is an event recorded in it.
  3. Give the SQL to add the Maracana Stadium to the VENUE table. The stadium is located in Avenida Maracana and has a capacity of 78,838.
  4. Give the SQL to add an attribute Sport to EVENT. (Possible values include Athletics, Swimming, Tennis, etc).
  5. Give the SQL to record the fact that the Maracana Stadium now has capacity 80,000.
Answer:

Here are the SQL statements to fulfill the requirements specified in your questions:

1. SQL to create the VENUE table

CREATE TABLE VENUE (
    VenueName VARCHAR(100) NOT NULL,
    Location VARCHAR(100) NOT NULL,
    Capacity INT NOT NULL,
    PRIMARY KEY (VenueName)
);

2. SQL to create the EVENT table

CREATE TABLE EVENT (
    EventName VARCHAR(100) NOT NULL,
    ScheduledStart DATETIME NOT NULL,
    VenueName VARCHAR(100) NOT NULL,
    PRIMARY KEY (EventName, ScheduledStart),
    FOREIGN KEY (VenueName) REFERENCES VENUE(VenueName) ON DELETE RESTRICT
);

3. SQL to add the Maracana Stadium to the VENUE table

INSERT INTO VENUE (VenueName, Location, Capacity) 
VALUES ('Maracana Stadium', 'Avenida Maracana', 78838);

4. SQL to add an attribute Sport to EVENT

ALTER TABLE EVENT 
ADD Sport VARCHAR(50) NOT NULL;

5. SQL to record the fact that the Maracana Stadium now has capacity 80,000

UPDATE VENUE 
SET Capacity = 80000 
WHERE VenueName = 'Maracana Stadium';

Explanation of Each SQL Statement:

  1. Creating the VENUE table: The VENUE table is created with three attributes: VenueName, Location, and Capacity. All attributes are set to NOT NULL, and VenueName is defined as the primary key.

  2. Creating the EVENT table: The EVENT table is created with three attributes: EventName, ScheduledStart, and VenueName. The primary key is a composite key consisting of EventName and ScheduledStart. The foreign key constraint ensures that a venue cannot be deleted if there are events associated with it.

  3. Inserting Maracana Stadium: This SQL statement inserts a new record into the VENUE table for the Maracana Stadium with its location and capacity.

  4. Adding the Sport attribute: This statement alters the EVENT table to add a new column Sport, which is set to NOT NULL.

  5. Updating the capacity of Maracana Stadium: This SQL statement updates the capacity of the Maracana Stadium to 80,000 in the VENUE table.

You can run these SQL statements in a database management system that supports SQL to demonstrate that they work correctly.