By Anil Kumar
Objectives Review database theory and history Review relational database concepts Learn about the evolution of data access technologies By Anil Kumar
Agenda Databases Relational Databases By Anil Kumar
Databases Databases Virtually all interesting applications require a structured, persistent data store E-Commerce: placing an order, fulfilling an order HR: Personnel data Sales CRM: Customer data Games Database needs vary with the type of application Transaction Processing/OLTP Business Intelligence/Data Warehouse/OLAP By Anil Kumar
Databases Database Requirements Can store, view and modify data Can move, copy and transform data Can backup and restore data Enforces data integrity rules Is scaleable and available High number of users Lots of data High throughput with low response time Is secure Facilitates application development By Anil Kumar
Databases Evolution of Database Technology File-based Hierarchical Network Relational (RDBMS) Object-oriented XML By Anil Kumar
Agenda Databases Theory and History Relational Databases By Anil Kumar
Relational Databases Tables Table (relation, entity) A collection of data about a specific type of thing Organized in rows and columns Column (attribute, field) Describes part of an entity (e.g. FirstName) Has a data type (e.g. integer, character, binary) Can be null Row (tuple, record) A single instance of data in a table Each row is unique AuthID FirstName LastName 1 Joe Smith 2 Diane Jones By Anil Kumar
Relational Databases Relating Data Tables can be related through primary/foreign key relationships (e.g., a book has an author) Primary key Guarantees the uniqueness of a row Can be composed of one or more columns Ensures entity integrity Foreign key Establishes logical relationship between tables One or more columns of a table that match the primary or alternate key of another table Referential integrity Relational databases do NOT get there name because they can relate data. They get their name from set theory: a relation is the set-theoretic term for a table. Primary/foreign key relationships are THE way data is related in a relational database. By Anil Kumar
Relational Databases Relating Data Schema diagram depicts tables, columns, primary keys, foreign keys Books BookID AuthID Title Type Authors AuthID FirstName LastName 1 ∞ Schema Diagram By Anil Kumar By Anil Kumar
Relational Databases Relating Data Books Table Primary Key BookID AuthID Title Type 1 2 My Life as a DBA Autobiography Database Handbook Reference PK/FK Relationship Foreign Key AuthID FirstName LastName 1 Joe Smith 2 Diane Jones Authors Table By Anil Kumar
Relational Databases Types of Relationships One-to-One (1:1) One row in table X matches one row in table Y A book has at most one Library of Congress entry One-to-Many (1:M) One row in table X matches 0+ rows in table Y A publisher publishes one or more books Many-to-Many (M:N) 1+ rows in table X matches 1+ rows in table Y An author writes one or more books; a book is written by one or more authors 1 1 Books LoC Entries Publishers 1 M Books M Authors N Books By Anil Kumar
Relational Databases M:N Relationships More complex Can result in very large tables (repeated data) Difficult to ensure data integrity The remedy: Create a third table The third table contains the primary key of the two original tables in a composite key Data is repeated in the third table, but not in the two original tables Authors 1 M BookAuth M 1 Books By Anil Kumar
Relational Databases M:N Relationships 1 ∞ Data is duplicated here 1 1 ∞ ∞ By Anil Kumar
Relational Databases Normalization/Denormalization The process of breaking large tables into multiple smaller tables Goal: minimize redundant data, maximize correctness Improves performance for updates Desirable in transaction-based applications Denormalization The process of combining smaller tables into fewer larger tables Goal: improve performance Introduces redundant data Improves performance for reads Desirable in data warehouse applications By Anil Kumar
Relational Databases Joins A join is a way of combining data in multiple tables, usually by resolving primary key/foreign key relationships Product table Vendor table Product Cost Vendor Vendor State Contact Widget $10 Acme Acme MA Linda A. Thingy $5 Acme Blecco WA Adam P. Widget $8 Blecco Foobar $25 Blecco By Anil Kumar
Relational Databases Joins Result of a natural join Product Cost Vendor State Contact Widget $10 Acme MA Linda A. Thingy $5 Acme MA Linda A. Widget $8 Blecco WA Adam P. Foobar $25 Blecco WA Adam P. By Anil Kumar
Relational Databases Structured Query Language (SQL) Standard language for accessing a relational database, standardized by American National Standards Institute (ANSI); SQL-92 Open, but not really Common functions are mostly the same across products Most vendors have proprietary extensions Subsets of SQL Data Definition Language (DDL) Data Manipulation Language (DML) Data Control Language (DCL) By Anil Kumar
Relational Databases DDL Examples Used to create and modify database objects CREATE DATABASE Bookstore CREATE TABLE tBooks ( BookID INT IDENTITY(1,1) PRIMARY KEY, Title VARCHAR(30) NOT NULL, PubDate DATE NOT NULL, [Description] VARCHAR(50), Category INT NOT NULL ) By Anil Kumar
Relational Databases DML Examples Select data to view SELECT * FROM tAuthors SELECT AuthID, FirstName, LastName FROM tAuthors SELECT AuthID, FirstName, LastName, Phone FROM tAuthors WHERE City = ‘Boston’ SELECT FirstName, LastName, Phone FROM tAuthors WHERE AuthID = 249 By Anil Kumar
Relational Databases DML Examples Using SELECT to join tables SELECT AuthID, FirstName, LastName, Phone, BookID, Title, PubDate, Description FROM tAuthors, tBooks WHERE tAuthors.AuthID = tBooks.AuthID SELECT AuthID, FirstName, LastName, Phone, BookID, Title, PubDate, Description FROM tAuthors INNER JOIN tBooks ON tAuthors.AuthID = tBooks.AuthID By Anil Kumar
Relational Databases DML Examples Insert, update and delete data INSERT INTO tBooks (Title, PubDate, [Description], Category) VALUES (‘Database Design’, GETDATE(), ‘How to design a database’, 3) UPDATE tAuthors SET Phone = ‘617-555-1234’ WHERE AuthID = 5 DELETE FROM tAuthors WHERE AuthID = 5 By Anil Kumar
Relational Databases DCL Examples Set security options on database objects GRANT INSERT, UPDATE, DELETE ON tAuthors TO Mary, John REVOKE CREATE TABLE FROM Joe DENY ALL ON tAuthors, tBooks TO Sally By Anil Kumar
Relational Databases Views A view is a virtual table Abstracts the underlying table structures Abstracts a (possibly complex) query Provides security abstraction from table In SQL Server 2000, a view can be Indexed Updated and inserted into By Anil Kumar
Relational Databases View Definition Example CREATE VIEW vwCustomerOrders AS SELECT o.OrderId, c.CompanyName FROM Customers c INNER JOIN Orders o ON c.CustomerID = O.CustomerID ORDER BY o.OrderId By Anil Kumar
Relational Databases View Usage Example SELECT * FROM vwCustomerOrders WHERE CompanyName = 'My Favorite Customer' OrderId CompanyName 101 My Favorite Customer 137 … By Anil Kumar
Relational Databases Stored Procedures A group of SQL statements that runs within the database Not part of SQL standard Provides greater performance Can control access to data Can accept parameters Can return data Output parameters Return values Result set By Anil Kumar
Relational Databases Stored Procedure Example CREATE PROCEDURE CustOrderHist @CustomerID nchar(5) AS SELECT ProductName, Total=SUM(Quantity) FROM Products P, [Order Details] OD, Orders O, Customers C WHERE C.CustomerID = @CustomerID AND C.CustomerID = O.CustomerID AND O.OrderID = OD.OrderID AND OD.ProductID = P.ProductID GROUP BY ProductName By Anil Kumar
Relational Databases Stored Procedure Examples exec CustOrderHist 'alfki' ProductName Total Aniseed Syrup 6 Chartreuse verte 21 ... By Anil Kumar
Relational Databases Stored Procedure Examples Use RETURN statement to return status 0 is default in SQL Server Can only be numeric Use OUTPUT parameters to return results RETURN 1 CREATE PROCEDURE MyProcedure @ReturnValue INT OUTPUT ... SELECT @ReturnValue = ColumnName FROM Table By Anil Kumar
Relational Databases Triggers Like stored procedures, triggers are code that runs within a database Not directly called by a user Executed when a specified data modification takes place (INSERT, UPDATE or DELETE) Enforces business rules FOR AFTER: trigger executes after triggering action completes FOR INSTEAD OF: trigger executes in place of triggering action By Anil Kumar
Relational Databases Transactions Transaction: a sequence of SQL statements that constitute a logical unit of work Must adhere to ACID properties Atomic: All statements execute successfully or all fail Consistent: Must leave the data in a consistent state when completed Isolated: Cannot see the modifications made by concurrent transactions Durable: Must be permanent when complete, even in the event of system failure By Anil Kumar
Relational Databases Concurrency Isolation levels Read Uncommitted Read Committed Repeatable Read Serializable Tradeoffs (concurrency vs. data integrity) Locking Ensures transactional integrity/database consistency Prevents users from seeing “phantom” data Can result in deadlocks By Anil Kumar
Thank You By Anil Kumar