Friday 29 April 2016

INSERTING ROWS INTO A TABLE


Now, let us see how to insert rows into COURSES table. SQL command INSERT is used to insert new row into the table. While inserting rows, you may enter value for each column of the table or selected columns. The following command inserts a row into COURSES table.

insert into courses
 values('ora','Oracle database',25,4500,'Knowledge of Windows');

Note:After inserting the required row, issues COMMIT command to make sure the changes are made permanent. We will discuss more about COMMIT command later in this book but for the time being it is sufficient to know that COMMIT command will make changes permanent. Without COMMIT, rows that are inserted might be lost if there is any power failure.

During insertion, character values are enclosed in single quotes. Unless otherwise specified we have to supply a value for each column of the table. If the value of any column is not known or available then you can give NULL as the value of the column. For example, the following insert will insert a new row with null value for PREREQUISITE column.


insert into courses
values('c','C Programming',25,3000,null);

Note: INSERT command can insert only one row at a time. For multiple row, INSERT command must be issued for multiple times.
DATE type values must be in the format DD-MON-YY or DD-MON-YYYY, where MON is the first three letters of the month (Jan, Feb). If only two digits are given for year then current century is used. For example, if you give 99 for year, Oracle will take it as 2099 as the current century is 2000. So it is important to remember this and give four digits if required.
The following is the complete syntax for INSERT command.
INSERT INTO tablename [(columns list)]
 {VALUES (value-1,...) | subquery }
We will see how to insert row into a table using a subquery later in this blog
Inserting a row with selected columns 
It is possible to insert a new row by giving values only for a few columns instead of giving values for all the available columns
The following INSERT command will insert a new row only two values.
insert into courses(ccode,name)
 values ('odba','Oracle Database Administration');
 The above command will create a new row in COURSES table with values for only two columns – CCODE and NAME. The remaining columns will take NULL value or the default value, if the column is associated with default value. We will discuss more about default value in the next blog


NULL value :

Null value means a value that is not available or not known. When a column’s value is not known then we store NULL value into the column. NULL value is neither 0 nor blank nor any other known value. We have already seen how to store null value into a column and when Oracle automatically stores null value into a column. We will discuss more about how to process null value later in this chapter.

No comments:

Post a Comment