6 Themepark Lab DDL

You might also like

Download as docx, pdf, or txt
Download as docx, pdf, or txt
You are on page 1of 16

PREREQUISITES

1. Make sure two services: 1) Apache and 2) MySQL are running

2. Open your browser and type http://localhost then press Enter. Make sure you get the
XAMPP dashboard as in the picture below. Locate the phpMyAdmin link at the top right
corner.

3. You will see this phpMyAdmin user interface as in the picture below.
4. Click on the SQL tab at the right panel, and you will see a SQL editor. You can type all
SQL commands inside the editor and click the Go button to execute the command.

ENTITY RELATIONSHIP DIAGRAM (ERD)


Study and examine ERD above and SQL CREATE command below before you start creating
the physical database and table structure.

STEP 1: CREATING PHYSICAL DATABASE


create database themeparkdb

STEP 2: CREATING PHYSICAL TABLES WITH ATTRIBUTES


* ALWAYS CREATE TABLE THAT DOES NOT CONTAIN ANY FOREIGN KEY FIRST

create table themepark


(
park_code varchar(10) primary key,
park_name varchar(35) not null,
park_city varchar(50) not null,
part_country char(2) not null
)

create table employee


(
emp_num integer primary key,
emp_title varchar(4),
emp_lname varchar(15) not null,
emp_fname varchar(15) not null,
emp_dob date not null,
emp_hire_date date not null,
emp_area_code varchar(4) not null,
emp_phone varchar(12),
park_code varchar(10),
foreign key (park_code) references themepark (park_code)
)

create table ticket


(
ticket_no integer primary key,
ticket_price decimal(4,2) default 00.00 not null,
ticket_type varchar(10),
park_code varchar(10),
foreign key (park_code) references themepark (park_code)
)

create table attraction


(
attract_no integer primary key,
attract_name varchar(35),
attract_age integer default 0 not null,
attract_capacity integer not null,
park_code varchar(10),
foreign key (park_code) references themepark (park_code)
)

* MULTIPLE PRIMARY KEY ATTRIBUTES IN A TABLE


create table hours
(
emp_num integer,
attract_no integer,
hours_per_attract integer not null,
hour_rate decimal(4,2) not null,
date_worked date not null,
primary key (emp_num, attract_no, date_worked),
foreign key (emp_num) references employee (emp_num),
foreign key (attract_no) references attraction (attract_no)
)

create table sales


(
transaction_no integer primary key,
park_code varchar(10),
sale_date date not null,
foreign key (park_code) references themepark (park_code)
)

create table sales_line


(
line_no integer not null,
transaction_no integer,
ticket_no integer not null,
line_qty integer default 0 not null,
line_price decimal(9,2) default 0.00 not null,
primary key (line_no, transaction_no),
foreign key (transaction_no) references sales (transaction_no),
foreign key (ticket_no) references ticket (ticket_no)
)

ADDING NEW ATTRIBUTE/COLUMN


Command:

alter table <table_name>


add column <attribute_name> <data_type>

E.g:

alter table employee


add column emp_email varchar(50);

REMOVING EXISTING ATTRIBUTE/COLUMN

Command:

alter table <table_name>


drop column <attribute_name>

E.g:

alter table employee


drop column emp_email

RENAME EXISTING ATTRIBUTE/DOMAIN NAME

Command:

* For MariaDB 10.5.2 version and above only


Source: https://mariadb.com/kb/en/alter-table/#rename-column

alter table <table_name>


rename column <existing_attribute_name> to <new_attribute_name>

* For MariaDB versions lower than 10.5.2 version

alter table <table_name>


change column <existing_attribute_name> <new_attribute_name>
<datatype>

E.g:

* For MariaDB 10.5.2 version and above only

alter table themepark


rename column part_country to park_country

* For MariaDB versions lower than 10.5.2 version

alter table themepark


change column part_country park_country char(2)
CHANGE DATA TYPE OR INCREASE CHARACTER SIZE
Command:

alter table <table_name>


modify column <attribute_name> <data_type>

E.g:

alter table employee


modify column emp_lname varchar(50)

ASSIGN PRIMARY KEY CONSTRAINT TO EXISTING ATTRIBUTE AFTER SUCCESSFULLY


CREATED THE TABLE [OPTIONAL]

Command:

alter table <table_name>


add primary key (<primary_key_attribute_name>)

E.g:

alter table themepark


add primary key (park_code)

ASSIGN FOREIGN KEY CONSTRAINT TO EXISTING ATTRIBUTE AFTER


SUCCESSFULLY CREATED THE TABLE [OPTIONAL]

Command:

alter table <table_name>


add foreign key (<attribute_name>) references <ref_table_name>
(<ref_pk_attribute_name>)

E.g:

alter table employee


add foreign key (park_code) reference themepark (park_code)

INSERTING RECORD INTO TABLE. PLEASE REFER TO THE DATASET GIVEN


METHOD 1: WITH ATTRIBUTE NAMES SPECIFIED

Command:

INSERT INTO <table_name> (<attribute_1>, <attribute_2>, ..,


<attribute_n>) VALUES (<value_1>, <value_2>, .., <value_n>)

E.g:

INSERT INTO themepark (PARK_CODE, PARK_NAME, PARK_CITY, PARK_COUNTRY)


VALUES
('FR1001', 'Fairy Land', 'PARIS', 'FR'),
('NL1202', 'Efling', 'NOORD', 'NL'),
('SP4533', 'Adventure Port', 'BARCELONA', 'SP'),
('SW2323', 'Labyrinthe', 'LAUSANNE', 'SW'),
('UK2622', 'Mini Land', 'WINDSOR', 'UK'),
('UK3452', 'Pleasureland', 'STOKE', 'UK'),
('ZA1342', 'Gold Town', 'JOHANNESBURG', 'ZA')

METHOD 2: WITHOUT ATTRIBUTE NAMES SPECIFIED

Command:

INSERT INTO <table_name> VALUES (<value_1>, <value_2>, .., <value_n>)

E.g:

INSERT INTO themepark VALUES ('FR1001', 'Fairy Land', 'PARIS', 'FR'),


('NL1202', 'Efling', 'NOORD', 'NL'),
('SP4533', 'Adventure Port', 'BARCELONA', 'SP'),
('SW2323', 'Labyrinthe', 'LAUSANNE', 'SW'),
('UK2622', 'Mini Land', 'WINDSOR', 'UK'),
('UK3452', 'Pleasureland', 'STOKE', 'UK'),
('ZA1342', 'Gold Town', 'JOHANNESBURG', 'ZA')

EXTRA: INSERTING 1 BY 1 RECORD INTO TABLE

INSERT INTO themepark (PARK_CODE, PARK_NAME, PARK_CITY, PARK_COUNTRY)


VALUES ('FR1001', 'Fairy Land', 'PARIS', 'FR')

INSERT INTO themepark (PARK_CODE, PARK_NAME, PARK_CITY, PARK_COUNTRY)


VALUES ('NL1202', 'Efling', 'NOORD', 'NL')

DATASET
Table: themepark

park_code park_name park_city park_country

FR1001 Fairy Land PARIS FR

NL1202 Efling NOORD NL

SP4533 Adventure Port BARCELONA SP

SW2323 Labyrinthe LAUSANNE SW

UK2622 Mini Land WINDSOR UK

UK3452 Pleasureland STOKE UK

ZA1342 Gold Town JOHANNESBURG ZA

Table: employee

emp_ emp_t emp_ln emp_fn emp_do emp_hire emp_area emp_p park_c


num itle ame ame b _date _code hone ode

Calderd 1972- 1992-03- 324- FR100


100 Ms ale Emma 06-15 15 181 9134 1

1978- 1996-04- 324- UK345


101 Ms Ricardo Marshel 03-19 25 181 4472 2

1969- 1990-12- 675- FR100


102 Mr Arshad Arif 11-14 20 7253 8993 1

1974- 1994-08- 898- UK345


103 Ms Roberts Anne 10-16 16 181 3456 2

1980- 2001-10- 504- ZA134


104 Mr Denver Enrica 11-08 20 7253 4434 2

Namow 1990- 2006-11- 890- FR100


105 Ms a Mirrelle 03-14 08 181 3243 1

1968- 1989-01- 324- ZA134


106 Mrs Smith Gemma 02-12 05 181 7845 2

Table: ticket
ticket_no ticket_price ticket_type park_code

11001 24.99 Adult SP4533

11002 14.99 Child SP4533

11003 10.99 Senior SP4533

13001 18.99 Child FR1001

13002 34.99 Adult FR1001

13003 20.99 Senior FR1001

67832 18.56 Child ZA1342

67833 28.67 Adult ZA1342

67855 12.12 Senior ZA1342

88567 22.5 Child UK3452

88568 42.1 Adult UK3452

89720 10.99 Senior UK3452

Table: attraction
Attract_no Attract_name Attract_age Attract_capacity Park_code

10034 Thunder Coaster 11 34 FR1001

10056 Spinning Teacups 4 62 FR1001

10067 Flight To Stars 11 24 FR1001

10078 Ant-Trap 23 30 FR1001

10082 NULL 10 40 ZA1342

10098 Carnival 3 120 FR1001

20056 3D-Lego Show 3 200 UK3452

30011 Black Hole 2 12 34 UK3452

30012 Pirates 10 42 UK3452

30044 Under Sea Word 4 80 UK3452

98764 Gold Rush 5 80 ZA1342

Table: hours
Emp_num Attract_no Hours_per_attract Hour_rate Date_worked

100 10034 6 6.5 2007-05-18

100 10034 6 6.5 2007-05-20

101 10034 6 6.5 2007-05-18

102 30012 3 5.99 2007-05-23

102 30044 6 5.99 2007-05-21

102 30044 3 5.99 2007-05-22

104 30011 6 7.2 2007-05-21

104 30012 6 7.2 2007-05-22

105 10078 3 8.5 2007-05-18

105 10098 3 8.5 2007-05-18

105 10098 6 8.5 2007-05-19

Table: sales
transaction_no park_code sale_date
12781 FR1001 2007-05-18

12782 FR1001 2007-05-18

12783 FR1001 2007-05-18

12784 FR1001 2007-05-18

12785 FR1001 2007-05-18

12786 FR1001 2007-05-18

34534 UK3452 2007-05-18

34535 UK3452 2007-05-18

34536 UK3452 2007-05-18

34537 UK3452 2007-05-18

34538 UK3452 2007-05-18

34539 UK3452 2007-05-18

34540 UK3452 2007-05-18

34541 UK3452 2007-05-18

67589 ZA1342 2007-05-18

67590 ZA1342 2007-05-18

67591 ZA1342 2007-05-18

67592 ZA1342 2007-05-18

67593 ZA1342 2007-05-18

Table: sales_line

transaction_no line_no ticket_no line_qty line_price

12781 1 13002 2 69.98


12781 2 13001 1 14.99

12782 1 13002 2 69.98

12783 1 13003 2 41.98

12784 2 13001 1 14.99

12785 1 13001 1 14.99

12785 2 13002 1 34.99

12785 3 13002 4 139.96

34534 1 88568 4 168.4

34534 2 88567 1 22.5

34534 3 89720 2 21.98

34535 1 88568 2 84.2

34536 1 89720 2 21.98

34537 1 88568 2 84.2

34537 2 88567 1 22.5

34538 1 89720 2 21.98

34539 1 89720 2 21.98

34539 2 88568 2 84.2

34540 1 88568 4 168.4

34540 2 88567 1 22.5

34540 3 89720 2 21.98

34541 1 88568 2 84.2

67589 1 67833 2 57.34

67589 2 67832 2 37.12

67590 1 67833 2 57.34

67590 2 67832 2 37.12


67591 1 67832 1 18.56

67591 2 67855 1 12.12

67592 1 67833 4 114.68

67593 1 67833 2 57.34

67593 2 67832 2 37.12

UPDATING EXISTING RECORD

Command:

update <table_name> SET attribute_1 = value_1, attribute_2 = value_2,


attribute_3 = value_3, …, attribute_4 = value_4 where <pk_attribute> =
<pk_value> [you can use condition(s) instead of pk_attribute]

E.g:

update employee set emp_fname = 'Khairul', emp_lname = 'Ikhwan' where


emp_num = 102

DELETING EXISTING RECORD

Command:

delete from <table_name> where <pk_attribute> = <pk_value> [you can


use condition(s) instead of pk_attribute]

E.g:

delete from employee where emp_num = 102

You might also like