Learning MySQL with basic SQL Commands
Tags: mysql, sql, database, datascience
Published: 2021-10-08 06:52:07

![alt text](https://encrypted-tbn0.gstatic.com/images?q=tbn:ANd9GcQMytSZWMtAirIFDn4DSx9_vylkmvs8Kzw8pQ&usqp=CAU)

### Introduction:
***MySQL is an open-source relational database management system. MySQL is free and open-source. MySQL is ideal for both small and large applications.***

To interact with MySQL database or MySQL server, ***we have to learn Structure Query Language.*** 

### Installation:
But before we start here is the documentation link from where installation of MySQL and its components:

[MySQL Installation Documentation Official](https://dev.mysql.com/doc/refman/8.0/en/installing.html)

[MySQL Installation Step By Step Guide - Javatpoint](https://www.javatpoint.com/how-to-install-mysql)

### The Awesome Stuff:
There are different types of Commands in SQL:
1] Data Definition Language (DDL) Commands
2] Data Manipulation Language (DML) Commands
3] Data Control Language (DCL) Commands
4] Transaction Control Language (TCL) Commands
5] Data Query Language (DQL) Commands

#### DATA DEFINITION LANGUAGE (DDL) Commands:
When we want to define the structure of the table or database which we create, DDL Commands come in handy. 

##### CREATE Command:
**It is very necessary to create a database first before going ahead to create tables.**

<code>CREATE DATABASE databaseName;</code>

**After the creation of the database, we have to tell the MySQL Command Line Client that we are going to work on the above-created database. We can do so like this:** 

<code>USE databaseName;</code>

Now, we can start MySQL Learning Journey.

MySQL keeps data in the form of tables. There can be more than one table in a database. We query for the data through the tables. Most of the commands work on the table or tables we create in the database. 
So to create a table, we can do this:

<code>CREATE TABLE Detail(Column Names Datatype.....);</code>

We can have any number of columns and specifying the data type of that column is very necessary.
The data types used in MySQL, shown here in this link: 
[Data types in MySQL Documentation](https://www.w3schools.com/sql/sql_datatypes.asp)

##### DROP Command:
Sometimes we feel that we have created a table that is not necessary as in its existence can be justified with any other table. 
Then we have used DROP Command.

<code>DROP TABLE TableName;</code>

Also, we can delete the whole database.

<code>DROP DATABASE databaseName;</code>

Though it is highly unlikely that we have to delete a database with records in it but it comes in very handy when we have to delete a table. Remember, if the table is connected to another table with a foreign key, first we have to delete that table to which it is connected. After deleting that table we can then safely delete the current table (to be deleted). Otherwise, it will produce an error.
 
##### ALTER Command:
Now, to change our table contents or any column data type, we can use ALTER Command. We can use ALTER for not only modifying purposes but also can be used to add a column inside the table like this:

<code>ALTER TABLE tableName ADD columnName ColumnDefination/s;</code>
<code>ALTER TABLE tableName MODIFY ColumnDefination/s;</code>
example: 
<code>ALTER TABLE details ADD (Address VARCHAR(50));</code>
<code>ALTER TABLE details MODIFY (Name VARCHAR(30));</code>

##### TRUNCATE Command:
In MySQL, the data inserted into the tables are in the form of rows. These records can be inserted as well as deleted. 
So, to delete all records from the table, we can use truncate in this way: 

<code>TRUNCATE TABLE tableName;</code>

But use TRUNCATE carefully as it even frees up the space which was taken by the records.

With the TRUNCATE command, we have completed the data definition language (DDL) commands.

#### DATA MANIPULATION LANGUAGE (DML) COMMANDS: 
DML commands are used to make changes in the data in the database. 
But there is a catch. DML commands are not auto-committed i.e. aren't auto-saved as they can be ROLLBACK too.

##### INSERT command:
The command we all are most anxious to use is the INSERT command. It answers the Question: 
***How to insert a record into the table?***

<code>INSERT INTO tableName(Col1,Col2,Col3,....,Coln) VALUES(Val1, Val2, Val3,....,Valn);</code>

example:

<code>INSERT INTO detail(Name,Rollnumber,City,Country) VALUES('Siddhesh Shankar',19070122168,'Mumbai','India')('John Jones', 1907134444,'Amsterdam','Netherlands');</code>

##### UPDATE Command: 
The command is used to change the existing records in the table. Any records which are not correct can cause a lot of problems while querying. Hence as soon as the wrong record is been put into the database, it needs to be rectified immediately.

<code>UPDATE tableName SET ColumnName1 = Val1, ColumnName2 = value2, ..... ColumnName n = valn WHERE condition;</code>

Example:

<code>UPDATE details SET Name = 'Roger Kook', City= 'Riga' WHERE ID= 1;</code>

Here, the WHERE clause condition is very important. Without the correct condition, we can end up making a bigger mistake in the database which can cause more problems.

##### DELETE Command: 
Now, after seeing the UPDATE as well as the TRUNCATE command, I was thinking that is there any command which is user-friendly or understands that a user might want to delete only some rows instead of the whole table records. 

Yes, there is a command called as DELETE command. 
Again the WHERE Clause Condition is super important. 

<code>DELETE FROM TableName WHERE Condition;</code>

#### DATA CONTROL LANGUAGE (DCL) Commands:
DCL is used to grant and take back authority from any database user. In other words, we can fiddle around with the rights, permissions a user has for the specific database.

##### COMMIT Command: 
COMMIT command is used to save all the changes, creations we have done with the database.

<code>COMMIT;</code>

example:

<code>DELETE FROM details WHERE ID = 1;
COMMIT;</code>

##### ROLLBACK Command:
This command is related to TRUNCATE Command as the TRUNCATE command can be ROLLBACK or can be undone. So, ROLLBACK can undo only those changes that aren't saved yet.

<code>ROLLBACK;</code>

example:

<code>DELETE FROM details WHERE ID = 1;
ROLLBACK;</code> 

##### SAVEPOINT Command:
We can create a save point which means that we can save the state of our changes so that if in the future we want to ROLLBACK to that spot, we can do so easily. 
It is like gaming where we can save our level before going for the next mission because if we lose in the mission ahead, we can start from the last save point/load point instead of going around the whole game once again. 

<code>SAVEPOINT savepointName;</code>

#### DATA QUERY LANGUAGE (DQL) Command:
Here it gets super exciting as we get to work through the records in the database via querying.
***There is only SELECT Command but there are loads of variations with different clauses which can be used with it.***

***If we want to get all the records in a specific table:***
<code>SELECT * FROM TableName;</code>

But sometimes we need to get records specific to what the use cases are. Here comes the use of variations or clauses. Some of them are: 

##### LIKE Clause: 
LIKE is used when we want to perform pattern matching. 
<code>SELECT * FROM tableName WHERE columnName LIKE 'Condition';</code>

Here the condition is having the utmost importance as according to that, the pattern matching takes place. It uses wildcards like % and _.

***Here are some examples of how LIKE Clause can be used:***

<code>SELECT * FROM details WHERE Name LIKE 'a%';</code>
This shows all the records in the table details whose names start with 'a'.

<code>SELECT * FROM details WHERE Name LIKE '%s';</code>
This shows all the records in the table details whose names end with s.

<code>SELECT * FROM details WHERE Name LIKE '_t%';</code>
This shows all the records in the table details whose names have 't' in the second position.

Lastly, 
<code>SELECT * FROM details WHERE Name LIKE 't___%';</code>
This shows all the records in the table details whose names have 't' in the start and the length of the name is at least 3 characters.

With the use of wildcards, we can get the records from the table easily.

##### ORDERBY Clause:
The ORDERBY Clause helps a lot when we want the records to be in an ordered format. 
Ascending or descending order gives a lot of information. For example, if we want the maximum of the price column where all the prices are stored. We can either use the maximum function in SQL i.e. **MAX()**, but we can also display the whole set of records with the highest in the first spot.

<code>SELECT * FROM TableName ORDERBY col1,col2,...,coln ASC;</code> 
This is for the ascending order format.

<code>SELECT * FROM TableName ORDERBY col1,col2,...,coln DESC;</code> 

For example:
<code>SELECT name, prices, id FROM details ORDERBY prices DESC;</code>

##### LIMIT Clause: 
Used to specify the number of records to return.

<code>SELECT ColName FROM TableName WHERE condition LIMIT number;</code>
for example:

<code>SELECT * FROM details WHERE Name LIKE 'a%' LIMIT 5;</code>
This code will show only 5 records that match the condition.

So, these were some of the basic commands I had learned first when I started learning SQL in MySQL Command Line Client.

[Here is my LinkedIn Profile Link](https://www.linkedin.com/in/siddhesh-shankar-b94711210/)

***Happy Querying ! Happy Learning !***