3. Bonus Time!
Tampilkan jumlah orang yang memiliki jenis
pekerjaan yang sama.
COUNT(JOB_ID) JOB_ID
SELECT COUNT(job_id), 1 AC_ACCOUNT
1 AC_MGR
job_id 1 AD_ASST
FROM employees 1
2
AD_PRES
AD_VP
GROUP BY job_id; 5 FI_ACCOUNT
1 FI_MGR
19 rows retrieved, sesuai jenis 1
5
HR_REP
IT_PROG
1 MK_MAN
pekerjaan yang ada dalam 1
1
MK_REP
PR_REP
tabel Employees 5
1
PU_CLERK
PU_MAN
5 SA_MAN
(field JOB_ID) 30
20
SA_REP
SH_CLERK
20 ST_CLERK
Kalkulus Relasional 3
5 ST_MAN
4. Bonus Time!
Buat kueri yang menampilkan selisih antara gaji
maksimum dengan gaji minimum. Beri label
“Perbedaan Gaji”.
SELECT MAX(salary) - MIN(salary) AS "Perbedaan
gaji"
FROM employees;
Perbedaan gaji
21900
Kalkulus Relasional 4
5. Tabel Movie
mID Title Year Director
101 Gone with the Wind 1939 Victor Fleming
102 Star Wars 1977 George Lucas
103 The Sound of Music 1965 Robert Wise
104 E.T. 1982 Steven Spielberg
105 Titanic 1997 James Cameron
106 Show White 1937
107 Avatar 2009 James Cameron
108 Raiders of the Lost Ark 1982 Steven Spielberg
Kalkulus Relasional 5
6. Tabel Reviewer
rID Name
201 Sarah Martinez
202 Daniel Lewis
203 Brittany Harris
204 Mike Anderson
205 Chris Jackson
206 Elizabeth Thomas
207 James Cameron
208 Ashley White
Kalkulus Relasional 6
8. Where do They Come From?
Kenapa terbagi ke dalam tiga tabel?
Kenapa masing-masing tabel memiliki jumlah kolom
tertentu?
Kenapa urutan kolomnya seperti itu?
Kalkulus Relasional 8
9. Data Integrity dan Consistency
Bagaimana jika ada judul film yang berubah?
Bagaimana jika ada data reviewer yang dihapus?
Bagaimana jika ada reviewer yang iseng mereview film
yang tidak pernah ada?
Kalkulus Relasional 9
10. Data Definition, Constraints,
and Schema Changes
Used to CREATE, DROP, and ALTER the descriptions
of the tables (relations) of a database
Constraint-ku ada lima,
macam-macam jenisnya
PRIMARY KEY
FOREIGN KEY
UNIQUE
NOT NULL
CHECK
Slide 8-10
11. Tipe Data
CHAR(n)
VARCHAR(n)
Oracle: VARCHAR2(n)
INTEGER
SQLite: INT
NUMBER(n[,d])
NUM(n[,d])
DECIMAL
Kalkulus Relasional 11
12. Tipe Data
DATE
TIME
TIMESTAMP
ISO Format
'YYYY/MM/DD'
Easily sortable
20/10/1984, 28/06/1980, 29/02/1980, 30/11/1982
1980/02/29, 1980/06/28, 1982/11/30, 1984/10/20
No ambiguity
12/11/1980: Dec or Nov?
Kalkulus Relasional 12
13. CHAR vs VARCHAR
Fixed length
Lebih cepat diakses
Boros space
Variable length
Lebih hemat ruang
Lebih lambat diakses (diatasi dengan INDEX)
Kalkulus Relasional 13
14. CREATE TABLE
Specifies a new base relation by giving it a name,
and specifying each of its attributes and their data
types (INTEGER, FLOAT, DECIMAL(i,j),
CHAR(n), VARCHAR(n))
A constraint NOT NULL may be specified on an
attribute
CREATE TABLE DEPARTMENT
( DNAME VARCHAR(10) NOT NULL,
DNUMBER INTEGER NOT NULL,
MGRSSN CHAR(9),
MGRSTARTDATE CHAR(9) );
Slide 8-14
15. CREATE TABLE
In SQL2, can use the CREATE TABLE command for
specifying the primary key attributes, secondary keys, and
referential integrity constraints (foreign keys).
Key attributes can be specified via the PRIMARY KEY and
UNIQUE phrases
CREATE TABLE DEPT
( DNAME VARCHAR(10) NOT NULL,
DNUMBER INTEGER NOT NULL,
MGRSSN CHAR(9),
MGRSTARTDATE CHAR(9),
PRIMARY KEY (DNUMBER),
UNIQUE (DNAME),
FOREIGN KEY (MGRSSN) REFERENCES EMP );
Slide 8-15
16. Bonus Time: Tabel Movie
mID Title Year Director
101 Gone with the Wind 1939 Victor Fleming
102 Star Wars 1977 George Lucas
103 The Sound of Music 1965 Robert Wise
104 E.T. 1982 Steven Spielberg
105 Titanic 1997 James Cameron
106 Show White 1937
107 Avatar 2009 James Cameron
108 Raiders of the Lost Ark 1982 Steven Spielberg
Kalkulus Relasional 16
17. Bonus Time: CREATE TABLE
Movie
CREATE TABLE Movie (
mID INT,
title VARCHAR(20),
year NUMBER(4),
director VARCHAR(20)
);
Kalkulus Relasional 17
18. Tabel Reviewer
rID Name
201 Sarah Martinez
202 Daniel Lewis
203 Brittany Harris
204 Mike Anderson
205 Chris Jackson
206 Elizabeth Thomas
207 James Cameron
208 Ashley White
Kalkulus Relasional 18
20. DROP TABLE
Used to remove a relation (base table) and its
definition
The relation can no longer be used in queries, updates,
or any other commands since its description no longer
exists
Example:
DROP TABLE DEPENDENT;
Slide 8-20
21. ALTER TABLE
Used to add an attribute to one of the base relations
The new attribute will have NULLs in all the tuples of the
relation right after the command is executed; hence, the
NOT NULL constraint is not allowed for such an attribute
Example:
ALTER TABLE EMPLOYEE ADD JOB VARCHAR(12);
The database users must still enter a value for the new
attribute JOB for each EMPLOYEE tuple. This can be done
using the UPDATE command.
Slide 8-21
22. REFERENTIAL INTEGRITY
OPTIONS
We can specify RESTRICT, CASCADE, SET NULL or SET
DEFAULT on referential integrity constraints (foreign keys)
CREATE TABLE DEPT
( DNAME VARCHAR(10) NOT NULL,
DNUMBER INTEGER NOT NULL,
MGRSSN CHAR(9),
MGRSTARTDATE CHAR(9),
PRIMARY KEY (DNUMBER),
UNIQUE (DNAME),
FOREIGN KEY (MGRSSN) REFERENCES EMP
ON DELETE SET DEFAULT ON UPDATE CASCADE );
Slide 8-22
23. REFERENTIAL INTEGRITY
OPTIONS (continued)
CREATE TABLE EMP
( ENAME VARCHAR(30) NOT NULL,
ESSN CHAR(9),
BDATE DATE,
DNO INTEGER DEFAULT 1,
SUPERSSN CHAR(9),
PRIMARY KEY (ESSN),
FOREIGN KEY (DNO) REFERENCES DEPT
ON DELETE SET DEFAULT ON UPDATE CASCADE,
FOREIGN KEY (SUPERSSN) REFERENCES EMP
ON DELETE SET NULL ON UPDATE CASCADE );
Slide 8-23
24. Additional Data Types in SQL2
and SQL-99
Has DATE, TIME, and TIMESTAMP data types
DATE:
Made up of year-month-day in the format yyyy-mm-dd
TIME:
Made up of hour:minute:second in the format hh:mm:ss
TIME(i):
Made up of hour:minute:second plus i additional digits
specifying fractions of a second
format is hh:mm:ss:ii...i
TIMESTAMP:
Has both DATE and TIME components
Slide 8-24
25. Additional Data Types in SQL2
and SQL-99 (cont.)
INTERVAL:
Specifies a relative value rather than an absolute
value
Can be DAY/TIME intervals or YEAR/MONTH
intervals
Can be positive or negative when added to or
subtracted from an absolute value, the result is an
absolute value
Slide 8-25
26. Pustaka
Elmasri dan Navathe, "Foundation of Database System"
http://tjerdastangkas.blogspot.com/search/label/ikd312
Kalkulus Relasional 26