5. relational databases fundamental concepts - c# and databases
DESCRIPTION
Relational Databases - Fundamental ConceptsTelerik Software Academy: http://academy.telerik.com/school-academy/meetings/details/2012/01/06/desktop-applications-csharp-databasesThe website and all video materials are in Bulgarian.Database ModelsRelational Database ModelsRDBMS SystemsTables, Relationships,Multiplicity, E/R DiagramsNormalizationConstraintsIndicesThe SQL languageStored ProceduresViewsTriggersTransactions and Isolation LevelsTRANSCRIPT
![Page 1: 5. Relational Databases Fundamental Concepts - C# and Databases](https://reader036.vdocuments.mx/reader036/viewer/2022062307/555a0391d8b42ad00a8b50d8/html5/thumbnails/1.jpg)
C# and Databases free course
Svetlin Nakov
Telerik Software Academyhttp://academy.telerik.com/
Manager Technical Trainerhttp://www.nakov.com
/
Relational Databases
Fundamental Concepts
![Page 2: 5. Relational Databases Fundamental Concepts - C# and Databases](https://reader036.vdocuments.mx/reader036/viewer/2022062307/555a0391d8b42ad00a8b50d8/html5/thumbnails/2.jpg)
Table of Contents
1. Database Models
2. Relational Database Models
3. RDBMS Systems
4. Tables, Relationships, Multiplicity, E/R Diagrams
5. Normalization
6. Constraints
7. Indices
2
![Page 3: 5. Relational Databases Fundamental Concepts - C# and Databases](https://reader036.vdocuments.mx/reader036/viewer/2022062307/555a0391d8b42ad00a8b50d8/html5/thumbnails/3.jpg)
Table of Contents (2)
8. The SQL language
9. Stored Procedures
10. Views
11. Triggers
12. Transactions and Isolation Levels
3
![Page 4: 5. Relational Databases Fundamental Concepts - C# and Databases](https://reader036.vdocuments.mx/reader036/viewer/2022062307/555a0391d8b42ad00a8b50d8/html5/thumbnails/4.jpg)
RDBMS SystemsRelational Databases, Database Servers
and RDBMS
![Page 5: 5. Relational Databases Fundamental Concepts - C# and Databases](https://reader036.vdocuments.mx/reader036/viewer/2022062307/555a0391d8b42ad00a8b50d8/html5/thumbnails/5.jpg)
Relational Databases Database models
Hierarchical (tree) Network / graph Relational (table) 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
![Page 6: 5. Relational Databases Fundamental Concepts - C# and Databases](https://reader036.vdocuments.mx/reader036/viewer/2022062307/555a0391d8b42ad00a8b50d8/html5/thumbnails/6.jpg)
Relational Database Management System
(RDBMS) 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 stored in the tables
Support for SQL language
Transaction management (optional)
6
![Page 7: 5. Relational Databases Fundamental Concepts - C# and Databases](https://reader036.vdocuments.mx/reader036/viewer/2022062307/555a0391d8b42ad00a8b50d8/html5/thumbnails/7.jpg)
RDBMS Systems RDBMS systems are also known as:
Database management servers Or just database servers
Popular RDBMS servers: Microsoft SQL Server Oracle Database IBM DB2 PostgreSQL MySQL Borland Interbase, SQLite, …
7
![Page 8: 5. Relational Databases Fundamental Concepts - C# and Databases](https://reader036.vdocuments.mx/reader036/viewer/2022062307/555a0391d8b42ad00a8b50d8/html5/thumbnails/8.jpg)
Tables and RelationshipsDatabase Tables, Relationships,
Multiplicity
![Page 9: 5. Relational Databases Fundamental Concepts - C# and Databases](https://reader036.vdocuments.mx/reader036/viewer/2022062307/555a0391d8b42ad00a8b50d8/html5/thumbnails/9.jpg)
Database tables consist of data, arranged in rows and columns For example (table Persons):
All rows have the same structure Columns have name and type
(number, string, date, image, or other)
Id First Name
Last Name
Employer
1 Svetlin Nakov Telerik
2 Stephen Forte Telerik
3 Steve Jobs Apple
Tables
9
![Page 10: 5. Relational Databases Fundamental Concepts - C# and Databases](https://reader036.vdocuments.mx/reader036/viewer/2022062307/555a0391d8b42ad00a8b50d8/html5/thumbnails/10.jpg)
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:
10
Persons ( Id: number, FirstName: string, LastName: string, Employer: string)
![Page 11: 5. Relational Databases Fundamental Concepts - C# and Databases](https://reader036.vdocuments.mx/reader036/viewer/2022062307/555a0391d8b42ad00a8b50d8/html5/thumbnails/11.jpg)
Primary Key Primary key is a column of the table
that uniquely identifies its rows (usually its is a number)
Two records (rows) are different if and only if their primary keys are different
The primary key can be composed by several columns (composite primary key)
Id First Name
Last Name
Employer
1 Svetlin Nakov Telerik
2 Stephen Forte Telerik
3 Steve Jobs Apple
11
Primary key
![Page 12: 5. Relational Databases Fundamental Concepts - C# and Databases](https://reader036.vdocuments.mx/reader036/viewer/2022062307/555a0391d8b42ad00a8b50d8/html5/thumbnails/12.jpg)
Relationships Relationships between tables are based on interconnections: primary key / foreign key
12
Id Name CountryId1 Sofia 12 Plovdiv 13 Munich 24 Berlin 25 Moscow 3
Id Name1 Bulgari
a2 German
y3 Russia
Towns
Countries
Primary key Prima
ry key
Foreign key
![Page 13: 5. Relational Databases Fundamental Concepts - C# and Databases](https://reader036.vdocuments.mx/reader036/viewer/2022062307/555a0391d8b42ad00a8b50d8/html5/thumbnails/13.jpg)
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
In the 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
![Page 14: 5. Relational Databases Fundamental Concepts - C# and Databases](https://reader036.vdocuments.mx/reader036/viewer/2022062307/555a0391d8b42ad00a8b50d8/html5/thumbnails/14.jpg)
Relationships' Multiplicity
Relationship one-to-many (or many-to-one) A single record in the first table has
many corresponding records in the second table
Used very often
14
Id Name CountryId
1 Sofia 12 Plovdiv 13 Munich 24 Berlin 25 Moscow 3
Id Name1 Bulgaria2 German
y3 Russia
Towns
Countries
![Page 15: 5. Relational Databases Fundamental Concepts - C# and Databases](https://reader036.vdocuments.mx/reader036/viewer/2022062307/555a0391d8b42ad00a8b50d8/html5/thumbnails/15.jpg)
Relationships' Multiplicity (2)
Relationship many-to-many Records in the first table have many
correspon-ding records in the second one and vice versa
Implemented through additional table
15
Id Name
1 Pesho
2 Minka
3 Gosho
4 Penka
Id Name
1 .NET2 Java3 PHP
StudentsCourses
StudentId
CourseId
1 1
1 2
3 2
3 3
4 2
StudentsCourses
![Page 16: 5. Relational Databases Fundamental Concepts - C# and Databases](https://reader036.vdocuments.mx/reader036/viewer/2022062307/555a0391d8b42ad00a8b50d8/html5/thumbnails/16.jpg)
Id Title1 Ph.D.
Relationships' Multiplicity (3)
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
16
Id Name Age1 Ivan
Daddy72
2 Goiko Dude
26
3 Grand Mara
24
Persons
Id Specialty2 Computer
Science
3 Chemistry
Students
Primary & Foreign key in the same time
Primary key
Primary & foreign key in the same time
Professors
![Page 17: 5. Relational Databases Fundamental Concepts - C# and Databases](https://reader036.vdocuments.mx/reader036/viewer/2022062307/555a0391d8b42ad00a8b50d8/html5/thumbnails/17.jpg)
Representing Hierarchical Data
How do we represent trees and graphs?
17
Root
Documents
Pictures
Birthday Party
![Page 18: 5. Relational Databases Fundamental Concepts - C# and Databases](https://reader036.vdocuments.mx/reader036/viewer/2022062307/555a0391d8b42ad00a8b50d8/html5/thumbnails/18.jpg)
Self-Relationships The primary / foreign key relationships can point to one and the same table Example: employees in a company
have a manager, who is also an employee
18
Id Folder ParentId1 Root (null)2 Documents 13 Pictures 14 Birthday Party 3
EmployeesPrimary key
Foreign key
Self-relations
hip
![Page 19: 5. Relational Databases Fundamental Concepts - C# and Databases](https://reader036.vdocuments.mx/reader036/viewer/2022062307/555a0391d8b42ad00a8b50d8/html5/thumbnails/19.jpg)
E/R DiagramsEntity / Relationship
Diagrams and DB Modeling Tools
![Page 20: 5. Relational Databases Fundamental Concepts - C# and Databases](https://reader036.vdocuments.mx/reader036/viewer/2022062307/555a0391d8b42ad00a8b50d8/html5/thumbnails/20.jpg)
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
![Page 21: 5. Relational Databases Fundamental Concepts - C# and Databases](https://reader036.vdocuments.mx/reader036/viewer/2022062307/555a0391d8b42ad00a8b50d8/html5/thumbnails/21.jpg)
E/R Diagrams – Examples
21
The diagram is created with
Microsoft SQL Server
Management Studio
![Page 22: 5. Relational Databases Fundamental Concepts - C# and Databases](https://reader036.vdocuments.mx/reader036/viewer/2022062307/555a0391d8b42ad00a8b50d8/html5/thumbnails/22.jpg)
E/R Diagrams – Examples (2)
22
The diagram
is created
with ERwin
![Page 23: 5. Relational Databases Fundamental Concepts - C# and Databases](https://reader036.vdocuments.mx/reader036/viewer/2022062307/555a0391d8b42ad00a8b50d8/html5/thumbnails/23.jpg)
E/R Diagrams – Examples (3)
23
The diagram is created with fabFORCE DB Designer for
MySQL
![Page 24: 5. Relational Databases Fundamental Concepts - C# and Databases](https://reader036.vdocuments.mx/reader036/viewer/2022062307/555a0391d8b42ad00a8b50d8/html5/thumbnails/24.jpg)
E/R Diagrams – Examples (4)
24
Department
PK DeptId
DeptName
Professor
PK,FK1 ProfessorId
Title
DepartmentProfessor
PK,FK1 DeptIdPK,FK2 ProfessorId
Student
PK,FK1 StudentId
FacultyNumber
Course
PK CourseId
CourseNameFK1 DeptIdFK2 ProfessorId
CourseStudent
PK,FK1 StudentIdPK,FK2 CourseId
Person
PK PersonId
FirstNameLastName
The diagram is created with MS
Visio
![Page 25: 5. Relational Databases Fundamental Concepts - C# and Databases](https://reader036.vdocuments.mx/reader036/viewer/2022062307/555a0391d8b42ad00a8b50d8/html5/thumbnails/25.jpg)
Tools for E/R Design Data modeling tools allow building E/R diagrams, generate / import DB schemas: SQL Server Management Studio
Oracle JDeveloper
Microsoft Visio
CASE Studio
Computer Associates ERwin
IBM Rational Rose
theKompany Data Architect 25
![Page 26: 5. Relational Databases Fundamental Concepts - C# and Databases](https://reader036.vdocuments.mx/reader036/viewer/2022062307/555a0391d8b42ad00a8b50d8/html5/thumbnails/26.jpg)
NormalizationAvoiding Duplicated Data through Database Schema
Normalization
![Page 27: 5. Relational Databases Fundamental Concepts - C# and Databases](https://reader036.vdocuments.mx/reader036/viewer/2022062307/555a0391d8b42ad00a8b50d8/html5/thumbnails/27.jpg)
Normalization Normalization of the relational schema removes repeating data
Non-normalized schemas can contain many data repetitions, e.g.
27
Product Producer
Price
Category Shop Town
yoghurt Mlexis LTD 0.67 food
store "Mente
"Sofia
bread "Dobrudja"
Bakery "Smoky" 0.85 food
store "Mente
"Sofia
beer "Zagorka"
Zagorka CO 0.68 soft
drinks
stall "non-stop"
Varna
beer "Tuborg"
Shumen Drinks
CO0.87 soft
drinks
stall "non-stop"
Varna
![Page 28: 5. Relational Databases Fundamental Concepts - C# and Databases](https://reader036.vdocuments.mx/reader036/viewer/2022062307/555a0391d8b42ad00a8b50d8/html5/thumbnails/28.jpg)
Normalization (2) 1-st Normal Form
Data is stored in tables
Fields in the rows are atomic (inseparable) values
There are no repetitions within a single row
A primary key is defined for each table
28
BookTitleISBN (PK) Author
AuthorEmail
.NET Framework
3847028437 Mr. Kiro bai-
Beginning SQL
7234534450 Santa dedo@mraz.
org
![Page 29: 5. Relational Databases Fundamental Concepts - C# and Databases](https://reader036.vdocuments.mx/reader036/viewer/2022062307/555a0391d8b42ad00a8b50d8/html5/thumbnails/29.jpg)
Normalization (3) 2-nd Normal Form
Retains all requirements of 1-st Normal Form
There are no columns that depend on part of the primary key (if it consists of several columns)
29
BookTitle (PK)
Author (PK) Price AuthorEmai
l.NET
Framework Mr. Kiro 37.25 [email protected]
Beginning SQL Santa Claus 19.95 [email protected]
The price depends on
the book
E-mail depends on the author
![Page 30: 5. Relational Databases Fundamental Concepts - C# and Databases](https://reader036.vdocuments.mx/reader036/viewer/2022062307/555a0391d8b42ad00a8b50d8/html5/thumbnails/30.jpg)
Normalization (4) 3-rd Normal Form
Retains all requirements of 2-nd Normal Form
The only dependencies between columns are of type "a column depends on the PK"
30
Id Product Produc
erIdPric
eCatego
ryIdShopId
TownId
1 Yoghourt 2 0.67 2 4 1
2 bread "Tipov" 3 0.85 2 4 1
3 rakiya "Biserna" 6 6.38 5 2 1
4 beer "Tuborg" 4 0.87 4 1 3
![Page 31: 5. Relational Databases Fundamental Concepts - C# and Databases](https://reader036.vdocuments.mx/reader036/viewer/2022062307/555a0391d8b42ad00a8b50d8/html5/thumbnails/31.jpg)
Normalization (5) 4-th Normal Form
Retains all requirements of 3-rd Normal Form
There is one column at most in each table that can have many possible values for a single key (multi-valued attribute)
31
AuthorId
Book Article
2 .NET Programming
Regular Expressions in .NET
4 Mastering JavaScript
AJAX Performance Patterns
One author can have
many books
One author can have
many articles
![Page 32: 5. Relational Databases Fundamental Concepts - C# and Databases](https://reader036.vdocuments.mx/reader036/viewer/2022062307/555a0391d8b42ad00a8b50d8/html5/thumbnails/32.jpg)
Normalization (6) Example of fully normalized schema (in 4th Normal Form):
32
Id
Name
2 "Milk" Ltd.
4 "Zagorka" AD
Id
Name
4 beer
2 food
Id Product ProducerId Price
CategoryI
d
ShopId
TownId
1 Youghurt 2 0.67 2 4 1
2 bread "Dobrudja" 3 0.55 2 4 1
3 rakia "Peshtera" 6 4.38 5 2 1
4 beer "Tuborg" 4 0.67 4 1 3I
dName
1 Billa
4 METRO
Id
Name
1 Sofia
3 Varna
Products
Producers Categories Shops Towns
![Page 33: 5. Relational Databases Fundamental Concepts - C# and Databases](https://reader036.vdocuments.mx/reader036/viewer/2022062307/555a0391d8b42ad00a8b50d8/html5/thumbnails/33.jpg)
Other Database ObjectsConstraints, Indices, SQL,
Stored Procedures, Views, Triggers
![Page 34: 5. Relational Databases Fundamental Concepts - C# and Databases](https://reader036.vdocuments.mx/reader036/viewer/2022062307/555a0391d8b42ad00a8b50d8/html5/thumbnails/34.jpg)
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
![Page 35: 5. Relational Databases Fundamental Concepts - C# and Databases](https://reader036.vdocuments.mx/reader036/viewer/2022062307/555a0391d8b42ad00a8b50d8/html5/thumbnails/35.jpg)
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:
35
(hour >= 0) AND (hour < 24)
name = UPPER(name)
![Page 36: 5. Relational Databases Fundamental Concepts - C# and Databases](https://reader036.vdocuments.mx/reader036/viewer/2022062307/555a0391d8b42ad00a8b50d8/html5/thumbnails/36.jpg)
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
Adding and deleting records in indexed tables is slower!
Indices should be used for big tables only (e.g. 50 000 rows)
36
![Page 37: 5. Relational Databases Fundamental Concepts - C# and Databases](https://reader036.vdocuments.mx/reader036/viewer/2022062307/555a0391d8b42ad00a8b50d8/html5/thumbnails/37.jpg)
The SQL Language SQL (Structured Query Language)
Standardized declarative language for manipulation of relational databases
SQL-99 is currently in use in most databases
SQL language supports:
Creating, altering, deleting tables and other objects in the database
Searching, retrieving, inserting, modifying and deleting table data (rows)
37
![Page 38: 5. Relational Databases Fundamental Concepts - C# and Databases](https://reader036.vdocuments.mx/reader036/viewer/2022062307/555a0391d8b42ad00a8b50d8/html5/thumbnails/38.jpg)
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:
38
SELECT Towns.Name, Countries.NameFROM Towns, CountriesWHERE Towns.CountryId = Countries.Id
![Page 39: 5. Relational Databases Fundamental Concepts - C# and Databases](https://reader036.vdocuments.mx/reader036/viewer/2022062307/555a0391d8b42ad00a8b50d8/html5/thumbnails/39.jpg)
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
![Page 40: 5. Relational Databases Fundamental Concepts - C# and Databases](https://reader036.vdocuments.mx/reader036/viewer/2022062307/555a0391d8b42ad00a8b50d8/html5/thumbnails/40.jpg)
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:
40
CREATE OR REPLACE PROCEDURE spInsertCountry(countryName varchar2) ISBEGIN INSERT INTO Countries(Name) VALUES(countryName);END;
![Page 41: 5. Relational Databases Fundamental Concepts - C# and Databases](https://reader036.vdocuments.mx/reader036/viewer/2022062307/555a0391d8b42ad00a8b50d8/html5/thumbnails/41.jpg)
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
![Page 42: 5. Relational Databases Fundamental Concepts - C# and Databases](https://reader036.vdocuments.mx/reader036/viewer/2022062307/555a0391d8b42ad00a8b50d8/html5/thumbnails/42.jpg)
Views – Example
42
Id Company TownId
1 Mente LTD 1
2 BulkSoft Inc.
2
3 HardSoft Corp.
4
4 Sputnik Corp.
3
Id Town CountryId
1 Sofia 1
2 New York
3
3 Moscow
2
4 Plovdiv 1
Companies Towns
Id Country
1 Bulgaria
2 Russia
3 USA
Countries
![Page 43: 5. Relational Databases Fundamental Concepts - C# and Databases](https://reader036.vdocuments.mx/reader036/viewer/2022062307/555a0391d8b42ad00a8b50d8/html5/thumbnails/43.jpg)
Views – Example (2)
43
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 Company1 Mente Ltd.
3 HardSoft Corp.V_BGCompanies
![Page 44: 5. Relational Databases Fundamental Concepts - C# and Databases](https://reader036.vdocuments.mx/reader036/viewer/2022062307/555a0391d8b42ad00a8b50d8/html5/thumbnails/44.jpg)
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 of the affected rows, e.g. To change the newly added data
To maintain logs and history44
![Page 45: 5. Relational Databases Fundamental Concepts - C# and Databases](https://reader036.vdocuments.mx/reader036/viewer/2022062307/555a0391d8b42ad00a8b50d8/html5/thumbnails/45.jpg)
Triggers – Example We have a table holding company names:
A trigger that appends "Ltd." at the end of the name of a new company:
45
CREATE TABLE Companies( Id number NOT NULL, Name varchar(50) NOT NULL)
CREATE OR REPLACE TRIGGER trg_Companies_INSERT BEFORE INSERT ON Company FOR EACH ROWBEGIN :NEW.Name := :NEW.Name || ' Ltd.';END;
![Page 46: 5. Relational Databases Fundamental Concepts - C# and Databases](https://reader036.vdocuments.mx/reader036/viewer/2022062307/555a0391d8b42ad00a8b50d8/html5/thumbnails/46.jpg)
TransactionsACID Transactions and Isolation
![Page 47: 5. Relational Databases Fundamental Concepts - C# and Databases](https://reader036.vdocuments.mx/reader036/viewer/2022062307/555a0391d8b42ad00a8b50d8/html5/thumbnails/47.jpg)
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
![Page 48: 5. Relational Databases Fundamental Concepts - C# and Databases](https://reader036.vdocuments.mx/reader036/viewer/2022062307/555a0391d8b42ad00a8b50d8/html5/thumbnails/48.jpg)
Rollback
Commit
DB Transactions Lifecycle
48
Read Write
Write
Durable
starting state
Durable,consisten
t,ending state
Sequence of reads
and writes
![Page 49: 5. Relational Databases Fundamental Concepts - C# and Databases](https://reader036.vdocuments.mx/reader036/viewer/2022062307/555a0391d8b42ad00a8b50d8/html5/thumbnails/49.jpg)
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
![Page 50: 5. Relational Databases Fundamental Concepts - C# and Databases](https://reader036.vdocuments.mx/reader036/viewer/2022062307/555a0391d8b42ad00a8b50d8/html5/thumbnails/50.jpg)
Transactions: Examples
Withdraw $100
1. Read savings acc.
2. New savings =current - 100
3. Read checking acc.
4. New checking =current + 100
5. Write the savings
6. Write the checking
1. Read the current balance
2. New balance = current - 100
3. Write the new balance
4. Dispense cash
Transfer $100Transfer $100
50
![Page 51: 5. Relational Databases Fundamental Concepts - C# and Databases](https://reader036.vdocuments.mx/reader036/viewer/2022062307/555a0391d8b42ad00a8b50d8/html5/thumbnails/51.jpg)
ACID Transactions Atomicity
Either execute everything or nothing Consistency
The database remains consistent with logically correct data (e.g. no constraint is violated)
Isolation Different transactions are isolated
from each other depending on the selected isolation level
Durability If a transaction is confirmed, it cannot
be lost
51
![Page 52: 5. Relational Databases Fundamental Concepts - C# and Databases](https://reader036.vdocuments.mx/reader036/viewer/2022062307/555a0391d8b42ad00a8b50d8/html5/thumbnails/52.jpg)
Transactions – Example We have a table with bank accounts:
We use a transaction to transfer money from one account into another
52
CREATE TABLE Accounts( Id int NOT NULL, Balance decimal NOT NULL)
CREATE OR REPLACE PROCEDURE sp_Transfer_Funds( fromAccount IN INT, toAccount IN INT, ammount IN NUMBER) ISBEGIN
![Page 53: 5. Relational Databases Fundamental Concepts - C# and Databases](https://reader036.vdocuments.mx/reader036/viewer/2022062307/555a0391d8b42ad00a8b50d8/html5/thumbnails/53.jpg)
Transactions – Example (2)
53
UPDATE Accounts set Balance = Balance - ammount WHERE id = fromAccount; IF SQL % ROWCOUNT = 0 THEN ROLLBACK; RAISE_APPLICATION_ERROR(-20001, 'Invalid src account!'); END IF;
UPDATE Accounts set Balance = Balance + ammount WHERE id = to_account; IF SQL % ROWCOUNT = 0 THEN ROLLBACK; RAISE_APPLICATION_ERROR(-20002, 'Invalid dst account!'); END IF; COMMIT;END;
![Page 54: 5. Relational Databases Fundamental Concepts - C# and Databases](https://reader036.vdocuments.mx/reader036/viewer/2022062307/555a0391d8b42ad00a8b50d8/html5/thumbnails/54.jpg)
Transactions and Isolation
Transactions can define different isolation levels that affect other parallel transactions
Stronger isolation ensures better consistency but works slower and the data is locked longer
Level of Isolation
Dirty Reads
Repeatable Reads
Phantom
ReadsRead Uncommitted Yes Yes Yes
Read Committed No Yes Yes
Repeatable Read No No Yes
Serializable No No No
54
![Page 55: 5. Relational Databases Fundamental Concepts - C# and Databases](https://reader036.vdocuments.mx/reader036/viewer/2022062307/555a0391d8b42ad00a8b50d8/html5/thumbnails/55.jpg)
Transactions Usage When transactions should be used?
Always use transactions when a business operation modifies more than one table
Example: At the cash-desk in a supermarket:
we buy a cart of products We either buy all of them and pay
for them or we buy nothing and pay no money
If any of the operations fail we cancel the entire transaction (the purchase)
55
![Page 56: 5. Relational Databases Fundamental Concepts - C# and Databases](https://reader036.vdocuments.mx/reader036/viewer/2022062307/555a0391d8b42ad00a8b50d8/html5/thumbnails/56.jpg)
форум програмиране, форум уеб дизайнкурсове и уроци по програмиране, уеб дизайн – безплатно
програмиране за деца – безплатни курсове и уроцибезплатен SEO курс - оптимизация за търсачки
уроци по уеб дизайн, HTML, CSS, JavaScript, Photoshop
уроци по програмиране и уеб дизайн за ученициASP.NET MVC курс – HTML, SQL, C#, .NET, ASP.NET MVC
безплатен курс "Разработка на софтуер в cloud среда"
BG Coder - онлайн състезателна система - online judge
курсове и уроци по програмиране, книги – безплатно от Наков
безплатен курс "Качествен програмен код"
алго академия – състезателно програмиране, състезания
ASP.NET курс - уеб програмиране, бази данни, C#, .NET, ASP.NETкурсове и уроци по програмиране – Телерик академия
курс мобилни приложения с iPhone, Android, WP7, PhoneGap
free C# book, безплатна книга C#, книга Java, книга C#Дончо Минков - сайт за програмиранеНиколай Костов - блог за програмиранеC# курс, програмиране, безплатно
?
? ? ??
?? ?
?
?
?
??
?
?
? ?
Questions?
?
Relational Databases
http://academy.telerik.com
![Page 57: 5. Relational Databases Fundamental Concepts - C# and Databases](https://reader036.vdocuments.mx/reader036/viewer/2022062307/555a0391d8b42ad00a8b50d8/html5/thumbnails/57.jpg)
Free Trainings @ Telerik Academy
"C# and Databases " course @ Telerik Academy academy.telerik.com/…csharp
-databases
Telerik Software Academy academy.telerik.com
Telerik Academy @ Facebook facebook.com/TelerikAcademy
Telerik Software Academy Forums forums.academy.telerik.com 57