0% found this document useful (0 votes)
4 views

MySQL LAB

The document outlines several experiments demonstrating different MySQL storage engines and administrative commands. It details the creation and manipulation of MyISAM and MEMORY tables, user privileges management using SHOW GRANTS, and administrative tasks using the mysqladmin command-line tool. Each experiment includes SQL commands and expected outputs to illustrate the functionality and operations performed.

Uploaded by

Francis Martin
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
4 views

MySQL LAB

The document outlines several experiments demonstrating different MySQL storage engines and administrative commands. It details the creation and manipulation of MyISAM and MEMORY tables, user privileges management using SHOW GRANTS, and administrative tasks using the mysqladmin command-line tool. Each experiment includes SQL commands and expected outputs to illustrate the functionality and operations performed.

Uploaded by

Francis Martin
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 12

Experiment No: 01 MySQL MERGE storage engine

Date:

Aim:
To demonstrate the functionality of the MySQL MERGE storage engine by creating and
manipulating tables that combine data from multiple MyISAM tables.
Procedures and Commands:
1. Create a new MyISAM table called t1:
sql
CREATE TABLE t1(
id INT AUTO_INCREMENT PRIMARY KEY,
name VARCHAR(50) NOT NULL
) ENGINE=MyISAM;
2. Create another MyISAM table called t2 with identical columns and data types:
sql
CREATE TABLE t2(
id INT AUTO_INCREMENT PRIMARY KEY,
name VARCHAR(50) NOT NULL
) ENGINE=MyISAM;
3. Insert rows into the t1 and t2 tables:
sql
INSERT INTO t1(name) VALUES('John');
INSERT INTO t1(name) VALUES('Jane');
INSERT INTO t2(name) VALUES('Bob');
INSERT INTO t2(name) VALUES('Alice');
4. Create a MERGE table that includes the MyISAM tables t1 and t2:
sql
CREATE TABLE t(
id INT AUTO_INCREMENT PRIMARY KEY,
name VARCHAR(50) NOT NULL
) ENGINE=MERGE UNION=(t1,t2) INSERT_METHOD=LAST;
5. Query data from the MERGE table:
sql
SELECT id, name FROM t;
6. Insert a new row into the MERGE table:
sql
INSERT INTO t(name) VALUES("Peter");
a. Note: The storage engine inserts the row into the t2 table because
INSERT_METHOD is set to LAST.
7. Query data from the t2 table to verify the insertion:
sql
SELECT * FROM t2;
8. Delete a row from the MERGE table with id of 2:
sql
DELETE FROM t WHERE id = 2;
a. Initially, this deletes from t1.
9. Query data from t1 to check the deletion:
sql
SELECT * FROM t1;
10. Delete another row with id 2 from t2:
sql
DELETE FROM t WHERE id = 2;
11. Query data from t2 to confirm the deletion:
sql
SELECT * FROM t2;
Output:

After querying the MERGE table (Step 5): After inserting "Peter" into the MERGE table
(Step 7):
+----+-------+
| id | name | +----+-------+
+----+-------+ | id | name |
| 1 | John | +----+-------+
| 2 | Jane | | 1 | Bob |
| 1 | Bob | | 2 | Alice |
| 2 | Alice | | 3 | Peter |
+----+-------+ +----+-------+
4 rows in set (0.00 sec) 3 rows in set (0.00 sec)

After deleting id 2 from t1 (Step 8): After second deletion of id 2 from t2 (Step 9):

+----+------+ +----+-------+
| id | name | | id | name |
+----+------+ +----+-------+
| 1 | John | | 1 | Bob |
+----+------+ | 3 | Peter |
1 row in set (0.00 sec) +----+-------+
2 rows in set (0.00 sec)
Experiment No: MySQL MEMORY storage engine
02
Date:

Aim:
To demonstrate the use of the MySQL MEMORY storage engine by creating a table, inserting
data into it, and querying the data, highlighting the temporary nature of this storage engine.
Procedures and Commands:
1. Creating a MEMORY table:
sql
CREATE TABLE caches (
id INT
) ENGINE = MEMORY;
o The caches table is created with one column id of type INT using the MEMORY
storage engine.
2. Insert data into a MEMORY table:
sql
INSERT INTO caches(id) VALUES (1), (2), (3);
o Three rows are inserted into the caches table.

3. Query data from the MEMORY table:


sql
SELECT id FROM caches;
o This statement retrieves all data from the caches table.
Output:

After querying the MEMORY table:

+------+
| id |
+------+
|1 |
|2 |
|3 |
+------+
3 rows in set (0.00 sec)
Experiment No: 03 Using the SHOW GRANTS statement to display the privileges granted
Date: to a user
Aim:
To illustrate how to create a database, user, table, and manage user privileges using MySQL,
specifically demonstrating the use of the SHOW GRANTS statement.
Procedures and Commands:
1. Create a new database named vehicles:
sql
CREATE DATABASE vehicles;
2. Select the vehicles database:
sql
USE vehicles;
3. Create a new table called cars in the vehicles database:
sql
CREATE TABLE cars (
id INT AUTO_INCREMENT,
make VARCHAR(100) NOT NULL,
model VARCHAR(100) NOT NULL,
PRIMARY KEY (id)
);
4. Create a new user called musk@localhost:
sql
CREATE USER musk@localhost IDENTIFIED BY 'Super1Pass!';
5. Show the default privileges granted to the user musk@localhost:
sql
SHOW GRANTS FOR musk@localhost;
o The output here would show GRANT USAGE ON *.* TO 'musk'@'localhost',
indicating no privileges are granted by default.
6. Grant all privileges on the vehicles database to the user musk@localhost:
sql
GRANT ALL ON vehicles.* TO musk@localhost;
7. Show the updated privileges granted to the user musk@localhost:
sql
SHOW GRANTS FOR musk@localhost;
Output:

After step 5 (Default privileges):

+--------------------------------------------------------------------------+
| Grants for musk@localhost |
+--------------------------------------------------------------------------+
| GRANT USAGE ON *.* TO 'musk'@'localhost' |
+--------------------------------------------------------------------------+
1 row in set (0.00 sec)

After step 7 (Updated privileges):

+--------------------------------------------------------------------------+
| Grants for musk@localhost |
+--------------------------------------------------------------------------+
| GRANT USAGE ON *.* TO 'musk'@'localhost' |
| GRANT ALL PRIVILEGES ON `vehicles`.* TO 'musk'@'localhost' |
+--------------------------------------------------------------------------+
2 rows in set (0.00 sec)
Experiment No: 04 Using the SHOW GRANTS statement to display the privileges granted
Date: for a role

Aim:
To demonstrate how to create a role, assign privileges to it, and use the SHOW GRANTS
statement to display those privileges in MySQL.

Procedures and Commands:


1. Create a new role called writer@localhost:
sql
CREATE ROLE writer@localhost;

2. Show privileges granted for the role writer@localhost:


sql
SHOW GRANTS FOR writer@localhost;
o At this point, the output would show no privileges as none have been granted yet.

3. Grant SELECT, INSERT, UPDATE, and DELETE privileges on the vehicles database to the
writer@localhost:
sql
GRANT SELECT, INSERT, UPDATE, DELETE ON vehicles.* TO writer@localhost;
4. Show privileges granted for the role writer@localhost
sql
SHOW GRANTS FOR writer@localhost;
Output:

After step 2 (Before granting any privileges):

+--------------------------------------------------------------------------+
| Grants for writer@localhost |
+--------------------------------------------------------------------------+
| GRANT USAGE ON *.* TO 'writer'@'localhost' |
+--------------------------------------------------------------------------+
1 row in set (0.00 sec)

After step 4 (After granting privileges):

+--------------------------------------------------------------------------+
| Grants for writer@localhost |
+--------------------------------------------------------------------------+
| GRANT USAGE ON *.* TO 'writer'@'localhost' |
| GRANT SELECT, INSERT, UPDATE, DELETE ON `vehicles`.* TO 'writer'@'localhost' |
+--------------------------------------------------------------------------+
2 rows in set (0.00 sec)
Experiment No: The mysqladmin common command
05
Date:
Aim:
To demonstrate various administrative tasks using the mysqladmin command-line tool in
MySQL.
Procedures and Commands:
1. Checking MySQL Server Status:
sql:
mysqladmin status
Output:

Uptime: 83279 Threads: 2 Questions: 3162 Slow queries: 0 Opens: 396 Flush tables: 3
Open tables: 265 Queries per second avg: 0.037

o This command provides essential server performance metrics.

2. Creating a New Database:


sql:
mysqladmin create sample
o Equivalent to CREATE DATABASE sample in SQL.

3. Deleting a Database:
sql:
mysqladmin drop sample
o Before deletion, it prompts:

Dropping the database is potentially a very bad thing to do. Any data stored in the database will
be destroyed. Do you really want to drop the 'sample' database [y/N]
o Confirm with 'Y' to proceed or 'N' to cancel.

4. Displaying MySQL Server Version:


sql:
mysqladmin version
5. Changing the Password for a User:
sql:
mysqladmin -u root -p password
o Requires the current password for the root user, then prompts for the new
password twice.
6. Flushing MySQL Server Privileges:
sql:
mysqladmin flush-privileges
o Reloads the grant tables to apply changes to privileges.

7. Reloading MySQL Configuration:


sql:
mysqladmin reload
o Reloads the server configuration without restarting the server.

8. Monitoring MySQL Process List:


sql:
mysqladmin processlist
o Sample Output:

o +----+-----------------+-----------------+----+---------+-------+------------------------
+------------------+
o | Id | User | Host | db | Command | Time | State | Info |

o +----+-----------------+-----------------+----+---------+-------+------------------------
+------------------+
o | 5 | event_scheduler | localhost | | Daemon | 84165 | Waiting on empty queue |
|
o | 41 | root | localhost:61467 | | Query | 0 | init | show processlist |

+----+-----------------+-----------------+----+---------+-------+------------------------+------------------+
9. Shutting down the MySQL Server:
sql:
mysqladmin shutdown
o Gracefully shuts down the MySQL server.

You might also like