1 / 41

Introduction to Database SQL

Introduction to Database SQL. วรวิทย์ พูลสวัสดิ์. ฐานข้อมูล (Database). Data and its relation Databases are designed to offer an organized mechanism for storing, managing and retrieving information. They do so through the use of tables ข้อมูลที่มีความสัมพันธ์กันนำมาเก็บรวมรวมไว้ด้วยกัน

myron
Download Presentation

Introduction to Database SQL

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. Introduction to Database SQL วรวิทย์ พูลสวัสดิ์

  2. ฐานข้อมูล (Database) Data and its relation Databases are designed to offer an organized mechanism for storing, managing and retrieving information. They do so through the use of tables ข้อมูลที่มีความสัมพันธ์กันนำมาเก็บรวมรวมไว้ด้วยกัน - เพื่อการใช้ข้อมูลเหล่านั้นร่วมกันอย่างมีประสิทธิภาพ - โดยมากมักทำเพื่อตอบสนองการทำงานด้านสารสนเทศ 2

  3. ระบบจัดการฐานข้อมูล (Database Management System) System Software ที่ ใช้ในการจัดการข้อมูล จุดประสงค์เพื่อสร้างสภาพแวดล้อมที่สะดวกและมีประสิทธิภาพในการจัดเก็บและเข้าถึงข้อมูล ของฐานข้อมูล การเข้าถึง/ใช้ข้อมูล จะใช้ภาษาเชิงโครงสร้าง (Sql) ที่ทำหน้าที่เป็นตัวกลางระหว่างฐานข้อมูลกับผู้ใช้งาน ประกอบไปด้วยฟังก์ชันต่างๆช่วยให้ระบบฐานข้อมูลมีความถูกต้อง (Integrity) และมีความสอดคล้องกัน (Consistency) 3

  4. ภาษา SQL • เป็นภาษาที่ใช้ในการเข้าถึงข้อมูล ในระบบจัดการฐานข้อมูล • เป็นภาษาในแบบ Non Procedural • การทำงานพื้นฐาน อยู่บน Set และ Relation • แบ่งการทำงานเป็น 3 กลุ่มใหญ่ๆได้แก่ DDL / DML และ DCL

  5. กลุ่มของคำสั่งในภาษา SQL – DDL • Data Definition Language (DDL) เป็นกลุ่มคำสั่งที่ใช้ในการกำหนดโครงสร้างต่างๆในระบบจัดการฐานข้อมูล ตัวอย่างของคำสั่งได้แก่ Create , Alter , Drop

  6. ตัวอย่างการใช้ภาษา SQL แบบ DDL CREATE TABLE project01 ( proj_num varchar(2) NOT NULL, proj_name varchar(20) NOT NULL ) ; ALTER TABLE project01 ADD proj_status VARCHAR( 5 ) NOT NULL ; DROP TABLE project01 ; CREATE USER user01 IDENTIFIED BY "1234" ; DROP USER user01 ; 6

  7. กลุ่มของคำสั่งในภาษา SQL – DML • Data Manipulation Language ( DML) เป็นกลุ่มคำสั่งที่ใช้ในการเข้าถึงข้อมูล แบ่งเป็น 2 กลุ่มหลักๆคือ • กลุ่มที่ใช้ในการแสดงผลข้อมูล ได้แก่ คำสั่ง Select • กลุ่มที่ใช้ในการปรับปรุงเนื้อหาข้อมูล ได้แก่คำสั่ง Insert , Update , Delete

  8. ตัวอย่างการใช้ภาษา SQL แบบ DML INSERT INTO project ( proj_num, proj_name) VALUES (16, “Burapha") ; SELECT * FROM project WHERE proj_num = 16 ; UPDATE Project set proj_name = "Burapha Chant.“ WHERE proj_num = 16 ; DELETE FROM project where proj_num = 16 ; 8

  9. กลุ่มของคำสั่งในภาษา SQL – DCL • Data Control Language (DCL) • Grant , Revoke GRANT SELECT , INSERT , UPDATE , DELETE , ALTER ON demo TO user02; REVOKE ALL PRIVILEGES ON demo FROM user02 ;

  10. เทคนิคการใช้งานภาษา SQL เพื่อใช้ในการแสดงผลข้อมูล วิเคราะห์ output • ดูว่าจะต้องแสดงผลลัพธ์อย่างไรตามความต้องการของโจทย์ วิเคราะห์ input • ต้องใช้ข้อมูล (column) จากตาราง (Table) อะไร วิเคราะห์ process • จำ syntax หรือ รูปแบบของคำสั่งให้ถูกต้อง • วิเคราะห์ว่า ต้องเขียนเงื่อนไขอย่างไร จึงจะได้ผลตามต้องการ 10

  11. Syntax : รูปแบบคำสั่ง

  12. 1 - คำสั่งพื้นฐาน Select <filed_1 , filed_2 , … , filed_n | * > From <Table_name> ; • หากเลือกทุก column สามารถใช้เครื่องหมาย * ได้ ตัวอย่าง select * from employee ; select emp_no , emp_name , emp_lname from employee ; 12

  13. 2 – คำสั่งแบบมีเงื่อนไข (condition) Select <filed_1 , filed_2 , … , filed_n / * > From <Table_name> Where < condition > ; 13

  14. Condition Condition หรือ เงื่อนไข หมายถึงการระบุความต้องการ มีรูปแบบการเขียนดังนี้คือ <field> <operator> < value > Filed คือ Filed ใดๆที่อยู่ในรายชื่อ Table อยู่ในคำสั่ง From Operator คือคำสั่งในการปฏิบัติการ เช่น = , > , < , != , is null Value ค่าที่ใช้เป็นเงื่อนไข หากเป็นตัวเลขสามารถระบุได้เลย หากเป็นตัวอักษรต้องมีการใส่ " ก่อนและหลังค่าที่เป็นเงื่อนไข

  15. ตัวอย่าง - คำสั่งแบบมีเงื่อนไข Select * from work Where proj_num = 15 ; (เลือกข้อมูลการทำงานโครงการ 15) Select * from work Where hours > 3 ; (เลือกข้อมูลการทำงานที่มากกว่า 3 ชั่วโมง) Select * from employee where job = "Programmer"; (เลือกพนักงานที่เป็น Programmer)

  16. 3 – คำสั่งแบบหลายเงื่อนไข Select <filed_1 , filed_2 , … , filed_n / * > From <Table_name> Where < condition-1 > <And / Or > <condition-2> ; And ให้ผลเหมือนการ Intersection Or ให้ผลเหมือนการ Union 16

  17. ตัวอย่าง – คำสั่งแบบหลายเงื่อนไข select * from work where proj_num = 15 and hours > 3 ; (เลือกพนักงานที่ทำงานโครงการ 15 และทำงานมากกว่า 3 ชั่วโมง : ต้องอยู่โครงการ 15) select * from work where proj_num = 15 or hours > 3 ; (เลือกพนักงานที่ทำงานโครงการ 15 หรือทำงานมากกว่า 3 ชั่วโมง : อาจทำงานอยู่โครงการอื่น)

  18. 4 – คำสั่งที่มีการใช้งานหลายตาราง (join) Select <filed_1 , filed_2 , … , filed_n / * > From <table_1 , table_2 , …, table_n> Where < condition-1 > ; • ต้องมีการเชื่อม PK และ FK ของทั้งสองตารางเข้าด้วยกัน (Join) • หากมี field ใดที่ชื่อซ้ำกันทั้งสองตาราง เมื่ออ้างถึง ต้อง ระบุชื่อตาราง ตามด้วยชื่อฟิลด์ เช่น work.emp_num หรือ employee.emp_num 18

  19. ตัวอย่าง- คำสั่งที่มีการใช้งานหลายตาราง (join) Select employee.emp_num , emp_name , hours From employee , work Where employee.emp_num = work.emp_num ; ( แสดงข้อมูลรหัสพนักงาน ชื่อพนักงาน และชั่วโมงทำงาน เนื่องจาก ชื่อพนักงาน อยู่คนละตารางกับ ข้อมูลชั่วโมงทำงาน จึงต้องมีการ join ตารางที่มีข้อมูลเข้าด้วยกัน)

  20. 5 - คำสั่งที่มีการเรียงลำดับ Select <filed_1 , filed_2 , … , filed_n / * > From <table_1 , table_2 , …, table_n> Where < condition-1 > Order by<[asc/desc] field_2 , [asc/desc] filed_n > ; อธิบายเพิ่มเติม • Filed ที่จะใช้คำสั่ง order ต้องเลือกมาก่อนแล้วจากการ select • การเรียงปกติจะเรียงจากน้อยไปมาก (asc) หากต้องการเรียงตากมาไปน้อยให้ใช้ (desc) 20

  21. ตัวอย่าง - คำสั่งที่มีการเรียงลำดับ Select employee.emp_num , emp_name , proj_num,hours From employee , work Where employee.emp_num = work.emp_num Order by employee.emp_num ; (แสดงข้อมูลรหัสพนักงาน ชื่อพนักงาน และชั่วโมงทำงาน เรียงลำดับข้อมูล ตามรหัสพนักงาน)

  22. 6 – การใช้ Operator : like Select <filed_1 , filed_2 , … , filed_n / * > From <table_1 , table_2 , …, table_n> Where <filed_1 like “%” > ; คำสั่ง like หมายถึงค่าที่คล้ายกัน โดยมีสัญญลักษ์ % เป็น wild-card ที่ใช้หมายถึงค่าใดๆก็ได้ 22

  23. ตัวอย่าง - การใช้ Operator : like Select * from employee where emp_name like “A%” ; (แสดงข้อมูล พนักงานที่ชื่อขึ้นต้นด้วยอักษร A ) Select * from employee where emp_name like “%A%” ; (แสดงข้อมูล พนักงานที่ชื่อมีอักษร A )

  24. 7 – การใช้ Operator : in หรือ การทำงานแบบเซต select <filed1 , filed2 , … , filedn / * > from <Table_name> where field_i in ( value_lits) ; ในวงเล็บทางขวาของ in หมายถึง การมีค่าได้หลายค่า แต่ละค่าคั่นด้วย เครื่องหมาย , (comma) เปรียบเสมือน value_list คือ เซตหรือกลุ่มของค่าที่เราต้องการใช้เป็น เงื่อนไข หากเราใช้ Operator = ค่าทางขวาจะมีได้ค่าเดียวเท่านั้น 24

  25. ตัวอย่าง – การใช้ Operator : in select * from employee where deptno in ( “Programmer” , “System Analyst”) ; (แสดงข้อมูลพนักงานที่เป็นโปรแกรมเมอร์หรือ นักวิเคราะห์ระบบ) select * from work where proj_num in ( 15 , 22 ) ; (แสดงข้อมูลการทำงานโครงการ 15 หรือ โครงการ 22 )

  26. 8 – การใช้ค่า Null • ในการเก็บข้อมูล ค่า Null ไม่เท่ากับ ค่าว่าง ค่า Null คือไม่มีการเก็บค่าใดๆใน Filed นั้น การทำงานกับค่า null ต้องใช้ Operator “is” • ตัวอย่างเช่น select * from employee where job is null ; (แสดงข้อมูลพนักงานที่ไม่มีหน้าที่)

  27. 9 - รูปแบบคำสั่ง : การทำงานเชิงนิเสธ select <filed1 , … , filedn / * > from <table_name> where field_inot in ( value_lits) ; select <filed1 , … , filedn / * > from <table_name> where field_i!= value ; 27

  28. ตัวอย่าง : การทำงานเชิงนิเสธ select * from work where proj_num not in ( 15 , 22) ; (แสดงข้อมูลการทำงาที่ไม่ใช่ โปรเจ็ค 15 และ 22) select * from employee where job != “Programmer” ; (แสดงข้อมูลการทำงานของพนักงานที่ไม่ใช่ Programmer)

  29. 10 - รูปแบบคำสั่ง : การทำงานแบบ sub query select <filed1 , filed2 , … , filedn / * > from <Table_name> where field_i = ( select field from table1) ; select <filed1 , filed2 , … , filedn / * > from <Table_name> where field_i in ( select field from table1) ; 29

  30. Sub-query • การ Select ในการทำ Sub-query สามารถเลือกได้ Filed เดียวเท่านั้น • สามารถเลือกใช้ Operator เชิงเซต (in) เมื่อ Sub-query ให้ผลมากกว่า 1 ค่า • จะสามารถใช้ Operator เชิงเปรียบเทียบ ( = , > , < ) ได้ เมื่อ การทำ sub-query ให้ผลค่าเดียวเท่านั้น

  31. ตัวอย่าง : การทำงานแบบ sub query select * from work where emp_num deptno in ( select emp_num from employee where job = “Programmer”) ; (แสดงข้อมูลการทำงานของผู้ที่ทำงานหน้าที่ Programmer – ที่อาจมีหลายคน) select * from work where proj_num = ( select proj_num from project where proj_name = “โรงงานแกลงการยาง” ); (แสดงข้อมูลการทำงานของผู้ที่งานในโครงการ โรงงานแกลงการยางค์ – มีโครงการเดียวแน่นอน) 31

  32. 11 -รูปแบบคำสั่ง : การทำงานแบบรวม (Aggregate function) Selectfield-0 , [sum(field1 , count(*), min(field2) , max(field3), avg(filed4) ] From <table_name> Group by filed-0 < Having ( condition of aggregate function) > 32

  33. การใช้งาน Aggregate function • หากใช้ aggregate function มักต้องใช้กับ function การจัดกลุ่ม ( group by ) • ทุก filed ที่ select มักจะต้องอยู่ใน group by ด้วย • function count มักใช้ parameter * แทนชื่อ field เนื่องจากอาจมีค่า null อยู่ในบางรายการ • aggregate functionไม่ใช่ Field ดังนั้นจึงไม่สามารถนำมาเป็นเงื่อนไขในช่วงคำสั่ง where ได้ แต่สามารถใช้เป็นเงื่อนไขในช่วงคำสั่ง Having

  34. ตัวอย่าง การทำงานแบบรวม (Aggregate function) select job , count(*) from employee group by job ; (นับจำนวนพนักงานจัดกลุ่มตามอาชีพ) select emp_num , sum(work_hours) from work group by emp_num ; (รวมชั่วโมงทำงาน จัดกลุ่มตามรหัสพนักงาน)

  35. ตัวอย่าง การทำงานแบบรวม (Aggregate function) (2) select proj_num , min(work_hours) , max(work_hours) from work group by proj_num ; (แสดงชั่วโมงทำงานน้อยที่สุด และมากที่สุด ชองแต่ละโครงการ)

  36. ตัวอย่าง การทำงานแบบรวม (Aggregate function) (3) select job, count(*) from employee group by job having count(*) > 2 ; (นับจำนวนพนักงาน จัดกลุ่มตามอาชีพ เฉพาะที่มีพนักงานมากกว่า 3 คน) select emp_num , sum(work_hours) from work group by emp_num having sum(work_hours) > 20; (รวมชั่วโมงทำงาน จัดกลุ่มตามรหัสพนักงาน เฉพาะที่มีชั่วโมงการทำงานรวมมากกว่า 20 ชั่วโมง)

  37. 12 รูปแบบคำสั่งที่มีการคำนวณ Select employee.emp_num , proj_num, chg_hours , work_hours , chg_hours * work_hours From employee , work Where employee.emp_num = work.emp_num ; (แสดงข้อมูลการทำงานของพนักงานแต่ละคนในแต่ละโครงการ และค่าตอบแทนที่ได้)

  38. 13 รูปแบบคำสั่งที่มีการเปลี่ยนชื่อการแสดงผล Select employee.emp_num , proj_num, chg_hours , work_hours , chg_hours * work_hours pay From employee , work Where employee.emp_num = work.emp_num ; (แสดงข้อมูลการทำงานของพนักงานแต่ละคนในแต่ละโครงการ และค่าตอบแทนที่ได้ แสดงคอลัมน์ค่าตอบแทนคือ pay)

  39. 14 รูปแบบคำสั่งที่มีการเปลี่ยนชื่อตาราง Select e.emp_num , proj_num, chg_hours , work_hours , chg_hours * work_hours From employee e, work w Where e.emp_num = w.emp_num ; (แสดงข้อมูลการทำงานของพนักงานแต่ละคนในแต่ละโครงการ และค่าตอบแทนที่ได้)

  40. สรุปรูปแบบคำสั่ง Select ….. From ….. Where ….. and (or) ….. Group by …. ( Having ….) Order by ….

  41. QUESTIONS

More Related