Md.
Abdul Awal Sajib | Roll-686397
Database Management System | Code-28561
Lab Report
National Polytechnic College
MySQL Operation
October 30, 2024
Introduction: MySQL is a popular open-source relational database management
system (RDBMS) used for storing and organizing data. It's known for its
reliability, performance, and ease of use, making it a preferred choice for many
developers and organizations.
This lab report will explore the fundamental concepts and operations of
MySQL, providing a hands-on experience with its capabilities.
Getting Start: After installing MySQL on our system and setting up the path in
our environment variables, we can check the version in the terminal using the
following command: mysql --version
How to connect to mysql server:
DDL(Data Manipulation Language) Commands: Create, Alter, Drop, Truncate.
1. CREATE TABLE: Creates a new table within a database.
CREATE DATABASE my_database;
Use my_database;
CREATE TABLE customers (
id INT PRIMARY KEY AUTO_INCREMENT,
name VARCHAR(50),
email VARCHAR(100)
);
1 Abdul Awal Sajib
2. ALTER TABLE: Modifies the structure of a database or the table.
ALTER TABLE customers ADD COLUMN phone VARCHAR(20);
ALTER TABLE customers MODIFY COLUMN name VARCHAR(100);
ALTER TABLE customers DROP COLUMN email;
3. DROP DATABASE: Deletes a database or the table.
DROP DATABASE my_database;
DROP TABLE customers;
4. TRUNCATE TABLE: This is a DDL (Data Definition Language) command in
MySQL used to remove all rows from a table while preserving its structure.
TRUNCATE TABLE customers;
SELECT * FROM customers;
Data Manipulation Language (DML) Commands: Select, Insert Update & Delete.
5. Insert: Inserts new rows into a table.
2 Abdul Awal Sajib
INSERT INTO customers (name, email) VALUES ('John Doe',
'
[email protected]');
6. Update: Modifies existing rows in a table.
UPDATE customers SET email = '
[email protected]' WHERE id = 1;
7. Delete: Modifies existing rows in a table.
Before,
After,
8. DELETE FROM customers WHERE id = 1;
More Queries Or Operations of Mysql:
9. AGGREGATION AND GROUPING: AVG(expr), COUNT(expr), MAX(expr),
MIN(expr), SUM(expr)
Example:
3 Abdul Awal Sajib
10. GROUP BY Clause:
SELECT species, COUNT(id) FROM animal GROUP BY species;
11. An example of a multiple-table query:
SELECT city.name, country.name
FROM city
INNER JOIN country
ON city.country_id = country.id;
12. TEXT OPERATORS: To fetch the city names that start with a 'P' or end with
an 's':
SELECT name
FROM city
WHERE name LIKE 'P%' or name LIKE '%s';
13. NUMERIC FUNCTIONS: Mod(),Round() etc.
To get the remainder of a division: SELECT MOD(13, 2); -- result: 1
14. EXTRACTING PARTS OF DATES: SELECT YEAR(CAST('2021-12-31' AS DATE));
4 Abdul Awal Sajib
15. CONCATENATION:
SELECT CONCAT('Hi ', 'there!'); -- result: Hi there!
To convert all letters to lowercase:
SELECT LOWER('LEARNSQL.COM'); -- result: learnsql.com
16. To check What Time is it?
SELECT CURRENT_TIMESTAMP;
Select Now();
17. BETWEEN Opeartor:
SELECT * FROM animals
WHERE weight BETWEEN 10 AND 30;
18. To convert all letters to uppercase:
SELECT UPPER('LearnSQL.com'); -- result: LEARNSQL.COM
19. UPDATING DATA To update the data in a table: use the UPDATE command:
UPDATE animal
SET
species = 'Duck',
name = 'Quack'
WHERE id = 2;
20. Creating a College Database & Performing crud operation on that
database:
Create Database college;
Use college;
5 Abdul Awal Sajib
21. creating necessary tables for college databases example:
CREATE TABLE student( CREATE TABLE books ( CREATE TABLE branch (
stu_id INT PRIMARY KEY, book_id INT PRIMARY KEY , brn_id INT PRIMARY KEY,
stu_name VARCHAR(30), book_name VARCHAR(30), brn_name VARCHAR(20),
stu_ph_no INT , athr_name brn_ctg VARCHAR(20)
stu_email VARCHAR(30) , VARCHAR(30),books );
stu_dob DATE book_brn_id INT,
); book_price INT
CREATE TABLE issues ( );
isu_id INT UNIQUE,
isu_date DATE ,
isu_book_id INT ,
isu_stu_id INT
);
22. USE college;
SHOW TABLES;
23. Altering College Table Structure example:
ALTER TABLE issues
ADD FOREIGN KEY(isu_book_id)
REFERENCES books(book_id)
ON DELETE CASCADE;
24. insert values in college tables example :
INSERT INTO user VALUES(101,'Jaden Clark',4836792,'
[email protected]');
INSERT INTO user VALUES(102,'Olive Yew',6385647,'
[email protected]');
INSERT INTO roles VALUES(10101,'DB_Admin',4);
INSERT INTO roles VALUES(10102,'Teacher',3);
INSERT INTO permission VALUES(151,'Total Control',NUll);
INSERT INTO permission VALUES(152,'Partial Control',NULL);
25. Read all data from the table:
6 Abdul Awal Sajib
Select * from branch;
26. update login I’d in login table:
SELECT * FROM issues WHERE isu_id = 001;
Before Update
UPDATE issues SET isu_stu_id = 920 WHERE isu_id = 001;
SELECT * FROM issues WHERE isu_id = 001;
After Update
27. Print Student Name with Department Name:
SELECT student_name, department_name FROM students_info;
28. Find how many students are in each department:
SELECT department_name, COUNT(student_id) as student_count FROM
students_info
GROUP BY department_name;
29. Select all students in the specific course/department:
SELECT student_name, department_name
FROM students_info
WHERE department_name = 'Computer Science'
GROUP BY student_name, department_name;
7 Abdul Awal Sajib
30. Find all students enrolled in courses with IDs greater than 120:
SELECT student_name, course_id
FROM students_info
WHERE course_id > 120;
31. Get the total number of students in the a specific department:
SELECT COUNT(student_id) AS
total_students
FROM students_info
WHERE department_name =
'Biology';
32. Update: Modify existing records.
Select * from Student_info Where student_name =”Lucas Gray”;
UPDATE students_info
SET department_name = 'Physics'
WHERE student_name = 'Lucas Gray';
Select * from Student_info Where student_name =”Lucas Gray”;
8 Abdul Awal Sajib
33. Delete: Remove records from the tables.
DELETE FROM students_info
WHERE student_name = 'Lucas Gray';
Select * from Student_info Where student_name =”Lucas Gray”;
34. Sorting and Filtering: Get Students from specific
Department Ordered by Name:
SELECT student_name
FROM students_info
WHERE department_name = 'Biology'
ORDER BY student_name;
35. create view From table information:
CREATE VIEW department_details AS
SELECT course_id , department_name
FROM students_info;
Note:
A view is a virtual table based on
the result-set of an SQL
statement. It doesn't store data
directly but presents a
customized view of the
underlying data from one or
more tables. Think of it as a saved
query that can be treated like a
real table.
9 Abdul Awal Sajib
36. print all information about issue:
SELECT * from issues;
37. Print All Login details: SELECT * FROM login;
38. Select & Student with their department: SELECT * from student_details;
Conclusion: This lab report has provided a comprehensive overview of MySQL's
fundamental concepts and operations. We have explored database creation,
table design, data manipulation, and query optimization techniques. Through
the hands-on exercises, you have gained practical experience in using MySQL to
store, retrieve, and manage data efficiently.
10 Abdul Awal Sajib