Introduction To MySQL Commands
The first word “My” is taken from the name of the co-founder’s (Michael Widenius) daughter and SQL stands for Structured Query Language. MySQL is an open-source RDBMS (Abr: Relational Database Management System) and is widely used for scalable web-based and embedded database applications. To interact with the MySQL database, we use some commands which would be able to perform create, update and modify the tables. The basis for these commands is SQL, the execution of commands further depends on the privileges assigned to users. Database management comes with a lot of responsibility and while performing operations one must know what exactly he wants.
Basic MySQL Commands
To interact with the database we use some commands or you can say a language that the system understands.
Below are some basic commands:
1. Show Database Command
If you want to see available databases use the below query.
Code:
Show databases;
2. Create Database Command
Below are the commands you can use to create databases. The second command checks if the database exists.
Code:
Create Database My_database;
Create Database if not exists My_database;
3. Create Table Command
In case you want to create a new table to store data, e.g., let’s say you want to store student information in a table.
You can use the below format:
Code:
Create Table Students(
Student_First_Name varchar(80),
Student_Last_Name varchar(80),
Student_ID varchar(10)
Student_Class varchar(10) );
Execute this and our table will be created. You can use the below command and see available table names.
Code:
Show tables;
4. Use Database Command
Once you see the available database or have created a database you can use the below commands.
Code:
Use database_name;
You shall see a message: Database changed
In case if you want to get the name of the currently connected database.
Code:
Select database():
5. Show Table Command
You can use this command to see the available tables in a database.
Code:
Show tables;
How To Describe And Delete The Table?
Once you have selected a database and now if you want to see the information of a particular table use the below commands.
Code:
Describe Table_name;
In case if you want to delete a table:
Code:
Drop Table_name;
Basic Select Command
You can use basic select commands to access the database and tables. Also, you can apply certain conditions on table columns to get specific information.
Below listed are some basic commands we can use for the same:
1. Select all the columns form a table.
Code:
Select * from Table_name;
2. Select specific columns from a table.
Code:
Select col_name1, col_name, Col_name3 from Table_name;
3. Using criteria with where clause: (for example consider col_name1 is a numeric value and we want to see the values where col_name1 has values greater than 1000).
Code:
Select * from Table_name where col_name1 > 1000 ;
How To Perform Count In Select Command?
Many a time you would need to find the number of records available in a table, sometimes it would be for all records, and other times it would with certain criteria.
Below are the commands to do the same:
Code:
Select count(*) from Table_name;
Using criteria with where clause: (considering the criteria mentioned earlier for the where clause).
Code:
Select count(*) from Table_name where col_name1 > 1000 ;
You can count the records for a specific column also, in that case you have to use column name instead of ‘*’.
Code:
Select count(col_name1) from Table_name where col_name1 > 1000 ;
Conclusion – MySQL Commands
In this article, we have seen the basics of MySQL, how it got this name, and its uses. As it is an RDBMS, you need the knowledge of commands to interact with the database. We have covered basic commands that are used in MySQL to access the database and create it.
Recommended Articles
This is a guide to MySQL Commands. Here we discuss basic MySQL commands, how to describe & delete the table? how to perform count in select command? You can also go through our other suggested articles to learn more –
Are you preparing for the entrance exam ?
Join our Data Science test series to get more practice in your preparation
View More