SQL INSERT INTO Statement
- The INSERT INTO statement is used to add new data to a database.
- The INSERT INTO statement adds a new record to a table.
- INSERT INTO can contain values for some or all of its columns.
- INSERT INTO can be combined with a SELECT to insert records.
The SQL INSERT INTO syntax
The general syntax is:
1) insert single row at a time on table
INSERT INTO table-name (column-names) VALUES (values);
or
INSERT table-name (column-names) VALUES (values);
2) insert multiple row at a time
INSERT INTO table-name (column-names) VALUES (values),(values),(values),.......;
or
INSERT table-name (column-names) VALUES (values),(values),(values),(values),.......;
or
INSERT table-name (column-names) VALUES (values),(values),(values),(values),.......;
SQL INSERT INTO Example
1) single row at a time
mysql> insert
into person(sno,name,age,sex) values ('3', 'chayan', 20, 'M');
Query OK, 1 row
affected (0.01 sec)
mysql> select
*from person
-> ;
+-----+--------+------+------+
| sno | name | age
| sex |
+-----+--------+------+------+
| 1 | subham |
25 | M |
+-----+--------+------+------+
1 rows in set (0.00
sec)
2) insert multiple row at a time
mysql> insert into person
(sno,name,age,sex) values ('1', 'subham', 25, 'M'),
('2', 'nunu', 30,
'F'),('3', 'chayan', 20,
'M');
mysql> select
*from person
-> ;
+-----+--------+------+------+
| sno | name | age
| sex |
+-----+--------+------+------+
| 1 | subham |
25 | M |
| 2 | nunu |
30 | F |
| 3 | chayan |
20 | M |
+-----+--------+------+------+
3 rows in set (0.00
sec)
Comments
Post a Comment