บทที่ 2 ระบบฐานข้ อมูล อ. ดร. ชุรี เตชะวุฒิ CS (204)321 ระบบฐานข้อมูล 1 (Database System I) เนื้อหา 1) ลักษณะการประมวลผลของระบบฐานข้อมูล (Major characteristic of database approach) สภาพแวดล้อมของระบบฐานข้อมูล (Database system environment) 3) ประเภทของการประยุกต์ฐานข้อมูล (The range of database application) 4) สถาปั ตยกรรมแบบรับและให้บริ การ (Client/Server Architecture) 5) ประโยชน์ของการใช้ระบบจัดการฐานข้อมูล 6) องค์ประกอบของระบบจัดการฐานข้อมูล 7) หน้าที่ของระบบจัดการฐานข้อมูล 8) หลักการของระบบฐานข้อมูลและสถาปั ตยกรรม 2) Major Characteristics of Database Approach Self-describing nature of a database system A DBMS catalog stores the description of the database. The description is called meta-data. This allows the DBMS software to work with different databases A traditional file processing can work with only one specific database. The description of the database is declared by data definition in the application program. For example, C and C++ program have “struct” or “class” declaration. Insulation between program and data Allows changing data storage structures without having to change the DBMS access programs called program-data independence. Changing in the structure of data files, embedded in the access programs, requires to change all programs that access this file. Major Characteristics of Database Approach Data abstractions A data model is used to hide storage details and present the user with a conceptual view of the database. (data model – logical concepts – objects, properties, relationships) In traditional file processing, the representation of the structure of data files is very complicated. Details that are not of interest cannot be hidden. Support of multiple views of data Each user may see a different view of the database, which describes only the data of interest to that user. A traditional file processing cannot provide a different view of the database. Major Characteristics of Database Approach Sharing of data and multiuser transaction processing DBMS allows multiple users to access the database at the same time. DBMS controls concurrent transactions from multiple users to operate correctly without interference so that data are updated correctly. A traditional file processing limits data sharing. Database System Environment Users/Programmers DATABASE SYSTEM Application Programs/Queries DBMS SOFTWARE Software to Process Queries/Programs Software to Access Stored Data Stored Database Definition (Meta-Data) Stored Database Source: Elmasri R. & Navathe S.B. (1994) Fundamentals of database systems Examples of Database A UNIVERSITY database for maintaining information concerning students, courses, and grades in a university environment. To define this database, we must specify 1) The structure of the records. For example, each STUDENT record includes data to represent the student’s Name, StudentNumber, Class (freshman or 1, sophomore or 2,…), and Major (MATH, computer science or COSC, …). Examples of Database 2) A data type for each data element within record. e.g. Name of STUDENT is a string of alphabetic characters, StudentNumber of STUDENT is an integer, Grade of GRADE_REPORT is a single character from the set {A,B,C,D,F,I}, Class of a STUDENT as 1 for freshman, 2 for sophomore, 3 for junior, 4 for senior, and 5 for graduate student. Examples of Database To construct the UNIVERSITY database, we store data to represent each student, course, section, grade report, and prerequisite as a record in the appropriate file. Records in the various files may be related to one another. Database manipulation involves querying and updating. Example of query is “retrieve all courses and grades of Smith”. Examples of Database STUDENT COURSE Name StudentNumber Class Major Smith 17 1 COSC Brown 8 2 COSC CourseName CourseNumber Intro to Computer Science COSC1310 4 COSC Data Structures COSC3320 4 COSC Discrete Mathematics MATH2410 3 MATH Database COSC3380 3 COSC PREREQUISITE CreditHours Department CourseNumber PrerequisiteNumber COSC3380 COSC3320 COSC3380 MATH2410 COSC3320 COSC1310 Source: Elmasri R. & Navathe S.B. (1994) Fundamentals of database systems Examples of Database SECTION SectionIdentifier CourseNumber Semester Year Instructor 85 MATH2410 Fall 91 King 92 COSC1310 Fall 91 Anderson 102 COSC3320 Fall 92 Knuth 112 MATH2410 Fall 92 Chang 119 COSC1310 Fall 92 Anderson 135 COSC3380 Fall 92 Stone GRADE_REPORT StudentNumber SectionIdentifier Grade 17 112 B 17 119 C 8 85 A 8 92 A 8 102 B 8 135 A Source: Elmasri R. & Navathe S.B. (1994) Fundamentals of database systems Database System Environment องค์ประกอบย่อย 5 ส่ วน ส่ วนอุปกรณ์ (Hardware) ได้แก่ Personal computer Mainframe Computer network Printer และอื่นๆ ส่ วนชุ ดคาสั่ ง (Software) ได้แก่ Operating system DBMS และ Application program ระเบียบวิธีปฏิบัติ (Procedure) คือคาสั่งหรื อกฎเกณฑ์ที่ใช้ในการออกแบบและ วิธีการใช้ระบบฐานข้อมูล ข้ อมูล คือ ข้อเท็จจริ งที่จะนาไปบันทึกในฐานข้อมูล อาจเป็ นข้อมูลดิ บที่จะถูกนาไป ประมวลผลและสร้างเป็ นสารสนเทศ บุคลากร (People) คือ บุคคลที่เกี่ยวข้องกับการทางานระบบฐานข้อมูล Different Types of Database Users Workers on the scene Persons whose job involves daily use of a large database. 1) Database administrators (DBA) Responsible for managing the database system – authorizing, coordinating and monitoring. 2) Database designers Responsible for designing the database – identifying and choosing appropriate structures. 3) End users The persons that use the database for querying, updating, generating reports, etc. Different Types of Database Users 3) End users The persons that use the database for querying, updating, generating reports, etc. There are several categories of end users. 3.1. Casual end users: They occasionally access the database. 3.2. Naïve or parametric end users: They perform canned transaction. e.g. bank teller or reservation clerk. 3.3. Sophisticated end users: They familiarize with DBMS and implement complex applications. 3.4. Stand-alone users: They maintain personal database by using ready-made program packages. 4) System analysts and application programmers They determine requirements, design, and implement canned transactions for parametric user. Different Types of Database Users Workers behind the scene Persons whose job involves design, development, operation, and maintenance of the DBMS software and system environment. 1) DBMS designers and implementers Design and implement the DBMS modules and interfaces as a software package. 2) Tool developers Design and implement tools that facilitate the use of DBMS software. Tools include design tools, performance tools, special interfaces, etc. 3) Operators and maintenance personnel They responsible for the actual running and maintenance of the hardware and software environment for the database system. Database Architectures|Client/Server Architecture user client Application Application client network Database system user network server Application server Database system Two-tier architecture Three-tier architecture Source: Silberschatz A., Korth, H.F. & Sudarshan S. (2006) Database system concepts. Database Architectures|Client/Server Architecture Two Tier Client-Server Architectures Application on client machine invokes database system functionality at the server machine through query language statements. Application program interface like ODBC (Open Database Connectivity) and JDBC (Java Database Connectivity) are used for interaction. Three Tier Client-Server Architectures Client machine communicates with application server only which means it does not contain any direct database calls. Application server communicates with a database system to access data. Appropriate for large applications, and web applications. ประโยชน์ ของการใช้ ระบบจัดการฐานข้ อมูล มีดงั นี้ การควบคุมความซ้ าซ้อนของข้อมูล (Controlling data redundancy) การหลีกเลี่ยงความขัดแย้งของข้อมูล (Data inconsistency) การใช้ขอ ้ มูลร่ วมกัน (Sharing of data) การควบคุมมาตรฐาน (Enforcement of standard) การบังคับใช้กฎความคงสภาพ (Enforcement of integrity constraints) ความเป็ นอิสระของข้อมูล (Program-data independence) ความง่ายในการพัฒนา (Reduced application development time) การสารองข้อมูลและการฟื้ นสภาพข้อมูล (Backup and recovery) การเข้าถึงและเรี ยกใช้ขอ ้ มูลได้หลากหลายวิธี (Multiple user interface) องค์ ประกอบของระบบจัดการฐานข้ อมูล Users/Programmers DATABASE SYSTEM Application Programs/Queries DBMS SOFTWARE Report writer/Report generator Queries processor Security system Database engine Stored Database Definition (Meta-Data) Stored Database หน้ าทีข่ องระบบจัดการฐานข้ อมูล มีดงั นี้ การจัดการพจนานุกรมข้อมูล (Data dictionary management) การจัดการหน่วยเก็บข้อมูล (Data storage management and transformation) การจัดการความปลอดภัย (Security management) การจัดการความคงสภาพของข้อมูล (Data integrity management) การควบคุมภาวะการทางานพร้อมกัน (Multi-user access control) การสารองข้อมูลและการฟื้ นสภาพข้อมูล (Backup and recovery management) ภาษาสาหรับเข้าถึงฐานข้อมูล (Database access language) การติดต่อประสานกับฐานข้อมูล (Database communication interface) หลักการของระบบฐานข้ อมูลและสถาปัตยกรรม Data Models Data model “A set of concepts that can be used to describe the structure of a database (data types and relationships) and certain constraints that the database should obey.” Data model operations “Operations for specifying database retrievals and updates by referring to the concepts of the data model.” Operations on the data model may include basic operations and user-defined operations. (e.g. A user-defined operation is COMPUTE_GPA which can be applied to a STUDENT object.) Data Models Categories of data models 1) Conceptual (high-level, semantic) data models: Provide concepts that are close to the way many users perceive data. 2) Physical (low-level, internal) data models: Provide concepts that describe the details of how data is stored in the computer. 3) Implementation (representational) data models: Provide concepts that fall between above two, balancing user views with some computer storage details. Schemas VS. Instances In any data model it is important to distinguish between the description of the database and the database itself. Database schema (or meta-data) “The description of database. It includes description of database structure and the constraints that should hold on the database.” The database schema is specified during database design and is not expected to change frequently. e.g. Name: string StudentNumber: string Class: integer Major: string Schemas VS. Instances Schema diagram “A diagrammatic display of a database schema – structure of each record type (not the actual instances of a record).” STUDENT Name StudentNumber Class COURSE CourseName CourseNumber PREREQUITSITE CourseNumber Major CreditHours Department PrerequisiteNumber SECTION SectionIdentifier CourseNumber Semester GRADE_REPORT StudentNumber SectionIdentifier Grade Year Instructor Schemas VS. Instances Schema construct “An object within the schema.” e.g. STUDENT, COURSE. Database instances “The actual data stored in a database at a particular moment in time. Also called database state or occurrence.” Many database instances can be constructed to correspond to a particular database schema. Schemas VS. Instances Define a new DB Data firstly loaded Specify DB schema DBMS catalog Update operation Database Database Database empty state initial state DBMS ensures valid state Schemas VS. Instances Distinction The database schema does not frequently change, but the database state changes every time the database is updated. Schema is also called intension, whereas state is called extension. Three-Schema Architecture The three-schema architecture was proposed to support DBMS characteristics of : Program-data independence. Supporting multiple views of the data. The goal of the three-schema architecture is to separate the user applications and the physical database. Three-Schema Architecture Schema can be defined at the following three level. 1) Internal schema at the internal level Describes physical storage structures and access paths. Typically uses a physical data model. 2) Conceptual schema at the conceptual level Describes the structure (such as entities, data type, relationship) and constraints for the whole database. Uses a conceptual or implementation data model. 3) External schemas at the external level Describes the various user views. Usually uses the same data model as the conceptual level. Three-Schema Architecture END USERS External level Conceptual level Internal level EXTERNAL VIEW1 EXTERNAL VIEWn CONCEPTUAL SCHEMA INTERNAL SCHEMA STORED DATABASE Source: Elmasri R. & Navathe S.B. (1994) Fundamentals of database systems. Three-Schema Architecture Mappings among schema levels are needed to transform requests and data. If the request is a database retrieval, the data extracted from the stored database must be reformatted to match the user’s external view. Programs refer to an external schema, and are mapped by the DBMS to the internal schema for execution. Notice that the three schemas are only descriptions of data; the only data that actually exists is at the physical level. Data Independence Two types of data independence: 1) Logical data independence The capacity to change the conceptual schema without having to change the external schemas and their application programs. 2) Physical data independence The capacity to change the internal schema without having to change the conceptual (or external) schema. Data Independence When a schema at a lower level is changed, only the mappings between this schema and higher-level schemas need to be changed in a DBMS that fully supports data independence. The higher-level schemas themselves are unchanged. Therefore, the application programs need not be changed since they refer to the external schemas. Three-Schema Architecture END USERS EXTERNAL VIEW1 Logical data independence External level Conceptual level EXTERNAL VIEWn CONCEPTUAL SCHEMA Physical data independence Internal level INTERNAL SCHEMA STORED DATABASE Source: Elmasri R. & Navathe S.B. (1994) Fundamentals of database systems. DBMS Language Once the design of a database is completed and a DBMS is chosen to implement the database: Data Definition Language (DDL) is used by the DBA and by database designers to define the conceptual schema for the database and any mapping between the two. Storage Definition Language (SDL) is used to specify the internal schema. View Definition Language (VDL) are used to specify external schema user views and their mappings to the conceptual schema. DBMS Language Once the database schemas are compiled and the database is populated with data: Data Manipulation Language (DML) are used to specify database retrievals and updates. DML commands (data sublanguage) can be embedded in a generalpurpose programming language (host language), such as COBOL, C or an Assembly Language. In object-oriented systems, the host and data sublanguages typically form one integrated language such as C++. Alternatively, a high-level DML used in stand-alone interactive manner is called a query language. DBMS Language Types of DML 1) Procedural DML (record-at-a-time or low-level DML) Must be embedded in a programming language. Typically retrieve individual records from the database, and use looping and other constructs of the host programming language to retrieve multiple records. Specify how to retrieve data. e.g. COBOL, C, etc. DBMS Language 2) Declarative or Non-procedural DML (set-at-a-time or high-level DML) Use as a stand-alone query language or embedded in a programming language. Typically retrieves information from multiple related database records in a single command. Specify what data to retrieve than how to retrieve. Also called declarative languages. e.g. SQL