1.264 Lecture 15. SQL transactions, security, indexes



Similar documents
1.264 Lecture 11. SQL: Basics, SELECT. Please start SQL Server before each class

1.204 Lecture 3. SQL: Basics, Joins SQL

The first time through running an Ad Hoc query or Stored Procedure, SQL Server will go through each of the following steps.

Introduction to Databases

Transactions and the Internet

1 File Processing Systems

David Dye. Extract, Transform, Load

Database 10g Edition: All possible 10g features, either bundled or available at additional cost.

DBMS Questions. 3.) For which two constraints are indexes created when the constraint is added?

DATABASE SYSTEM CONCEPTS AND ARCHITECTURE CHAPTER 2

SQLBase 8.0 and COM+ Transactions

Setting Up ALERE with Client/Server Data

Configuring Apache Derby for Performance and Durability Olav Sandstå

Database System Architecture & System Catalog Instructor: Mourad Benchikh Text Books: Elmasri & Navathe Chap. 17 Silberschatz & Korth Chap.

Demystified CONTENTS Acknowledgments xvii Introduction xix CHAPTER 1 Database Fundamentals CHAPTER 2 Exploring Relational Database Components

Administering a Microsoft SQL Server 2000 Database

Chapter 2 Database System Concepts and Architecture

Database Management. Chapter Objectives

SQL Server. SQL Server 100 Most Asked Questions: Best Practices guide to managing, mining, building and developing SQL Server databases

Transaction Management Overview

Oracle Database Links Part 2 - Distributed Transactions Written and presented by Joel Goodman October 15th 2009

Oracle. Brief Course Content This course can be done in modular form as per the detail below. ORA-1 Oracle Database 10g: SQL 4 Weeks 4000/-

PostgreSQL Concurrency Issues

Administering a Microsoft SQL Server 2000 Database

æ A collection of interrelated and persistent data èusually referred to as the database èdbèè.

Configuring Apache Derby for Performance and Durability Olav Sandstå

Microsoft Query, the helper application included with Microsoft Office, allows

FileMaker 12. ODBC and JDBC Guide

W I S E. SQL Server 2008/2008 R2 Advanced DBA Performance & WISE LTD.

Introduction to Database Systems. Chapter 1 Introduction. Chapter 1 Introduction

50238: Introduction to SQL Server 2008 Administration

D61830GC30. MySQL for Developers. Summary. Introduction. Prerequisites. At Course completion After completing this course, students will be able to:

Introduction to Database Management Systems

Transactions and Recovery. Database Systems Lecture 15 Natasha Alechina

Basic Unix/Linux 1. Software Testing Interview Prep

FileMaker 13. ODBC and JDBC Guide

eedge Basics: Get Your Database in Order Using eedge mycontacts

Explain how to prepare the hardware and other resources necessary to install SQL Server. Install SQL Server. Manage and configure SQL Server.

1.264 Lecture 19 Web database: Forms and controls

Guide to Upsizing from Access to SQL Server

MSSQL quick start guide

Maintaining a Microsoft SQL Server 2008 Database

CHAPTER 23: USING ODBC

PERFORMANCE TIPS FOR BATCH JOBS

Commercial Database Software Development- A review.

SQL Databases Course. by Applied Technology Research Center. This course provides training for MySQL, Oracle, SQL Server and PostgreSQL databases.

Basic SQL Server operations

5. CHANGING STRUCTURE AND DATA

3 Setting up Databases on a Microsoft SQL 7.0 Server

2 SQL in iseries Navigator

Chapter 13 File and Database Systems

Chapter 13 File and Database Systems

IceWarp Server. Log Analyzer. Version 10

Technical White Paper. Automating the Generation and Secure Distribution of Excel Reports

TRANSACÇÕES. PARTE I (Extraído de SQL Server Books Online )

CASE STUDY: Oracle TimesTen In-Memory Database and Shared Disk HA Implementation at Instance level. -ORACLE TIMESTEN 11gR1

Principles of Database. Management: Summary

Chapter 24: Creating Reports and Extracting Data

Data processing goes big

SQL Server for developers. murach's TRAINING & REFERENCE. Bryan Syverson. Mike Murach & Associates, Inc. Joel Murach

DATABASE MANAGEMENT SYSTEMS

Outline. Failure Types

MODULE FRAMEWORK : Dip: Information Technology Network Integration Specialist (ITNIS) (Articulate to Edexcel: Adv. Dip Network Information Specialist)

PERFORMANCE TUNING IN MICROSOFT SQL SERVER DBMS

SQL Server 2008 R2 Express Edition Installation Guide

AV-005: Administering and Implementing a Data Warehouse with SQL Server 2014

Chapter 14: Recovery System

Recover EDB and Export Exchange Database to PST 2010

Microsoft SQL Server 2008 Bible

<Insert Picture Here> Oracle In-Memory Database Cache Overview

FileMaker 11. ODBC and JDBC Guide

Oracle Database 10g Express

Team Foundation Server 2012 Installation Guide

InterBase SMP: Safeguarding Your Data from Disaster

Administering a Microsoft SQL Server 2000 Database

Java DB Performance. Olav Sandstå Sun Microsystems, Trondheim, Norway Submission ID: 860

SEER Enterprise Shared Database Administrator s Guide

BUILDER 3.0 Installation Guide with Microsoft SQL Server 2005 Express Edition January 2008

What is a database? COSC 304 Introduction to Database Systems. Database Introduction. Example Problem. Databases in the Real-World

Contents CHAPTER 1 IMail Utilities

Inmagic Content Server Workgroup Configuration Technical Guidelines

Goals. Managing Multi-User Databases. Database Administration. DBA Tasks. (Kroenke, Chapter 9) Database Administration. Concurrency Control

Recovery and the ACID properties CMPUT 391: Implementing Durability Recovery Manager Atomicity Durability

An Oracle White Paper June Security and the Oracle Database Cloud Service

Stellar Phoenix. SQL Database Repair 6.0. Installation Guide

Information Systems. Computer Science Department ETH Zurich Spring 2012

Migrating helpdesk to a new server

ACCESS Importing and Exporting Data Files. Information Technology. MS Access 2007 Users Guide. IT Training & Development (818)

Toad for Oracle 8.6 SQL Tuning

MySQL Storage Engines

Concepts of Database Management Seventh Edition. Chapter 7 DBMS Functions

Would-be system and database administrators. PREREQUISITES: At least 6 months experience with a Windows operating system.

Toad for Data Analysts, Tips n Tricks

Foreign and Primary Keys in RDM Embedded SQL: Efficiently Implemented Using the Network Model

Database Administration with MySQL

ORACLE DATABASE 11G: COMPLETE

Course Syllabus. At Course Completion

Oracle 10g PL/SQL Training

Transcription:

1.264 Lecture 15 SQL transactions, security, indexes Download BeefData.csv and Lecture15Download.sql Next class: Read Beginning ASP.NET chapter 1. Exercise due after class (5:00) 1

SQL Server diagrams Select Database Diagrams under your database New diagram Add tables Make sure the diagram matches the data model Offices SalesReps Customers Orders OfficeNbr City Region Target State RepNbr Name Quota Sales RepOffice CustNbr Company CreditLimit CustRep OrderNbr Prod Qty Amt Disc Phone Cust Image by MIT OpenCourseWare. Don t need to hand this in 2

SQL Server script creation After building a table, you can save its design Right click on the table in the explorer Select Script table as -> Create to (or other options) This allows you to automate creating and working with the database. Write script to query window hand in. 3

SQL Server import/export To import data from a file, spreadsheet, etc: Start->All Programs-> MS SQL Server 2012- >Import/Export (64 bit) Wizard starts Data source: Flat file source (or other option listed) Browse to file (usually csv comma separated or txt) Select database (MIT1264 in this example) New table is created with data from file Exercise: Import Lecture 15 Beef Data.csv No need to hand in 4

Data definition language (DDL) We ve explored the data manipulation language (DML) so far: SELECT, INSERT, UPDATE, DELETE SQL also has a data definition language (DDL): CREATE DATABASE CREATE TABLE CREATE INDEX (and other CREATE statements) ALTER TABLE ALTER VIEW (and other ALTER statements) DROP DATABASE DROP TABLE DROP VIEW (and other DROP statements) You ve seen some of these in the initial.sql file that created our database, and in the script example earlier 5

Transactions Group of operations often must be treated as atomic unit Start transaction Insert OrderHeader While more OrderDetail (line items) exist: Select Part Update Part inventory Insert OrderDetail row Commit transaction if everything succeeds Roll back transaction if any error occurs: In Order Header In OrderDetail Server crashes Disk crashes Network dies Etc. 6

Transaction properties (ACID) Atomicity. Either all of transactions are executed or all are rolled back Account transfer debit and credit both succeed or fail Consistency. Only legal states can exist If order detail cannot be written, order header is rolled back Isolation. Results not seen by other transactions until the transaction is complete Account transfer debit and credit either both seen or neither is seen Durability. Data is persistent even if hardware or software crashes: What is written on the disk is correct Account balance is maintained 7

Transactions Multi-user databases have other transaction issues Two database actions conflict if one or both are write operations. Examples of problems: Lost updates: 7 parts in inventory Transactions 1 and 2 simultaneously read 7 as the current quantity Transaction 1 finishes first, adds 3 parts, writes 10 as quantity Transaction 2 finishes second, subtracts 5 parts, writes 2 as quantity! Uncommitted changes: Transaction 1 adds 3 parts, writes 10 as quantity Transaction 2 reads 10 as quantity Transaction 1 aborts (rolls back), leaving transaction 2 with wrong data Databases handle all these cases automatically 8

Transactions Databases use locks for concurrency. One simple scheme is pessimistic locking: Writes obtain exclusive lock on a record, preventing reads or writes Reads obtain nonexclusive locks, allowing other reads but preventing a writer from obtaining an exclusive lock Another, with higher performance, is optimistic locking: No locks. Check if row exists and is same after operation If not, issue error and program must retry Databases use logs for transactions, rollbacks, recovery. Log file of all changes is written in addition to making the changes in the database. (This is a key bottleneck in software architecture.) Transaction can t be committed until log is written to stable storage Transactions usually committed before tables actually updated on disk If a change is rolled back, the log is read to reverse the transactions. If a system or disk crashes, the log is rerun from the last checkpoint to restore the database. 9

Transaction example and exercise INSERT Customers VALUES (212, 'Smith Co', 89, 20000) INSERT Orders VALUES (212, 'Lathe', 3, 20000, 0.1) INSERT Orders VALUES (212, 'Latte', 10, 2, 0.0) -- Independent INSERTs -- INSERTs as a transaction (Usually done in program code, which is simpler) BEGIN TRAN INSERT Customers VALUES (213, 'Wang Co', 53, 100000) IF @@ERROR = 0 BEGIN INSERT Orders VALUES (213, 'Mill', 1, 50000, 0.2) IF @@ERROR = 0 BEGIN INSERT Orders VALUES (213, 'Malt', 1, 2, 0.0) IF @@ERROR = 0 COMMIT TRAN ELSE ROLLBACK TRAN END ELSE END ELSE ROLLBACK TRAN ROLLBACK TRAN Exercise: Modify the transaction: It s in Lecture15Download.sql on Web INSERT Customer 214 INSERT first order for 214 correctly INSERT 2 nd order incorrectly: leave out the last two fields Then open Customers and Orders: Are any of the INSERTs present? Hand in your changed.sql file 10

Security (and short exercise) Security options Use operating system logon/password (weak) to identify user User gets access to all databases, all tables ( Windows authentication ) Use database logon/password (stronger) Restrict access to databases, tables, but can still use all applications SQL Server authentication : we ll use this for the Web->db connection Application level security (stronger still, but tough to administer) Each application must look in its database to see if user authorized Network level security (strongest), using certificates/tickets Use Kerberos, MS Active Directory, others (covered under security) Classes of users: super-user (dba or sa), data owner, data user Assignment of database privileges (permissions) GRANT and REVOKE: E.g., GRANT INSERT ON TableName TO PUBLIC WITH GRANT OPTION REVOKE INSERT ON TableName FROM PUBLIC CASCADE Order matters for GRANTs and REVOKEs. Last one governs. Try these two statements; look at the table properties in client. Hand in. 11

Indexes Index is a separate data object in the database that lists table rows in order to allow rapid lookup Each index for each table is a separate object Primary keys and foreign keys automatically indexed Rapid access to indexed columns Each index may be updated when a row is updated, so indexes slow updates, insertions and deletes If a database is mostly read, use many indexes to speed performance If database is mostly updates, use as few indexes as possible Practical maximum of 3 or 4 indexes per table. If others are needed on occasion, add and drop them as needed (Use indexes when working with transit fare, bus data) Clustered indexes Physically rearrange rows by a single index to maximize disk access speed 12

Indexes (and short exercise) Customer database Customer ID is primary key We also want to search by: Customer name (last, first) City, state Postal (zip) code Address Index the name, city/state, zip and address Four indexes: slow insert, update, delete, but fast lookup If customer database is fairly stable, this is fine Similar logic for parts catalog, bill of materials, etc. Internet search engines use text retrieval engines Index every word in the entire database; count occurrences and rank matches. Recent advances (frequency of links, usage ) enhance this. Exercise (hand in): CREATE INDEX IX_Orders ON Orders (Cust, OrderNbr) Use the MIT1264 database 13

ODBC, ADO.NET, JDBC ODBC, etc. are a library of procedures (methods) to connect from an application (Web, Windows, Java) to a database, execute SQL statements and retrieve results SQL syntax based on SQL-92 standard Standard set of error codes Standard way to connect and log on to database Standard representation of data types Standard methods for data type conversions These features overcome many nonstandard SQL issues noted in the first SQL lecture We ll use ADO.NET when building Web sites and services We will look at database performance and costs in the system architecture lectures later 14

MIT OpenCourseWare http://ocw.mit.edu 1.264J / ESD.264J Database, Internet, and Systems Integration Technologies Fall 2013 For information about citing these materials or our Terms of Use, visit: http://ocw.mit.edu/terms.