Modul 12a
Modul 12a
MODUL PERKULIAHAN
F062100006 -
Pemrograman
PL/SQL
Insert Statement
12
Andi Nugroho
Ilmu Komputer Sistem Informasi
Using Group By and Having Clauses
Objectives
This lesson covers the following objectives:
• Explain the importance of being able to alter the data in a database
• Construct and execute INSERT statements which insert a single row using a
VALUES clause
• Construct and execute INSERT statements that use special values, null values,
and date values
• Construct and execute INSERT statements that copy rows from one table to
another using a subquery
Purpose
For Example :
To verify and view the copy of the table, use the following DESCRIBE and
SELECT statements:
Insert
• Another way to insert values in a table is to implicitly add them by omitting the
column names.
• One precaution: the values for each column must match exactly the default
order in which they appear in the table (as shown in a DESCRIBE statement),
and a value must be provided for each column.
• The INSERT statement in this example was written without explicitly naming
the columns.
• For clarity, however, it is best to use the column names in an INSERT clause.
• Before inserting data into a table, you must check several table details.
• The DESCRIBE tablename statement will return a description of the table
structure in the table summary chart.
• COPY_DEPARTMENTS TABLE SUMMARY:
Table Summary
• As shown in the example, the table summary provides information about each
column in the table, such as:
– the allowance of duplicate values
– the type of data allowed
– the amount of data allowed
– the allowance of NULL values
• The INSERT statement need not specify every column— the Nullable columns
may be excluded.
• If every column that requires a value is assigned a value, the insert works.
• In our example, the EMAIL column is defined as a NOT NULL column.
• An implicit attempt to add values to the table as shown would generate an
error.
• Special values such as SYSDATE and USER can be entered in the VALUES
list of an INSERT statement.
• SYSDATE will put the current date and time in a column.
• USER will insert the current session's username, which is
OAE_PUBLIC_USER in Oracle Application Express.
• This example adds USER as the last name, and SYSDATE for hire date.
• Each INSERT statement we have seen so far adds only one row to the table.
• But suppose we want to copy 100 rows from one table to another.
• We do not want to have to write and execute 100 separate INSERT
statements, one after the other.
• That would be very time-consuming.
• Fortunately, SQL allows us to use a subquery within an INSERT statement.
• All the results from the subquery are inserted into the table.
• So we can copy 100 rows – or 1000 rows – with one multiple-row subquery
within the INSERT.
• As you would expect, you don't need a VALUES clause when using a subquery
to copy rows because the inserted values will be exactly the values returned by
the subquery.
• In the example shown, a new table called SALES_REPS is being populated
with copies of some of the rows and columns from the EMPLOYEES table.
• The WHERE clause is selecting those employees that have job IDs like '%REP
%'.
• The number of columns and their data types in the column list of the INSERT
clause must match the number of columns and their data types in the
subquery.
• The subquery is not enclosed in parentheses as is done with the subqueries in
the WHERE clause of a SELECT statement.
• Again, this will work only if both tables have the same number of columns with
matching data types, and they are in the same order
Terminology
Key terms used in this lesson included:
• INSERT INTO
• USER
• Transaction
• Explicit
Summary
• In this lesson, you should have learned how to:
• Explain the importance of being able to alter the data in a database
• Construct and execute INSERT statements which insert a single row using a
VALUES clause
• Construct and execute INSERT statements that use special values, null values,
and date values
• Construct and execute INSERT statements that copy rows from one table to
another using a subquery