Fundamental Concepts Svetlin Nakov Telerik Corporation www.telerik.com.

Slides:



Advertisements
Similar presentations
Numbers Treasure Hunt Following each question, click on the answer. If correct, the next page will load with a graphic first – these can be used to check.
Advertisements

Simplifications of Context-Free Grammars
Variations of the Turing Machine
1 Senn, Information Technology, 3 rd Edition © 2004 Pearson Prentice Hall James A. Senns Information Technology, 3 rd Edition Chapter 7 Enterprise Databases.
3rd Annual Plex/2E Worldwide Users Conference 13A Batch Processing in 2E Jeffrey A. Welsh, STAR BASE Consulting, Inc. September 20, 2007.
AP STUDY SESSION 2.
Advanced SQL Topics Edward Wu.
1
Copyright © 2003 Pearson Education, Inc. Slide 1 Computer Systems Organization & Architecture Chapters 8-12 John D. Carpinelli.
Information Retrieval from Relational Databases
Relational Database and Data Modeling
David Burdett May 11, 2004 Package Binding for WS CDL.
Local Customization Chapter 2. Local Customization 2-2 Objectives Customization Considerations Types of Data Elements Location for Locally Defined Data.
Process a Customer Chapter 2. Process a Customer 2-2 Objectives Understand what defines a Customer Learn how to check for an existing Customer Learn how.
Custom Statutory Programs Chapter 3. Customary Statutory Programs and Titles 3-2 Objectives Add Local Statutory Programs Create Customer Application For.
Add Governors Discretionary (1G) Grants Chapter 6.
CALENDAR.
Relational data integrity
1 Advanced Tools for Account Searches and Portfolios Dawn Gamache Cindy Bylander.
1.
Welcome. © 2008 ADP, Inc. 2 Overview A Look at the Web Site Question and Answer Session Agenda.
Break Time Remaining 10:00.
Turing Machines.
Table 12.1: Cash Flows to a Cash and Carry Trading Strategy.
Information Systems Today: Managing in the Digital World
Database Performance Tuning and Query Optimization
PP Test Review Sections 6-1 to 6-6
Chapter 7 Working with Databases and MySQL
9 Copyright © 2004, Oracle. All rights reserved. Using DDL Statements to Create and Manage Tables.
Introduction to Structured Query Language (SQL)
R ELATIONAL M ODEL TO SQL Data Model. 22 C ONCEPTUAL D ESIGN : ER TO R ELATIONAL TO SQL How to represent Entity sets, Relationship sets, Attributes, Key.
Microsoft Confidential. We look at the world... with our own eyes...
© Copyright by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. 1 Outline 24.1 Test-Driving the Ticket Information Application.
Creating Databases and E/R Diagrams with SQL Server Management Studio Svetlin Nakov Telerik Corporation
Copyright © 2012, Elsevier Inc. All rights Reserved. 1 Chapter 7 Modeling Structure with Blocks.
 Copyright I/O International, 2013 Visit us at: A Feature Within from Item Class User Friendly Maintenance  Copyright.
Note: A bolded number or letter refers to an entire lesson or appendix. A Adding Data Through a View ADD_MONTHS Function 03-22, 03-23, 03-46,
Chapter 1: Expressions, Equations, & Inequalities
Adding Up In Chunks.
Chapter A: Network Model
MaK_Full ahead loaded 1 Alarm Page Directory (F11)
1 10 pt 15 pt 20 pt 25 pt 5 pt 10 pt 15 pt 20 pt 25 pt 5 pt 10 pt 15 pt 20 pt 25 pt 5 pt 10 pt 15 pt 20 pt 25 pt 5 pt 10 pt 15 pt 20 pt 25 pt 5 pt Synthetic.
Functional Dependencies and Normalization for Relational Databases
1 Non Deterministic Automata. 2 Alphabet = Nondeterministic Finite Accepter (NFA)
Chapter 2 Entity-Relationship Data Modeling: Tools and Techniques
1 hi at no doifpi me be go we of at be do go hi if me no of pi we Inorder Traversal Inorder traversal. n Visit the left subtree. n Visit the node. n Visit.
1 Let’s Recapitulate. 2 Regular Languages DFAs NFAs Regular Expressions Regular Grammars.
Speak Up for Safety Dr. Susan Strauss Harassment & Bullying Consultant November 9, 2012.
Chapter 12: Designing Databases
Essential Cell Biology
Converting a Fraction to %
Clock will move after 1 minute
14 Databases Foundations of Computer Science ã Cengage Learning.
Physics for Scientists & Engineers, 3rd Edition
Select a time to count down from the clock above
A Data Warehouse Mining Tool Stephen Turner Chris Frala
1 Dr. Scott Schaefer Least Squares Curves, Rational Representations, Splines and Continuity.
1 Non Deterministic Automata. 2 Alphabet = Nondeterministic Finite Accepter (NFA)
Introduction to Structured Query Language (SQL)
Introduction To Databases IDIA 618 Fall 2014 Bridget M. Blodgett.
Database Technical Session By: Prof. Adarsh Patel.
Relational Database Management Systems. A set of programs to manage one or more databases Provides means for: Accessing the data Inserting, updating and.
DBMS Fundamental Concepts Svetlin Nakov Telerik Software Academy academy.telerik.com Manager Technical Training
Telerik Software Academy Telerik School Academy DBMS Fundamental Concepts.
Database Systems Design, Implementation, and Management Coronel | Morris 11e ©2015 Cengage Learning. All Rights Reserved. May not be scanned, copied or.
Relational Database Management System(RDBMS) Structured Query Language(SQL)
Database Systems  DBMS Fundamental Concepts. Table of Contents 1. Database Models 2. Relational Database Model 3. DBMS & RDBMS Systems 4. Tables, Relationships,
Fundamentals of DBMS Notes-1.
Presentation transcript:

Fundamental Concepts Svetlin Nakov Telerik Corporation

1. Database Models 2. Relational Database Models 3. RDBMS Systems 4. Tables, Relationships, Multiplicity, E/R Diagrams 5. Normalization 6. Constraints 7. Indices 2

8. The SQL language 9. Stored Procedures 10. Views 11. Triggers 12. Transactions and Isolation Levels 3

Relational Databases, Database Servers and RDBMS

 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

 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

 RDBMS systems are also known as:  Database management servers  Or just database servers  Popular RDBMS servers:  Microsoft SQL Server  Oracle Database  IBM DB 2  PostgreSQL  MySQL  Borland Interbase, SQLite, … 7

Database Tables, Relationships, Multiplicity

 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 Employer1SvetlinNakovTelerik 2StephenForteTelerik 3SteveJobsApple 9

 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, Id: number, FirstName: string, FirstName: string, LastName: string, LastName: string, Employer: string Employer: string)

 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 Employer1SvetlinNakovTelerik 2StephenForteTelerik 3SteveJobsApple 11 Primary key

 Relationships between tables are based on interconnections: primary key / foreign key 12IdName CountryId 1Sofia1 2Plovdiv1 3Munich2 4Berlin2 5Moscow3 IdName1Bulgaria 2Germany 3Russia Towns Countries Primary key Foreign key

 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

 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 14IdName CountryId 1Sofia1 2Plovdiv1 3Munich2 4Berlin2 5Moscow3 IdName1Bulgaria 2Germany 3Russia Towns Countries

 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 15IdName1Pesho 2Minka 3Gosho 4Penka IdName1.NET 2Java 3PHP Students Courses StudentId CourseId StudentsCourses

IdTitle1Ph.D.  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 IdIdIdIdNameAge1 Ivan Daddy 72 2 Goiko Dude 26 3 Grand Mara 24 PersonsIdSpecialty2 Computer Science 3Chemistry Students Primary & Foreign key in the same time Primary key Primary & foreign key in the same time Professors

 How do we represent trees and graphs? 17 Root DocumentsPictures Birthday Party

 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 18IdFolderParentId1Root (null) 2Documents1 3Pictures1 4 Birthday Party 3 Employees Primary key Foreign key Self- relationship

Entity / Relationship Diagrams and DB Modeling Tools

 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

21 The diagram is created with Microsoft SQL Server Management Studio

22 The diagram is created with ERwin

23 The diagram is created with fabFORCE DB Designer for MySQL

24 The diagram is created with MS Visio

 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

Avoiding Duplicated Data through Database Schema Normalization

 Normalization of the relational schema removes repeating data  Non-normalized schemas can contain many data repetitions, e.g. 27ProductProducerPriceCategoryShopTownyoghurt Mlexis LTD 0.67food 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 CO 0.87 soft drinks stall "non- stop" Varna

 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 28BookTitle ISBN (PK) AuthorAuthor .NET Framework Mr. Kiro Beginning SQL

 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 Author .NET Framework Mr. Kiro Beginning SQL Santa Claus The price depends on the book depends on the author

 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" 30IdProduct Producer Id Price Category Id Shop Id Town Id 1Yoghourt bread "Tipov" rakiya "Biserna" beer "Tuborg"

 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) 31AuthorIdBookArticle2.NET Programming Regular Expressions in.NET 4 Mastering JavaScript AJAX Performance Patterns One author can have many books One author can have many articles

 Example of fully normalized schema (in 4 th Normal Form): 32 IdIdIdIdName2 "Milk" Ltd. 4 "Zagorka" AD IdName4beer 2food IdProductProducerIdPrice Catego ryId Shop Id Town Id 1Youghurt bread "Dobrudja" rakia "Peshtera" beer "Tuborg" IdIdIdIdName1Billa 4METROIdName1Sofia 3Varna Products Producers Categories Shops Towns

Constraints, Indices, SQL, Stored Procedures, Views, Triggers

 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

 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 = 0) AND (hour < 24) name = UPPER(name)

 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 rows) 36

 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

 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.Name FROM Towns, Countries WHERE Towns.CountryId = Countries.Id

 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 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) IS BEGIN INSERT INTO Countries(Name) INSERT INTO Countries(Name) VALUES(countryName); VALUES(countryName);END;

 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

42IdCompany TownId 1 Mente LTD 1 2 BulkSoft Inc. 2 3 HardSoft Corp. 4 4 Sputnik Corp. 3 IdTownCountryId1Sofia1 2 New York 3 3Moscow2 4Plovdiv1 CompaniesTownsIdCountry1Bulgaria 2Russia 3USA Countries

43 CREATE VIEW V_BGCompanies AS SELECT SELECT Companies.Id AS Id, Companies.Id AS Id, Companies.Company AS Company Companies.Company AS Company FROM Companies INNER JOIN FROM Companies INNER JOIN (Towns INNER JOIN Countries ON (Towns INNER JOIN Countries ON Towns.CountryId = Countries.Id) Towns.CountryId = Countries.Id) ON Companies.TownId = Towns.Id ON Companies.TownId = Towns.Id WHERE WHERE Countries.Country = "Bulgaria"; Countries.Country = "Bulgaria";IdCompany1 Mente Ltd. 3 HardSoft Corp. V_BGCompanies

 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 history 44

 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, Id number NOT NULL, Name varchar(50) NOT NULL) Name varchar(50) NOT NULL) CREATE OR REPLACE TRIGGER trg_Companies_INSERT BEFORE INSERT ON Company BEFORE INSERT ON Company FOR EACH ROW FOR EACH ROWBEGIN :NEW.Name := :NEW.Name || ' Ltd.'; :NEW.Name := :NEW.Name || ' Ltd.';END;

ACID Transactions and Isolation

 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

Rollback Commit 48 Read Write Write Durable starting state Durable,consistent, ending state Sequence of reads and writes

 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

Withdraw $ Read savings acc. 2. New savings = current Read checking acc. 4. New checking = current Write the savings 6. Write the checking 1. Read the current balance 2. New balance = current Write the new balance 4. Dispense cash Transfer $100 50

 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

 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, Id int NOT NULL, Balance decimal NOT NULL) Balance decimal NOT NULL) CREATE OR REPLACE PROCEDURE sp_Transfer_Funds( fromAccount IN INT, fromAccount IN INT, toAccount IN INT, toAccount IN INT, ammount IN NUMBER) IS ammount IN NUMBER) ISBEGIN

53 UPDATE Accounts set Balance = Balance - ammount UPDATE Accounts set Balance = Balance - ammount WHERE id = fromAccount; WHERE id = fromAccount; IF SQL % ROWCOUNT = 0 THEN IF SQL % ROWCOUNT = 0 THEN ROLLBACK; ROLLBACK; RAISE_APPLICATION_ERROR(-20001, 'Invalid src account!'); RAISE_APPLICATION_ERROR(-20001, 'Invalid src account!'); END IF; END IF; UPDATE Accounts set Balance = Balance + ammount UPDATE Accounts set Balance = Balance + ammount WHERE id = to_account; WHERE id = to_account; IF SQL % ROWCOUNT = 0 THEN IF SQL % ROWCOUNT = 0 THEN ROLLBACK; ROLLBACK; RAISE_APPLICATION_ERROR(-20002, 'Invalid dst account!'); RAISE_APPLICATION_ERROR(-20002, 'Invalid dst account!'); END IF; END IF; COMMIT; COMMIT;END;

 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 Reads Read Uncommitted YesYesYes Read Committed NoYesYes Repeatable Read NoNoYes SerializableNoNoNo 54

 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

Questions?