Uploaded by aimen.riyadh

dbase lab

advertisement
IT344 - Database Management Systems
Lab 02: Database Indexes Implementation (MySQL)
Instructors
1. Dr. Abdul Wahab Muzaffar
2. Dr. Dinesh Malavuru
3. Dr. Saima Lashari
4. Dr. Urooj Jaleel
Objectives
1. Create Database Indexes
2. Show Indexes
3. Drop the Indexes
References
Instructions
IT344
1. https://www.mysqltutorial.org/mysql-index/
1. Read the lab instructions.
2. Provide question answers and screenshots in the supplied answer sheet.
3. After finishing the lab, upload your saved answer sheet to Lab 2 folder on
Blackboard shared by you Lab instructor.
Page 1 of 7
IT344 - Database Management Systems
Part 1: Create Indexes in Database
1. Open MYSQL Workbench.
2. Click on Database in the top Menu and then click on connect to Database.
3. Enter the root password in the window.
IT344
Page 2 of 7
IT344 - Database Management Systems
4. In the Query window, write the following query:
SELECT employeeNumber, lastName, firstName
FROM employees
WHERE jobTitle = 'Sales Rep';
Here is the output:
We have 17 rows indicating that 17 employees whose job title is the Sales Rep.
IT344
Page 3 of 7
IT344 - Database Management Systems
5. To see how MySQL internally performed this query, you add the EXPLAIN clause at the beginning of
the SELECT statement as follows:
EXPLAIN SELECT employeeNumber, lastName, firstName
FROM employees
WHERE jobTitle = 'Sales Rep';
Lab sheet 1.1: Copy this screenshot showing the output of the above query from your
computer and paste it in answer sheet.
The output is:
As you can see, MySQL had to scan the whole table which consists of 23 rows to find the employees with
the Sales Rep job title.
6. Now, let’s create an index for the jobTitle column by using the CREATE INDEX statement:
CREATE INDEX jobTitle ON employees(jobTitle);
And execute the above statement again:
EXPLAIN SELECT employeeNumber, lastName, firstName
FROM employees
WHERE jobTitle = 'Sales Rep';
Lab sheet 1.2: Copy this screenshot showing the output of the above query from your
computer and paste it in answer sheet.
The output is:
As you can see, MySQL just had to locate 17 rows from the jobTitle index as indicated in the key column
without scanning the whole table.
IT344
Page 4 of 7
IT344 - Database Management Systems
Part 2: Show Indexes
1. To show the indexes of a table, you use the SHOW INDEXES statement:
SHOW INDEXES FROM employees;
Lab sheet 2.1: Copy this screenshot showing the output of the above query from your
computer and paste it in answer sheet.
Here is the output:
IT344
Page 5 of 7
IT344 - Database Management Systems
Part 3: Drop Indexes
1. Create a new table for the demonstration:
CREATE TABLE leads(
lead_id INT AUTO_INCREMENT,
first_name VARCHAR (100) NOT NULL,
last_name VARCHAR (100) NOT NULL,
email VARCHAR(255) NOT NULL,
information_source VARCHAR(255),
INDEX name(first_name,last_name),
UNIQUE email(email),
PRIMARY KEY(lead_id)
);
2. To show the indexes of a table, you use the SHOW INDEXES statement:
SHOW INDEXES FROM employees;
Lab sheet 3.1: Copy this screenshot showing the output of the above query from your
computer and paste it in answer sheet.
Here is the output:
3. The following statement removes the name index from the leads table:
DROP INDEX name ON leads;
4. The following statement drops the email index from the leads table with a specific algorithm and lock:
DROP INDEX email ON leads
ALGORITHM = INPLACE
LOCK = DEFAULT;
IT344
Page 6 of 7
IT344 - Database Management Systems
5. MySQL DROP PRIMARY KEY index
To drop the primary key whose index name is PRIMARY, use the following statement:
DROP INDEX `PRIMARY` ON table_name;
6. To show the indexes of a table, you use the SHOW INDEXES statement:
SHOW INDEXES FROM employees;
Lab sheet 3.2: Copy this screenshot showing the output of the above query from your
computer and paste it in answer sheet.
IT344
Page 7 of 7
Download