0% found this document useful (0 votes)
16 views

Modul 12a

This document discusses using INSERT statements in SQL to add data to database tables. It covers inserting single rows, using special values like NULL and SYSDATE, and date formatting. The document also explains copying multiple rows between tables using a subquery.

Uploaded by

Mik Asa
Copyright
© © All Rights Reserved
Available Formats
Download as DOC, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
16 views

Modul 12a

This document discusses using INSERT statements in SQL to add data to database tables. It covers inserting single rows, using special values like NULL and SYSDATE, and date formatting. The document also explains copying multiple rows between tables using a subquery.

Uploaded by

Mik Asa
Copyright
© © All Rights Reserved
Available Formats
Download as DOC, PDF, TXT or read online on Scribd
You are on page 1/ 15

1

MODUL PERKULIAHAN

F062100006 -
Pemrograman
PL/SQL
Insert Statement

Abstrak Sub-CPMK (1,2,3)

Mahasiswa dapat membuat Mampu menginsert data dengan


statement insert dan kedalam basis data, dengan insdert
mengetahui fungsi dan singgle row, dengan special values,
tujuannya. date values

Fakultas Program Studi Tatap Muka Disusun Oleh

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

• Up to now, you have been learning how to access data in a database.


• It's time to learn how to make changes to the data in the database.
• In business, databases are dynamic.
• They are constantly in the process of having data inserted, updated, and
deleted.
• Think how many times the school's student database changes from day to day
and year to year.
• Unless changes are made, the database would quickly lose its usefulness.
• In this lesson, you will begin to use data manipulation language (DML)
statements to make changes to a database.

Copy Tables Before Inserting

• You will be responsible for altering tables in your schema.


• You will also be responsible for restoring them just as a real Database
Administrator would assume that responsibility.
• To keep your schema tables in their original state, you will make a copy of
each table before completing the practice activities in this and later lessons.
• In each practice activity, you will use the copy of the table that you create, not
the original.

2021 Nama Mata Kuliah dari Modul


2 Andi Nugroho
Biro Bahan Ajar E-learning dan MKCU
http://pbael.mercubuana.ac.id/
• If you inadvertently alter a table copy, you can use the original table to restore
the copy.
• You should name each copied table: copy_tablename.
• The table copies will not inherit the associated primaryto-foreign-key integrity
rules (relationship constraints) of the original tables.
• The column data types, however, are inherited in the copied tables.

Syntax to Create a Copy of a Table

Create Table syntax :

For Example :

To verify and view the copy of the table, use the following DESCRIBE and
SELECT statements:

Insert

2021 Nama Mata Kuliah dari Modul


3 Andi Nugroho
Biro Bahan Ajar E-learning dan MKCU
http://pbael.mercubuana.ac.id/
• The INSERT statement is used to add a new row to a table. The statement
requires three values:
– the name of the table
– the names of the columns in the table to populate
– corresponding values for each column
• How can we INSERT the data below to create a new department in the
copy_departments table?

• The syntax below uses INSERT to add a new department to the


copy_departments table.
• This statement explicitly lists each column as it appears in the table.
• The values for each column are listed in the same order.
– Note that number values are not enclosed in single quotation marks.

• 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.

2021 Nama Mata Kuliah dari Modul


4 Andi Nugroho
Biro Bahan Ajar E-learning dan MKCU
http://pbael.mercubuana.ac.id/
Check the Table First

• 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

2021 Nama Mata Kuliah dari Modul


5 Andi Nugroho
Biro Bahan Ajar E-learning dan MKCU
http://pbael.mercubuana.ac.id/
• Notice where the “Data Type” column is a character data type the “Length”
column specifies the maximum number of characters permitted.

2021 Nama Mata Kuliah dari Modul


6 Andi Nugroho
Biro Bahan Ajar E-learning dan MKCU
http://pbael.mercubuana.ac.id/
• First_name has data type VARCHAR2, and Length 20, this means that up to
20 characters can be entered for this column.

2021 Nama Mata Kuliah dari Modul


7 Andi Nugroho
Biro Bahan Ajar E-learning dan MKCU
http://pbael.mercubuana.ac.id/
• For Number data types the brackets specify the Precision and Scale.
• Precision is the total number of digits, and Scale is the number of digits to the
right of the decimal place.

2021 Nama Mata Kuliah dari Modul


8 Andi Nugroho
Biro Bahan Ajar E-learning dan MKCU
http://pbael.mercubuana.ac.id/
• The SALARY column allows numbers with a Precision of 8 and a Scale of 2.
• The maximum value allowed in this column is 999999.99.

2021 Nama Mata Kuliah dari Modul


9 Andi Nugroho
Biro Bahan Ajar E-learning dan MKCU
http://pbael.mercubuana.ac.id/
Inserting Rows With 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.

2021 Nama Mata Kuliah dari Modul


10 Andi Nugroho
Biro Bahan Ajar E-learning dan MKCU
http://pbael.mercubuana.ac.id/
• An implicit insert will automatically insert a null value in columns that allow
nulls.
• To explicitly add a null value to a column that allows nulls, use the NULL
keyword in the VALUES list.
• To specify empty strings and/or missing dates, use empty single quotation
marks (with no spaces between them like this '') for the missing data.

Inserting Spesial Values

• 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.

2021 Nama Mata Kuliah dari Modul


11 Andi Nugroho
Biro Bahan Ajar E-learning dan MKCU
http://pbael.mercubuana.ac.id/
Inserting Specific Date Values

• The default format model for date data types is DDMon-YYYY.


• With this date format, the default time of midnight (00:00:00) is also included.
• In an earlier section, we learned how to use the TO_CHAR function to convert
a date to a character string when we want to retrieve and display a date value
in a non-default format.
• Here is a reminder of TO_CHAR:

• Similarly, if we want to INSERT a row with a non-default format for a date


column, we must use the TO_DATE function to convert the date value (a
character string) to a date.

• A second example of TO_DATE allows the insertion of a specific time of day,


overriding the default time of midnight.

2021 Nama Mata Kuliah dari Modul


12 Andi Nugroho
Biro Bahan Ajar E-learning dan MKCU
http://pbael.mercubuana.ac.id/
Using A Subquery To Copy Rows

• 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.

2021 Nama Mata Kuliah dari Modul


13 Andi Nugroho
Biro Bahan Ajar E-learning dan MKCU
http://pbael.mercubuana.ac.id/
• If we want to copy all the data – all rows and all columns – the syntax is even
simpler.
• To select all rows from the EMPLOYEES table and insert them into the
SALES_REPS table, the statement would be written as shown:

• 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

2021 Nama Mata Kuliah dari Modul


14 Andi Nugroho
Biro Bahan Ajar E-learning dan MKCU
http://pbael.mercubuana.ac.id/
Daftar Pustaka

Alapati R SAM, Expert Oracle Database 11g Administration, Apress, 2009


Pribyl Bill, Feuerstein Steven, Learning Oracle PL/SQL, O’Reilly, 2001
Oracle academy
Oracle academy 2019

2021 Nama Mata Kuliah dari Modul


15 Andi Nugroho
Biro Bahan Ajar E-learning dan MKCU
http://pbael.mercubuana.ac.id/

You might also like