ikd312-07-ddl

580 views
491 views

Published on

Published in: Technology, Business
0 Comments
0 Likes
Statistics
Notes
  • Be the first to comment

  • Be the first to like this

No Downloads
Views
Total views
580
On SlideShare
0
From Embeds
0
Number of Embeds
11
Actions
Shares
0
Downloads
10
Comments
0
Likes
0
Embeds 0
No embeds

No notes for slide

ikd312-07-ddl

  1. 1. Data Definition Language 24 November 2011
  2. 2. Bonus Time! Tampilkan gaji minimum, gaji maksimum, jumlah gaji, dan rata-rata gaji untuk MIN(SALAR MAX(SALA SUM(SALA AVG(SALAR Y) RY) RY) Y) setiap jenis pekerjaan. 4200 12008 9000 12008 28800 12008 5760 12008 SELECT MIN(salary), MAX(salary), SUM(salary), 8300 8300 8300 8300 5800 8200 36400 7280 AVG(salary) 11000 11000 11000 11000 FROM employees 4400 4400 4400 4400 17000 17000 34000 17000 GROUP BY job_id; 2500 4200 64300 3215 6900 9000 39600 7920 19 rows retrieved 12008 12008 12008 12008 2500 3100 13900 2780 only 1 if GROUP BY clause 10500 13000 14000 13000 61000 13000 12200 13000 10000 10000 10000 10000 ommitted 24000 6100 24000 11500 24000 250500 24000 8350 6000 6000 6000 6000 2100 3600 55700 2785 6500 6500 6500 6500
  3. 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. 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. 5. Tabel MoviemID Title Year Director101 Gone with the Wind 1939 Victor Fleming102 Star Wars 1977 George Lucas103 The Sound of Music 1965 Robert Wise104 E.T. 1982 Steven Spielberg105 Titanic 1997 James Cameron106 Show White 1937107 Avatar 2009 James Cameron108 Raiders of the Lost Ark 1982 Steven Spielberg Kalkulus Relasional 5
  6. 6. Tabel ReviewerrID Name201 Sarah Martinez202 Daniel Lewis203 Brittany Harris204 Mike Anderson205 Chris Jackson206 Elizabeth Thomas207 James Cameron208 Ashley White Kalkulus Relasional 6
  7. 7. rID mID Stars RatingDate 201 101 2 2011-01-22Tabel Rating 201 101 4 2011-01-27 202 106 4 203 103 2 2011-01-20 203 108 4 2011-01-12 201 108 2 2011-01-30 204 101 3 2011-01-09 205 103 3 2011-01-27 205 104 2 2011-01-22 205 108 4 206 107 3 2011-01-15 206 106 5 2011-01-19 207 107 5 2011-01-20 208 104 3 2011-01-02 Kalkulus Relasional 7
  8. 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. 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. 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  CHECKSlide 8-10
  11. 11. Tipe Data CHAR(n) VARCHAR(n)  Oracle: VARCHAR2(n) INTEGER  SQLite: INT  NUMBER(n[,d])  NUM(n[,d]) DECIMAL Kalkulus Relasional 11
  12. 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. 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. 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. 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. 16. Bonus Time: Tabel MoviemID Title Year Director101 Gone with the Wind 1939 Victor Fleming102 Star Wars 1977 George Lucas103 The Sound of Music 1965 Robert Wise104 E.T. 1982 Steven Spielberg105 Titanic 1997 James Cameron106 Show White 1937107 Avatar 2009 James Cameron108 Raiders of the Lost Ark 1982 Steven Spielberg Kalkulus Relasional 16
  17. 17. Bonus Time: CREATE TABLEMovieCREATE TABLE Movie ( mID INT, title VARCHAR(20), year NUMBER(4), director VARCHAR(20)); Kalkulus Relasional 17
  18. 18. Tabel ReviewerrID Name201 Sarah Martinez202 Daniel Lewis203 Brittany Harris204 Mike Anderson205 Chris Jackson206 Elizabeth Thomas207 James Cameron208 Ashley White Kalkulus Relasional 18
  19. 19. rID mID Stars RatingDate 201 101 2 2011-01-22Tabel Rating 201 101 4 2011-01-27 202 106 4 203 103 2 2011-01-20 203 108 4 2011-01-12 201 108 2 2011-01-30 204 101 3 2011-01-09 205 103 3 2011-01-27 205 104 2 2011-01-22 205 108 4 206 107 3 2011-01-15 206 106 5 2011-01-19 207 107 5 2011-01-20 208 104 3 2011-01-02 Kalkulus Relasional 19
  20. 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. 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. 22. REFERENTIAL INTEGRITYOPTIONS  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. 23. REFERENTIAL INTEGRITYOPTIONS (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. 24. Additional Data Types in SQL2and 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 componentsSlide 8-24
  25. 25. Additional Data Types in SQL2and 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 valueSlide 8-25
  26. 26. Pustaka Elmasri dan Navathe, "Foundation of Database System" http://tjerdastangkas.blogspot.com/search/label/ikd312 Kalkulus Relasional 26
  27. 27. Kamis, 24 November 2011

×