INSERT SQL command

In DML, Insert is another type of command. DML statements are typically used for managing data in a database. The commands are not auto-committed, meaning that changes made by the DML command are not permanent to a database, but can be rolled back.

Now, in the case of the Insert command, for instance, whenever you post a tweet on Twitter, the text is stored in some table, and then if you post a new tweet, a new record gets inserted in that table.

 

INSERT command

The Insert command in DML is used to insert data into a table. This can be done with the syntax shown below;

INSERT INTO table_name VALUES(data1, data2, ...)

Example for Insert command

Let’s consider a table with the title ‘student’ with the following fields.

s_idnameage

 

INSERT INTO student VALUES(11, 'Alexa', 18);

The command above will insert a new record into the table ‘student’.

s_idnameage
11Alexa18

 

Insert value into only specific columns

You can insert values in some specific columns of a row by using the Insert command. You can also specify the column names along with the values to be inserted as shown below;

INSERT INTO student(id, name) values(12, 'Nick');

The SQL query above can only be used to insert ‘id’ and ‘name’ values in the newly inserted record.

 

Insert NULL value to a column

In this case, Both the statements below will insert the 'Null’ value into the 'age’ 

INSERT INTO student(id, name) values(13, 'Ada Chan');

OR

INSERT INTO Student VALUES(13,'Ada Chan', null);

In other words, the command above will insert only two column values and the other column is set to null.

 

Insert Default value to a column

INSERT INTO Student VALUES(14,'Chris', default)
S_idS_Nameage
11Alexa18
12Nick 
13Ada Chan 
14Chris18

Take, for instance, the column age in our table has a default value of 18.

More so, when you run the below query, it will automatically insert the default value into the ‘age’ column, whatever the default value may be.

INSERT INTO Student VALUES(103,'Chris')