0% found this document useful (0 votes)
11 views

Database-Systems-Overview

Uploaded by

Maverick
Copyright
© © All Rights Reserved
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
11 views

Database-Systems-Overview

Uploaded by

Maverick
Copyright
© © All Rights Reserved
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 52

Database Systems

 DBMS Fundamental Concepts


Table of Contents
1. Database Models
2. Relational Database Model
3. DBMS & RDBMS Systems
4. Tables, Relationships, Multiplicity, E/R Diagrams
5. Normalization
6. Constraints
7. Indices
8. The SQL language
2
Table of Contents (2)

9. Stored Procedures
10. Views
11. Triggers
12. Transactions and Isolation Levels
13. NoSQL Databases

3
RDBMS Systems
Relational Databases, Database Servers and RDBMS
Relational Databases

 Database models
 Hierarchical (tree)
 Network / graph
 Relational (tables)
 Object-oriented
 Relational databases
 Represent a bunch of tables together with the relationships between them
 Rely on a strong mathematical foundation: the relational algebra

5
Relational Database Management
System
 Relational Database Management Systems (RDBMS) manage data
stored in tables
 RDBMS systems typically implement
 Creating / altering / deleting tables and relationships between them
(database schema)
 Adding, changing, deleting, searching and retrieving of data ows stored in
the tables
 Support for the SQL language
 Transaction management (optional)

6
RDBMS Systems

 RDBMS systems are also known as:


 Database management servers
 Or just database servers
 Popular RDBMS servers:
 Microsoft SQL Server
 Oracle Database
 MySQL
 PostgreSQL
 SQLite

7
Tables and Relationships
Database Tables, Relationships, Multiplicity
Tables
 Database tables consist of data, arranged in rows and columns
 For example (table Persons):

Id First Name Last Name Employer


1 Svetlozar Mirov VMWare
2 Hristo Tenchev XS Software
3 Vladimir Georgiev Huawei

 All rows have the same structure


 Columns have name and type (number, string, date, image, …)
Table Schema

 The schema of a table is an ordered sequence of column


specifications (name and type)
 For example the Persons table has the following schema:

Persons (
Id: number,
FirstName: string,
LastName: string,
Employer: string
)
10
Primary Key

 Primary key is a column of the table that uniquely


identifies its rows (usually its is a number)
Id First Name Last Name Employer
Primary
1 Svetlin Nakov SoftUni
key
2 Hristo Tenchev XS Software
3 Vladimir Georgiev SoftUni

 Two records (rows) are different if and only if their


primary keys are different
 Composite primary key – composed by several columns
11
Relationships

 Relationships between tables are based on interconnections: primary


key / foreign key
Primary Foreign
Key Towns Key Prima
ry Key Countries
Id Name CountryId
Id Name
1 Sofia 1
1 Bulgaria
2 Varna 1
2 Germany
3 Munich 2
3 Russia
4 Berlin 2
5 Moscow 3

12
Relationships (2)

 The foreign key is an identifier of a record located in


another table (usually its primary key)
 By using relationships we avoid repeating data in the
database
 Inthe last example the name of the country is not
repeated for each town (its number is used instead)
 Relationships have multiplicity:
 One-to-many – e.g. country / towns
 Many-to-many – e.g. student / course
 One-to-one – e.g. example human / student
13
Relationships' Multiplicity – One-To-
Many
 Relationship one-to-many (or many-to-one) – used often
 A single record in the first table has many corresponding records in the
second table
Towns
Id Name CountryId Countries
1 Sofia 1 Id Name
2 Varna 1 1 Bulgaria
3 Munich 2 2 Germany
4 Berlin 2 3 Russia
5 Moscow 3

14
Relationships' Multiplicity – Many-To-
Many
 Relationship many-to-many
 Records in the first table have many corresponding records in the second one and vice versa
 Implemented through additional table

Students StudentsCourses
Id Name
StudentId CourseId Courses
1 1 Id Name
1 Pesho
1 2
2 Minka 1 .NET
3 2
3 Gosho 2 Databases
3 3
4 Jivka 3 JavaScript
4 2
15
Relationships' Multiplicity – One-To-
One
 Relationship one-to-one

A single record in a table corresponds to a single


record in the other table
 Used to model inheritance between tables

Primary & Professors


Prima Id Title
foreign key
ry Key 1 Ph.D.
Persons
Id Name Age Students
1 Baba Mara 67 Id Specialty
2 Computer Science
2 Stancho 33
3 Chemistry
3 Bay Gosho 45 16
Representing Hierarchical Data

 How do we represent trees and graphs?


Root

Docume
Pictures
nts

Birthda
y Party 17
Self-Relationships

 The primary / foreign key relationships can point to one and the same
table
 Example: folders hold sub-folders
Primary Foreign Self-
Key Key Relationship
Folders
Id Folder ParentId
1 Root NULL
2 Documents 1
3 Pictures 1
4 Birthday Party 3
18
E/R Diagrams
Entity / Relationship Diagrams
and DB Modeling Tools
Relational Schema

 Relational schema of a DB is the collection of:


 The schemas of all tables
 Relationships between the tables
 Any other database objects (e.g. constraints)
 The relational schema describes the structure of the database
 Doesn't contain data, but metadata
 Relational schemas are graphically displayed in Entity / Relationship
diagrams (E/R Diagrams)

20
E/R Diagrams – Examples The diagram is
created with
Microsoft SQL
Server
Management
Studio

21
E/R Diagrams – Examples (2) The
diagram is
created
with
ERwin

22
E/R Diagrams – Examples (3)The diagram is
created with
fabFORCE DB
Designer for
MySQL

23
E/R Diagrams – Examples (4)
The diagram is
created with MS
Visio
DepartmentProfessor Person
Professor
PK,FK1 DeptId PK PersonId
PK,FK1 ProfessorId
PK,FK2 ProfessorId
Title FirstName
LastName

Course
Department
PK CourseId
PK DeptId Student
CourseName
PK,FK1 StudentId
DeptName FK1 DeptId
FK2 ProfessorId
FacultyNumber
CourseStudent

PK,FK1 StudentId
PK,FK2 CourseId

24
Tools for E/R Design

 Data modeling tools allow building E/R diagrams, generate / import


DB schemas:
 SQL Server Management Studio
 MySQL Workbench
 Oracle JDeveloper
 Microsoft Visio
 CASE Studio
 Computer Associates ERwin
 IBM Rational Rose

25
DB Normalization
Avoiding Duplicated Data through Database Schema Normalization
Normalization

 Normalization of the relational schema removes repeating data


 Non-normalized schemas can contain many repeated data, e.g.

Product Producer Pric Categor Shop Town


e y
yoghurt Mlexis Ltd. 0.67 food store Sofia
"Mente"
bread Bakery "Smoky" 0.85 food store Sofia
"Dobrudja" "Mente"
beer Zagorka Corp. 0.68 soft stall "non- Varna
"Zagorka" drinks stop"27
beer "Tuborg" Shoumen Drinks 0.87 soft stall "non- Varna
Normalization (6)
 Example of fully normalized schema (in 4th Normal Form):
Products
I Product Produce Pric Categor Shop TownI
d rId e yId Id d
1 Youghurt 2 0.67 2 4 1
2 bread 3 0.55 2 4 1
"Dobrudja"
3 rakia 6 4.38 5 2 1
"Peshtera"
4 beer "Tuborg" 4 0.67 4 1 3

Producers Categories Shops Towns


I Name I Name I Name I Name
d d d d
2 "Milk" Ltd. 4 beer 1 Billa 1 Sofia
32
4 "Zagorka" 2 food 4 METR 3 Varna
AD O
Other Database Objects
Constraints, Indices, SQL, Stored Procedures, Views, Triggers
Integrity Constraints

 Integrity constraints ensure data integrity in the database tables


 Enforce data rules which cannot be violated
 Primary key constraint
 Ensures that the primary key of a table has unique value for each table
row
 Unique key constraint
 Ensures that all values in a certain column (or a group of columns) are
unique

34
Integrity Constraints (2)

 Foreign key constraint


 Ensures that the value in given column is a key from another table
 Check constraint
 Ensures that values in a certain column meet some predefined condition
 Examples:

(hour >= 0) AND (hour < 24)

name = UPPER(name) 35
Indices

 Indices speed up searching of values in a certain column or group of


columns
 Usually implemented as B-trees
 Indices can be built-in the table (clustered) or stored externally (non-
clustered)
 Adding and deleting records in indexed tables is slower!
 Indices should be used for big tables only (e.g. 50 000 rows)

36
The SQL Language

 SQL (Structured Query Language)


 Standardized declarative language for manipulation of relational
databases
 SQL-2011 is currently in use in most databases

 http://en.wikipedia.org/wiki/SQL#Standard
ization
 SQL language supports:
 Creating, altering, deleting tables and other DB objects
 Searching, retrieving, inserting, modifying and deleting table data (rows)

37
The SQL Language (2)

 SQL consists of:


 DDL – Data Definition Language
 CREATE, ALTER, DROP commands
 DML – Data Manipulation Language
 SELECT, INSERT, UPDATE, DELETE commands

 Example of SQL SELECT query:

SELECT Towns.Name, Countries.Name


FROM Towns, Countries
WHERE Towns.CountryId = Countries.Id 38
Stored Procedures

 Stored procedures (database-level procedures)


 Consist of SQL-like code stored in the database
 Code executed inside the database server
 Much faster than an external code
 Data is locally accessible
 Can accept parameters
 Can return results
 Single value
 Record sets

39
Stored Procedures (2)

 Stored procedures are written in a language extension of SQL


 T-SQL – in Microsoft SQL Server
 PL/SQL – in Oracle
 Example of stored procedure in Oracle PL/SQL:

CREATE OR REPLACE PROCEDURE spInsertCountry(countryName


varchar2) IS
BEGIN
INSERT INTO Countries(Name)
VALUES(countryName);
END; 40
Views

 Views are named SQL SELECT queries which are used as tables
 Simplify data access
 Facilitate writing of complex SQL queries
 Used also to apply security restrictions:
 E.g. a certain user isn't given permissions on any of the tables in the
database
 The user is given permissions on few views (subset of DB) and few stored
procedures only

41
Views – Example
Companies Towns
I Company TownI I Town County
d d d Id
1 Mente LTD 1 1 Sofia 1
2 BulkSoft 2 2 New 3
Inc. York
3 HardSoft 4 3 Moscow 2
Corp.
Countries 4 Plovdiv 1
4 Sputnik 3
Corp. I Countr
d y
1 Bulgari
a 42

2 Russia
Views – Example (2)
CREATE VIEW V_BGCompanies AS
SELECT
Companies.Id AS Id,
Companies.Company AS Company
FROM Companies INNER JOIN
(Towns INNER JOIN Countries ON
Towns.CountryId = Countries.Id)
ON Companies.TownId = Towns.Id
WHERE
Countries.Country = "Bulgaria";

Id Company

V_BGCompanies 1 Mente Ltd.


3 HardSoft Corp. 43
Triggers

 Triggers are special stored procedures that are activate when some
event occurs, for instance:
 When inserting a record
 When changing a record
 When deleting a record
 Triggers can perform additional data processing, e.g.
 To change the newly added data
 To maintain logs and history on change

44
Triggers – Example

 We have a table holding company names:

CREATE TABLE Companies(


Id number NOT NULL,
Name varchar(50) NOT NULL)
 A trigger that appends "Ltd." at the end of the name of a new
company:

CREATE OR REPLACE TRIGGER


trg_Companies_INSERT
BEFORE INSERT ON Company
FOR EACH ROW
BEGIN
:NEW.Name := :NEW.Name || ' Ltd.'; 45

END;
Transactions
ACID Transactions and Isolation
Transactions

 Transactions are a sequence of database operations which are


executed as a single unit:
 Either all of them execute successfully
 Or none of them is executed at all
 Example:
 A bank transfer from one account into another (withdrawal + deposit)
 If either the withdrawal or the deposit fails the entire operation should be
cancelled

47
DB Transactions Lifecycle

Read Write

Durabl Writ Sequence Commit Durable,


e e of reads consisten
startin and t,
g state writes ending
Rollback state

48
Transactions Behavior

 Transactions guarantee the consistency and the integrity of the


database
 All changes in a transaction are temporary
 Changes become final when COMMIT is successfully executed
 At any time all changes done in the transaction can be canceled by
executing ROLLBACK
 All operations are executed as a single unit
 Either all of them pass or none of them

49
NoSQL Databases
Non-Relational Database Systems
Non-Relational Data Models

 Document model (e.g. MongoDB, CouchDB)


 Set of documents, e.g. JSON strings
 Key-value model (e.g. Redis)
 Set of key-value pairs
 Wide-column model (e.g. Cassandra)
 Key-value model with schema
 Object model (e.g. Caché)
 Set of OOP-style objects

51
What is NoSQL Database?

 NoSQL (non-relational) databases


 Use document-based model (non-relational)
 Schema-free document storage
 Still support CRUD operations
(create, read, update, delete)
 Still support indexing and querying
 Still supports concurrency and transactions
 Highly optimized for append / retrieve
 Great performance and scalability
 NoSQL == “No SQL” or “Not Only SQL”?

52
Relational vs. NoSQL Databases

 Relational databases
 Data stored as table rows
 Relationships between related rows
 Single entity spans multiple tables
 RDBMS systems are very mature, rock solid
 NoSQL databases
 Data stored as documents
 Single entity (document) is a single record
 Documents do not have a fixed structure

53
Relational vs. NoSQL
Relational Model Models
Document Model
Name Svetlin
Nakov Name: Svetlin Nakov
Gender male Gender: male
Phone +359333777 Phone: +359333777555
555
Address:
Email nakov@abv.
* - Street: Tintyava 15-17
bg1
Street
Site Tintyava 15-
www.nakov. - Post Code: 1113
17
com - Town: Sofia
Post 1113
*
1 - Country: Bulgaria
Code
Town Sofia Email: [email protected]
*
1 Site: www.nakov.com
Country Bulgaria 54
NoSQL Database Systems

 Redis
 Ultra-fast in-memory data structures server
 MongoDB
 Mature and powerful JSON-document database
 CouchDB
 JSON-based document database with REST API
 Cassandra
 Distributed wide-column database


DB Ranking: http://db-engines.com/en/ranking
55
Summary / Questions

 What is relational database?


 Examples of RDBMS?
 What is E/R data model?
 What is primary key?
 What relationships do you know?
 What is constraint?
 What is transaction?
 What
56

is NoSQL database?

You might also like