CS108 Lecture 18: Databases and SQL

Similar documents
Web Traffic - pct of Page Views

CS108 Lecture 19: The Python DBAPI

CGS 3066: Spring 2017 SQL Reference

Introduction to File Structures

B.H.GARDI COLLEGE OF MASTER OF COMPUTER APPLICATION. Ch. 1 :- Introduction Database Management System - 1

CS317 File and Database Systems

CS317 File and Database Systems

7. Query Processing and Optimization

G64DBS Database Systems. G64DBS Module. Recommended Textbook. Assessment. Recommended Textbook. Recommended Textbook.

Course Outline Faculty of Computing and Information Technology

1. Data Definition Language.

CS50 Quiz Review. November 13, 2017

EE221 Databases Practicals Manual

Introduction and Overview

Database Server. 2. Allow client request to the database server (using SQL requests) over the network.

Data Base Concepts. Course Guide 2

Data! CS 133: Databases. Goals for Today. So, what is a database? What is a database anyway? From the textbook:

CS143: Relational Model

John Edgar 2

CSC 261/461 Database Systems Lecture 20. Spring 2017 MW 3:25 pm 4:40 pm January 18 May 3 Dewey 1101

CAS CS 460/660 Introduction to Database Systems. Fall

Introduction and Overview

Introduction. Example Databases

CMSC 424 Database design Lecture 2: Design, Modeling, Entity-Relationship. Book: Chap. 1 and 6. Mihai Pop

DATABASES SQL INFOTEK SOLUTIONS TEAM

Oral Questions and Answers (DBMS LAB) Questions & Answers- DBMS

INFORMATION TECHNOLOGY NOTES

BIS Database Management Systems.

MIS Database Systems.

Part I What are Databases?

Introduction JDBC 4.1. Bok, Jong Soon

Lecture 8. Database vs. Files SQL (I) Introduction to SQL database management systems (DBMS)

Why are you here? Introduction. Course roadmap. Course goals. What do you want from a DBMS? What is a database system? Aren t databases just

Outline. Database Management Systems (DBMS) Database Management and Organization. IT420: Database Management and Organization

Chapter 7. Introduction to Structured Query Language (SQL) Database Systems: Design, Implementation, and Management, Seventh Edition, Rob and Coronel

In This Lecture. SQL Data Definition SQL SQL. Non-Procedural Programming. Notes. Database Systems Lecture 5 Natasha Alechina

Introduction to Database Systems. Motivation. Werner Nutt

CS 564: DATABASE MANAGEMENT SYSTEMS. Spring 2018

! Define terms. ! Interpret history and role of SQL. ! Write single table queries using SQL. ! Establish referential integrity using SQL

Ministry of Higher Education and Scientific research

(ADVANCED) DATABASE SYSTEMS (DATABASE MANAGEMENTS) PROF. DR. HASAN HÜSEYİN BALIK (6 TH WEEK)

Bases de Dades: introduction to SQL (indexes and transactions)

Operating systems fundamentals - B07

Administrivia Carnegie Mellon Univ. Dept. of Computer Science /615 - DB Applications

SQL Simple Queries. Chapter 3.1 V3.01. Napier University

User Perspective. Module III: System Perspective. Module III: Topics Covered. Module III Overview of Storage Structures, QP, and TM

Introduction. Who wants to study databases?

origin destination duration New York London 415 Shanghai Paris 760 Istanbul Tokyo 700 New York Paris 435 Moscow Paris 245 Lima New York 455

SQLite vs. MongoDB for Big Data

CS 4604: Introduction to Database Management Systems. B. Aditya Prakash Lecture #1: Introduction

Introduction to Database S ystems Systems CSE 444 Lecture 1 Introduction CSE Summer

Introduction to Data Management. Lecture #2 (Big Picture, Cont.) Instructor: Chen Li

Chapter 8: Working With Databases & Tables

What s a database anyway?

Administrivia. CS 235: Introduction to Databases. Examples. What is a DBMS? The DBMS Marketplace. Relational Model

DATABASE PART 2. Components and Functions

Outline. Quick Introduction to Database Systems. Data Manipulation Tasks. What do they all have in common? CSE142 Wi03 G-1

Solved MCQ on fundamental of DBMS. Set-1

CSC 355 Database Systems

Introduction to Data Management. Lecture #2 (Big Picture, Cont.)

CS313D: ADVANCED PROGRAMMING LANGUAGE

CS150 Introduction to Computer Science 1. What is CS150? Who Are We? CS150 is a programming course You will learn

Introduction. Random things to do after this course. Course roadmap. CPS 116 Introduction to Database Systems

CSE 544 Principles of Database Management Systems

Introduction to Databases, Fall 2005 IT University of Copenhagen. Lecture 10: Transaction processing. November 14, Lecturer: Rasmus Pagh

Database Systems: Design, Implementation, and Management Tenth Edition. Chapter 7 Introduction to Structured Query Language (SQL)

Databases and Database Systems

Database Management Systems MIT Introduction By S. Sabraz Nawaz

B.C.A DATA BASE MANAGEMENT SYSTEM MODULE SPECIFICATION SHEET. Course Outline

Chapter # 7 Introduction to Structured Query Language (SQL) Part I

SQL language. Jaroslav Porubän, Miroslav Biňas, Milan Nosáľ (c)

SQL: Concepts. Todd Bacastow IST 210: Organization of Data 2/17/ IST 210

Chapter 1 Introduction

Data about data is database Select correct option: True False Partially True None of the Above

CSE 344 JANUARY 3 RD - INTRODUCTION

Database Management System

Practical Database Design Methodology and Use of UML Diagrams Design & Analysis of Database Systems

CMPT 354 Database Systems I. Spring 2012 Instructor: Hassan Khosravi

Introduction to Database Systems

Informatics 1: Data & Analysis

1. Data Model, Categories, Schemas and Instances. Outline

Introduction to CS 4604

CS 245: Principles of Data-Intensive Systems. Instructor: Matei Zaharia cs245.stanford.edu

SQL: Transactions. Announcements (October 2) Transactions. CPS 116 Introduction to Database Systems. Project milestone #1 due in 1½ weeks

TIM 50 - Business Information Systems

Now go to bash and type the command ls to list files. The unix command unzip <filename> unzips a file.

COURSE OVERVIEW THE RELATIONAL MODEL. CS121: Relational Databases Fall 2017 Lecture 1

Using a DBMS. Shan-Hung Wu & DataLab CS, NTHU

Chapter Five Physical Database Design

Database System Concepts and Architecture. Copyright 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley

Administrivia Carnegie Mellon Univ. Dept. of Computer Science /615 - DB Applications

Introduction to SQL Part 1 By Michael Hahsler based on slides for CS145 Introduction to Databases (Stanford)

CPSC 421 Database Management Systems. Lecture 11: Storage and File Organization

COURSE OVERVIEW THE RELATIONAL MODEL. CS121: Introduction to Relational Database Systems Fall 2016 Lecture 1

Manual Trigger Sql Server 2008 Update Inserted Rows

SQL Data Definition Language: Create and Change the Database Ray Lockwood

Quick Facts about the course. CS 2550 / Spring 2006 Principles of Database Systems. Administrative. What is a Database Management System?

Oracle Database 10g: Introduction to SQL

Lecture 1. Monday, August 25, 2014

Relation Databases. By- Neha Tyagi PGT CS KV 5 Jaipur II Shift Jaipur Region. Based on CBSE Curriculum Class -11. Neha Tyagi, PGT CS II Shift Jaipur

Transcription:

CS108 Lecture 18: Databases and SQL Databases for data storage and access The Structured Query Language Aaron Stevens 4 March 2013 What You ll Learn Today How does Facebook generate unique pages for each user? How is data stored within a computer? What is a database, and how does a database fit into an application? What are the operations we can do with data stored in a database? 1

Storing Data: Main Memory Memory is used to store programs and other data that are currently in use. LOAD, STOR operations Advantage of memory: short access times Read/write times in nanoseconds (10-9 sec) Disadvantages of memory: relatively expensive ($/byte) volatile Storing Data: Secondary Storage Secondary storage is used to store data for later use (examples: disks, CD, DVD). Data is written from memory to disk. When needed, data is read back into memory. 2

Secondary Storage Advantages of secondary storage: relatively inexpensive ($/byte) not volatile Disadvantage of secondary storage: long access times Read times in milliseconds (10-3 sec). in 10 ms, a modern CPU can perform millions of operations! Thus, it's important to minimize the number of times that the disk is accessed. Example: Facebook Profile What info is on a Facebook profile page? First Name Last Name Email Password Birthday About me Activities Favorite Books Favorite Movies Favorite Music Favorite Quotes Favorite TV shows No matter which page you view, it has these same elements These data are stored in a database table. 3

Whats a Table? Each table (sub-collection) is a collection of records, and each record contains fields. Example: a profiles table The primary key is a field which uniquely identifies one record within a table. Consider this URL: http://www.facebook.com/profile.php?id=919184 Primary Key The Primary Key: uniquely identifies a record within a table is an ideal search key is a way to create relationships between different tables Consider this URL again... http://www.facebook.com/profile.php?id=919184 4

Example: Status Updates Not all data fits neatly into the profile table. Consider status updates A separate Status table tracks status updates for all users. timestamp, userid, status Example: Status Table Each status message is related to exactly one profile by the foreign key (the field called id). These ids are called a foreign key, because they are primary keys in another table. 5

Example: Friends The Facebook friendrelationship is created by an entry in a friend table. Each record has two user ids: These ids are called a foreign keys, because they are primary keys in another table. Databases A database is: a collection of data stored in a way to enable quick access (by primary key) organized into related sub-collections called tables. Example: a mini facebook database: A profiles table has records of each user A status table has records of status messages A friends table has records friend relationships Each table (sub-collection) is organized by records, and each record contains fields. 6

DBMS A database is a collection of data (not software). A database management system (DBMS) is the software which manages a database. Functions of a DBMS: Efficient storage Providing a logical view of data (tables, records) Query processing Transaction management DBMS Efficient storage and retrieval. Indexing enables locating a record by unique attribute, called a key. Example: looking up stocks by their symbol. Logical representation - storage by record. Example: update the record for ID= 5. 7

Query Processing A query language is used to access and modify the data. SQL (Structured Query Language) is the standard for relational databases. Many different database vendors support SQL: Oracle, Sybase, IBM DB2, MS SQL Server, MS Access MySQL, SQLite (free/open-source) Transaction Integrity A transaction is an atomic sequence of operations that must complete together (or fail completely) but must not be half-done. Example: using the bank machine Check available balance, dispense cash, update balance. DBMS guarantees transaction integrity: completion or failure of a the entire sequence of steps that make up a transaction. 8

Database Applications End users rarely interact with a database directly. A database-enabled application allows the users to interact with the database without needing to know the query language. Structured Query Language You ll need to know some SQL to write databaseenabled applications. independent language -- syntax and semantics. SQL is comprised of 2 sub-languages: Data Manipulation Language (DML): SELECT, INSERT, UPDATE, DELETE Data Definition Language (DDL): CREATE TABLE, DROP TABLE, ALTER TABLE We ll focus on the DML for interacting with records. 9

Using sqlclient Program You may use the sqliteclient.py program to experiment with SQL statements: http://cs-webapps.bu.edu/cs108/util/sqlclient.py Example: Mini FB Database Lets consider just 2 tables: Notice that these tables share some data: ID is a primary key in profiles, and a foreign key in status; this enables cross-table relationships. 10

The SELECT Query General form: SELECT <field1>, <fields2>, FROM <table1>, <table2>, [WHERE <field>=<value> ] The minimal SELECT query requires only a list of fields (or * for all) and a single table. Example: SELECT * FROM profiles The SELECT Query (continued) The SELECT query can specify conditions using the WHERE clause, which creates a more refined result set (e.g. only matching records are returned). Example: 11

SQL Comparison Operators SQL WHERE clauses can support all of the usual comparison operations. Here are the SQL comparison operators. The SELECT Query (continued) Multiple WHERE criteria can be joined together using the logical operators AND, OR and NOT. Example: 12

The SELECT Query (continued) WHERE criteria can use wildcard comparisons as well, using the LIKE clause for nearmatches. Example: The INSERT Query General form: INSERT INTO <table> VALUES (<val1>, <val2>,<val3> ) The INSERT query will insert a record into the table. It requires a list values one value for each field in the record. Example: 13

The UPDATE Query General form: UPDATE <table> SET <field1>=<value1>, <field2>=<value2> [WHERE <field>=<value>] Example: Always use a WHERE clause in an UPDATE! The DELETE Query General form: DELETE FROM <table> [WHERE <field>=<value>] Example: The DELETE query is extremely dangerous. Always verify your criteria before deleting! Always use a WHERE clause in a DELETE! 14

What You Learned Today Databases persistence versus volatility. DBMS: efficiency, logical view, query language, and transaction integrity. Structured Query Language SELECT, UPDATE, INSERT, DELETE Constraints with WHERE clause Next Time: The Python DB API Python defines a standard API (objects and methods) for interaction with databases. No standard implementation of this interface. 3 rd party developers write their own libraries which conforms to the standard. We will be using 2 different DMBS in CS108: The SQLite3 DBMS comes standard with Python Free, nothing additional to install We ll move to the MySQL DBMS for web-application projects starting in 2 weeks 15

Using sqliteclient Program You may use the sqliteclient.py program to experiment with SQL statements. It is accesible here: http://cs-webapps.bu.edu/cs108/util/sqlclient.py Check your SQL statements against this client to rule out SQL syntax errors. Then implement the SQL with parameterized data in your client program. Announcements and To Do Readings: SQL Tutorial (today) http://www.firstsql.com/tutor.htm Python DBAPI and sqlite3 (for Wednesday) http://docs.python.org/library/sqlite3.html 16

Facebook Haiku Avoiding Facebook Became too much of a chore So I relented! - Scott M. Sokol Using Python with MS Access Should you want to use Python with Microsoft Access, you will need to download and install 2 components: 1- install Mark Hammond's pywin32-210 for python 2.5 http://starship.python.net/crew/mhammond/win32/downloads.html 2- download adodbapi.zip from http://adodbapi.sourceforge.net/ Unzip adodbapi.zip into C:\Python25\Lib\site-packages\ This is only available for Windows! 17