Professional Documents
Culture Documents
In this section you will learn how to create a small database called Theme Park from the ERD.
This will involve you creating the table structures in MySQL using the
CREATE TABLE command. In order to do this, appropriate data types will need to be selected
from the data dictionary for each table structure along with any constraints that have been
imposed (e.g. primary and foreign key). Converting any ER model to a set of tables in a
database requires following specific rules that govern the conversion. The application of those
rules require an understanding of the effects of updates and deletions on the tables in the
database.
THEMEPARK ATTRACTION
belongs to
PARK_CODE (PK) ATTRACT_NO (PK)
PARK_NAME PARK_CODE* (FK)
PARK_CITY ATTRACT_NAME
PARK_COUNTRY ATTRACT_AGE
ATTRACT_CAPACITY
makes
issues
generates
SALES TICKET
TRANSACTION_NO(PK) TICKET_NO (PK)
PAR_CODE * (FK) TICKET_PRICE HOURS
SALE_DATE TICKET_TYPE
EMP_NUM* (PK/FK1)
PARK_CODE* (FK)
ATTRACT_NO* (PK/FK2)
HOURS_PER_ATTRACT
HOUR_RATE
contains DATE_WORKED
contains
employed for
SALES_LINE
TRANSACTION_NO* (PK/FK1)
LINE_NO(PK) EMPLOYEE
TICKET_NO* (FK2)
LINE_QTY EMP_NUM(PK)
LINE_PRICE EMP_TITLE
EMP_LNAME
EMP_FNAME
EMP_DOB
EMP_HIRE_DATE
EMP_AREACODE
EMP_PHONE
PARK_CODE* (FK)
ERD
1
Table below Shows the Data Dictionary for the Theme Park database which will be used to
create each table structure.
2
3
Data Types in MySQL
Data Type Example Description
CHAR(size) fieldName CHAR(10) Stores up to 255 characters. If the
EX: COURSECODE CHAR(5) content is smaller than the field size, the
String / content will have trailing spaces
Alphanumneric appended
VARCHAR(size) fieldName VARCHAR(100) Stores up to 255 characters, and a
EX: ADDRESS VARCHAR(50) minimum of 4 characters. No trailing
spaces are appended to the end of this
datatype.
INT fieldName INT Round number
EX: QUANTITY INT
Numeric FLOAT fieldName FLOAT Used for single precision floating point
numbers.
DOUBLE fieldName DOUBLE Used for double precision floating point
numbers
DATE fieldName DATE Stores dates in the format YYYY-MM-DD.
TIMESTAMP(size) fieldName DATETIME Stores dates and times in the format
YYYY-MM-DD HH:MM:SS.
Date/Time TIME fieldName TIME Stores times in the format HH:MM:SS.
YEAR(size) fieldName YEAR(4) Stores the year as either a 2 digit number,
or a 4 digit number, depending on the
size provided.
• The NOT NULL specifications for the attributes ensure that a data entry will be made.
The NOT NULL specification will not allow the end user to leave the attribute empty
(with no data entry at all).
4
• The UNIQUE specification creates a unique index in the respective attribute. Use it to
avoid duplicated values in a column.
• The primary key attributes contain both a NOT NULL and a UNIQUE. If the NOT NULL
and UNIQUE specifications are not supported, use PRIMARY KEY without the
specifications.
• The entire table definition is enclosed in parentheses. A comma is used to separate each
table element (attributes, primary key, and foreign key) definition.
• The DEFAULT constraint is used to assign a value to an attribute when a new row is
added to a table. In MYSQL the default value must be a constant; it cannot be a function
or an expression. This means, for example, that you cannot set the default for a date
column to be the value of a function
5
Create table Syntax;
6
2. Creating the EMPLOYEE TABLE
CREATE TABLE EMPLOYEE (
EMP_NUM NUMERIC(4) NOT NULL UNIQUE,
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,
EMP_AREA_CODE VARCHAR(4) NOT NULL,
EMP_PHONE VARCHAR(12) NOT NULL,
PARK_CODE VARCHAR(10),
PRIMARY KEY (EMP_NUM),
CONSTRAINT FK_EMP_PARK FOREIGN KEY(PARK_CODE) REFERENCES
THEMEPARK(PARK_CODE));
b. EMP_NUM NUMERIC(4) NOT NULL UNIQUE …….then kena set PRIMARY KEY
(EMP_NUM) di akhir statement (cara 2)
7
cara nak set foreign key:
FOREIGN KEY (ATTRIBUTE NAME) REFERENCES SOURCE TABLE(ATTRIBUTE NAME)
8
6. Creating the SALES TABLE
CREATE TABLE SALES (
TRANSACTION_NO NUMERIC PRIMARY KEY,
PARK_CODE VARCHAR(10),
SALE_DATE DATE NOT NULL,
FOREIGN KEY(PARK_CODE) REFERENCES THEMEPARK(PARK_CODE));
The DROP TABLE command permanently deletes a table (and thus its data) from the
database schema.
EX : DROP TABLE SALES_LINE;
9
Listing all tables
All changes in the table structure are made by using the ALTER TABLE command,
followed by a keyword that produces the specific change you want to make.
Three options are available: ADD, MODIFY, and DROP.
ADD enables you to add a column,
EX: ALTER TABLE < TABLE NAME>
ADD ATTRIBUTE NAME DATA TYPE;
10
MODIFY enables you to change column characteristics
ALTER TABLE< TABLE NAME>
MODIFY ATTRIBUTE NAME DATATYPE
** ATTRACT_CAPACITY
DAH
BERTUKAR KE
DECIMAL(4,0)
11
// THEMEPARK ROW
INSERT INTO THEMEPARK VALUES ('FR1001','FairyLand','PARIS','FR');
INSERT INTO THEMEPARK VALUES ('UK3452','PleasureLand','STOKE','UK');
INSERT INTO THEMEPARK VALUES ('NL1202','Efling','NOORD','NL');
INSERT INTO THEMEPARK VALUES ('SP4533','AdventurePort','BARCELONA','SP');
INSERT INTO THEMEPARK VALUES ('SW2323','Labyrinthe','LAUSANNE','SW');
INSERT INTO THEMEPARK VALUES ('UK2622','MiniLand','WINDSOR','UK');
INSERT INTO THEMEPARK VALUES ('ZA1342','GoldTown','JOHANNESBURG','ZA');
//EMPLOYEE ROW
// TICKET ROW
//ATTRACTION ROW
12
INSERT INTO ATTRACTION VALUES (10078,'Ant-Trap',23,30,'FR1001');
INSERT INTO ATTRACTION VALUES (10098,'Carnival',3,120,'FR1001');
INSERT INTO ATTRACTION VALUES (20056,'3D-Lego_Show',3,200,'UK3452');
INSERT INTO ATTRACTION VALUES (30011,'BlackHole2',12,34,'UK3452');
INSERT INTO ATTRACTION VALUES (30012,'Pirates',10,42,'UK3452');
INSERT INTO ATTRACTION VALUES (30044,'UnderSeaWord',4,80,'UK3452');
INSERT INTO ATTRACTION VALUES (98764,'GoldRush',5,80,'ZA1342');
//HOURS ROW
//SALES ROW
//SALES_LINE ROW
13
INSERT INTO SALES_LINE VALUES (12781,1,13002,2,69.98);
INSERT INTO SALES_LINE VALUES (12781,2,13001,1,14.99);
INSERT INTO SALES_LINE VALUES (12782,1,13002,2,69.98);
INSERT INTO SALES_LINE VALUES (12783,1,13003,2,41.98);
INSERT INTO SALES_LINE VALUES (12784,2,13001,1,14.99);
INSERT INTO SALES_LINE VALUES (12785,1,13001,1,14.99);
INSERT INTO SALES_LINE VALUES (12785,2,13002,1,34.99);
INSERT INTO SALES_LINE VALUES (12785,3,13002,4,139.96);
INSERT INTO SALES_LINE VALUES (34534,1,88568,4,168.40);
INSERT INTO SALES_LINE VALUES (34534,2,88567,1,22.50);
INSERT INTO SALES_LINE VALUES (34534,3,89720,2,21.98);
INSERT INTO SALES_LINE VALUES (34535,1,88568,2,84.20);
INSERT INTO SALES_LINE VALUES (34536,1,89720,2,21.98);
INSERT INTO SALES_LINE VALUES (34537,1,88568,2,84.20);
INSERT INTO SALES_LINE VALUES (34537,2,88567,1,22.50);
INSERT INTO SALES_LINE VALUES (34538,1,89720,2,21.98);
INSERT INTO SALES_LINE VALUES (34539,1,89720,2,21.98);
INSERT INTO SALES_LINE VALUES (34539,2,88568,2,84.20);
INSERT INTO SALES_LINE VALUES (34540,1,88568,4,168.40);
INSERT INTO SALES_LINE VALUES (34540,2,88567,1,22.50);
INSERT INTO SALES_LINE VALUES (34540,3,89720,2,21.98);
INSERT INTO SALES_LINE VALUES (34541,1,88568,2,84.20);
INSERT INTO SALES_LINE VALUES (67589,1,67833,2,57.34);
INSERT INTO SALES_LINE VALUES (67589,2,67832,2,37.12);
INSERT INTO SALES_LINE VALUES (67590,1,67833,2,57.34);
INSERT INTO SALES_LINE VALUES (67590,2,67832,2,37.12);
INSERT INTO SALES_LINE VALUES (67591,1,67832,1,18.56);
INSERT INTO SALES_LINE VALUES (67591,2,67855,1,12.12);
INSERT INTO SALES_LINE VALUES (67592,1,67833,4,114.68);
INSERT INTO SALES_LINE VALUES (67593,1,67833,2,57.34);
INSERT INTO SALES_LINE VALUES (67593,2,67832,2,37.12);
Lab exercise:
Run all the statement and snap in the word document. Please submit to me
14