Introduction To MySQL Commands
MySQL is an open source widely used relational database management system which helps to deliver applications with high performance, and scalable web-based and embedded database applications to the customers. MySQL is widely used as a database component of the software stack for a web application. MySQL Commands is very powerful and we will have a look into MySQL commands which are very helpful and consequential for every developer to know and use these queries to interact with the system and MySQL database. MySQL RDBMS is based on a structured query language (SQL) and it will support and run on Linux, UNIX, and Windows.
Basic MySQL Commands
Q1. Write a query to create a table countries with column names country name, country id and region id?
Create a table countries in MySQL is done by using the below query and the output is followed:
CREATE TABLE countries(
Q2. How to get a list of all databases present?
We can get a list of all running databases in MySQL using the below query
4.8 (612 ratings)
Q3. How to get all tables in a database using MySQL?
We can get all the tables present in a database in MySQL using the below query:
Q4. How to know all the filed names and type of table in MySQL?
We can get all the filed names and type of a table in MySQL Command using the below query
Here os_users is a table with filed names u_id and password and the output is as below:
Q5. How to delete a table and a database in MySQL?
We can delete a table using the below MySql Command query
Query to delete a table:
Drop table lookup;
Where lookup is a table name. We can delete a database using the below query
Query to delete a database:
Drop database users;
Where a user is a database
Output for delete tables is:
Q6. How to get data from a table where a particular field is filled with data “MyIsam”. We need to get the creation time and engine field for where the engine field is filled with “MyIsam”.
We can write a MySQL Command query for the above requirement as below: Let us say we have a table XYZ with fields creation_time and engine and the engine filed is filled with string data and creation_time field is filled with date and time.
Select create_time, engine from xyz where engine=”MyIsam”;
The above query will give creation_time and engine where engine field is filled with “MyIsam”
Q7. Query to get all fields from a table where a name is ‘tecmint’ and web address is ‘tecmint.com’?
Let us consider a table as XYZ with field’s id, name, and web address.
We will get the fields of a table where name matched as “tecmint” and web address is matched as “tecmint.com”
Select * from xyz where name=”tecmint” and webaddress=”tecmint.com”;
The above query will display all the fields where the field name is matched with tecmint and web address is matched with tecmint.com
Q8. How to know the total number of rows in a table?
We can the number of rows in a table using the query as below:
Select count(*) from Tables;
Q9. How to select particular filed rows in a table?
We can select a particular filed let us say uid from a table called os_users is
Select uid from table os_users;
Q10. How to use a particular database in MySQL?
We can switch to use a particular database using the below query
Use a database cloud;
Intermediate MySQL Commands
There are several other popular MySQL commands that the PHP developer also uses, which are not very basic but work with MySQL more. Some of the following types of intermediate MySQL commands:
Q11. Query to get a specific field from a table where a constraint is matched( emp_id=100)?
Select emp_id, emp_name from table employee where emp_id=100;
Q12. Query to order the results from a table based on certain constraint and using ‘order by’?
Query using with orderby for sorting the results from a table is
Select emp_id, emp_name from employee where city=”Hyderabad” order by emp_id;
Q13. Query to sort the results from a table based on some constraint and using “Group by”?
Query using “Group By” to sort the results from a table is as below:
Select name, age from patients where age > 45 group by age order by name;
Q14. Query to get the total number of customers from a customer’s table?
Query to get all the customers from a customer table is:
Select count(customer_id), country from customers group by country;
Q15. Query to calculate the total sum of the salary given to each employee in a company?
Select sum(salary) from employee where emp_age > 30;
Q16. Query to list all the views available in the schema?
Select * from myschema.views;
Q17. How to create a view from a table?
Query to create a view is as below:
Create view A_students as select student_name, student_id from student where GPA > 80;
Q18. How to update a view?
Query to update the existing view is as below:
Create or replace view product_list As select product_id, Product name, category from products where discount=no;
Q19. Query to display primary keys from a table?
The following displays all the fields in a table’s primary key:
Select * from Sys. objects where type=’PK’;
Q20. Query to display all the user tables in a system?
Select * from Sys. objects where type=’u’;
Advanced MySQL commands
Q21. Why IFNULL() statement is used in MySQL?
The IFNULL() statement will check its first argument and return if it is not a null or second argument.
Select name, IFNULL(id,’unknown’) As ‘id’ from the taxpayer;
Q22. Query to show only five rows from the result of a query in MySQL?
In order to achieve this we need to use LIMIT in the query as below:
Select * from students limit 5
Q23. Query to select all users except one user using the not operator?
Query using not operator is:
Select * from users where user_name !=SAM;
Q24. Query to get the present date in MySQL?
The following query will give the current date
Q25. How to export all tables to an XML file in MySQL?
We need to use –e option to export all the tables to an XML file as below query:
Mysql –u user_name -xml -e ‘select * from table_name’ > tables.xml
Tips and Tricks to use MySQL Commands
Some common users who frequently use MySQL commands normally use some tips and tricks to correctly use MySQL commands output. These types of tricks usually solve some user-specific queries and display the execution output to understand it correctly. Some of the key tricks most commonly used are:
- Add Explain statement before the select queries as it gives a better explanation about what query is exactly doing.
- Need to enable query cache as it improves the performance of execution of queries and it is one of the best methods for database performance.
- Use stack_trace in MySQL which will help you to differentiate between bugs, track, and fix them.
- We can take the database backup using a mysqldump command in MySQL which will help you to keep track and backup.
Finally, it’s an overview of MySQL commands of different levels and tips and tricks to use them. I hope you will have a good understanding and knowledge about the MySQL commands and how to use them after reading this article.