MySQL tutorial - 3 Data Insertion

nadirbasalamah

nadirbasalamah

Posted on March 4, 2021

MySQL tutorial - 3 Data Insertion

In MySQL, data insertion can be done using INSERT query. The data type that will be inserted into table must match with the data type that already specified in each columns.

This is the basic query structure of data insertion.

INSERT INTO table_name (column_name,column_name) VALUES (data_value,data_value)
Enter fullscreen mode Exit fullscreen mode

In this example, a new data will be inserted into shop table.

-- create a new table called shop
CREATE TABLE shop(
    id INT PRIMARY KEY AUTO_INCREMENT,
    product_name VARCHAR(255) NOT NULL,
    quantity INT NOT NULL,
    price FLOAT NOT NULL
);
Enter fullscreen mode Exit fullscreen mode
-- insert new data
INSERT INTO shop (id, product_name, quantity, price) VALUES (0,"Chocolate",12,12.5);
Enter fullscreen mode Exit fullscreen mode

When insertion is succeed, there is a number of row affected of data insertion.

This is the shorthand version of data insertion query,

INSERT INTO table_name VALUES (data_value, data_value)
Enter fullscreen mode Exit fullscreen mode

Here is the example of data insertion with shorthand version.

INSERT INTO shop VALUES (0,"Mango",90,7.8);
Enter fullscreen mode Exit fullscreen mode

To view all the data that exist in a table, use SELECT query. This query will be explained in separated article.

SELECT * FROM shop;
Enter fullscreen mode Exit fullscreen mode

Based on the query above, all of the column will be retrieved using * notation besides SELECT. This is the output example.

+----+---------------+----------+-------+
| id | product_name  | quantity | price |
+----+---------------+----------+-------+
|  1 | Mango         |       90 |   7.8 |
|  2 | Milk          |      100 |   8.8 |
|  3 | Apple         |      110 |   7.8 |
|  4 | Fresh Chicken |       25 |  10.3 |
|  5 | Corn Flakes   |       19 |  2.99 |
+----+---------------+----------+-------+
Enter fullscreen mode Exit fullscreen mode

I hope this article is helpful for learning SQL, If you have any thoughts or comments you can write in the discussion section below.

💖 💪 🙅 🚩
nadirbasalamah
nadirbasalamah

Posted on March 4, 2021

Join Our Newsletter. No Spam, Only the good stuff.

Sign up to receive the latest update from our blog.

Related