SQL INSERT INTO Values
This article will help you in getting a better understanding of a very important SQL INSERT INTO VALUES function.
INSERT INTO statement is used to insert or add a new record into a particular/specific table.
New records can be inserted into a table in the following two ways:
- Inserting only values
- Inserting both column names and values
This article will mainly focus on Inserting only values method.
Inserting only Values:
By using this method only values can be inserted into the table. The syntax is mentioned below.
Syntax:
INSERT INTO table_name VALUES(val1, val2, val3………...)
Here table_name represents the name of the table into which the records are inserted.
val1, val2, val3……… represent the values that are to be inserted into the table.
Examples of Insert into values
Example 1:
Let us create a table called Player_info which contains basic information related to the player and insert VALUES into it using Insert Into statement.
CREATE TABLE Player_info(
ID INT,
First_Name VARCHAR(255),
Last_Name VARCHAR(255),
Year_Of_Birth INT,
DayOfYear_Of_Birth INT,
Place_Of_Birth VARCHAR(255),
Country VARCHAR(255),
PRIMARY KEY (ID)
);
INSERT INTO Player_info VALUES(1, 'Sanju', 'Samson', 1981, 340, 'Kerala', 'India');
INSERT INTO Player_info VALUES(2, 'Jonathan', 'Trott', 1981, 114, 'CapeTown', 'SouthAfrica');
INSERT INTO Player_info VALUES(3, 'Lasith', 'Malinga', 1977, 303, 'Matale', 'Srilanka');
INSERT INTO Player_info VALUES(4, 'Virat', 'Kohli', 1988, 310, 'Delhi', 'India');
INSERT INTO Player_info VALUES(5, 'Karn', 'Sharma’, 1987, 126, 'Haryana', 'India');
INSERT INTO Player_info VALUES(6, 'Ravindra', 'Jadeja', 1988, 341, 'Nagpur', 'India');
INSERT INTO Player_info VALUES(7, 'Stuart', 'Broad’, 1982, 186, 'Manchester', 'England');
We can check the data of table by using the following command:
SELECT * FROM Player_info;
Output:
First_Name | Last_Name | Year_Of_Birth | DayOfYear_Of_Birth | Place_Of_Birth | Country |
Sanju | Samson | 1981 | 340 | Kerala | India |
Jonathan | Trott | 1981 | 114 | Capetown | South Africa |
Lasith | Malinga | 1977 | 303 | Matale | Srilanka |
Virat | Kohli | 1988 | 310 | Delhi | India |
Karn | Sharma | 1987 | 126 | Haryana | India |
Ravindra | Jadeja | 1988 | 341 | Nagpur | India |
Stuart | Board | 1982 | 186 | Manchester | England |
Example 2:
Let us create a table called Subscription_info which consists of customer name, Subscription_type, and purchasetime, and insert VALUES into it with the help of INSERT INTO statement.
create table Subscription_info(customer_name VARCHAR(200), Subscription_ info VARCHAR(200) purchase time VARCHAR(200));
let us insert VALUES into the table.
INSERT INTO Subscription_info VALUES('Krishna', 'Gold', ‘03:13:20’);
INSERT INTO Subscription_info VALUES('Abrar', 'Basic', ‘02: 23:39’);
INSERT INTO Subscription_info VALUES('Ali', 'Premium', ‘05:45:30’);
INSERT INTO Subscription_info VALUES('Mahesh', 'Basic', ‘06:30:32’);
INSERT INTO Subscription_info VALUES('Hafeez', 'Gold', ‘11:25:35’);
INSERT INTO Subscription_info VALUES('Joseph', 'Premium', ‘10:32:50’);
INSERT INTO Subscription_info VALUES('Nikhil', 'Gold', ‘09:22:55’);
INSERT INTO Subscription_info VALUES('Ramya', 'Premium', ‘11:42:53’);
INSERT INTO Subscription_info VALUES(‘Kavya’, ‘Basic’, ‘12:30:00’);
INSERT INTO Subscription_info VALUES(‘Priyanka’, ‘Basic’, ‘10:55:00’);
We can check the data of table by using the following command:
SELECT * FROM Subscription_info;
Output:
Customer_name | Subscription_name | purchasetime |
Krishna | Gold | 03:13:20 |
Abrar | Basic | 02:23:39 |
Ali | Premium | 05:45:30 |
Mahesh | Basic | 06:30:32 |
Hafeez | Gold | 11:25:35 |
Joseph | Premium | 10:32:50 |
Nikhil | Gold | 09:22:55 |
Ramya | Premium | 11:42:53 |
Kavya | Basic | 12:30:00 |
Priyanka | Basic | 10:55:00 |
Example 3:
Let us create a table with the following attributes:
CREATE TABLE worker( Namevarchar(100), city varchar(100), email varchar(100))
Now let us insert some VALUES into this table
Insert into worker VALUES(“Ramesh”, “Hyderabad”, “[email protected]”)
Insert into worker VALUES (“Keerthi”, “Jaipur”, “[email protected]”),
Insert into worker VALUES (“Saleem”, “Hyderabad”, “[email protected]”),
Insert into worker VALUES(“Arjun”, “London”, “[email protected]”),
Insert into worker VALUES (“Ritika”,” Paris”, “[email protected]”);
Insert into worker VALUES (“Priyanka”, “Washington DC”,“[email protected]”);
Insert into worker VALUES (“Jimmy”, “Berlin”, “[email protected]”);
Insert into worker VALUES (“Kalyan”, “Mumbai”, “[email protected]”);
We can check the data of table by using the following command:
SELECT * FROM worker;
Output:
Name | City | |
Ramesh | Hyderabad | [email protected] |
Keerthi | Jaipur | [email protected] |
Saleem | Hyderabad | [email protected] |
Arjun | London | [email protected] |
Ritika | Paris | [email protected] |
Priyanka | Washington DC | [email protected] |
Jimmy | Berlin | [email protected] |
Kalyan | Mumbai | [email protected] |
Example 4:
Let us create a table called product_desc which consists of the selling price, buying price, selling date, etc related details of the product.
CREATE TABLE Product_desc(
Product_id INT AUTO_INCREMENT,
Product_name VARCHAR(100),
Buying_price INT,
Selling_price INT,
Selling_Date Date,
);
Let us insert some VALUES into it.
INSERT INTO Product_desc VALUES ('Wireless mouse’, 5000, 6000, '2019-11-12'),
INSERT INTO Product_desc VALUES ('Laptop', 60000, 65000, '2008-04-25'),
INSERT INTO Product_desc VALUES ('Tablet', 40000, 50000, '2015-07-21'),
INSERT INTO Product_desc VALUES ('Speaker', 2000, 2500, '2018-05-08' ),
INSERT INTO Product_desc VALUES ('Earphones', 1000, 1200, '2018-01-12' ),
INSERT INTO Product_desc VALUES ('SSD disk’, 7500, 9000, '2020-12-11' ),
INSERT INTO Product_desc VALUES ('Pendrive', 500, 700, '2021-07-06' ),
INSERT INTO Product_desc VALUES ('Charging_cable',1200, 1500, '2022-04-18' ) ;
We can check the data of table by using the following command:
SELECT * FROM Product_desc;
Output:
Product_id | Product_name | Buying_price | Selling_price | Selling_date |
1 | Wireless_mouse | 5000 | 6000 | 2019-11-12 |
2 | Laptop | 60000 | 65000 | 2008-04-25 |
3 | Tablet | 40000 | 50000 | 2015-07-21 |
4 | Speaker | 2000 | 2500 | 2018-05-08 |
5 | Earphones | 1000 | 1200 | 2018-01-12 |
6 | SSD disk | 7500 | 9000 | 2020-12-11 |
7 | Pendrive | 500 | 700 | 2021-07-06 |
8 | Charging_cable | 1200 | 1500 | 2022-04-18 |
Example 5:
Let us create a table called shop_timings which contains shop names and their opening time.
create table shop_timings(shop_no varchar(10), OpeningTimeStamp VARCHAR(10));
let us insert some VALUES into it.
INSERT INTO shop_timings VALUES(‘S1’, TIMESTAMP('2019-03-18', '16:12:45.765421’);
INSERT INTO shop_timings VALUES(‘S2’, TIMESTAMP('2020-11-12', '05:52:25.147236');
INSERT INTO shop_timings VALUES(‘S3’, TIMESTAMP('2019-03-05', '16:12:45.987534’);
INSERT INTO shop_timings VALUES(‘S4’, TIMESTAMP('2022-06-28', '05:02:34.549831');
INSERT INTO shop_timings VALUES(‘S5’, TIMESTAMP('2021-07-30’,’08:20:18.167865’);
INSERT INTO shop_timings VALUES(‘S6’, TIMESTAMP('2016-12-23, '18:26:41.985312’);
INSERT INTO shop_timings VALUES(‘S7’, TIMESTAMP('2019-03-18', '21:04:18.856438');
We can check the data of table by using the following command:
SELECT * FROM shop_timings;
Output:
shop_no | OpeningTimeStamp |
S1 | 2019-03-18 16:12:45.765421 |
S2 | 2020-11-12 05:52:25.147236 |
S3 | 2019-03-05 16:12:45.987534 |
S4 | 2022-06-28 05:02:34.549831 |
S5 | 2021-07-30 08:20:18.237865 |
S6 | 2016-12-23 18:26:41.985312 |
S7 | 2019-03-18 21:04:18.856438 |
This is all about INSERT INTO statement in MYSQL. Hope you understood this topic.