Oracle db dapat mengandung banyak tipe objek  beberapa objek utama database:  › › › › › ›  Table Index Constraint View Sequence Synonym Ada objek yang dapat berdiri sendiri, ada yang bergantung dengan keberadaan.

Download Report

Transcript Oracle db dapat mengandung banyak tipe objek  beberapa objek utama database:  › › › › › ›  Table Index Constraint View Sequence Synonym Ada objek yang dapat berdiri sendiri, ada yang bergantung dengan keberadaan.

Oracle db dapat mengandung banyak tipe objek
 beberapa objek utama database:

›
›
›
›
›
›

Table
Index
Constraint
View
Sequence
Synonym
Ada objek yang dapat berdiri sendiri, ada yang
bergantung dengan keberadaan objek lain
•
•
•
•
•
beberapa objek memerlukan space atau storage,
ada yang tidah memerlukan.
objek db yang memerlukan storage yang signifikan
dikenal dengan nama segmen
contoh segmen adalah tables dan index
setiap nilai pada objek tersebut disimpan dalam
kolom pada tiap barisnya, sehingga memerlukan
space fisik
views, constrain, sequence dan sysnonim adalah
beberapa objek yang tidak memerlukan space besar,
Db menyimpan definisi dari semua db objek
pada data dictionary
 Contoh : jika ada query select * from
employees, maka db akan mengembalikan
employee_id, last_name,dll
 Db mencari kolom apa saja pada table
employees di data dictionary
 Data dictionary tersebut dapat diakses oleh
seluruh user (yang berhak) dan dapat diakses
dengan sql plus maupun oracle objek browser


semua data di DBMS disimpan di dalam table,
cara penamaan dalam table harus sesuai dengan
kaidah :
› Must begin with a letter
› Must be 1 to 30 characters long
› Must contain only A - Z, a - z, 0 - 9, _ (underscore), $,
and #
› Must not duplicate the name of another object
owned by the same user
› Must not be an Oracle Server reserved word





Nama table harus menjelaskan tentang isi table
Contoh untuk table yang menyimpan data
employee, maka beri nama employees, atau
pegawai jangan peoples atau orang
Nama juga tidak case sensitive, jadi employees dan
EmployeEs sama, isi data yang case sensitive
Pembuatan table adalah bagian dari Data Definition
language (DDL)
Penggunaan DDL yang lain adalah untuk change,
remove structure table dengan ALTER,DROP, dll
Untuk membuat table baru, user harus memiliki hak CREATE
TABLE dan memiliki quota storage pada tablespace
 DBA dapat menggunakan Data Control Language (DCL) untuk
memeberik privileg pada user dan memberi quota
tablespace
 Table milik user satu tidak ada dalam schema user yang lain,
sehingga user tidak dapat mengakses table user lain
 jika user memiliki hak untuk melihat table user lain, maka
user dapat menggunakan sintaks

SELECT * FROM nama_user.nama_table
 Contoh

SELECT * FROM hr.employees






To create a new table, use the following syntax
details:
table is the name of the table
column is the name of the column
datatype is the column's data type and length
DEFAULT expression specifies a default value if a
value is omitted in the INSERT statement
CREATE TABLE table
(column datatype [DEFAULT expression],
(column datatype [DEFAULT expression],
(……[ ] );
CREATE TABLE cd_collection
(cd_number NUMBER(2),
title VARCHAR2(14),
artist VARCHAR2(13),
purchase_date DATE DEFAULT SYSDATE);





Cara kedua untuk create table adalah create table
yang mengimplementasikan subquery clause
Table ada di buat berdasarkan hasil dari subquery
Hanya datatype dan NOT NULL constrain yang akan
didapatkan di table yang dibuat dengan subquery
Hal ini karena bisa jadi table yang baru memiliki
konteks yang berbeda dengan table awal
Sehingga bisa jadi penggunaan PK dan FK juga
berbeda

Syntax:
CREATE TABLE tablename
[(column, column, …)]
AS subquery;
Note: The column list is optional unless you want
the column names to differ from the subquery
column names
CREATE TABLE dept80
AS
SELECT employee_id, last_name, salary*12 ANNSAL, hire_date
FROM employees
WHERE department_id = 80;

Beberapa hal yang harus diperhatikan :
› Nama kolom di table baru identik dengan tabel
asal, kecuali subquery menggunakan alias
› Tipe data table baru identik dengan table awal
› Hanyak constrain NOT NULL yang dicopy ke table
baru






jika seseorang keluar dari negaranya, maka ia akan
membutuhkan matauang negara yang dituju untuk
bertransaksi
Analogi ini mirip penggunaan datatype di SQL.
Tipe data yang berbeda memiliki perbedaan karakter
dan tujuan
Hal ini agar penggunaan db lebih efisien
Setiap nilai yang dimanipulasi oleh oracle memiliki tipe
data
Setiap tipe data memiliki karakter yang berbeda,
sehingga cara memperlakukan setiap nilai berbeda juga

Different data types offer several advantages:
› Columns of a single type produce consistent results.
For example, DATE data type columns always produce
date values.
› You cannot insert the wrong type of data into a
column. For example, columns of data type DATE will
prevent NUMBER type data from being inserted.
› For these reasons, each column in a relational
database can hold only one type of data. You cannot
mix data types within a column.

Tipe data yang paling sering digunakan :

Untuk character values:
› CHAR (fixed size, maximum 2000 characters);
› VARCHAR2 (variable size, maximum 4000 characters);
› CLOB (variable size, maximum 128 terabytes)

For number values:
› NUMBER (variable size, maximum precision 38 digits)

For date and time values:
› DATE, TIMESTAMP …., INTERVAL

For binary values (eg. multimedia: JPG, WAV, MP3, and
so on): RAW (variable size, maximum 2000 bytes); BLOB
(variable size, maximum 128 terabytes).
For character values, it is usually better to use
VARCHAR2 or CLOB than CHAR, because it saves
space.
 For example, an employee’s last name is ‘Chang’. In
a VARCHAR2(30) column, only the 5 significant
characters are stored: C h a n g. But in a CHAR(30)
column, 25 trailing spaces would be stored as well,
to make a fixed size of 30 characters.
 Number values can be negative as well as positive.
For example, NUMBER(6,2) can store any value from
+9999.99 down to –9999.99.

The DATE data type stores a value of centuries down
to whole seconds, but cannot store fractions of a
second. ’21/Aug/2003 17:25:30’ is a valid value, but
’21/Aug/2003 17:25:30.255’ is not.
 The TIMESTAMP data type is an extension of the
DATE data type which allows fractions of a second.
 For example, TIMESTAMP(3) allows 3 digits after the
whole seconds, allowing values down to
milliseconds to be stored.

Digunakan untuk :
 Add a new column
 Modify an existing column
 Define a DEFAULT value for a column
 Drop a column
Dengan alter table, dba dapat menambah atau
memodifikasi table
 tetapi tidak dapat spesifik menentukan dimana
kolom akan tampil
 Kolom baru biasanya diletakkan di akhir table
 Dan jika table baru sudah berisi beberapa baris
data, kolom barus harus diinisiasi null

 Syntax
ALTER TABLE tablename
ADD (column name datatype [DEFAULT expression],
column name datatype [DEFAULT expression], ...
 Example
ALTER TABLE copy_f_staffs
ADD (e_mail_address VARCHAR2(80));
Modifikasi kolom dapat digunakan untuk :
mengubah tipe data, ukuran dan default value
 Aturan terkait modifikasi kolom adalah:

- Dba dapat menambah lebar dari kolom numerik
- Dba dapat menambah lebar dari kolom karakter
- dba dapat mengurangi ukuran kolom hanya jika
kolom tidak berisi data
- jika kolom berisi data, maka dba dapat memperlebar
value hingga ke nilai terbesar
Dba hanya bisa mengubah tipe data pada kolom
yang berisi null values/kosong
 Dba dapat mengubah CHAR to VARCHAR2 atau
sebaliknya jika kolom berisi null values atau jika
ukuran tidak lebih kecil dari isi data yang ada


Example: a table has been created with two
columns:
CREATE TABLE mod_emp
(
last_name VARCHAR2(20),
salary NUMBER(8,2)
);
 Dibawah ada beberapa perubahan kolom,
mana yang diperbolehkan ?
ALTER TABLE mod_emp
MODIFY (last_name VARCHAR2(30));
ALTER TABLE mod_emp
MODIFY (salary NUMBER(8,2) DEFAULT 50);
ALTER TABLE mod_emp
MODIFY (salary NUMBER(10,2));
ALTER TABLE mod_emp
MODIFY (last_name VARCHAR2(10));

Beberapa aturan terkait drop kolom
› Hanya satu kolom yang dapat di drop sekali sintaks
› Tidak dapat menghapus semua kolom dalam table,
minimal harus disisakan satu kolom
› Jika kolom di drop, maka data akan hilang dan tidak
dapat dikembalikan

SQL Syntax:
ALTER TABLE tablename
DROP COLUMN column name;

For Example:
ALTER TABLE copy_f_staffs
DROP COLUMN manager_target;
Drop table statemen akan menghapus definisi dari
table oracle
 Table akan dihapus beserta isi dan index yang ada di
dalamnya
 Beberapa isu terkait drop table

- Semua data akan dihapus dari table
- Deskripsi table akan dihapus dari data dictionary
- Oracle tidak akan memberikan pertanyaan apa table mau
di drop atau tidak,
- tetapi table dapat di kembalikan meski tidak dijamin
berhasil
Only the creator of the table or a user with DROP
ANY TABLE privilege (usually only the DBA) can
remove a table.
 SINTAKS

DROP TABLE tablename

EXAMPLE
DROP TABLE copy_f_staffs;
Jika drop table karena kesalahan, dba dapat
mengembalikan table dan data kembali
 Setiap user db memiliki recycle bin, untuk
menyimpan objek yang dihapus user, dan dapat
dikembalikan dengan sintaks FLASHBACK TABLE
 Sintaks ini untuk restore sebuah table, views
atau index yang terhapus, sintaksnya
 FLASHBACK TABLE tablename TO BEFORE DROP;


For example, if you drop the EMPLOYEES table in
error, you can restore it by simply issuing the
command:
› FLASHBACK TABLE employees TO BEFORE DROP;
As the owner of a table, you can issue the flashback
command, and if the table that you are restoring
had any indexes, then these are also restored.
 It is possible to see which objects can be restored by
querying the data dictionary view USER_RECYCLEBIN


The USER_RECYCLEBIN view can be queried like
all other data dictionary views:
› SELECT original_name, operation, droptime
› FROM user_recyclebin
To change the name of a table, use the RENAME
 statement. This can be done only by the owner of
 the object or by the DBA.
 Syntax:

› RENAME old_name to new_name;

Example:
› RENAME copy_f_staffs to copy_fastfood_staffs;
We will see later that we can rename other types of
 objects such as views, sequences, and synonyms.

Database rules
 Semua constrain disimpan dalam data dictionary
 Constrain menjaga table dari delete data jika
berkaitan dengan table lain
 Constrain menjaga integrasi data dari proses
yang terkait dengan data manipulation language
(DML) seperti insert, update, delete


Ada dua cara pembuatan constrain
› Level kolom  constraint ditulis pada kolom, setelah
nama dan tipe data
› Level table  constrain ditulis pada akhir sintaks
pembuatan table, setelah semua kolom
› Contoh level kolom :
CREATE TABLE clients
(client_number NUMBER(4) CONSTRAINT
clients_client_num_pk PRIMARY KEY,
first_name VARCHAR2(14),
last_name VARCHAR2(13));

Nama konstrain bebas, tetapi dibatasi hanya 30
karakter, beberapa contoh penamaan :
› A primary key constraint on client_number would be
named clients_client_no_pk
› A not null constraint on last_name would be named
clients_last_name_nn
› A unique constraint on e-mail_address would be
named clients_email_uk
CREATE TABLE clients
(client_number NUMBER(4) CONSTRAINT
clients_client_num_pk PRIMARY KEY,
last_name VARCHAR2(13) NOT NULL,
email VARCHAR2(80));

Constrain table level :
CREATE TABLE clients (
client_number NUMBER(6) NOT NULL,
first_name VARCHAR2(20),
last_name VARCHAR2(20),
phone VARCHAR2(20),
email VARCHAR2(10) NOT NULL,
CONSTRAINT clients_phone_email_uk UNIQUE
(email,phone));
Constraint
Meaning
CHECK
Specifies the value for a column, or group of columns, must satisfy a
certain condition.
NOT NULL
Specifies a column doesn't allow storage of null values. This is actually
enforced as a CHECK constraint. You can check NOT NULL columns
using the DESCRIBE command.
PRIMARY KEY Specifies the primary key of a table. A primary key is made up of one
or more columns that uniquely identify each row in a table.
FOREIGN KEY Specifies a foreign key for a table. A foreign key references a column in
another table, or a column in the same table in the case of a selfreference.
UNIQUE
Specifies a column, or group of columns, can only store unique values.
CHECK
OPTION
Specifies that DML operations on a view must satisfy the subquery.
You'll learn about views later in the section “Views.”
READ ONLY
Specifies that a view may only be read from.
Contraint yang mengacu pada lebih satu kolom
(composite key) harus didefinisikan di akhir tabel
 NOT NULL constraint hanya dapat dispesifikkan
pada kolom level
 UNIQUE, PRIMARY KEY, FOREIGN KEY, and CHECK
dapat didefinisikan pada kolom atau table level
 Jika ada kata CONSTRAINT digunakan pada
statement CREATE TABLE, maka harus diberi
nama constrain
