Lecture 15: Databases II Wednesday Feburary 28, 2018

Slides:



Advertisements
Similar presentations
PHP (2) – Functions, Arrays, Databases, and sessions.
Advertisements

Fundamentals, Design, and Implementation, 9/e Chapter 7 Using SQL in Applications.
DT228/3 Web Development Databases. Database Almost all web application on the net access a database e.g. shopping sites, message boards, search engines.
Chapter 4 Relational Databases Copyright © 2012 Pearson Education, Inc. publishing as Prentice Hall 4-1.
Chapter 4 Relational Databases Copyright © 2012 Pearson Education 4-1.
Databases Dan Otero Alex Loddengaard
Introduction To Databases IDIA 618 Fall 2014 Bridget M. Blodgett.
Database System Concepts and Architecture Lecture # 3 22 June 2012 National University of Computer and Emerging Sciences.
Databases From A to Boyce Codd. What is a database? It depends on your point of view. For Manovich, a database is a means of structuring information in.
Database Design for DNN Developers Sebastian Leupold.
MongoDB An introduction. What is MongoDB? The name Mongo is derived from Humongous To say that MongoDB can handle a humongous amount of data Document.
1 PHP and MySQL. 2 Topics  Querying Data with PHP  User-Driven Querying  Writing Data with PHP and MySQL PHP and MySQL.
M1G Introduction to Database Development 6. Building Applications.
Rensselaer Polytechnic Institute CSCI-4380 – Database Systems David Goldschmidt, Ph.D.
1 CS 430 Database Theory Winter 2005 Lecture 16: Inside a DBMS.
Database Design and Management CPTG /23/2015Chapter 12 of 38 Functions of a Database Store data Store data School: student records, class schedules,
1 CS 430 Database Theory Winter 2005 Lecture 2: General Concepts.
Efficient RDF Storage and Retrieval in Jena2 Written by: Kevin Wilkinson, Craig Sayers, Harumi Kuno, Dave Reynolds Presented by: Umer Fareed 파리드.
1 CS 430 Database Theory Winter 2005 Lecture 14: Additional SQL Topics.
Introduction.  Administration  Simple DBMS  CMPT 454 Topics John Edgar2.
MongoDB First Light. Mongo DB Basics Mongo is a document based NoSQL. –A document is just a JSON object. –A collection is just a (large) set of documents.
8 Chapter Eight Server-side Scripts. 8 Chapter Objectives Create dynamic Web pages that retrieve and display database data using Active Server Pages Process.
Session 1 Module 1: Introduction to Data Integrity
CP476 Internet Computing Perl CGI and MySql 1 Relational Databases –A database is a collection of data organized to allow relatively easy access for retrievals,
Unit-8 Introduction Of MySql. Types of table in PHP MySQL supports various of table types or storage engines to allow you to optimize your database. The.
Introduction to Core Database Concepts Getting started with Databases and Structure Query Language (SQL)
Database Management Systems 3ed, R. Ramakrishnan and J. Gehrke1 The Relational Model Chapter 3.
7.5 Using Stored-Procedure and Triggers NAME MATRIC NUM GROUP Muhammad Azwan Bin Khairul Anwar CS2305A Muhammad Faiz Bin Badrol Shah CS2305B.
Introduction to Database Programming with Python Gary Stewart
Data Integrity & Indexes / Session 1/ 1 of 37 Session 1 Module 1: Introduction to Data Integrity Module 2: Introduction to Indexes.
SQL IMPLEMENTATION & ADMINISTRATION Indexing & Views.
More SQL: Complex Queries, Triggers, Views, and Schema Modification
Database and Cloud Security
Logical Database Design and the Rational Model
for-each PROS CONS easy to use access to ALL items one-by-one
Information Retrieval in Practice
Chapter 14: System Protection
MongoDB Er. Shiva K. Shrestha ME Computer, NCIT
MongoDB Distributed Write and Read
Applied CyberInfrastructure Concepts Fall 2017
CS101 Introduction to Computing Lecture 19 Programming Languages
Chapter 4 Relational Databases
SQL: Advanced Options, Updates and Views Lecturer: Dr Pavle Mogin
NOSQL databases and Big Data Storage Systems
Pig Latin - A Not-So-Foreign Language for Data Processing
Un</br>able’s MySecretSecrets
Relational Algebra Chapter 4, Part A
Translation of ER-diagram into Relational Schema
ISC440: Web Programming 2 Server-side Scripting PHP 3
Database Fundamentals
The Relational Model Relational Data Model
Teaching slides Chapter 8.
CS 440 Database Management Systems
Advanced SQL: Views & Triggers
Intro to Relational Databases
Unit I-2.
Chapter 7 Using SQL in Applications
Introduction of Week 11 Return assignment 9-1 Collect assignment 10-1
Computer Science Projects Database Theory / Prototypes
Chapter 7 Using SQL in Applications
CS5220 Advanced Topics in Web Programming Introduction to MongoDB
Database Management Systems
ICOM 5016 – Introduction to Database Systems
INFO/CSE 100, Spring 2006 Fluency in Information Technology
Lecture 14: Databases February 26, 2018 Get a PSQL or mysql window up
CPSC-608 Database Systems
NoSQL & Document Stores
Information Retrieval and Web Design
NoSQL databases An introduction and comparison between Mongodb and Mysql document store.
INTRODUCTION A Database system is basically a computer based record keeping system. The collection of data, usually referred to as the database, contains.
Presentation transcript:

Lecture 15: Databases II Wednesday Feburary 28, 2018 A bit long – no time to run mongo queries.

Embedded SQL SQL is used inside programs CS132 Lecture 14: Databases Embedded SQL SQL is used inside programs Actually built into some languages Create a string representing the query Pass that string to the database to interpret Concepts Connection: connection to the database Accessed by a URL giving type, host, database, user, pwd,… Statement: set of SQL statements to be executed as one Typically a single query or set of updates ResultSet: iterator over a returned table Can iterate over tuples in the returned values Can access fields of the current tuple 4/10/2019

Using Embedded SQL Safely CS132 Lecture 14: Databases Using Embedded SQL Safely Queries and updates are built as strings Provides flexibility Lets program add user-entered values to the queries Can be problematic if user values are non-standard Prepared statements Queries with variables are defined as strings Variables in the query are represented by $, $i, or ? Values are passed when the query is used Can be faster (database can optimize the query once) Safer and more secure Use when possible (and its always possible) 4/10/2019

CS132 Lecture 14: Databases SQL INSERT Statement INSERT INTO table ( field, …, field ) VALUES ( val,…,val) List of fields is optional, but should be there Avoids problems in future, reminds reader of what is what Values can be DEFAULT Constants [ true, false, 0, … ] Built-in Functions [ CURRENT_TIMESTAMP ] Variables – use $, $i, ? and pass values separately Results of queries [ (SELECT id FROM Artist A WHERE A.name = ‘nsync’) ] INSERT INTO table (field, …, field) SELECT … 4/10/2019

SQL UPDATE Statement UPDATE table SET field = value WHERE condition CS132 Lecture 14: Databases SQL UPDATE Statement UPDATE table SET field = value WHERE condition SET field = value, field = value, … Values as in an insert statement WHERE as in normal select Can update multiple rows at once Be careful with the condition 4/10/2019

SQL DELETE Statement DELETE FROM table WHERE condition CS132 Lecture 14: Databases SQL DELETE Statement DELETE FROM table WHERE condition Removes all rows in table where condition is true If condition is omitted, deletes all rows 4/10/2019

Relational Database Schema CS132 Lecture 14: Databases Relational Database Schema The tables define the database schema Table names Set of attributes/fields for each table Each attribute is typed Tables can have a KEY Tables can have permissions Tables can have constraints (e.g. NOT NULL, FOREIGN KEY) Schema can contain other information Indices on the tables Virtual tables (VIEWS) 4/10/2019

Rules for Table Organization CS132 Lecture 14: Databases Rules for Table Organization How the tables are organized is important Based on the relationships among data items If x => y (e.g. DiskId => Title, length,…) put in same table If x is used multiple times, make it a separate table Especially if x has multiple values or long values If x can occur multiple times for y, create a separate table Just for relating x and y If table will be referenced by other tables, add an id field As a key field 4/10/2019

Database Manager Position CS132 Lecture 14: Databases Database Manager Position In general this is a difficult problem Need to understand semantics of the data Need to understand how the data will evolve If you get it wrong, what happens? With only queries, not much (performance) If the data is update, it might become inconsistent Artist information for example If the data relationships are updated, might not fit structure Multiple artist CDs Database manager position Handles setting up, changing, updating, etc. the database What is the inconsistency with artists? 4/10/2019

Transactions CS132 Lecture 14: Databases Multiple operations might need to be done together BEGIN TRANSACTION name Operation COMMIT TRANSACTION name Example: withdraw and deposit to transfer funds Database guarantees that transactions appear sequential No intermediate changes by others Operations all done or all not done (atomic) ACID semantics Use any-db-transaction when using Node.JS 4/10/2019

CS132 Lecture 14: Databases Batch Processing Large numbers of inserts (or updates) can be inefficient But doing them all at once can be a lot faster Can create BATCH updates Like transactions (can use transactions for this) Part of Statement interface for embedded SQL Most databases provide a means for batch inserts Insert multiple tuples into a relation Data comes from a file 4/10/2019

Schema Updates CS132 Lecture 14: Databases Tables are going to change Applications evolve over time New data is needed, old data no longer needed Adding tables is easy Modifying tables is possible ALTER TABLE ADD newfield int DEFAULT 0 Can remove fields, add constraints, add indices, … Beware of constraints between tables This can be an expensive operation And might require other computation (computing initial values) Can be tricky Might involve dependencies, indices, constraints Maintaining a test database and a production database 4/10/2019

Permissions External database systems control access CS132 Lecture 14: Databases Permissions External database systems control access Users (independent of system users) Permissions (on a table-by-table basis) When you set up the database you can set up users With different permissions on different tables This can provide a safeguard in your application Application normally runs as a limited access user Administrative tasks run with more privileges Think about permissions when you set up your database Create a one or more separate users for your application 4/10/2019

CS132 Lecture 18: Other Server Technologies SQL Databases Pros ACID semantics (transactions, guaranteed consistency) Queries handled by the database engine (little code) Portable Data consistency through the schema Supports complex indices, triggers, constraints Cons Replication of the database is difficult Doesn’t scale that well to huge databases Not good at storing unstructured documents (blobs) Complex Database Manager issues Changing schema requires thought and effort Can we do better for web applications? 4/10/2019

CS132 Lecture 14: Databases Question Suppose we want to go beyond SQL databases. NoSQL databases are a response to the needs of modern web applications. Which is not a characteristic of such databases? They can be accessed directly from HTML5 using database extensions They support eventual consistency rather than immediate consistency They typically use a simple or specialized query language. They provide the ability to easily shard or replicate data They provide specialized implementations suitable for particular types of applications. 4/10/2019

NOSQL Databases CS132 Lecture 18: Other Server Technologies No SQL Essentially key-value stores Arbitrary values, single key; look up by key Indexed by key Eventual consistency (no transactions) Not-Only SQL Trend to make these more like SQL databases NewSQL databases combine SQL queries with No-SQL-style stores Index on particular fields Field value -> { key } is what is in index Simple queries Given a set of field values, find corresponding data values Done my intersecting key sets Take resultant key set and allow iteration over values 4/10/2019

MongoDB : a simple (sophisticated) NoSQL CS132 Lecture 14: Databases MongoDB : a simple (sophisticated) NoSQL Store JSON objects db.cds.insert( { diskid : '2a04b804', title : 'Body & Soul', artist : 'SPEED', length : 1210, genre : 'jpop', year : 1996, tracks : [ { name : 'Body & Soul',artist : 'SPEED',length : 302,number : 0,offset : 187 }, { name : 'I Remember',artist : 'SPEED',length : 278,number : 1,offset : 22892 }, { name : 'Body & Soul (hand Bag Mix)',artist : 'SPEED',length : 323,number : 2,offset : 43777 }, { name : 'Body & Soul (instrumental)',artist : 'SPEED',length : 302,number : 3,offset : 68070 } ] } ); 4/10/2019

MongoDB Collections Basic Storage is a collection Set of json objects CS132 Lecture 14: Databases MongoDB Collections Basic Storage is a collection Set of json objects Each has a unique identifier (generated by Mongo) Fast lookup based on identifier Access methods on a collection db.<collection>.find( query, projection) Query: { field : { $eq : <value> }, … } ($eq,$gt, …) Field can be “field” or “field.subfield” Can do most queries over a single collection Projection: { field : 0|1, …. } 1 => include field, 0 => exclude field 4/10/2019

MongoDB Example Queries CS132 Lecture 14: Databases MongoDB Example Queries db.cds.count() db.cds.find( { artist : ‘Taylor Swift’ } ) db.cds.find( { “tracks.artist” : ‘Jaques Brel’, “tracks.name” : “Mathilde” } ) db.cds.runCommand(“text”, { search: ‘Paris’ } ) 4/10/2019

MongoDB Indices db.<collection>.createIndex(keys,options) CS132 Lecture 14: Databases MongoDB Indices db.<collection>.createIndex(keys,options) Keys: { field : 1 } (for ascending) ensureIndex in some versions Mongo supports text indexing Keys: { field : ‘text’ } Keys: { “$**” : “text” } Text query db.<collection>.runCommand(‘text’, { search: ‘words’ }) Words: if multiple, then OR of the words (ala Google, w/ ranking) Can use ‘ ”word1” “word2” “word phrase” ‘ as well (and) 4/10/2019

MongoDB CRUD db.<collection>.insert ( { … } ) CS132 Lecture 14: Databases MongoDB CRUD db.<collection>.insert ( { … } ) Newer versions support insertMany db.<collection>.update( { query}, { update }) Query: similar to a find query Update: { $set : { field : ‘value’ } } db.<collection>.remove( { query } ) Bulk operations via db.<collection>.bulkWrite( [ … ] ) 4/10/2019

Distributed Mongo Mongo makes it easy to split up a collection CS132 Lecture 14: Databases Distributed Mongo Mongo makes it easy to split up a collection Into distinct portions called shards Based on mongo-generated key or on user key Can shard database to match different servers Makes very large databases possible Makes very large databases faster Also does distributed processing Handles scaling to very large databases 4/10/2019

Mongo Transactions No ACID guarantees CS132 Lecture 14: Databases Mongo Transactions No ACID guarantees Clients may see writes before they are committed If a query updates multiple documents, might read some updates and not others BASE instead High availability Eventual consistency Newer versions provide transactions 4/10/2019

NOSQL Databases Pros Can store arbitrary objects CS132 Lecture 18: Other Server Technologies NOSQL Databases Pros Can store arbitrary objects Easy to set up and use (little management) Easy to change Scales nicely Cons No transactional guarantees Data consistency is up to the user Complex queries need to be coded explicitly 4/10/2019

What Type of Database to Use CS132 Lecture 14: Databases What Type of Database to Use How important are ACID guarantees Newer versions of Mongo provide some of this How important is data consistency SQL provides constraints and triggers How important is scalability Newer versions of SQL database provide sharding, etc. How varied and complex are the queries Complex, unanticipated NoSQL queries require code How complex is the data and how often does the schema change Complex data is much easier in NoSQL Might want a combination of databases Some for complex data Some for users, authentication, etc. where ACID is useful 4/10/2019

CS132 Lecture 14: Databases Elevator Talks Three Minute Sales Pitch (includes 30 seconds for questions) Convince a someone to invest in your project Important points What is the problem What is your solution Why is this important Why should the audience be interested 4/10/2019

Next Time Database LAB Do the pre labs CS132 Lecture 14: Databases Next Time Database LAB Do the pre labs Look up Six Degrees of Kevin Bacon Monday/Wednesday: Elevator talks 4/10/2019

Which SQL Database System to Use CS132 Lecture 14: Databases Which SQL Database System to Use Sqlite3, Derby Only for experimentation, small-scale applications Embedded use only (single access point) MySQL, PostgreSQL Most common for web applications Extensions to both to handle scaling are available Roughly equivalent Db2, SqlServer, Oracle For larger databases, better support 4/10/2019