Insert Command in Detail
Insert Command in Detail
There are three ways to insert “new record / records” into a table
(In this tutorial we will work with MySQL and Oracle SQL Plus Database)
Method 1:
Giving only values for the columns
Simple Syntax :
insert into table_name values (column1_value, column2_value,......
columnN_value);
Note : There are other syntax in Oracle which could be used to insert
multiple records using a single statement but as it is not in our Scope, we
are not going to look into it.
The column values given in the insert command should match with the data
type of the corresponding column. If it does not match then an error will occur
as shown in the below image. This happens because the SQL interpreter matches
the column values with the data type of the columns given in the table based on
its position, i.e., the first column value will matched with the data type of the
first column in the table, the second column value with the data type of the
second column and so on.....
MySQL :
In the above image, third column “age” has date type as integer (int – number
type) and when in the insert command, a character type value (‘Ten’) was given
for that column “age”, error occurred.
Oracle SQL Plus will also produce error when the above insert query in the
image is given.
Important Note : While providing values in the insert command, the non
numeric values like char type, varchar type, date should be given within
quotes (‘ ’).
It is also important that number of values given in the insert command should
match with the number of the columns available in the table else it will
produce error as shown below
No values provided
Oracle SQL Plus does not support the above method of leaving the values
empty.
Method 2
Giving column values by specifying column name.
The columns names and values will be split i.e given in separate ( ) parenthesis
Simple Syntax :
insert into table_name (column1_name, column2_name,...columnN_name)
values (column1_value, column2_value,...... columnN_value);
Consider the following table is already created – Table Name = “Student”
Kindly note, in the above statement we have given only two column names(id,
name) and their corresponding values (1004, ‘Vanitha’).. the column “age” and
its corresponding value is skipped(not given in the command). So after
executing the above SQL command, the column “age” will get the value as
“null” as there is no default value set for that column, if a default value was
provided for the column “age” at the time of table creation then that default
value would appear instead of ‘null’..
MySQL :
Oracle SQL :
In Oracle, the word “NULL” will not be displayed for the columns consisting
NULL values, instead it will be left empty.
Setting the default for the age column to ‘18’
Now when executing the below query by skipping (not providing) the column
name “age” and its corresponding value, the default value 18 will get added to
the record for the column “age”..
(Note: the column “age” and its corresponding value is not provided in the
below query)
Eg:
insert into student (id, name ) values (1005, ‘Sanjay’);
MySQL :
Eg:
insert into student (id, name, age) values (1006, ‘Aarushi’, 14) ,
(1007, ‘Gowtham’, 11);
MySQL:
In this method of inserting record, if a column name given in the column list of
the insert command is not available in the table then it will lead to an error as
shown in the below image, this is common to both MySQL and Oracle – SQL
Plus
the column name “student” given in the insert query is not in the table, so the
error appears.
In this type of insert query it is not mandatory that the column names given in
the column list of the insert query should be in the same order as they are in the
table, they can be in any order but it is important that the value provided for
each column should match with the data type of its corresponding column or
else it will lead to error as seen before.
Table after insert query is run
Values given in
correspondence
Order of columns in
with the order of
Table and insert query
column names
does not match
Simple Syntax:
Eg:
insert into student values (&id, &name, &age);
When the above query is executed, the user will be asked to enter the value
for each column one by one, once all the columns values are inserted, the SQL
interpreter will substitute the values in place of the column names and will
generate the insert query and run it...
MySQL :
This method of inserting record will not work in MySQL, as it does not support
that syntax.
Oracle – SQL Plus:
Executing the following insert query in Oracle – SQL Plus, kindly note that
after the query is executed, the Oracle SQL interpreter asks the user to enter the
values for the columns given in the insert query.
Note : While entering the values for the columns interactively, the non
numeric values like char, varchar, date should be given within quotes.
In this method, while giving the insert query all the column names must be
specified or else it will lead to error as shown below
One weird thing that goes out of logic in this method is, it is not mandatory
to give the column names as they are in the table, even when a column name
that is not in the table is given in the insert query, it will work perfectly fine
without giving an error. Kindly note the column names in the below image.
As said above, in the below image while giving the values interactively after
giving the insert query, the value given for the third column ‘age’ is of
character type while the datatype for that column “age” is number-integer,
therefore we get error as shown below. The interpreter matches the values based
on the position. i.e., the first value with the datatype of first column, the second
value with the datatype of second column and so on.. (Note: this method of
matching is also followed in Method 1 )