You are on page 1of 7

Lab 5: MySQL Commands

Introduction:

MySQL is a popular open-source relational database management system (RDBMS) known for its
ease of use, flexibility, and robust performance. Developed by MySQL AB, now owned by Oracle
Corporation, MySQL is widely used to store and manage large volumes of structured data. It
employs a client-server architecture, where applications interact with the database through queries
and commands. MySQL supports multiple programming languages, making it a versatile choice
for web development and other data-driven applications. With its efficient storage and retrieval
mechanisms, it has become the go-to choice for websites, content management systems, and
various data-driven applications. MySQL's wide adoption, extensive community support, and
continuous development ensure it remains a cornerstone in the realm of database management
systems.

Features of MySQL:

• Open-source and freely available for use.


• Cross-platform compatibility, supporting various operating systems.
• Fast and efficient, offering excellent performance for read-heavy workloads.
• Scalable, capable of handling large datasets and high traffic.
• ACID-compliant, ensuring data integrity and consistency.
• Comprehensive security features with user access controls and encryption options.
• Replication support for creating backups and distributing data across multiple servers.
• Extensive community support and documentation resources.
• Integration with various programming languages and frameworks.
• Full-text search capabilities for efficient text-based searches.
• Triggers, stored procedures, and views for advanced database functionality.

Some SQL Commands:

1. Create Database
This command is used to create a new database in MySQL. Databases serve as containers
for storing tables, views, and other database objects.
The syntax for it is:
CREATE DATABASE database_name;
Here, a database named my_database is created.

2. Use Database:
The USE DATABASE command is used to switch to a specific database within the
MySQL server. After executing this command, all subsequent queries will be executed in
the context of the selected database.
The syntax for this statement is:
USE database_name;
Here, the newly created database is being selected for use.

3. Create Table:
The CREATE TABLE command allows you to create a new table within a database. Tables
are used to store structured data in rows and columns.
The syntax to create a table is:
CREATE TABLE table_name (
column1 datatype constraint,
column2 datatype constraint,
...
);
The creation of a table is demonstrated below:
4. Insert Data:
The INSERT INTO command is used to add new rows of data into an existing table. It
allows you to specify the values for each column in the table.
The syntax to insert data into the table is:
INSERT INTO table_name (column1, column2, ...) VALUES (value1, value2, ...);
The demonstration is as follows:

5. Update Record:
The UPDATE command is used to modify existing records in a table. You can use the
SET clause to specify the new values for the columns and the WHERE clause to filter the
rows to be updated.
The syntax to update the existing record is as follows:
UPDATE table_name SET column1 = new_value1, column2 = new_value2 WHERE
condition;
Here, a record is updated in the table employees.
6. Delete Record:
The DELETE FROM command is used to remove rows from a table based on the specified
condition in the WHERE clause.
The syntax to delete a record is as follows:
DELETE FROM table_name WHERE condition;
Here, a record is deleted from the table employees.

7. Joins:
Joins are used to combine rows from two or more tables based on related columns between
them. The JOIN keyword is used in conjunction with the ON keyword to specify the
condition for joining the tables. The syntax for join is:
SELECT columns
FROM table1
JOIN table2 ON condition;
We created a new table called "departments" to store department information and then
demonstrate different types of joins (INNER JOIN, LEFT JOIN, RIGHT JOIN, and FULL
JOIN) with the "employees" table.

We inserted some data into the new table departments.


• Inner Join
The INNER JOIN is used to combine rows from two or more tables based on a
related column between them. It returns only the rows that have matching values
in both tables. If there is no match, the row will not be included in the result set.
We inner joined two table emplouyees and departments on the basis of
department_id.

• Left Join
The LEFT JOIN returns all the rows from the left table and the matching rows from
the right table. If there is no match for a row in the left table, the result will contain
NULL values for columns from the right table.

• Right Join
The RIGHT JOIN is similar to the LEFT JOIN, but it returns all the rows from the
right table and the matching rows from the left table. If there is no match for a
row in the right table, the result will contain NULL values for columns from the
left table.
• Full Join
The FULL JOIN returns all the rows when there is a match in either the left or the
right table. If there is no match for a row in either table, the result will contain
NULL values for columns from the table that does not have a match.

8. Limit:
The LIMIT clause is used to limit the number of rows returned by a SELECT query. It
helps in implementing pagination or fetching a specific number of rows from the result set.
The syntax is:
SELECT columns FROM table_name LIMIT number_of_rows;
We used the limit clause in the following way:
9. Sorting:
The ORDER BY clause is used to sort the result set in ascending (ASC) or descending
(DESC) order based on one or more columns.
The syntax for sorting the results is:
SELECT columns FROM table_name ORDER BY column_name [ASC | DESC];
The descending order:

The ascending order is:

You might also like