Deprecated: mysql_connect(): The mysql extension is deprecated and will be removed in the future: use mysqli or PDO instead in /home/lsp4you/public_html/connect.php on line 2 LSP4YOU - Learner's Support Publications

SQL - INSERT INTO Statement

The INSERT INTO statement is used to insert new records in a table.

It is possible to write the INSERT INTO statement in two ways.
The first way specifies both the column names and the values to be inserted:
INSERT INTO table_name (column1, column2, column3, ...)
VALUES (value1, value2, value3, ...);

If adding values for all the columns of the table, no need to specify the column names in the SQL query. However, the order of the values are in the same order as the columns in the table. The INSERT INTO syntax would be as follows:

INSERT INTO table_name
VALUES (value1, value2, value3, ...);

Remarks:
  • If adding values not for all the columns,
    then specify the required column names and values only.

Examples:
mysql> insert into department (dept_id, dept_name, location) 
    -> values('D01', 'Computer', 'South Block');
Query OK, 1 row affected (0.12 sec)

mysql> insert into department (dept_id, dept_name, location)
    -> values('D02', 'Commerce', 'E K Block');
Query OK, 1 row affected (0.08 sec)

mysql> insert into department 
    -> values('D03', 'Administration', 'E K Block Wing-1');
Query OK, 1 row affected (0.07 sec)

mysql> insert into department 
    -> values('D04', 'Psychology', 'E K Block Wing-3');
Query OK, 1 row affected (0.08 sec)

mysql> insert into department (dept_id, dept_name) 
    -> values('D05', 'Electronics');
Query OK, 1 row affected (0.09 sec)

mysql> insert into department 
    -> values('D06', 'Biosciences', 'Main Block');
Query OK, 1 row affected (0.08 sec)

mysql>

To see the records contained in the DEPARTMENT table:
mysql> select * from department;
+---------+----------------+------------------+
| dept_id | dept_name      | location         |
+---------+----------------+------------------+
| D01     | Computer       | South Block      |
| D02     | Commerce       | E K Block Wing-2 |
| D03     | Administration | E K Block Wing-1 |
| D04     | Psychology     | E K Block Wing-3 |
| D05     | Electronics    | NULL             |
| D06     | Biosciences    | Main Block       |
+---------+----------------+------------------+
6 rows in set (0.01 sec)

mysql>