What is an SQL Query? Check out the full explanation!

For those of you who are new to databases or just learning MySQL databases . You must be familiar with the term database query. This query is very familiar in database processing.

However, what is a query? Then, how do you use MySQL database queries?

Come on! Check out our article below! Here we will discuss thoroughly the explanation of database queries and the use of database queries in MySQL.

Table of contents close

1 What is Query?

2 Three Types of Database Queries in SQL

2.1 1. DDL (Data Definition Language)

2.2 2. DML (Data Manipulation On Language)

2.3 3. DCL (Data Control Language)

3 Examples of Using Database Queries

3.1 1. Creating Database

3.2 2. Deleting the Database

3.3 3. Creating Tables

3.4 4. Viewing Table Structure

3.5 5. Deleting Tables

3.6 6. Entering Data into Tables

3.7 7. Updating the Data Contents

3.8 8. Deleting Data

4 Conclusion

What is a Query?

Query is a syntax or command used to access and display data on a database system. Query has the ability to set which data needs to be displayed the way you want. In addition, queries can be used to make data interact with each other.

Query is also commonly referred to as query language or query language. Currently the most popular query language among Database Administrators is SQL.

Three Types of Database Queries in SQL

There are three types of database queries in SQL, namely:

  • DDL ( Data Definition Language )
  • DML ( Data Manipulation Language )
  • DCL ( Data Control Language ).

These three queries function to create / define database objects such as creating tables, manipulating the database, and controlling the database.

To be more complete, here is an explanation of the three types of database queries.

  1. DDL ( Data Definition Language)

DDL is an SQL query method used to define data in a database. With this query you can create new tables, modify tables, create indexes, define table storage structures and so on. The following is the query that DDL has:

Query Name Description
CREATE Used to create databases and tables.
Drop Used to delete tables and databases.
Alter Used to make changes to the table structure that has been created. For example, adding a Field ( Add ), renaming the Field ( Change ) or renaming it ( Rename ), and deleting the Field ( Drop ).
  1. DML ( Data ManipulatIon Language)

DML is a query method that is used when a DDL has been created. i This DML query is used to perform database manipulation. The following is the query that DML has:

Query Name Description
INSERT Used to enter data in database tables.
UPDATE Used to change existing data in database tables.
DELETE Used to delete data in database tables.
  1. DCL ( Data Control Language)

DCL is an SQL query method used to grant database access authorization rights, database usage audits, space allocation, and space definitions. The following is the query that DCL has:

Query Name Description
GRANT Used to allow users to access tables in the database.
REVOKE Used to cancel user rights permissions.
COMMIT Used to specify database storage.
ROLLBACK Used to cancel database storage.

Examples of Using Database Queries

In this tutorial, we will provide some basic guidelines for using database queries in a MySQL Database. For this tutorial we are using an Ubuntu 16.04 VPS server which has MySQL installed . The following is an example of using a database query.

  1. Create Database

Database is the main medium where to build a database. It is in this database that you can place multiple tables later. The following is the command used to create a database on MySQL:

create database nama_database;

In the above example, Query OK shows that the database creation with the name office was successfully created. To view existing databases in MySQL, you can use the following command:

show databases;

Then the output from the command above will display the database set on your server. Here is a picture of the command above.

  1. Deleting the Database

To delete a database that was created, you can use the following SQL query command:

drop database nama_database;

Drop means delete. This SQL query command functions to delete a database, as in the example in the following image:

  1. Make Tables

Tables are objects that are used to store all data in the database. Therefore, tables are objects that must exist in the database.

Tables are located in a database, so table creation is done after a database has been created. In the table there are rows and columns. Rows are called recordsets while columns are called fields .

To create a table or more, the database must be activated first. Because the table will be entered into an active database. The following is the command to activate the Database:

use nama_database;

After entering the database, you can create a table or more. To create a table, you can use the command below:

create table data_diri (no int(3),nama varchar(35),alamat varchar(60),divisi varchar(40),no_telepon varchar(15),);

In the above example, Query OK states that the creation of the table named data_diri was successfully created. To view the tables in the database, you can use the following command:

show tables;

  1. Seeing the Table Structure

After the table is created, you can view the data type and recordset length by displaying the table structure. The command used to display the table structure is as follows:

desc nama_tabel;

 

Atau

 

describe nama_tabel;

Then the output will be like in the image below:

  1. Deleting Tables

To delete a table that has been created, you can use the SQL query command: DROP. Here’s an example:

drop table nama_tabel;

  1. Entering Data into the Table

In order to enter data, you must first have a table in the database. Then, enter data or data entry with the INSERT command . All programs that use SQL queries use the same standard commands.

Here’s an example when the INSERT command is used:

insert into data_diri values(‘001′,’Dapit Kurniawan’,’Gg Code’,’SEO Team’,’085613548789′);

To view the results of the data entered in the table, use the following command:

select * from nama_tabel;

  1. Updating the Data Contents

If you need to update the data content or update data, use the SQL query command used is UPDATE .

update data_diri set nama=’Amanda Blyth’ where nama=”Dapit Kurniawan”;

When you check the table, the data will change as shown below:

  1. Deleting Data

To delete data, MySQL has a query called DELETE . To use this query, you need to add the name of the data to be deleted. If you want to delete all the data contained in the table, here are the commands:

delete from nama_tabel;

If you want to remove certain data from a table, the following syntax is used:

DELETE FROM nama_tabel WHERE kondisi;

To delete data with id number 3 contained in the data_diri table, use a query like the one below:

delete from data_diri where no=’3′;

Conclusion

After you finish learning the basics of using MySQL database queries, you can use these queries to search for data or add data to the database. Not only that database queries also help you to manipulate databases and analyze databases.

This is the article about SQL queries and the use of SQL queries in MySQL. If you still have questions, don’t hesitate to add them via the comments column below.

 

by Abdullah Sam
I’m a teacher, researcher and writer. I write about study subjects to improve the learning of college and university students. I write top Quality study notes Mostly, Tech, Games, Education, And Solutions/Tips and Tricks. I am a person who helps students to acquire knowledge, competence or virtue.

Leave a Comment