Presentation is loading. Please wait.

Presentation is loading. Please wait.

DATABASE SYSTEM.

Similar presentations


Presentation on theme: "DATABASE SYSTEM."— Presentation transcript:

1 DATABASE SYSTEM

2 Database Relational Model
Chapter 2 History of relationship model Structure of data relationship Database Scheme Mathematic Relationship: Algebra, Calculus

3 Data Models A Database models some portion of the real world. Data Model is link between user’s view of the world and bits stored in computer. Many models have been proposed. Student (sid: string, name: string, login: string, age: integer, gpa:real) 3

4 Describing Data: Data Models
A data model is a collection of concepts for describing data. A database schema is a description of a particular collection of data, using a given data model. The relational model of data is the most widely used model today. Main concept: relation, basically a table with rows and columns. Every relation has a schema, which describes the columns, or fields. 5 4

5 Levels of Abstraction Users DB Physical Schema Conceptual Schema
Views describe how users see the data. Conceptual schema defines logical structure Physical schema describes the files and indexes used. (sometimes called the ANSI/SPARC model) Physical Schema Conceptual Schema View 1 View 2 View 3 DB 6 5

6 Data Independence:The Big Breakthrough of the Relational Model
A Simple Idea: Applications should be insulated from how data is structured and stored. Physical Schema Conceptual Schema View 1 View 2 View 3 DB Logical data independence: Protection from changes in logical structure of data. Physical data independence: Protection from changes in physical structure of data. 6

7 Why Study the Relational Model?
Most widely used model currently. DB2, MySQL, Oracle, PostgreSQL, SQLServer, … Note: some “Legacy systems” use older models e.g., IBM’s IMS Object-oriented concepts have recently merged in object-relational model Informix, IBM DB2, Oracle 8i Early work done in POSTGRES research project at Berkeley XML (semi-structured)models emerging? 7

8 Relational Database: Definitions
Relational database: a set of relations. Relation: made up of 2 parts: Schema : specifies name of relation, plus name and type of each column. E.g. Students(sid: string, name: string, login: string, age: integer, gpa: real) Instance : a table, with rows and columns. #rows = cardinality #fields = degree / arity Can think of a relation as a set of rows or tuples. i.e., all rows are distinct 8

9 Example: University Database
Conceptual schema: Students(sid: string, name: string, login: string, age: integer, gpa:real) Courses(cid: string, cname:string, credits:integer) Enrolled(sid:string, cid:string, grade:string) External Schema (View): Course_info(cid:string,enrollment:integer) One possible Physical schema : Relations stored as unordered files. Index on first column of Students. Physical Schema Conceptual Schema View 1 View 2 View 3 DB 7 9

10 Relational Model Terminology
A relation is a table with columns and rows. Only applies to logical structure of the database, not the physical structure. Attribute is a named column of a relation. Domain is the set of allowable values for one or more attributes.

11 Relational Model Terminology
Tuple is a row of a relation. Degree is the number of attributes in a relation. Cardinality is the number of tuples in a relation. Relational Database is a collection of normalized relations with distinct relation names.

12 Instances of Branch and Staff Relations

13 Examples of Attribute Domains

14 Alternative Terminology for Relational Model

15 A Logical View of Data Relational model Table
View data logically rather than physically Table Structural and data independence Resembles a file conceptually Relational database model is easier to understand than hierarchical and network models 15

16 Tables and Their Characteristics
Logical view of relational database is based on relation Relation thought of as a table Table: two-dimensional structure composed of rows and columns Persistent representation of logical relation Contains group of related entities (entity set) 16

17 17

18 18

19 Keys Keys are a way to associate tuples in different relations
Keys are one form of integrity constraint (IC) Enrolled Students PRIMARY Key FORIEGN Key 19

20 Primary Keys A set of fields is a superkey if:
No two distinct tuples can have same values in all key fields A set of fields is a candidate key for a relation if : It is a superkey No subset of the fields is a superkey what if >1 key for a relation? one of the candidate keys is chosen (by DBA) to be the primary key. E.g. sid is a key for Students. What about name? The set {sid, gpa} is a superkey. 20

21 Primary and Candidate Keys in SQL
Possibly many candidate keys (specified using UNIQUE), one of which is chosen as the primary key. Keys must be used carefully! “For a given student and course, there is a single grade.” CREATE TABLE Enrolled (sid CHAR(20) cid CHAR(20), grade CHAR(2), PRIMARY KEY (sid,cid)) PRIMARY KEY (sid), UNIQUE (cid, grade)) vs. “Students can take only one course, and no two students in a course receive the same grade.” 21

22 Foreign Keys, Referential Integrity
Foreign key : Set of fields in one relation that is used to `refer’ to a tuple in another relation. Must correspond to the primary key of the other relation. Like a `logical pointer’. If all foreign key constraints are enforced, referential integrity is achieved (i.e., no dangling references.) 22

23 Foreign Keys in SQL CREATE TABLE Enrolled
E.g. Only students listed in the Students relation should be allowed to enroll for courses. sid is a foreign key referring to Students: CREATE TABLE Enrolled (sid CHAR(20),cid CHAR(20),grade CHAR(2), PRIMARY KEY (sid,cid), FOREIGN KEY (sid) REFERENCES Students ) Enrolled Students English102 A 23

24 Keys Each row in a table must be uniquely identifiable
Key is one or more attributes that determine other attributes Key’s role is based on determination If you know the value of attribute A, you can determine the value of attribute B Functional dependence Attribute B is functionally dependent on A if all rows in table that agree in value for A also agree in value for B 24

25 25

26 Keys (cont’d.) Primary key Composite key Key attribute Superkey
Candidate key selected to identify tuples uniquely within relation. Composite key Composed of more than one attribute Key attribute Any attribute that is part of a key Superkey Any key that uniquely identifies a tuple within a relation. Candidate key A superkey such that no proper subset is a superkey within the relation 26

27 Keys (cont’d.) Nulls No data entry Not permitted in primary key
Should be avoided in other attributes Can represent: An unknown attribute value A known, but missing, attribute value A “not applicable” condition 27

28 Keys (cont’d.) Nulls (cont’d.)
Can create problems when functions such as COUNT, AVERAGE, and SUM are used Can create logical problems when relational tables are linked 28

29 Keys (cont’d.) Controlled redundancy
Makes the relational database work Tables within the database share common attributes Enables tables to be linked together Multiple occurrences of values not redundant when required to make the relationship work Redundancy exists only when there is unnecessary duplication of attribute values 29

30 30

31 31

32 Keys (cont’d.) Foreign key (FK) Referential integrity Secondary key
An attribute whose values match primary key values in the related table Referential integrity FK contains a value that refers to an existing valid tuple (row) in another relation Secondary key Key used strictly for data retrieval purposes 32

33 33

34 Integrity Rules Many RDBMs enforce integrity rules automatically
Safer to ensure that application design conforms to entity and referential integrity rules Designers use flags to avoid nulls Flags indicate absence of some value 34

35 35

36

37 Database Relations Relation schema Relational database schema
Named relation defined by a set of attribute and domain name pairs. Let A1, A2… An be attributes of domains D1, D2, … Dn Then set {A1 : D1, A2: D2,… An:Dn} Ex : {(branchNo: B005, street: 22 Deer Rd, city: London)} Relational database schema Set of relation schemas, each with a distinct name. If R1, R2, … Rn are set of relation schemas, then the relational database schema, R as R={R1, R2,… Rn}

38 Properties of Relations
Relation name is distinct from all other relation names in relational schema. Each cell of relation contains exactly one atomic (single) value. Each attribute has a distinct name. Values of an attribute are all from the same domain.

39 Properties of Relations
Each tuple is distinct; there are no duplicate tuples. Order of attributes has no significance. Order of tuples has no significance, theoretically.

40 Relational Keys Superkey Candidate Key
An attribute, or set of attributes, that uniquely identifies a tuple within a relation. Candidate Key Superkey (K) such that no proper subset is a superkey within the relation. A candidate key (K) for a relation (R) has 2 properties: In each tuple of R, values of K uniquely identify that tuple (uniqueness). No proper subset of K has the uniqueness property (irreducibility).

41 Relational Keys Primary Key Alternate Keys Foreign Key
Candidate key selected to identify tuples uniquely within relation. Alternate Keys Candidate keys that are not selected to be primary key. Foreign Key Attribute, or set of attributes, within one relation that matches candidate key of some (possibly same) relation.

42 Integrity Constraints
Null Represents value for an attribute that is currently unknown or not applicable for tuple. Deals with incomplete or exceptional data. Represents the absence of a value and is not the same as zero or spaces, which are values.

43 Integrity Constraints
Entity Integrity In a base relation, no attribute of a primary key can be null. Referential Integrity If foreign key exists in a relation, either foreign key value must match a candidate key value of some tuple in its home relation or foreign key value must be wholly null.

44 Integrity Constraints
General Constraints Additional rules specified by users or database administrators that define or constrain some aspect of the enterprise.

45 Views Base Relation View
Named relation corresponding to an entity in conceptual schema, whose tuples are physically stored in database. View Dynamic result of one or more relational operations operating on base relations to produce another relation.

46 Views A virtual relation that does not necessarily actually exist in the database but is produced upon request, at time of request. Contents of a view are defined as a query on one or more base relations. Views are dynamic, meaning that changes made to base relations that affect view attributes are immediately reflected in the view.

47 Purpose of Views Provides powerful and flexible security mechanism by hiding parts of database from certain users. Permits users to access data in a customized way, so that same data can be seen by different users in different ways, at same time. Can simplify complex operations on base relations.

48 Updating Views All updates to a base relation should be immediately reflected in all views that reference that base relation. If view is updated, underlying base relation should reflect change. Pearson Education © 2009

49 Updating Views There are restrictions on types of modifications that can be made through views: Updates are allowed if query involves a single base relation and contains a candidate key of base relation. Updates are not allowed involving multiple base relations. Updates are not allowed involving aggregation or grouping operations. Pearson Education © 2009

50 Updating Views Classes of views are defined as:
theoretically not updateable; theoretically updateable; partially updateable. Pearson Education © 2009


Download ppt "DATABASE SYSTEM."

Similar presentations


Ads by Google