Database Management Systems NIT Srinagar
Database Management Systems NIT Srinagar
The learning objectives of this lab are to : Create table structures using MySQL data types Apply SQL constraints to MySQL tables Create a simple index
In this section you will learn how to create a small database called Theme Park from the ERD shown in Figure (Already given as supplement). 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 (Already given as supplement) 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 specific rules that govern the conversion.
In order to build tables in MySQL you will need to specify the data type for each column. (Already given as supplement).
Type CHAR[Length] VARCHAR(Length) TINYTEXT TEXT MEDIUMTEXT LONGTEXT TINYINT[Length] SMALLINT[Length] MEDIUMINT[Length]
INT[Length] BIGINT[Length]
Size Length bytes String length + 1 bytes String length + 1 bytes String length + 2 bytes String length + 3 bytes String length + 4 bytes 1 byte 2 bytes 3 bytes
4 bytes 8 bytes
Description A fixed-length field from 0 to 255 characters long. A fixed-length field from 0 to 255 characters long. A string with a maximum length of 255 characters A string with a maximum length of 65,535 characters. A string with a maximum length of 16,777,215 characters. A string with a maximum length of 4,294,967,295 characters. Range of -128 to 127 or 0 to 255 unsigned. Range of -32,768 to 32,767 or 0 to 65,535 unsigned. Range of -8,388,608 to 8,388,607 or 0 to 16,777,215 unsigned
Range of -2,147,483,648 to 2,147,483,647 or 0 to 4,294,967,295 unsigned Range of -9,223,372,036,854,775,808 to 9,223,372,036,854,775,807 or 0 to 18,446,744,073,709,551,615 unsigned A small number with a floating decimal point. A large number with a floating decimal point. A DOUBLE stored as a string, allowing for a fixed decimal point. In the format YYYY-MM-DD In the format YYYY-MM-DD HH:MM:SS. In the format YYYYMMDDHHMMSS; acceptable range ends in the year 2037. In the format of HH:MM:SS. Short for enumeration, that is, each column can have one
FLOAT DOUBLE[Length, Decimals] DECIMAL[Length, Decimals] DATE DATETIME TIMESTAMP TIME ENUM
4 bytes 8 bytes Length +1 bytes or Length + 2 bytes 3 bytes 8 bytes 4 bytes 3 bytes 1 or 2 bytes
Use the following SQL commands to create the table structures for the Theme Park database. Enter each one separately to ensure that you have no errors. Successful table creation will prompt MySQL to say Query OK. It is useful to store each correct table structure in a script file, in case the entire database needs to be recreated again at a later date. You can use a simple text editor such as notepad in order to do this. Save the file as themepark.sql
The NOT NULL specifications for the attributes ensure that a data entry will be made. When it is crucial to have the data available, the NOT NULL specification will not allow the end user to leave the attribute empty (with no data entry at all).. 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 specification. Those specifications enforce the entity integrity requirements. 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. The end user may, of course, enter a value other than the default value. 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 such as the system date like you can do in an ORACLE DBMS.
Enter the following SQL command to create the THEMEPARK table: CREATE TABLE THEMEPARK ( PARK_CODE VARCHAR(10) PRIMARY KEY, PARK_NAME VARCHAR(35) NOT NULL, PARK_CITY VARCHAR(50) NOT NULL, PARK_COUNTRY CHAR(2) NOT NULL);
Notice that when you create the THEMEPARK table structure you set the stage for the enforcement of entity integrity rules by using: PARK_CODE VARCHAR(10) PRIMARY KEY, As you create this structure, also notice that the NOT NULL constraint is used to ensure that the columns PARK_NAME, PARK_CITY and PARK_COUNTRY does not accept nulls. Remember to store this CREATE TABLE structure in your themepark.sql script.
CREATE TABLE EMPLOYEE ( EMP_NUM NUMERIC(4) 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, EMP_AREA_CODE VARCHAR(4) NOT NULL, EMP_PHONE VARCHAR(12) NOT NULL, PARK_CODE VARCHAR(10), INDEX (PARK_CODE), CONSTRAINT FK_EMP_PARK FOREIGN KEY(PARK_CODE) REFERENCES THEMEPARK(PARK_CODE));
As you look at the CREATE TABLE sequence, note that referential integrity has been enforced by specifying a constraint called FKP_EMP_PARK. In order to use foreign key constraints in MySQL, notice that the PARK_CODE column is first indexed. This foreign key constraint definition ensures that you cannot delete a Theme Park from the THEMEPARK table if at least one employee row references that Theme Park and that you cannot have an invalid entry in the foreign key column. Remember to store this CREATE TABLE structure in your themepark.sql script.
Creating the TICKET TABLE Creating the ATTRACTION TABLE Creating the HOURS TABLE Creating the SALES TABLE Creating the SALESLINE TABLE
The command DESCRIBE is used to display the structure of an individual table. To see the structure of the EMPLOYEE table you would enter the command: DESCRIBE EMPLOYEE
Use the SHOW TABLES command as shown in Figure, to list all tables that have been created within the THEMEPARK database
The DROP TABLE command permanently deletes a table (and thus its data) from the database schema. When you write a script file to create a database schema, it is useful to add DROP TABLE commands at the start of the file.
DROP DROP DROP DROP DROP DROP DROP TABLE TABLE TABLE TABLE TABLE TABLE TABLE SALES_LINE; SALES; HOURS; ATTRACTION; TICKET; EMPLOYEE; THEMEPARK;