You are on page 1of 30

PRACTICAL 1

MySQL Workbench Installation: A Step-by-Step


What is MySQL?
MySQL is a publicly accessible Relational Database Management System (RDBMS) that
uses Structured Query language (SQL) to interact with databases. MySQL stores data in the
form of tables that can be modified using Structured Query Language. Its adaptability with
different computing systems like Windows, Linux, macOS, and Ubuntu has made it an easy-
going RDBMS software option.

What is MySQL Workbench?


MySQL Workbench is a unified software used to add functionality and ease to SQL
development work. MySQL Workbench provides data modeling, SQL development, and
various administration tools for configuration. It also offers a graphical interface to work with
the databases in a structured way.
How to Install MySQL Workbench?

Moving on, you will look at how to install MySQL Workbench on Windows. The installation
process is similar to other operating systems.

1. Open the MySQL website on a browser. Click on the following link: MySQL Downloads.

2. Select the Downloads option.

3. Select MySQL Installer for Windows.

4. Choose the desired installer and click on download.


5. After the download, open the installer.

6. It will ask for permission; when it does, click Yes. The installer will then open. Now, it will
ask to choose the setup type. Here, select Custom.

7. Click on Next. With this, you will install MySQL server, MySQL Workbench, and MySQL
shell.
8. Open MySQL Servers, select the server you want to install, and move it to
the Products/Features to be installed window section. Now, expand Applications, choose
MySQL Workbench and MySQL shell. Move both of them to ‘Products/Features to be
installed’.

9. Click on the Next button. Now, click on the Execute button to download and install the
MySQL server, MySQL Workbench, and the MySQL shell.

10. Once the product is ready to configure, click on Next. Under Type and Networking, go
with the default settings and select Next.
11. For authentication, use the recommended strong password encryption.

12. Set your MySQL Root password and click on next.

13. Go for the default windows service settings and under apply configuration, click on
execute. Once the configuration is complete, click on finish.
14. Complete the installation. This will now launch the MySQL Workbench and the MySQL
Shell.

Once MySQL Workbench is installed, select the Local instance and enter the password.
Now, you can use the MySQL query tab to write your SQL queries.

Manually back up your MySQL for IU Sitehosting schemas


Create a backup using MySQL Workbench
Note:

Store this backup file in a manner consistent with the data classification appropriate to your
site's content. Sites which store sensitive internal or critical data must be stored in secure
locations.

If you're using versions of Workbench before 6.3.8, including on IUanyWare, you will have an
unhandled exception when trying to export on the new system. You will be unable to export until
you update Workbench.

1. Connect to your MySQL database.

2. Click Server on the main tool bar.

3. Select Data Export.

4. Select the tables you want to back up.

5. Under Export Options, select where you want your dump saved. By default, it will save to
your Documents folder in a subfolder titled dumps.
6. Click Start Export.

Note:

You may get a message about a mismatch between your mysqldump.exe version and the
MySQL Server version. You can update your local MySQL version or continue.

7. You now have a backup version of your site. Store this content securely in a manner consistent
with your MySQL schema content's data classification.
PRACTICAL 2
Create a simple database for Social Networking Platform with the
following entities
(i) Create tables with fields of appropriate data types:

CREATE TABLE users (


id INT AUTO_INCREMENT PRIMARY KEY,
username VARCHAR(60),
email VARCHAR(255),
address VARCHAR(150),
dob TIMESTAMP,
is_active TINYINT,
registered_on TIMESTAMP,
last_logged_on TIMESTAMP
);

CREATE TABLE friends (


id INT AUTO_INCREMENT PRIMARY KEY,
user_id INT UNSIGNED NOT NULL,
friend_name VARCHAR(60)
);

CREATE TABLE users_profiles (


id INT PRIMARY KEY,
user_id INT,
location VARCHAR(150),
FOREIGN KEY (user_id) REFERENCES users(id)
);
(ii) Verify the table created using the DESCRIBE command:
DESCRIBE users;
DESCRIBE friends;
DESCRIBE users_profiles;

(iii) Insert 10 users and some friendship data in the


friends table:
INSERT INTO users (username, email, address, dob, is_active,
registered_on, last_logged_on)
VALUES
('John Doe', 'johndoe@example.com', '123 Main St, Anytown USA', '2000-
01-01', 1, NOW(), NOW()),
('Jane Smith', 'janesmith@example.com', '456 Elm St, Anytown USA',
'1995-05-05', 1, NOW(), NOW()),
('Bob Johnson', 'bobjohnson@example.com', '789 Oak St, Anytown USA',
'1980-12-25', 0, NOW(), NOW()),
('Sarah Lee', 'sarahlee@example.com', '246 Maple St, Anytown USA',
'1998-03-15', 1, NOW(), NOW()),
('David Kim', 'davidkim@example.com', '135 Pine St, Anytown USA',
'1985-09-10', 0, NOW(), NOW()),
('Linda Chen', 'lindachen@example.com', '369 Cedar St, Anytown USA',
'1993-07-01', 1, NOW(), NOW()),
('Michael Wong', 'michaelwong@example.com', '753 Spruce St, Anytown
USA', '1990-11-20', 1, NOW(), NOW()),
('Emily Davis', 'emilydavis@example.com', '852 Birch St, Anytown USA',
'1997-02-28', 0, NOW(), NOW()),
('Tom Smith', 'tomsmith@example.com', '951 Walnut St, Anytown USA',
'1975-06-12', 1, NOW(), NOW()),
('Amy Lee', 'amylee@example.com', '258 Cherry St, Anytown USA', '1989-
08-08', 1, NOW(), NOW());

INSERT INTO friends (user_id, friend_name)


VALUES
(1, 'Jane Smith'),
(1, 'Sarah Lee'),
(2, 'John Doe'),
(2, 'Bob Johnson'),
(2, 'Linda Chen'),
(3, 'David Kim'),
(4, 'Michael Wong'),
(5, 'Emily Davis'),
(6, 'Tom Smith'),
(7, 'Amy Lee');
(iv) Add a 'gender' field of type CHAR(1). Allow NULL
values for this field:
ALTER TABLE users ADD COLUMN gender CHAR(1) NULL;

(v) Rename the friends table to users_friends:


ALTER TABLE friends RENAME TO users_friends;

(vi) Modify the dob field type to date_of_birth:


ALTER TABLE users MODIFY COLUMN dob DATE
PRACTICAL-3
Perform the following operations on database created in
Ex.no.2 using SELECTcommand.

(i) To fetch the most recent 5 registered users, we can use


the following SQL query:
SELECT * FROM usersORDER BY registered_on DESC LIMIT 5;

(ii) To fetch all the friends of a particular user (lets


say user_id = 'userx'), we can use the following SQL query:
SELECT * FROM friends WHERE user_id = 'userx';

(iii) To fetch all the users who are above 32 years old, we
can use the following SQL query:
SELECT * FROM users WHERE TIMESTAMPDIFF(YEAR, dob, CURDATE()) > 32;
(iv) To find the count of users who signed-up with a Gmail
ID, we can use the following SQL query:
SELECT COUNT(*) FROM users WHERE email LIKE '%@gmail.com';

(v) To fetch all the users who registered last month, we


can use the following SQL query:
SELECT * FROM users WHERE MONTH(registered_on) = MONTH(NOW() - INTERVAL 1
MONTH) AND YEAR(registered_on) = YEAR(NOW() - INTERVAL 1 MONTH);
(vi) To fetch all the users of 'Chennai' location, we can
use the following SQL query:
SELECT * FROM users_profiles WHERE location = 'Chennai';

(vii) To find the count of actively monthly and weekly


users, we can use the following SQL queries:
SELECT COUNT(*) FROM users WHERE last_logged_on >= (NOW() - INTERVAL 15
DAY);

(viii) To find the number of users who have not mentioned


their gender, we can use the following SQL query:
SELECT COUNT(*) FROM users WHERE gender IS NULL;
PRACTICAL-4
(i) Create a database 'Polytechnic_College'.
CREATE DATABASE Polytechnic_College;

(ii) Create 2 users namely ‘Staff’ and ‘student’.


CREATE USER 'Staff'@'localhost' IDENTIFIED BY 'password';
CREATE USER 'student'@'localhost' IDENTIFIED BY 'password';

- Grant all privileges to the user 'Staff ‘and grant only ‘create’privilege to
‘student’user and verify the same .
Grant all privileges to the user 'Staff’

Grant only 'create' privilege to 'student' user:


- Revoke all privileges to the 2 users and verify the same.

b) Implement the following transaction control statements.


i) Commit

ii) Roll back

iii) Save point


Practical-5
Create a table ‘author’ with the following structure author_id author_name
address mobile book_title pages published_on

Insert 4 books published by 3 authors each. (12records)


Fetch all the rows and observe how the data duplicated.

Apply 1st and 2nd normal forms to fixit.


Practical-6
Create table, "mail" with the following fields DATE TIME, # when message was
sent srcuser VARCHAR(8), # sender (source user and host) srchost
VARCHAR(20), dstuser VARCHAR(8), # recipient (destination user and host)
dsthost VARCHAR(20), size BIGINT, # message size in bytes
create database practical_6;
CREATE TABLE mail (
`DATE` DATE,
`TIME` TIME,
srcuser VARCHAR(8),
srchost VARCHAR(20),
dstuser VARCHAR(8),
dsthost VARCHAR(20),
size BIGINT
);
INSERT INTO mail (`DATE`, `TIME`, srcuser, srchost, dstuser, dsthost, size)
VALUES
('2023-05-25', '10:30:00', 'user1', 'sender1', 'user2', 'receiver1', 4000000),
('2023-05-26', '11:45:00', 'user3', 'sender2', 'user2', 'receiver1', 3000000),
('2023-05-27', '09:15:00', 'user2', 'sender1', 'user1', 'receiver2', 28000000),
('2023-05-28', '02:20:00', 'user1', 'sender1', 'user3', 'receiver2', 15000000),
('2023-05-28', '02:20:00', 'user1', 'sender1', 'user3', 'receiver2', 15000000);

 Sort the mail with the largest mail being first.


 List the mails that is over 25MB

 Remove the duplicate rows from result set. 71

 Execute a 'SELECT' query and store its result in a user defined variable.
Use another ‘SELECT' to display the value of the variable
Practical-7
Create two tables with the following structure.
a) Requests table request_id - UNSIGNED, INT, AUTO INCREMENT,
PRIMARY KEY from_id - INTto_id – INT
create database prcatical_7;
CREATE TABLE requests (
request_id INT UNSIGNED AUTO_INCREMENT PRIMARY KEY,
from_id INT,
to_id INT
);
INSERT INTO requests (from_id, to_id)
VALUES (1, 2),
(3, 4),
(5, 6);

b) requests_log table request_id - FOREIGN KEY refers to request_id field of


requests table request_status - enum ("PENDING", "APPROVED",
"REJECTED")
CREATE TABLE requests_log (
request_id INT UNSIGNED,
request_status ENUM("PENDING", "APPROVED", "REJECTED"),
FOREIGN KEY (request_id) REFERENCES requests(request_id)
);
INSERT INTO requests_log (request_id, request_status)
VALUES (1, 'PENDING'),
(1, 'APPROVED'),
(2, 'PENDING'),
(3, 'APPROVED'),
(3, 'REJECTED'),
(3, 'PENDING');

c) Create a view combining both tables to display all the requests along with
their most recent status for the requests.
CREATE VIEW requests_view AS
SELECT requests.request_id,from_id, to_id, request_status
FROM requests,requests_log
where requests.request_id = requests_log.request_id;
SELECT * FROM requests_view;
Practical-8
Create a library Table with proper fields.
CREATE TABLE library (
book_id INT UNSIGNED AUTO_INCREMENT PRIMARY KEY,
title VARCHAR(255) NOT NULL,
author VARCHAR(255) NOT NULL,
publication_year INT,
category VARCHAR(50)
);

Inserting values in library table.


INSERT INTO library (title, author, publication_year, category)
VALUES
('Book 1', 'Author 1', 2005, 'Fiction'),
('Book 2', 'Author 2', 2010, 'Non-fiction'),
('Book 3', 'Author 3', 2018, 'Mystery');

Create another table called Library1 and insert rows from Library table.
Practical-9
Create a table to store the details of a customer in a Bank.
create database practical_9;
CREATE TABLE customer (
customer_id INT UNSIGNED AUTO_INCREMENT PRIMARY KEY,
name VARCHAR(255) NOT NULL,
account_number VARCHAR(20) NOT NULL,
credit_limit DECIMAL(10, 2) NOT NULL,
balance DECIMAL(10, 2) DEFAULT 0.00
);

Inserting Values
INSERT INTO customer (name, account_number, credit_limit, balance)
VALUES
('John Doe', '1234567890', 100000.00, 50000.00),
('Jane Smith', '9876543210', 50000.00, 25000.00),
('Mike Johnson', '5678901234', 200000.00, 150000.00);
select * from customer;

Do some transactions like withdrawal, deposit.

UPDATE customer SET balance = balance - 10000.00 WHERE customer_id = 1;


UPDATE customer SET balance = balance + 5000.00 WHERE customer_id = 2;

Find the Balance amount (Credit Limit). Based on customer’s credit limit, write
a program using IF or CASE flow control statements to find the customer levels
namely SILVER, GOLD or PLATINUM.
If the Credit limit is
• greater than 50K, then the customer level is PLATINUM
• less than 50K and greater than 10K, then the customer level is GOLD
• less than 10K, then the customer level is SILVER
SELECT
customer_id,
name,
account_number,
credit_limit,
balance,
IF(credit_limit > 50000, 'PLATINUM', IF(credit_limit > 10000, 'GOLD', 'SILVER')) AS
customer_level
FROM customer;
Practical-10
Create two tables with the following structure.
a) users - tablename user_id - UNSIGNED, INT, AUTO INCREMENT,
PRIMARYKEY username -VARCHAR (60) password - VARCHAR (128)
email- VARCHAR (255)
create database practical_10;
CREATE TABLE users (
user_id INT UNSIGNED AUTO_INCREMENT PRIMARY KEY,
username VARCHAR(60),
password VARCHAR(128),
email VARCHAR(255)
);

b) users_profiles user_id - FOREIGN KEY refers to user_id field of user table


first_name - VARCHAR(60) last_name - VARCHAR(60) mobile -
VARCHAR(15)
CREATE TABLE users_profiles (
profile_id INT UNSIGNED AUTO_INCREMENT PRIMARY KEY,
user_id INT UNSIGNED,
first_name VARCHAR(60),
last_name VARCHAR(60),
mobile VARCHAR(15),
FOREIGN KEY (user_id) REFERENCES users(user_id)
);
select * from users_profiles;
 SELECT all the users along with their profile details. (Hint: Use
INNERJOIN)
SELECT u.user_id, u.username, u.email, p.first_name, p.last_name, p.mobile
FROM users u
INNER JOIN users_profiles p
ON u.user_id = p.user_id;

 SELECT the users who do not have profiles (Hint: USE LEFT JOIN
and exclude the rows generated with NULL values from joining table)
SELECT u.user_id, u.username, u.email
FROM users u
LEFT JOIN users_profiles p
ON u.user_id = p.user_id
WHERE p.user_id
IS NULL;
Practical-11
Create an employee database and create a stored procedure that accepts
employee _Id as input and returns complete details of employee as output.
create database practical_11;
CREATE TABLE employees (
employee_id INT PRIMARY KEY,
first_name VARCHAR(50),
last_name VARCHAR(50),
email VARCHAR(100),
phone_number VARCHAR(20),
hire_date DATE,
job_title VARCHAR(100),
department VARCHAR(100)
);

INSERT INTO employees (employee_id, first_name, last_name, email, phone_number, hire_date,


job_title, department)
VALUES
(1, 'John', 'Doe', 'john.doe@example.com', '1234567890', '2021-01-01', 'Manager', 'Sales'),
(2, 'Jane', 'Smith', 'jane.smith@example.com', '9876543210', '2021-02-01', 'Engineer', 'IT'),
(3, 'Mark', 'Johnson', 'mark.johnson@example.com', '5555555555', '2021-03-01', 'Analyst',
'Finance');
DELIMITER //

CREATE PROCEDURE GetEmployeeDetails(IN emp_id INT)


BEGIN
SELECT *
FROM employees
WHERE employee_id = emp_id;
END

//DELIMITER ;
CALL GetEmployeeDetails(2);

You might also like