Вы находитесь на странице: 1из 15

Database Management Systems

1. A database management system (DBMS), or simply a database system (DBS), consists of o A collection of interrelated and persistent data (usually referred to as the database (DB)). o A set of application programs used to access, update and manage that data (which form the data management system (MS)). 2. The goal of a DBMS is to provide an environment that is both convenient and efficient to use in o Retrieving information from the database. o Storing information into the database. 3. Databases are usually designed to manage large bodies of information. This involves o Definition of structures for information storage (data modeling). o Provision of mechanisms for the manipulation of information (file and systems structure, query processing). o Providing for the safety of information in the database (crash recovery and security). o Concurrency control if the system is shared by users.

Purpose of Database Systems


1. To see why database management systems are necessary, let's look at a typical ``fileprocessing system'' supported by a conventional operating system. The application is a savings bank: Savings account and customer records are kept in permanent system files. Application programs are written to manipulate files to perform the following tasks: Debit or credit an account. Add a new account. Find an account balance. Generate monthly statements. 2. Development of the system proceeds as follows: o New application programs must be written as the need arises. o New permanent files are created as required. o but over a long period of time files may be in different formats, and o Application programs may be in different languages. 3. So we can see there are problems with the straight file-processing approach: o Data redundancy and inconsistency Same information may be duplicated in several places. All copies may not be updated properly. o Difficulty in accessing data
o o

May have to write a new application program to satisfy an unusual request. E.g. find all customers with the same postal code. Could generate this data manually, but a long job... Data isolation Data in different files. Data in different formats. Difficult to write new application programs. Multiple users Want concurrency for faster response time. Need protection for concurrent updates. E.g. two customers withdrawing funds from the same account at the same time - account has $500 in it, and they withdraw $100 and $50. The result could be $350, $400 or $450 if no protection. Security problems Every user of the system should be able to access only the data they are permitted to see. E.g. payroll people only handle employee records, and cannot see customer accounts; tellers only access account data and cannot see payroll data. Difficult to enforce this with application programs. Integrity problems Data may be required to satisfy constraints. E.g. no account balance below $25.00. Again, difficult to enforce or to change constraints with the file-processing approach.

These problems and others led to the development of database management systems.

Data Abstraction
1. The major purpose of a database system is to provide users with an abstract view of the system. The system hides certain details of how data is stored and created and maintained Complexity should be hidden from database users. 2. There are several levels of abstraction: 1. Physical Level: How the data are stored. E.g. index, B-tree, hashing. Lowest level of abstraction. Complex low-level structures described in detail. 2. Conceptual Level:

Next highest level of abstraction. Describes what data are stored. Describes the relationships among data. Database administrator level. 3. View Level: Highest level. Describes part of the database for a particular group of users. Can be many different views of a database. E.g. tellers in a bank get a view of customer accounts, but not of payroll data. Fig. 1.1 (figure 1.1 in the text) illustrates the three levels.

Figure 1.1: The three levels of data abstraction

Data Models
1. Data models are a collection of conceptual tools for describing data, data relationships, data semantics and data constraints. There are three different groups: 1. Object-based Logical Models. 2. Record-based Logical Models. 3. Physical Data Models. We'll look at them in more detail now.

Object-based Logical Models


1. Object-based logical models: o Describe data at the conceptual and view levels. o Provide fairly flexible structuring capabilities. o Allow one to specify data constraints explicitly. o Over 30 such models, including Entity-relationship model. Object-oriented model. Binary model.

Semantic data model. Infological model. Functional data model. 2. At this point, we'll take a closer look at the entity-relationship (E-R) and object-oriented models. The E-R Model 1. The entity-relationship model is based on a perception of the world as consisting of a collection of basic objects (entities) and relationships among these objects. o An entity is a distinguishable object that exists. o Each entity has associated with it a set of attributes describing it. o E.g. number and balance for an account entity. o A relationship is an association among several entities. o e.g. A cust_acct relationship associates a customer with each account he or she has. o The set of all entities or relationships of the same type is called the entity set or relationship set. o Another essential element of the E-R diagram is the mapping cardinalities, which express the number of entities to which another entity can be associated via a relationship set. We'll see later how well this model works to describe real world situations. 2. The overall logical structure of a database can be expressed graphically by an E-R diagram: o rectangles: represent entity sets. o ellipses: represent attributes. o diamonds: represent relationships among entity sets. o lines: link attributes to entity sets and entity sets to relationships. See figure 1.2 for an example.

Figure 1.2: A sample E-R diagram.


The Object-Oriented Model 1. The object-oriented model is based on a collection of objects, like the E-R model. o An object contains values stored in instance variables within the object. o Unlike the record-oriented models, these values are themselves objects. o Thus objects contain objects to an arbitrarily deep level of nesting. o An object also contains bodies of code that operate on the the object.

o o o o o o o o

These bodies of code are called methods. Objects that contain the same types of values and the same methods are grouped into classes. A class may be viewed as a type definition for objects. Analogy: the programming language concept of an abstract data type. The only way in which one object can access the data of another object is by invoking the method of that other object. This is called sending a message to the object. Internal parts of the object, the instance variables and method code, are not visible externally. Result is two levels of data abstraction.

For example, consider an object representing a bank account.


The object contains instance variables number and balance. The object contains a method pay-interest which adds interest to the balance. Under most data models, changing the interest rate entails changing code in application programs. o In the object-oriented model, this only entails a change within the pay-interest method. 2. Unlike entities in the E-R model, each object has its own unique identity, independent of the values it contains: o Two objects containing the same values are distinct. o Distinction is created and maintained in physical level by assigning distinct object identifiers.
o o o

Record-based Logical Models


1. Record-based logical models: o Also describe data at the conceptual and view levels. o Unlike object-oriented models, are used to Specify overall logical structure of the database, and Provide a higher-level description of the implementation. o Named so because the database is structured in fixed-format records of several types. o Each record type defines a fixed number of fields, or attributes. o Each field is usually of a fixed length (this simplifies the implementation). o Record-based models do not include a mechanism for direct representation of code in the database. o Separate languages associated with the model are used to express database queries and updates. o The three most widely-accepted models are the relational, network, and hierarchical. o This course will concentrate on the relational model. o The network and hierarchical models are covered in appendices in the text. The Relational Model

Data and relationships are represented by a collection of tables. Each table has a number of columns with unique names, e.g. customer, account.

Figure 1.3 shows a sample relational database.

Figure 1.3: A sample relational database.


he Network Model

Data are represented by collections of records. Relationships among data are represented by links. Organization is that of an arbitrary graph. Figure 1.4 shows a sample network database that is the equivalent of the relational database of Figure 1.3.

Figure 1.4: A sample network database


The Hierarchical Model

Similar to the network model. Organization of the records is as a collection of trees, rather than arbitrary graphs. Figure 1.5 shows a sample hierarchical database that is the equivalent of the relational database of Figure 1.3.

Figure 1.5: A sample hierarchical database


The relational model does not use pointers or links, but relates records by the values they contain. This allows a formal mathematical foundation to be defined.

Physical Data Models


1. Are used to describe data at the lowest level.

2. Very few models, e.g. o Unifying model. o Frame memory. 3. We will not cover physical models.

Instances and Schemes


1. Databases change over time. 2. The information in a database at a particular point in time is called an instance of the database. 3. The overall design of the database is called the database scheme. 4. Analogy with programming languages: o Data type definition - scheme o Value of a variable - instance 5. There are several schemes, corresponding to levels of abstraction: o Physical scheme o Conceptual scheme o Subscheme (can be many)

Data Independence
1. The ability to modify a scheme definition in one level without affecting a scheme definition in a higher level is called data independence. 2. There are two kinds: o Physical data independence The ability to modify the physical scheme without causing application programs to be rewritten Modifications at this level are usually to improve performance o Logical data independence The ability to modify the conceptual scheme without causing application programs to be rewritten Usually done when logical structure of database is altered 3. Logical data independence is harder to achieve as the application programs are usually heavily dependent on the logical structure of the data. An analogy is made to abstract data types in programming languages.

Data Definition Language (DDL)


1. Used to specify a database scheme as a set of definitions expressed in a DDL 2. DDL statements are compiled, resulting in a set of tables stored in a special file called a data dictionary or data directory. 3. The data directory contains metadata (data about data)

4. The storage structure and access methods used by the database system are specified by a set of definitions in a special type of DDL called a data storage and definition language 5. basic idea: hide implementation details of the database schemes from the users

Data Manipulation Language (DML)


1. Data Manipulation is: o retrieval of information from the database o insertion of new information into the database o deletion of information in the database o modification of information in the database 2. A DML is a language which enables users to access and manipulate data. The goal is to provide efficient human interaction with the system. 3. There are two types of DML: o procedural: the user specifies what data is needed and how to get it o nonprocedural: the user only specifies what data is needed Easier for user May not generate code as efficient as that produced by procedural languages 4. A query language is a portion of a DML involving information retrieval only. The terms DML and query language are often used synonymously.

Database Manager
1. The database manager is a program module which provides the interface between the low-level data stored in the database and the application programs and queries submitted to the system. 2. Databases typically require lots of storage space (gigabytes). This must be stored on disks. Data is moved between disk and main memory (MM) as needed. 3. The goal of the database system is to simplify and facilitate access to data. Performance is important. Views provide simplification. 4. So the database manager module is responsible for o Interaction with the file manager: Storing raw data on disk using the file system usually provided by a conventional operating system. The database manager must translate DML statements into low-level file system commands (for storing, retrieving and updating data in the database). o Integrity enforcement: Checking that updates in the database do not violate consistency constraints (e.g. no bank account balance below $25) o Security enforcement: Ensuring that users only have access to information they are permitted to see o Backup and recovery: Detecting failures due to power failure, disk crash, software errors, etc., and restoring the database to its state before the failure

Concurrency control: Preserving data consistency when there are concurrent users. 5. Some small database systems may miss some of these features, resulting in simpler database managers. (For example, no concurrency is required on a PC running MS-DOS.) These features are necessary on larger systems.

Database Administrator
1. The database administrator is a person having central control over data and programs accessing that data. Duties of the database administrator include: o Scheme definition: the creation of the original database scheme. This involves writing a set of definitions in a DDL (data storage and definition language), compiled by the DDL compiler into a set of tables stored in the data dictionary. o Storage structure and access method definition: writing a set of definitions translated by the data storage and definition language compiler o Scheme and physical organization modification: writing a set of definitions used by the DDL compiler to generate modifications to appropriate internal system tables (e.g. data dictionary). This is done rarely, but sometimes the database scheme or physical organization must be modified. o Granting of authorization for data access: granting different types of authorization for data access to various users o Integrity constraint specification: generating integrity constraints. These are consulted by the database manager module whenever updates occur.

Database Users
1. The database users fall into several categories: o Application programmers are computer professionals interacting with the system through DML calls embedded in a program written in a host language (e.g. C, PL/1, Pascal). These programs are called application programs. The DML precompiler converts DML calls (prefaced by a special character like $, #, etc.) to normal procedure calls in a host language. The host language compiler then generates the object code. Some special types of programming languages combine Pascal-like control structures with control structures for the manipulation of a database. These are sometimes called fourth-generation languages. They often include features to help generate forms and display data. o Sophisticated users interact with the system without writing programs. They form requests by writing queries in a database query language. These are submitted to a query processor that breaks a DML statement down into instructions for the database manager module.

Specialized users are sophisticated users writing special database application programs. These may be CADD systems, knowledge-based and expert systems, complex data systems (audio/video), etc. Naive users are unsophisticated users who interact with the system by using permanent application programs (e.g. automated teller machine).

Overall System Structure


1. Database systems are partitioned into modules for different functions. Some functions (e.g. file systems) may be provided by the operating system. 2. Components include: o File manager manages allocation of disk space and data structures used to represent information on disk. o Database manager: The interface between low-level data and application programs and queries. o Query processor translates statements in a query language into low-level instructions the database manager understands. (May also attempt to find an equivalent but more efficient form.) o DML precompiler converts DML statements embedded in an application program to normal procedure calls in a host language. The precompiler interacts with the query processor. o DDL compiler converts DDL statements to a set of tables containing metadata stored in a data dictionary. In addition, several data structures are required for physical system implementation: Data files: store the database itself. Data dictionary: stores information about the structure of the database. It is used heavily. Great emphasis should be placed on developing a good design and efficient implementation of the dictionary. o Indices: provide fast access to data items holding particular values. 3. Figure 1.6 shows these components.
o o

Figure 1.6: Database system structure.

Entities and Entity Sets


An entity is an object that exists and is distinguishable from other objects. For instance, John Harris with S.I.N. 890-12-3456 is an entity, as he can be uniquely identified as one particular person in the universe. An entity may be concrete (a person or a book, for example) or abstract (like a holiday or a concept). An entity set is a set of entities of the same type (e.g., all persons having an account at a bank). Entity sets need not be disjoint. For example, the entity set employee (all employees of a bank) and the entity set customer (all customers of the bank) may have members in common. An entity is represented by a set of attributes. o E.g. name, S.I.N., street, city for ``customer'' entity. o The domain of the attribute is the set of permitted values (e.g. the telephone number must be seven positive integers). Formally, an attribute is a function which maps an entity set into a domain. o Every entity is described by a set of (attribute, data value) pairs. o There is one pair for each attribute of the entity set. o E.g. a particular customer entity is described by the set {(name, Harris), (S.I.N., 890-123-456), (street, North), (city, Georgetown)}.

An analogy can be made with the programming language notion of type definition.

The concept of an entity set corresponds to the programming language type definition. A variable of a given type has a particular value at a point in time. Thus, a programming language variable corresponds to an entity in the E-R model.

Figure 2-1 shows two entity sets. We will be dealing with five entity sets in this section:

branch, the set of all branches of a particular bank. Each branch is described by the attributes branch-name, branch-city and assets. customer, the set of all people having an account at the bank. Attributes are customername, S.I.N., street and customer-city. employee, with attributes employee-name and phone-number. account, the set of all accounts created and maintained in the bank. Attributes are account-number and balance. transaction, the set of all account transactions executed in the bank. Attributes are transaction-number, date and amount.

RELATIONAL MODEL -- Integrity Constraints

Integrity Constraints over Relations


An integrity constraint (IC) is a condition specified on a database schema and restricts the data that can be stored in an instance of the database. If a database instance satisfies all the integrity constraints specifies on the database schema, it is a legal instance. A DBMS permits only legal instances to be stored in the database. Many kinds of integrity constraints can be specified in the relational model: 1. Domain Constraints: A relation schema specifies the domain of each field in the relation instance. These domain constraints in the schema specify the condition that each instance of the relation has to satisfy: The values that appear in a column must be drawn from the domain associated with that column. Thus, the domain of a field is essentially the type of that field. 2. Key Constraints: A Key Constraint is a statement that a certain minimal subset of the fields of a relation is a unique identifier for a tuple.

Super Key: An attribute, or set of attributes, that uniquely identifies a tuple within a relation.

However, a super key may contain additional attributes that are not necessary for a unique identification.

Ex: Tthe customer_id of the relation customer is sufficient to distinguish one tuple from other. Thus, customer_id is a super key. Similarly, the combination of customer_id and customer_name is a super key for the relation customer. Here the customer_name is not a super key, because several people may have the same name.

We are often interested in super keys for which no proper subset is a super key. Such minimal super keys are called candidate keys.

Candidate Key: A super key such that no proper subset is a super key within the relation. There are two parts of the candidate key definition:
i.

Two distinct tuples in a legal instance cannot have identical values in all the fields of a key. No subset of the set of fields in a candidate key is a unique identifier for a tuple. A relation may have several candidate keys. Ex: The combination of customer_name and customer_street is sufficient to distinguish the members of the customer relation. Then both, {customer_id} and {customer_name, customer_street} are candidate keys. Although customer_id and customer_name together can distinguish customer tuples, their combination does not form a candidate key, since the customer_id alone is a candidate key. Primary Key: The candidate key that is selected to identify tuples uniquely within the relation. Out of all the available candidate keys, a database designer can identify a primary key. The candidate keys that are not selected as the primary key are called as alternate keys. Features of the primary key:

ii.

i.

Primary key will not allow duplicate values. Primary key will not allow null values. Only one primary key is allowed per table.

ii.

iii.

Ex: For the student relation, we can choose student_id as the primary key. Foreign Key: Foreign keys represent the relationships between tables. A foreign key is a column (or a group of columns) whose values are derived from the primary key of some other table. The table in which foreign key is defined is called a Foreign table or Details table. The table that defines the primary key and is referenced by the foreign key is called the Primary table or Master table. Features of foreign key:
i.

Records cannot be inserted into a detail table if corresponding records in the master table do not exist. Records of the master table cannot be deleted or updated if corresponding records in the detail table actually exist. General Constraints: Domain, primary key, and foreign key constraints are considered to be a fundamental part of the relational data model. Sometimes, however, it is necessary to specify more general constraints. For example, we may require that student ages be within a certain range of values. Giving such an IC, the DBMS rejects inserts and updates that violate the constraint. Current database systems support such general constraints in the form of table constraints and assertions. Table constraints are associated with a single table and checked whenever that table is modified. In contrast, assertions involve several tables and are checked whenever any of these tables is modified.

ii.

Вам также может понравиться