98
What is a Database System? Application program End-user DBMS 1

What is dbms

  • Upload
    -

  • View
    189

  • Download
    0

Embed Size (px)

Citation preview

Page 1: What is dbms

What is a Database System?

Application program

End-user

DBMS

1

Page 2: What is dbms

Database Management System (DBMS)

• Collection of interrelated data• Set of programs to access the data • DBMS contains information about a particular enterprise• A Database Management System (DBMS) is a software

package designed to store and manage databases.

• Database Applications:– Banking: all transactions– Airlines: schedules– Universities: registration, grades– Sales: customers, products, purchases– Human resources: employee records, salaries

2

Page 3: What is dbms

Major components of a database system:

• Data: is raw, unprocessed information• Hardware: CPU, Main Memory, ...• Software: DBMS• Users:

1. End users 2. Database administrator (DBA)

3

Page 4: What is dbms

CHAR Used to store character string value of fixed length. The maximum no. of characters the data type can hold is 255 characters . It's 50% faster than VARCHAR. Uses static memory .

VARCHAR Used to store variable length alphanumeric data. The maximum this data type can hold is up to 255 characters. It's slower than CHAR. Uses dynamic memory.

Data

4

Page 5: What is dbms

5

Page 6: What is dbms

1. VARCHAR is going to be replaced by VARCHAR2 in next version. So, Oracle suggests the use VARCHAR2 instead of VARCHAR while declaring datatype.

2. VARCHAR can store up to 2000 bytes of characters while VARCHAR2 can store up to 4000 bytes of characters.

3. If we declare datatype as VARCHAR then it will take space for NULL values, In case of VARCHAR2 datatype it will not take any space.

VARCHAR Vs VARCHAR2

6

Page 7: What is dbms

number NUMBER(p,s) where:

p is the precision, or the total number of digits. Oracle guarantees the portability of numbers with precision of up to 38 digits.

s is the scale, or the number of digits to the right of the decimal point. The scale can range from -84 to 127.

7

Page 8: What is dbms

Scale Greater Than Precision

8

Page 9: What is dbms

The DATE datatype stores date and time information.

DATE '1998-12-25‘ TO_DATE('98-DEC-25 17:30','YY-MON-DD

HH24:MI')

Can you insert date of your computer if you use SYSDATE for example :

INSERT INTO my_table VALUES (1, SYSDATE);

DATE

9

Page 10: What is dbms

Database Basics

Traditional database organizes data in a hierarchy. A field is the smallest element. Fields are within a record. Records are organized in a data file. Database is a collection of data files.

10

Page 11: What is dbms

Levels of Abstraction

Physical Schema

Conceptual Schema

View 1 View 2 View 3

DB

Users• Many views:

– Views describe how users see the data.

• conceptual (logical) schema– defines logical structure of DB and

relationship between tables.

• physical schema:– It describes how data is stored in a

DB such as disk, tape, CD, or other media.

11

Page 12: What is dbms

Data Independence• Applications insulated from how

data is structured and stored.

• Logical data independence: Protection from changes in logical structure of data.

• Physical data independence: Protection from changes in physical structure of data.

Physical Schema

Conceptual Schema

View 1 View 2 View 3

DB

12

Page 13: What is dbms

Example: University Database

• Conceptual schema: – Students(sid: integer, name: string, login: string,

age: integer)– Courses(cid: string, cname: string, credits: integer) – Enrolled(sid: integer, cid:string, grade: string)

• Physical schema:– Relations stored as unordered files. – Index on first column of Students.

• External Schema (View): – Course_info For example: we might want to allow students

to find out the names of faculty’s members teaching courses.

13

Page 14: What is dbms

14

Page 15: What is dbms

• Reduced application development time. • Data independence • Efficient data access.• Data integrity under updates.• Concurrent access• Recovery from crashes.• Security• Uniform data administration.

Why Use a DBMS?

15

Page 16: What is dbms

• Reduced application development time.

Since the DBMS provides several important functions required by applications, such as high level query facilities

16

Page 17: What is dbms

Data independence and efficient access. Database application programs are independent of the details of data representation and storage.

Example – Database MetadataA database system normally contains a lot of data in addition to users’ data. For example, it stores data about data, known as metadata, to locate and retrieve data easily.

• Data independence

17

Page 18: What is dbms

This metadata, see Figure provides information such as field names, labels, primary key, field type (i.e. varchar2 or char).

18

Page 19: What is dbms

• Efficient data access.

A DBMS uses several powerful functions to store and retrieve data efficiently.

Before appear DBMS19

Page 20: What is dbms

• Data integrity under updates.Data integrity means that the data contained in the database is both accurate and consistent. Therefore, data values being entered for the storage could be checked to ensure that they fall within a specified range and are of the correct format.

an example of data integrity, consider the tables employees and departments and the business rules for the information in each of the tables

20

Page 21: What is dbms

• Concurrent access

21

Page 22: What is dbms

• Recovery from crashes.

System Crash there are problems − external to the system − that may cause the system to stop suddenly and cause the system to crash.

For example, interruptions in power supply may cause the failure of underlying hardware or software failure.

DBMS in recovering as well asthe logs of each transaction writing them onto some stable storage before actually modifying the database.

22

Page 23: What is dbms

• SecuritySuch confidential data must not be accessed by unauthorized persons. The DBA who has the ultimate responsibility for the data in theDBMS.

• Uniform data administration.Responsibilities include designing, implementing, and maintaining the database system

23

Page 24: What is dbms

Data Models• A collection of tools for describing

– data – data relationships

24

Page 25: What is dbms

Types of Data Models:

• Entity-Relationship model

• Relational model

• Other models:

– object-oriented model– network model and hierarchical model

25

Page 26: What is dbms

• Entity-Relationship model(or ER model) is a way of graphically representing the logical relationships of entities (or objects) in order to create a database

attributesrelationship entity

Primary Key

26

Page 27: What is dbms

• Relational modelA relationship exists between two database tables when one table has a foreign key that references the primary key of another table.

27

Page 28: What is dbms

PRIMARY KEY ConstraintThe PRIMARY KEY constraint uniquely identifies each record in a database table.

Primary keys must contain UNIQUE values.A primary key column cannot contain NULL values.Most tables should have a primary key, and each table can

have only ONE primary key.

28

Page 29: What is dbms

FOREIGN KEY Constraint

A FOREIGN KEY in one table points to a PRIMARY KEY in another table.an example. Look at the following two tables:

29

Page 30: What is dbms

Types of Relationships » One to One Relationships : Both tables can have only one record on either side of the relationship.

An example of a one-to-one relationship is a Person can have only one Passport and that a Passport can belong to only one Person. Another example of one-to-one relationshipThink about a husband and wife. A husband can only have one wife . A wife can only have one husband

30

Page 31: What is dbms

» One to Many / Many to One Relationships : The primary key table contains only one record that relates to none, one, or many records in the related table.

Example of a one-to-many relationshipThink about a mother and her children.A mother can have many childrenA child can have only one mother

31

Page 32: What is dbms

» Many to Many Relationships : Each record in both tables can relate to any number of records (or no records) in the other table.

Example of a many-to-many relationshipThink about videos in a video hire shop and the shop's customers.A video can be hired by many customersCustomers can hire many videos

32

Page 33: What is dbms

Summary Relational Model

Very simple and elegant data representationEven users can understand the contents of a database

Supports a popular high level query language SQL

Complex queries can be easily expressed

33

Page 34: What is dbms

• Other models: object-oriented model: is a DBMS in which information is

represented in the form of objects as used in OOP.In this example, a Student is a type of Person. Likewise, a

Employee is a type of Person. Both Student and Employee inherit all the attributes and methods of Person.

So, if you would look at a Student object, you would see attributes of name, date of birth, parents, children, and student ID.

34

Page 35: What is dbms

network model and hierarchical model :

network model: In hierarchical DBMS, we can have only one parent to a child. But in Network, we can have more than one.

So the above example, student has two parents which tell us this is the Network DBMS model. 35

Page 36: What is dbms

hierarchical model: The structure allows representing information using parent/child relationships:

each parent can have many children, but each child has only one parent (also known as a 1-to-many relationship).

Books

Data BaseProgramming

SQL Fox Pro Microsoft Access

1:m

1:m

36

Page 37: What is dbms

37

Structured Query Language (SQL)SQL (Structured Query Language) is a database computer language designed for managing data in relational database management systems (RDBMS), and originally based upon Relational Algebra.

It is a set of statements to manage databases, tables, and data.Some common relational database management systems that use SQL are: Oracle, Sybase, Microsoft SQL Server and Microsoft Access.

Page 38: What is dbms

38

Page 39: What is dbms

39

1. Data Definition Language (DDL) :statements are used to define the database structure or schema. Some examples:   SQL for creating, altering and dropping tables Example:Alter a table, you can give the following statement

ALTER TABLE departmentssADD salary number(6,2);

SQL falls into classes:

Page 40: What is dbms

40

2. Data Manipulation Language (DML) : statements are used for managing data within schema objects. Some examples: * SELECT - retrieve data from the a database* INSERT - insert data into a table* UPDATE - updates existing data within a table* DELETE - deletes all records from a table.example: Below is a selection from the “Departmentss" table:

UPDATE Departmentss SET SALARY = 5801 WHERE DP_NAME = ‘COMPUTER’;

Page 41: What is dbms

41

3. Data Control Language (DCL)is used to control privilege in Database. To perform any operation in the database, such as for creating tables, or views we need privileges. DCL defines two commands, Grant : Gives user access privileges to database. Revoke : Take back permissions from user.

Ex: grant create table to username;Another Ex: grant drop any table to username;

Ex: revoke create table from username;

Page 42: What is dbms

42

Relational Data Structure

A relational database : consists of a collection of tables, each of which assigned a unique name.

Attribute :An attribute is a named column of a relation. For example, the information on branch offices is represented by the Branch relation,with columns for attributes branchNo (the branch number), street, city, and postcode.

Tuple : is a row of a relation, The table consists of a number of rows . A row in a table represents a relation among a set of values.

Cardinality: The cardinality of a relation is the number of tuples it contains.

Page 43: What is dbms

43

Degree: The degree of a relation is the number of attributes it contains.The Branch relation in Figure has four attributes or degree four.

Page 44: What is dbms

44

This means that each row of the table is a four-tuple, containing four values, A relation with only one attribute would have degree one and be called a unary relation or one-tuple. A relation with two attributes is called binary, one with three attributes is called ternary, and after that the term n-ary is usually used.

Domain :A domain is the set of allowable values for one or more attributes. The Figure down shows the domains for some ofthe attributes of the Branch and Staff relations

Page 45: What is dbms

45

Alternative terminology

In fact, a third set of terms is sometimes used: a relation may be referred to as a file, the tuples as records, and the attributes as fields.

This terminology stems from the fact that, physically, the RDBMS may store each relation in a file. Table summarizes the different terms for the relational model.

Page 46: What is dbms

46

Instance and schemasDatabase changes over time as information is inserted and deleted.Instance : is the collection of information stored in the database at a particular moment is called an instance of the database.

Schema : the overall design of the database is called the database schema, Schemas are changed infrequently.

For example: the schema that describe the table in could be :Account table-Schema= (Branch-name , Account-number , Balance)

Page 47: What is dbms

47

Relational Algebra and SQL

Page 48: What is dbms

48

Relational Algebra RELATIONAL ALGEBRA IS A COLLECTION OF

OPERATIONS TO MANIPULATE OR ACCESS TABLES.

IT IS A PROCEDURAL LANGUAGE WITH OPERATIONS PERFORMED ON ONE OR MORE EXISTING RELATIONS TO DERIVE RESULTING RELATIONS WITHOUT CHANGING THE ORIGINAL RELATIONS.

RELATIONAL ALGEBRA OPERATINS CAN BE DIVIDED INTO TWO GROUPS i.e

1. SET-ORIENTED OPERATIONS 2. NATIVE(SPECIAL) RELATIONAL OPERATIONS.

Page 49: What is dbms

4949

NATIVE RELATIONAL OPERATIONSNAME SYMBOL

SELECTION σPROJECTION ΠJOIN

BASIC SET ORIENTED OPERATIONSNAME SYMBOL

UNION U

INTERSECTION ∩DIFFERENCE -PRODUCT ×

Page 50: What is dbms

50

Selection (or Restriction)• predicate (R)

– Works on a single relation R and defines a relation that contains only those tuples (rows) of R that satisfy the specified condition (predicate).

ID NAME SALARY AGE

101 AMIT 4500 30102 AJAY 5000 35105 JOHN 5000 36106 ABHI 4000 28107 ALI 4800 31108 SUNIL 3500 33

EMPLOYEE Example

RESULTING TABLE

σAGE >32 (EMPLOYEE)

ID NAME SALARY AGE

102 AJAY 5000 35105 JOHN 5000 36108 SUNIL 3500 33

Page 51: What is dbms
Page 52: What is dbms

52

Projection

•  ∏ (pi) symbol used to choose attributes from a relation.

∏ <attribute list>(relation)• col1, . . . , coln(R)

– Works on a single relation R and defines a relation that contains a vertical subset of R, extracting the values of specified attributes and eliminating duplicates.

Page 53: What is dbms

53

Example

Id Name Address Hobby

1123 John 123 Main stamps

1123 John 123 Main coins

5556 Mary 7 Lake Dr hiking

9876 Bart 5 Pine St stamps

Person

∏Name,Hobby(Person)Name Hobby

John stamps

John coins

Mary Hiking

Bart stamps

Page 54: What is dbms

5454

Another example

EMPLOYEE ID NAME SALARY AGE

101 AMIT 4500 30102 AJAY 5000 35105 JOHN 5000 36106 ABHI 4000 28107 ALI 4800 31108 SUNIL 3500 33109 JOHN 4600 29

RESULTING TABLES

A. π ID (EMPLOYEE) B. π NAME,AGE (EMPLOYEE)

NAME AGE

AMIT 30AJAY 35JOHN 36ABHI 28ALI 31SUNIL 33JOHN 29

ID

101102105106107108109

B

A

Page 55: What is dbms

55

Union• R S

– Union of two relations R and S defines a relation that contains all the tuples of R, or S, or both R and S, duplicate tuples being eliminated.

– Example: we have two table

Page 56: What is dbms

56

Example

A RESULT=STUDENT INSTRUCTORB

SQL for previous example Fig 6.4: (SELECT Fn, Ln FROM STUDENT) UNION (SELECT Fname, Lname FROM INSTRUCTOR);

Page 57: What is dbms

57

Set Difference• R – S

– Defines a relation consisting of the tuples that are in relation R, but not in S.

Page 58: What is dbms

SET DIFFERENCE operationExample

B

SQL for previous example Fig : (SELECT Fn, Ln FROM STUDENT) MINUS(SELECT Fname, Lname FROM INSTRUCTOR);

(d) RESULT=INSTRUCTOR - STUDENTA B

(e) RESULT=STUDENT - INSTRUCTOR

INSTRUCTOR - STUDENT

STUDENT - INSTRUCTOR

58

Page 59: What is dbms

59

Intersection• R S

– Defines a relation consisting of the set of all tuples that are in both R and S.

• Expressed using basic operations:R S = R – (R – S)

Page 60: What is dbms

INTERSECTION operationExample

RESULT=STUDENT INSTRUCTOR

SQL for previous example Fig 6.4: (SELECT Fn, Ln FROM STUDENT) INTERSECT (SELECT Fname, Lname FROM INSTRUCTOR);

A B

60

Page 61: What is dbms

61

Cartesian product• R X S

– Defines a relation that is the concatenation of every tuple of relation R with every tuple of relation S.

Page 62: What is dbms

62

CARTESIAN PRODUCT operation

Example

B

SQL: SELECT * FROM Faculty, Student;

FacSSN 111-11-1111 222-22-2222 333-33-3333

FacSSN StdSSN 111-11-1111 111-11-1111 111-11-1111 444-44-4444 111-11-1111 555-55-5555 222-22-2222 111-11-1111 222-22-2222 444-44-4444 222-22-2222 555-55-5555 333-33-3333 111-11-1111 333-33-3333 444-44-4444 333-33-3333 555-55-5555

Faculty

Student

Faculty PRODUCT Student

StdSSN 111-11-1111 444-44-4444 555-55-5555

Relational Algebra: RESULT= Faculty Student✕

62

Page 63: What is dbms

63

ROLL.NO NAME

12502 SORAN12505 KAMAL12507 NORA12521 ASO

A

ROLL.NO NAME

12507 SALWA12519 AKO

B

R=A×BROLL.NO NAME ROLL.NO NAME

12502 SORAN 12507 SALWA12502 SORAN 12519 AKO12505 KAMAL 12507 SALWA12505 KAMAL 12519 AKO12507 NORA 12507 SALWA12507 NORA 12519 AKO12521 ASO 12507 SALWA12521 ASO 12521 AKO

Another example:

Page 64: What is dbms

64

JOIN • The JOIN operation is denoted by the R S symbol .

• A JOIN is a means for combining fields from two tables by using values common to each.

Table Joining is a formal specification of which column(s) in a row in one table should be matched to a column(s) in a row in another table.

• The notation used is R JOIN join condition S

Page 65: What is dbms

65

There are kinds of SQL joins:1- INNER JOIN2- OUTER [LEFT | RIGHT | FULL] JOIN3- NATURAL JOIN4- CROSS JOIN

Assume that we have two basic tables, TableA and TableB, which are filled with some example data. Since we'll be joining tables on namecolumn, we distinguish the rows of the same name by highlighting them red.

Page 66: What is dbms

66

CROSS JOIN

A CROSS JOIN is a Cartesian product of TableA and TableB. Every row from TableA is matched with every row from TableB; that’s why a CROSS JOIN doesn’t make sense in most situations.

Page 67: What is dbms

67

select *from tableAcross join tableB;

Page 68: What is dbms

68

TableA and TableB contain 4 rows. The resulting table will have 4 * 4 = 16 rows and will look as follows:

Page 69: What is dbms

69

INNER JOINAn INNER JOIN merges ONLY the matching rows in BOTH tables. A JOIN without any other JOIN keywords (like INNER, OUTER, LEFT, etc) is an INNER JOIN. Results are found in the overlapping area.

Page 70: What is dbms

70

SELECT *from tableA inner join tableBon tableA.name = tableB.name

The resulting table will be as follows:

Page 71: What is dbms

71

OUTER JOINSFULL OUTER JOIN returns matched and unmatched rows from both tables (it's an union of both). If there is no match, the missing side will contain null.

Page 72: What is dbms

72

Select *FROM TableAFULL OUTER JOIN TableBOn TableA.name = TableB.name;

Page 73: What is dbms

73

The resulting table will be as follows:

Page 74: What is dbms

74

A LEFT OUTER JOIN returns all rows from the left table (TableA) with the matching rows from the right table (TableB) or null – if there is no match in the right table.The results can be found in the entire left circle:

Page 75: What is dbms

75

Select *FROM TableALEFT OUTER JOIN TableBon tableA.name = tableB.name;

The resulting table will be as follows:

Page 76: What is dbms

76

A RIGHT OUTER JOIN returns all rows from the right table (TableB) with the matching rows from the left table (TableA) or null – if there is no match in the left table.The results can be found in the entire right circle:

Page 77: What is dbms

77

Select *FROM tableARIGHT OUTER JOIN tableBOn tableA.name = tableB.name

The resulting table will be as follows:

Page 78: What is dbms

78

Joins based on operatorsEqui-join implementationThis JOIN is made by using the equality-operator (=) to compare values of the Primary Key of one table and the Foreign Key values of another table.

SELECT  *FROM TableAINNER/OUTER JOIN TableBON TableA.PK =TableB.Fk;

Theta-join implementation (non-equi)This is the same as the equi JOIN but it allows all other operators like >, <, >= etc.

Select *FROM TableA INNER/OUTER JOIN TableBOn tableA.Pk <= tableB.Fk;

Page 79: What is dbms

79

Indexing and Hashingan index is any data structure that takes as input a property of records - typically the value of one or more fields - and finds the records with that property "quickly." In particular,an index lets us find a record without having to look at more than a small fraction of all possible records. The field(s) on whose values the index is based is called the search key. or just "key" if the index is understood.

Page 80: What is dbms

80

The Concept of Index• Searching a Book…

• Index– A data structure that helps us find data quickly

Index by Author

Index by Title

Databaseby Rao

Databaseby Rui

Programmingby Alistair

Catalogs Books

Where are books by

Rui ?

Where is “Database” ?

Page 81: What is dbms

81

Basic Concepts• Indexing mechanisms used to speed up access to desired

data.– E.g., author catalog in library

• Search Key - attribute to set of attributes used to look up records in a file.

• An index file consists of records (called index entries) of the form

Index files are typically much smaller than the original file • Two basic kinds of indices:

– Ordered indices: search keys are stored in sorted order

– Hash indices: search keys are distributed uniformly across “buckets” using a “hash function”.

search-key pointer

Page 82: What is dbms

82

Ordered Indices :These are used to gain fast random access to records in a file. Each index structure is associated with a particular search key.

The index stores the values of the search keys in sorted order.The record in the indexed file may themselves be sorted in some way. The file may have several indices of different search key.

Primary index : if the file containing the record is sequentially ordered , the index whose search key specifies the sequential order of the file , this index is a primary index for that file.

Secondary index : is the index of the file whose search key specifies an order different from the order of the file.

Page 83: What is dbms

83

Ordered Primary IndexThe figure shows an ordered file for account records.

Page 84: What is dbms

84

The file of figure 6.1 is sorted on a search key order, with branch name is used as the search key.Indices Types : There are two types of ordered indices : Dense and Sparse indices.Dense index : an index entry appears for every search key value in the file. The index record contains the search key value and a pointer to the first data record with that search key value, as shown in figure for the account file.

Page 85: What is dbms

85

Suppose that we are locking up records for the PEERYIDGE branch using the dense index of figure , we follow the pointer directly to the first PEERYIDGE record .

We process this record and follow the pointer in that record to locate the next record in search key (branch name) order.

We continue processing records until we encounter a record for a branch other than PEERYIDGE.

Page 86: What is dbms

86

Spars index : An index record is created for only some of the values. To locate a record we find the index entry with the largest search key value that is less than or equal to the search key value for which we are locking . We start at the record pointed to by that index entry, and follow the pointer in the file until we find the desired record. As shown in figure for the account file.

Page 87: What is dbms

87

If we are using the spars index of figure to find PEERYIDGE , we do not find an index entry for PEERYIDGE in the index. Since the last entry (in alphabetic order) before BEERYIDGE is MIANUS , we follow that pointer.We then read the account file in sequential order until we find the first PEERYIDEG record and begin processing at that point.As we have seen , it is faster to locate a record by using a dense index rather than a sparse index, but sparse indices require less space.

Index UpdateEvery index must be updated whenever a record is inserted into the file or deleted from the file .Deletion : To delete a record we first look up the record to be deleted .In dense indices ,if the deleted record was the only record with its particular search key value, then the search key value is deleted from the index.For sparse indices , we delete a key value by replacing its entry (if one exist) in theindex with the next search key value . if the next search key value already has anindex entry , the entry is deleted instead of being replace.Insertion : First we perform a lookup using the search key value that appears in the record to be inserted . In dense index and the value does not appear in the index, the value is inserted in the index.

Page 88: What is dbms

88

Page 89: What is dbms

05/01/2023 89Yan Huang - ER

Notations

Page 90: What is dbms

05/01/2023 90Yan Huang - ER

Notations

Page 91: What is dbms

FOREIGN KEY Constraint

A FOREIGN KEY in one table points to a PRIMARY KEY in another table.an example. Look at the following two tables:

91

Page 92: What is dbms

CREATE TABLE Orders(O_Id int NOT NULL PRIMARY KEY,OrderNo int NOT NULL,P_Id int FOREIGN KEY REFERENCES Persons(P_Id))

FOREIGN KEY Constraint on ALTER TABLETo create a FOREIGN KEY constraint on the "P_Id" column when the "Orders" table is already created, use the following: ALTER TABLE OrdersADD FOREIGN KEY (P_Id)REFERENCES Persons(P_Id)

92

Page 93: What is dbms

To allow naming of a FOREIGN KEY constraint, and for defining a FOREIGN KEY constraint on multiple columns, use the following:

ALTER TABLE OrdersADD CONSTRAINT fk_PerOrdersFOREIGN KEY (P_Id)REFERENCES Persons(P_Id)

To DROP a FOREIGN KEY ConstraintTo drop a FOREIGN KEY constraint, use the following SQL:

ALTER TABLE OrdersDROP CONSTRAINT fk_PerOrders

93

Page 94: What is dbms

94

Page 95: What is dbms

95

Page 96: What is dbms

96

Page 97: What is dbms

97

Page 98: What is dbms

98