Basic Language "MySQL Query" #2 : Change Data Tables, Delete Data Tables, And Display Data Table

in #utopian-io8 years ago (edited)

What will I learn?

  • Changing Table Data
  • Delete Data Tables
  • Display Data Table

Requirements

  • MySQL
  • You have basic about MySQL

Difficulty

  • Intermediate

Tutorial Contents

Changing Table Data

There are times when we want to correct the wrong data or want to update the old data. For the purpose of changing the data already in the table, available UPDATE query. Here's the basic MySQL query UPDATE format:


UPDATE table_name SET column_name = new_name WHERE condition

The key of the UPDATE query is in the condition section. This condition will limit which data you want to change. For example, I want to change the name of Jakarta province to DKI Jakarta. This means I will change the data in column name_prov become DKI Jakarta where the column has id_prov = 1. Here is the query:

gambar 1.jpg

Forgot to write WHERE condition for UPDATE query, can be fatal. Because the entire column will change.

Delete Data Tables
To delete the data contained in a table, there is a DELETE query. Here's the format
essentially:


DELETE FROM name_table WHERE conditions

Just like the UPDATE query, the removal process key is in the condition. For example, I want to delete rows of tables that have the condition of capital_city = Banjarmasin. Here's the command:

2.png

Visible, the data of South Kalimantan province with ibu_kota = "Banjarmasin" has been erased from the provincial table.

Display Data Table

Of the many queries in MySQL, the command to display data is the most commonly used, ie SELECT query. The process of creating tables is usually only necessary once at the beginning, but the process of displaying data is constantly being called during the application is used. To display data, we need at least 3 things:

  • What columns you want to show.
  • The name of the table to be displayed.
  • Condition to display data.

SELECT queries need these three things. Here is the basic format of SELECT query writing:


SELECT column1, column2 FROM name_table WHERE conditions;

Especially for conditions, if not written then all data table will be displayed.
For example, to display column name_prov and ibu_kota from provincial table, can use the following query:

3.png

If you want to display the entire column, the column names can be replaced with a star (*):

4.png

To limit which rows you want to display, can write a section of conditions. For example, I want to display data for id_prov 4 and 5. The query is:

5.png

Here I use the OR operator to retrieve data with id_prov 4 and 5. In addition there are many other operators that can be used to create conditions. We will discuss it in a later chapter. SELECT query also provides an optional ORDER BY command to sort the data. The basic format of writing is as follows:


SELECT column1, column2 FROM table_name
WHERE condition ORDER BY nama_kolom_urut [ASC or DESC]

The column_name section is the ordering column. This option can be adjusted with the addition of ASC instruction (short for ascending) for sorting from small to large, and DESC (short for descending) for large to small order. If it is not written, by default ORDER BY will use ASC.

For example, I want to display the entire province data sorted by column nama_prov:
6.png

What if sorted by population starting from the largest? add DESC command:
7.png

SELECT queries also have additional instructions to limit how many rows to show, ie LIMIT commands.
Here's the basic format:


SELECT column1, column2 FROM table_name
WHERE condition LIMIT line_awal, sum_baris

Where line_early is the beginning of the row you want to display, and the_number number is the number of rows sorted from line_early. The MySQL line number begins with the number 0. Suppose I want to display the data of the top 2 provinces that are based on nama_prov, then the query is:

8.png

If we only give 1 number behind the LIMIT instruction, then MySQL assumes that the number is calculated from the top row (row to 0). So the previous view can also be obtained with the following command:

MariaDB [indonesia]> SELECT * FROM province ORDER BY nama_prov LIMIT 2;

Curriculum

Basic Language "MySQL Query" : Creating Tables, Removing Tables, And Adding Data Into Tables.


Finished already the tutorial above, may be useful.



Posted on Utopian.io - Rewarding Open Source Contributors

Sort:  

Thank you for the contribution. It has been approved.

You can contact us on Discord.
[utopian-moderator]

Hey @kizilelma, I just gave you a tip for your hard work on moderation. Upvote this comment to support the utopian moderators and increase your future rewards!

Hey @suparmanyunus2 I am @utopian-io. I have just upvoted you!

Achievements

  • You have less than 500 followers. Just gave you a gift to help you succeed!
  • Seems like you contribute quite often. AMAZING!

Suggestions

  • Contribute more often to get higher and higher rewards. I wish to see you often!
  • Work on your followers to increase the votes/rewards. I follow what humans do and my vote is mainly based on that. Good luck!

Get Noticed!

  • Did you know project owners can manually vote with their own voting power or by voting power delegated to their projects? Ask the project owner to review your contributions!

Community-Driven Witness!

I am the first and only Steem Community-Driven Witness. Participate on Discord. Lets GROW TOGETHER!

mooncryption-utopian-witness-gif

Up-vote this comment to grow my power and help Open Source contributions like this one. Want to chat? Join me on Discord https://discord.gg/Pc8HG9x

Coin Marketplace

STEEM 0.04
TRX 0.32
JST 0.078
BTC 65711.11
ETH 1725.05
USDT 1.00
SBD 0.41