Giáo trình cơ sở dữ liệu

68 1.6K 0
Giáo trình cơ sở dữ liệu

Đang tải... (xem toàn văn)

Tài liệu hạn chế xem trước, để xem đầy đủ mời bạn chọn Tải xuống

Thông tin tài liệu

1 1 Data Data : Data is collection of records. : Data is collection of records. Database Database : A database is a collection of data that represents important objects in a user's business. : A database is a collection of data that represents important objects in a user's business. Usage of database Usage of database : Database is being used in almost all possible aspects of business and life. : Database is being used in almost all possible aspects of business and life. 1) S 1) S ingle User ingle User : 2) : 2) Multiple Users Multiple Users : 3) : 3) Very Large Application Very Large Application : 4) : 4) Database used by Internet Database used by Internet Application Application : : Evolution of Database Management System Evolution of Database Management System GIÁO TRÌNH CƠ SỞ DỮ LIỆU Nguyễn Quang Tấn Bài 1 DATABASE MANAGEMENT SYSTEM DATABASE MANAGEMENT SYSTEM : : APPLICATION PROGRAM FILE FILE FILE FILE DBMS FILE- PROCESSING SYSTEM: FILE- PROCESSING SYSTEM: APPLICATION PROGRAM 2 2 1. 1. It is self-describing It is self-describing 2. 2. It is a collection of integrated records (Users Data, Meta Data, Indexes, Application It is a collection of integrated records (Users Data, Meta Data, Indexes, Application Metadata) Metadata) 3. A Database is Model of Model 3. A Database is Model of Model Therefore based on the characteristics of the Database we can formulate the definition as: A Therefore based on the characteristics of the Database we can formulate the definition as: A database is self-describing collection of integrated records. database is self-describing collection of integrated records. GIÁO TRÌNH CƠ SỞ DỮ LIỆU Nguyễn Quang Tấn Bài 1 Characteristics of Database Management System Characteristics of Database 1. It is collection of databases 2. It is a computerized record-keeping system 3. It contains facilities that allow the user to • Add and delete files to the system • Insert, retrieve, update, and delete data in existing files • A DBMS may contain many databases that can be used for separate purposes or combined to provide useful information A database management system (DBMS) is a program that allows users to define, manipulate, and process the data in a database in order to produce meaningful information. 3 3 Important models in Database Management Systems Important models in Database Management Systems 1. 1. The Relational Model : concepts of relational algebra to the problem of storing large data. The Relational Model : concepts of relational algebra to the problem of storing large data. (E.F.Codd- 1970) (E.F.Codd- 1970) . . Relational Database: databases in which data is organized into tables. Relational Database: databases in which data is organized into tables. GIÁO TRÌNH CƠ SỞ DỮ LIỆU Nguyễn Quang Tấn Bài 1 E.F.Codd formulated rules for Relational Database Manangement Systems which can be tablulated as below: Information Representation All data must be represented logically in tables Guaranteed logical accessibility All data is accessible logically using a combination of table name, key name, key value and column NULL support A Null value represents missing information and are not to be confused with empty, blank or zero filled data. They also are not necessarily equal Dynamic online catalog The definition of data is represented in the same manner as data so that it can be relationally accessed. Example every database has catalog tables Comprehensive data sub-language (HLL Like SQL ) A unique language which is supported along with several other programming languages to achieve data definition, view and manipulation Updatable views All views that can theoretically exists can be updated High level insert, updates and delete Both base and derived relations can be handled as singular requests and apply to retrieval, insert, update and deletion of data Physical data independence Program and terminal activities are preserved when changes are made in storage representation or access methods Logical data independence Program and terminal activities are preserved when changes are made to base tables Lintegrity independence Integrity constraints must be definable in the unique sub-language and stored in the catalog Distribution independence e The unique sub-language must support database distribution while preserving program and terminal activities Nonsubersion A relational system which perform low-level processing of records cannot supplant the integrity rules in effect in high level, record processing by a relational language Relational Table database Defined as a rectangular array of columns and rows where columns are homogenous, non-grouped, uniquely named data items( attributes ) and rows are not duplicated. It is also important to impose the property that column and row order within the array is immaterial 4 4 GIÁO TRÌNH CƠ SỞ DỮ LIỆU Nguyễn Quang Tấn Bài 1 Microcomputer DBMS Products Dbase-II (Ashon-Tate) 1979 Client Server Database Application Access FoxPro • A client server application • Database server MS SQL, SQL Anywhere, DB2, Oracle Database Using Internet Applications • Distributed Database Processing • Object- Oriented DBMS (ODBMS) ASP, ColdFusion, XML Single Database Application 5 5 GIÁO TRÌNH CƠ SỞ DỮ LIỆU Nguyễn Quang Tấn Bài 1 Components of Database 1. User Data: StudentID Student Name StudentPhone InstructorName InstructorPhone 100 Adam 111-222-0987 Gary 111-222-3333 101 John 111-221-3457 Gary 111-222-3333 102 Jenny 111-220-9822 Tim 111-219-3267 103 Smantha 111-220-7644 Tim 111-219-3267 104 Stacy 111-222-3478 Gary 111-222-3333 Table StudentIns StudentID Student Name StudentPhone InstructorName 100 Adam 111-222-0987 Gary 101 John 111-221-3457 Gary 102 Jenny 111-220-9822 Tim 103 Smantha 111-220-7644 Tim 104 Stacy 111-222-3478 Gary Table Student InstructorName InstructorPhone Gary 111-222-3333 Tim 111-219-3267 Table Instructor 2. MetaData: Table Name Number of Columns Primary Key Student 4 StudentID Instructor 2 InstructorName Course 4 Courseid Registration 4 {Reg_No, zip} Hypothetical Systems Table 6 6 GIÁO TRÌNH CƠ SỞ DỮ LIỆU Nguyễn Quang Tấn Bài 1 3. Indexes: Components of Database (cont.) StudentName StudentID Adam 100 Smith 101 Chris 102 Nancy 103 Andrew 104 Student Name Index 4. Application Metadata Application metadata stores the structure and format of user forms, reports, queries and other application components. Usually application metadata is accessed by using DBMS tools. 7 7 GIÁO TRÌNH CƠ SỞ DỮ LIỆU Nguyễn Quang Tấn Bài 1 Features of DBMS subsystem OPERATING SYSTEM DBMS ENGINE DESIGN TOOLS: •TABLE CREATION •QUERY CREATION • REPORT CREATION • PROCEDURAL LANGUAGE RUN-TIME FACILITIES: •FORM PROCESSOR •QUERY PROCESSOR • REPORT WRITE • PROCEDURAL RUN TIME 8 8 GIÁO TRÌNH CƠ SỞ DỮ LIỆU Nguyễn Quang Tấn Bài 2 The Entity Relationship Model (E-R Model) Entities StudentID StudentName Address City State Zip TelephoneNumber Student entity StudentID StudentName Address City State Zip TelephoneNumber 101 Adam 100 Gateway Drive Seattle WA 98101 206-334-9876 Instance of STUDENT Attributes Student Name, Student ID, Class ID etc. Attributes is analogous to a column in the relational table. Composite Attribute Example of Composite Attribute is Address, which consists of group of attributes [Street, City, State, Zip] Multi Value Attributes Example can be anyone's E-mail ID as person may have more then one E-Mail ID. Relationships Entity Relationship Diagrams A Relationship represents an association between two or more entities. • Entity classes are shown by rectangles •Relationships are shown by diamonds •Maximum cardinality of the relationship is inside the diamond • The name of the relationship is shown near the diamond • To show Minimum cardinality one can place a hash mark (|) across the relationship line to indicate that the entity must exist in the relationship and place an oval across the relationship line to indicate that there may or may not be an entity in the relationship 9 9 GIÁO TRÌNH CƠ SỞ DỮ LIỆU Nguyễn Quang Tấn Bài 2 Important Utilities of SQL Server 7.0 Creating database in SQL Server The SQL Server Enterprise Manager •The Create Database statement • The database creation Wizard Create database Dbase_name [ ON [PRIMARY] ( [ NAME = logical_file_name, ] FILENAME = 'os_file_name' [, SIZE = size] [, MAXSIZE = { max_size | UNLIMITED } ] [, FILEGROWTH = growth_increment] ) [, n]] [ LOG ON { ( [ NAME = logical_file_name, ] FILENAME = 'os_file_name' [, SIZE = size] [, MAXSIZE = { max_size | UNLIMITED } ] [, FILEGROWTH = growth_increment] ) [, n] [, n]} ] [ FOR LOAD | FOR ATTACH ] Create Tables Can be created both by using Query Analyzer and Enterprise Manager (EM) Syntax Create Table [database. [Owner.] table_name ( column_name datatype [identity|constraint|NULL|NOT NULL] […] ) Example Create Table student1 (sid tinyint IDENTITY NOT NULL, Fname char (20), Lname char (20) NOT NULL, Address1 varchar (30), Address2 varchar (30), City varchar (30), State varchar (30), Zipcode char (10), Add_date datetime, marks1 tinyint NOT NULL, marks2 tinyint NOT NULL, Total AS marks1+marks2 ) SQL Server 7.0 10 10 GIÁO TRÌNH CƠ SỞ DỮ LIỆU Nguyễn Quang Tấn Bài 2 SQL Server Data Types I. Strings (char (n), varchar)- String contain character data made up of letters, numbers and symbols. In case of char (n), data will always contain n bytes of information. If its less then n, it will be padded with spaces and if more then n, data will be truncated. Varchar (n) is used when one expects a wide variation in data size. Varchar option is good from memory standpoint but SQL Server has to do overhead in determining the variable-length field. II. Binary Data (binary (n), varbinary) - Binary data is stored as a series of 0s and 1s, which are represented on input and output as a hexadecimal pairs. These hexadecimal pairs are made up of characters 0 through 9 and A through F III. Integers (tinyint, smallint, int)- Of these three types of integer data type difference is in the amount of storage space they require and range of values they can store Data Type Length Range TinyInt 1 0-255 SmallInt 2 +32,767 Int 4 +2,147,483,647 IV. Approximate and Exact Numeric Data Types Approximate Numeric data types (float (n), real)- Floats and Reals are useful for scientific and statstical data where absolute accuracy is not required, but where value reaches from extremely small to extremely large numbers. Floats can have a precision from 1 to 38 digits. Real have a precision of 7 digits and require 4 bytes of data storage. So Float with a precision less then 7 is real data type. Ex. Variable1 real; variable2 float (8) Exact Numeric data Types (decimal and numeric)- Ex. decimal (7,2) [...]... B and A -> C 28 Nguyễn Quang Tấn Consider the following table : GIÁO TRÌNH CƠ SỞ DỮ LIỆU B C D a1 b1 c1 d1 a1 b2 c1 d2 a2 b2 c2 d2 a3 b3 c1 d3 a4 1 2 3 4 A b4 c3 Bài 5 d2 Translate this table to relation R Which of properties is the key ? Look for the key of relation ! A -> B, A -> C may be FD ? 29 Nguyễn Quang Tấn GIÁO TRÌNH CƠ SỞ DỮ LIỆU Consider the following tables : A a1 B b1 c1 S Q R C Bài 5... c1 d2 a1 b2 c1 d2 a2 b2 c3 D2 a2 b2 c2 d2 a3 b3 c1 D3 a3 b3 c1 d3 a4 b4 c3 D2 a4 b4 c3 d2 1 Union: R + Q 2 Difference R - Q 3 Product R x S 30 Nguyễn Quang Tấn GIÁO TRÌNH CƠ SỞ DỮ LIỆU Bài 6 31 Nguyễn Quang Tấn      GIÁO TRÌNH CƠ SỞ DỮ LIỆU Bài 6 Relational Modelling: Relational Modelling: Advantages Advantages The model is understandable by users as well as technologists: managers and users can... different users 32 Nguyễn Quang Tấn GIÁO TRÌNH CƠ SỞ DỮ LIỆU Bài 6 Relational Data Model Relational Data Model  DATA STRUCTURES (domain, attribute, relation, tuple, primary key, degree, cardinality)  INTEGRITY CONSTRAINTS (e.g attribute value ranges)  DATA MANIPULATION OPERATIONS (such as insert, update, delete, retrieve) 33 Nguyễn Quang Tấn GIÁO TRÌNH CƠ SỞ DỮ LIỆU Bài 6 Relational Data Relational... consisting of attributes based on domains 34 Nguyễn Quang Tấn GIÁO TRÌNH CƠ SỞ DỮ LIỆU Bài 6 Relational Data Structure -Relational Data Structure Example Example Relation EMPLOYEE Emp# Name Emp Sex E1 Jones Male E65 E6 Smith Male E28 E28 Jones Female - Gender Domain Mgr Emp# Attribute Heading Body Female Male 35 Nguyễn Quang Tấn GIÁO TRÌNH CƠ SỞ DỮ LIỆU Bài 6 Properties of Relations Properties of Relations... Fee • Also order of the rows and columns in a relationship is insignificant 100 IT203 2220 101 IT161 1000 102 IT107 2000 103 IT121 1000 104 IT331 200 A Relation: Courses 14 Nguyễn Quang Tấn GIÁO TRÌNH CƠ SỞ DỮ LIỆU Bài 3 Second Normal Form Consider once again Relation Course1, now this relation also suffers from modification anomalies as discussed before In this relation one student can register for... of it Therefore we also conclude that if a relation has a single attribute as a key then it's automatically in 2NF Thus 2NF concerns only those, which have composite keys 15 Nguyễn Quang Tấn GIÁO TRÌNH CƠ SỞ DỮ LIỆU Bài 3 Third Normal Form Relations in 2NF too can have anomalies Consider HOUSING Relation below: StudentId 100 101 102 103 104 105 106 C BldgName CityHall CityWoods CityHall CalmPlace UrbaneColony... CityWoodsl Relation: Student-Bldg BldgName CityHall CityWoods CityHall CalmPlace UrbaneColony CityHall CityWoodsl C2 Rent 1200 1000 1200 1500 1200 1200 1000 Relation: BldgN-Rent 16 Nguyễn Quang Tấn GIÁO TRÌNH CƠ SỞ DỮ LIỆU Bài 3 Boyce-Codd Normal Form (BCNF) A relation is in BCNF if every determinant is a candidate key Key: One or more attributes that uniquely identify a tuple (row) in a relation Those determinants... relation is as per definition of 1NF • 2NF because all of the non-key attributes (Manager) is dependant on all of the key • 3NF because there are no transitive dependencies 17 Nguyễn Quang Tấn GIÁO TRÌNH CƠ SỞ DỮ LIỆU Bài 3 Consider what happens if we delete the tuple with FundID 22 We loose the fact that Brown manages the InvestmentType "Growth Stocks." The following are steps to normalize a relation... Manager YES •Manager NO Create a new relation from the functional dependency: Rnew (Manager, InvestmentType) : Key - Manager Rorig (FundID, Manager) : Key - FundID, Manager 18 Nguyễn Quang Tấn GIÁO TRÌNH CƠ SỞ DỮ LIỆU Bài 3 Fourth Normal Form Id -> -> Major Id -> ->Minor This attribute dependency is called multi-value dependency • Student can enroll in several different majors and several different minors... (SID, Major) : Key(SID, Major) (SID, Minor) : Key (SID, Minor) A relation meets the requirements of 4NF only if it is in 3NF (or BCNF) and has no multi-valued dependencies 19 Nguyễn Quang Tấn GIÁO TRÌNH CƠ SỞ DỮ LIỆU Bài 3 Foundations of Relational Implementation 1 Data Definition language (DDL) - Allows one to define database objects at the conceptual level It consists of commands to create objects . Application Application : : Evolution of Database Management System Evolution of Database Management System GIÁO TRÌNH CƠ SỞ DỮ LIỆU Nguyễn Quang Tấn Bài 1 DATABASE MANAGEMENT SYSTEM DATABASE MANAGEMENT SYSTEM : : . of integrated records. database is self-describing collection of integrated records. GIÁO TRÌNH CƠ SỞ DỮ LIỆU Nguyễn Quang Tấn Bài 1 Characteristics of Database Management System Characteristics. into tables. Relational Database: databases in which data is organized into tables. GIÁO TRÌNH CƠ SỞ DỮ LIỆU Nguyễn Quang Tấn Bài 1 E.F.Codd formulated rules for Relational Database Manangement

Ngày đăng: 18/07/2014, 19:00

Mục lục

  • Data: Data is collection of records. Database: A database is a collection of data that represents important objects in a user's business. Usage of database: Database is being used in almost all possible aspects of business and life. 1) Single User: 2) Multiple Users: 3) Very Large Application: 4) Database used by Internet Application:

  • Relational Data Structure - Example

Tài liệu cùng người dùng

Tài liệu liên quan