Presentasi sedang didownload. Silahkan tunggu

Presentasi sedang didownload. Silahkan tunggu

DATABASE ADMINISTRATION

Presentasi serupa


Presentasi berjudul: "DATABASE ADMINISTRATION"— Transcript presentasi:

1 DATABASE ADMINISTRATION
Pertemuan ke-11

2 Database Schema Objects
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

3 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,

4 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

5 Table Creation 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

6 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

7 CREATE TABLE 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

8 CREATE TABLE Syntax 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], (……[ ] );

9 Example create table CREATE TABLE cd_collection (cd_number NUMBER(2), title VARCHAR2(14), artist VARCHAR2(13), purchase_date DATE DEFAULT SYSDATE);

10 Creating a Table Using a Subquery
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

11 Creating a Table Using a Subquery 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

12 Creating a Table Using a Subquery Examples
CREATE TABLE dept80 AS SELECT employee_id, last_name, salary*12 ANNSAL, hire_date FROM employees WHERE department_id = 80;

13 Creating a Table Using a Subquery
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

14 Data Types 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

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

16 Common Data Types For date and time values:
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).

17 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 down to –

18 DATE-TIME Data Types The DATE data type stores a value of centuries down to whole seconds, but cannot store fractions of a second. ’21/Aug/ :25:30’ is a valid value, but ’21/Aug/ :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.

19 ALTER TABLE Digunakan untuk : Add a new column
Modify an existing column Define a DEFAULT value for a column Drop a column

20 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

21 ALTER TABLE: Adding a Column
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));

22 ALTER TABLE: Modifying a Column
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

23 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

24 ALTER TABLE: Modifying a Column Example
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 ?

25 ALTER TABLE mod_emp MODIFY (last_name VARCHAR2(30)); MODIFY (salary NUMBER(8,2) DEFAULT 50); MODIFY (salary NUMBER(10,2)); MODIFY (last_name VARCHAR2(10));

26 ALTER TABLE: Dropping a Column
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

27 ALTER TABLE: Dropping a Column (cont.)
SQL Syntax: ALTER TABLE tablename DROP COLUMN column name; For Example: ALTER TABLE copy_f_staffs DROP COLUMN manager_target;

28 DROP TABLE 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

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

30 FLASHBACK TABLE 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;

31 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

32 The USER_RECYCLEBIN view can be queried like all other data dictionary views:
SELECT original_name, operation, droptime FROM user_recyclebin

33 RENAME 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.

34 constraints 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

35 Cara pembuatan 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));

36 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 _address would be named clients_ _uk

37 CREATE TABLE clients (client_number NUMBER(4) CONSTRAINT clients_client_num_pk PRIMARY KEY, last_name VARCHAR2(13) NOT NULL, VARCHAR2(80));

38 Constraints (cont) Constrain table level : CREATE TABLE clients (
client_number NUMBER(6) NOT NULL, first_name VARCHAR2(20), last_name VARCHAR2(20), phone VARCHAR2(20), VARCHAR2(10) NOT NULL, CONSTRAINT clients_phone_ _uk UNIQUE ( ,phone));

39 Tipe constraint 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 self-reference. 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.

40 Beberapa aturan pembuatan constraint
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

41


Download ppt "DATABASE ADMINISTRATION"

Presentasi serupa


Iklan oleh Google