0% found this document useful (0 votes)
34 views13 pages

Insert Command in Detail

In this document, various methods of inserting new record/records in a table with regard to MySQL and Oracle SQL Plus database are discussed

Uploaded by

SAVAN MILAN
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
34 views13 pages

Insert Command in Detail

In this document, various methods of inserting new record/records in a table with regard to MySQL and Oracle SQL Plus database are discussed

Uploaded by

SAVAN MILAN
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 13

Inserting records in SQL

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);

Consider the following table is already created – Table Name = “Student”


Eg. (considering the above given table – Student)
insert into student values (1003, ‘Madan’, 12);
MySql :

Record got inserted into


the table

Oracle – SQL Plus :


We can even insert multiple records using a single insert statement in the
following way
Eg.
insert into student values (1004, 'Karn', 14) , ( 1005, 'Sujash', 11),
(1006, 'Karthiga', 12);
MySQL :

Three Records got inserted


into the table

Oracle – SQL plus :


The above query to insert multiple records will not work in Oracle as it is not in
their Oracle syntax.

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

Number of columns in the table – “Student” is 3, but the values given in


insert command is 2, so the error occurs
Oracle SQL Plus will also produce error when the above insert query in the
image is given.
MySQL allows to have no values in the parenthesis which when executed will
add “null” to all the columns if no default value is provided, if any column has
been set default value then that value occurs for that column.

No values provided

After executing the above insert


command, the “null” value got
inserted for all the columns in
the record

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”

Eg. (considering the above given table – Student)


insert into student (id, name, age) values (1003, ‘Suman’, 12);

column name list corresponding column values


MySQL:

Record got inserted into


the table
Oracle – SQL Plus
In this method of inserting records, we could even skip giving column name and
its corresponding value, consider
Eg:
insert into student (id, name) values (1004, ‘Vanitha’) ;

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 :

Default value added

Oracle (SQL Plus) - In oracle the above query works similarly as in


MySQL
We could also insert multiple records using single insert statement with this
method of insert query

Eg:
insert into student (id, name, age) values (1006, ‘Aarushi’, 14) ,
(1007, ‘Gowtham’, 11);

MySQL:

Two records got inserted into


the table

Oracle – SQL Plus

As seen previously, the above command of inserting multiple records using


single SQL statement will not work in Oracle, it will produce error.
Note:

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

List of columns in the


table

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

Column names Values

Above scenario works similarly in both MySQL and Oracle – SQL


Method 3: (Oracle Specific) - will not work in MySQL
Interactively getting values from the user.

Simple Syntax:

insert into table_name values(&column1_name, &column2_name,....


&columnN_name);

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:

Table before executing the insert query

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.

insert into student values (&id, &name, &age);

Record got inserted into the table

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.

Record got inserted into the table


Note : While inserting values interactively, they should match the column data
type for each corresponding column in the table. If they do not match then as
logically you ll get error.

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 )

You might also like