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)
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
);
-- insert new data
INSERT INTO shop (id, product_name, quantity, price) VALUES (0,"Chocolate",12,12.5);
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)
Here is the example of data insertion with shorthand version.
INSERT INTO shop VALUES (0,"Mango",90,7.8);
To view all the data that exist in a table, use SELECT
query. This query will be explained in separated article.
SELECT * FROM shop;
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 |
+----+---------------+----------+-------+
I hope this article is helpful for learning SQL, If you have any thoughts or comments you can write in the discussion section below.
Top comments (0)