General Overview - rel. model. Carnegie Mellon Univ. Dept. of Computer Science Database Applications. Reminder: our Mini-U db

Similar documents
Carnegie Mellon Univ. Dept. of Computer Science Database Applications. General Overview - rel. model. Overview - detailed - SQL

CS 4604: Introduc0on to Database Management Systems. B. Aditya Prakash Lecture #4: SQL---Part 2

Carnegie Mellon Univ. Dept. of Computer Science Database Applications. General Overview - rel. model. Overview - detailed - SQL

Database Applications (15-415)

Carnegie Mellon Univ. Dept. of Computer Science /615 - DB Applications. Today's Party. Example Database. Faloutsos/Pavlo CMU /615

The Relational Model of Data (ii)

Database Applications (15-415)

EGCI 321: Database Systems. Dr. Tanasanee Phienthrakul

Faloutsos - Pavlo CMU SCS /615

Overview. Carnegie Mellon Univ. School of Computer Science /615 - DB Applications. Concepts - reminder. History

The Relational Model. Relational Data Model Relational Query Language (DDL + DML) Integrity Constraints (IC)

The Relational Model. Outline. Why Study the Relational Model? Faloutsos SCS object-relational model

Chapter 4. Basic SQL. Copyright 2011 Pearson Education, Inc. Publishing as Pearson Addison-Wesley

The Relational Model. Roadmap. Relational Database: Definitions. Why Study the Relational Model? Relational database: a set of relations

Relational Databases BORROWED WITH MINOR ADAPTATION FROM PROF. CHRISTOS FALOUTSOS, CMU /615

Chapter 3: Introduction to SQL

Chapter 4. Basic SQL. SQL Data Definition and Data Types. Basic SQL. SQL language SQL. Terminology: CREATE statement

Introduction to Data Management. Lecture #4 (E-R Relational Translation)

Copyright 2016 Ramez Elmasri and Shamkant B. Navathe

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

QQ Group

The SQL data-definition language (DDL) allows defining :

Introduction to Data Management. Lecture #5 Relational Model (Cont.) & E-Rà Relational Mapping

Database Applications (15-415)

Carnegie Mellon Univ. Dept. of Computer Science Database Applications. Outline. We ll learn: Faloutsos CMU SCS

SQL DATA DEFINITION LANGUAGE

The Relational Model. Chapter 3. Comp 521 Files and Databases Fall

Data Modeling. Yanlei Diao UMass Amherst. Slides Courtesy of R. Ramakrishnan and J. Gehrke

The Relational Model. Why Study the Relational Model? Relational Database: Definitions

Introduction to Data Management. Lecture #4 (E-R à Relational Design)

SQL: Data Definition Language. csc343, Introduction to Databases Diane Horton Fall 2017

Chapter 3: Introduction to SQL. Chapter 3: Introduction to SQL

Slides by: Ms. Shree Jaswal

CSEN 501 CSEN501 - Databases I

The Relational Model 2. Week 3

Principles of Data Management

Chapter 1 SQL and Data

SQL Data Definition and Data Manipulation Languages (DDL and DML)

Chapter 3: Introduction to SQL

An Introduction to Structured Query Language

The Relational Model. Chapter 3. Comp 521 Files and Databases Fall

An Introduction to Structured Query Language

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

SQL. SQL DDL Statements

Sql Server Syllabus. Overview

Announcements (September 18) SQL: Part II. Solution 1. Incomplete information. Solution 3? Solution 2. Homework #1 due today (11:59pm)

SQL DATA DEFINITION LANGUAGE

SQL: DDL. John Ortiz Cs.utsa.edu

D B M G. SQL language: basics. Managing tables. Creating a table Modifying table structure Deleting a table The data dictionary Data integrity

Database Applications (15-415)

Comp 5311 Database Management Systems. 4b. Structured Query Language 3

COSC344 Database Theory and Applications. Lecture 5 SQL - Data Definition Language. COSC344 Lecture 5 1

Debapriyo Majumdar DBMS Fall 2016 Indian Statistical Institute Kolkata

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

Introduction to Data Management. Lecture 16 (SQL: There s STILL More...) It s time for another installment of...

Relational data model

Data Modelling and Databases. Exercise Session 7: Integrity Constraints

An Introduction to Structured Query Language

COGS 121 HCI Programming Studio. Week 03 - Tech Lecture

Carnegie Mellon Univ. Dept. of Computer Science /615 - DB Applications. Outline. We ll learn: Faloutsos/Pavlo CMU /615

Outline. Carnegie Mellon Univ. Dept. of Computer Science /615 - DB Applications. We ll learn: We ll learn (cnt d) Faloutsos/Pavlo CMU /615

Content: How to transform the relational model into a database schema DDL: create table students(studentnr int, name varchar);

Accessing databases in Java using JDBC

SQL: Part II. Announcements (September 18) Incomplete information. CPS 116 Introduction to Database Systems. Homework #1 due today (11:59pm)

COSC 304 Introduction to Database Systems SQL DDL. Dr. Ramon Lawrence University of British Columbia Okanagan

The Relational Model. Chapter 3

An Introduction to Structured Query Language

DATABASE TECHNOLOGY - 1MB025

An Introduction to Structured Query Language

SQL: Data Definition Language

DATABASTEKNIK - 1DL116

Based on handout: Carnegie Mellon Univ. Dept. of Computer Science /615 - DB Applications. Goal. Faloutsos & Pavlo CMU SCS /615

CS2300: File Structures and Introduction to Database Systems

The Relational Model. Chapter 3. Database Management Systems, R. Ramakrishnan and J. Gehrke 1

Lab # 4. Data Definition Language (DDL)

Module 3 MySQL Database. Database Management System

ERwin and JDBC. Mar. 6, 2007 Myoung Ho Kim

How to program applications. CS 2550 / Spring 2006 Principles of Database Systems. SQL is not enough. Roadmap

Relational Model. Topics. Relational Model. Why Study the Relational Model? Linda Wu (CMPT )

Introduction to SQL Server. nikos bikakis

COSC344 Database Theory and Applications. Lecture 6 SQL Data Manipulation Language (1)

Introduction to SQL. ECE 650 Systems Programming & Engineering Duke University, Spring 2018

SQL: Part III. Announcements. Constraints. CPS 216 Advanced Database Systems

First lecture of this chapter is in slides (PPT file)

Where Are We? Next Few Lectures. Integrity Constraints Motivation. Constraints in E/R Diagrams. Keys in E/R Diagrams

MySQL Creating a Database Lecture 3

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

DATABASE TECHNOLOGY - 1MB025

Chapter 4: SQL. Basic Structure

Basic SQL. Basic SQL. Basic SQL

Chapter 1 An introduction to relational databases and SQL

SQL: Data De ni on. B0B36DBS, BD6B36DBS: Database Systems. h p:// Lecture 3

SQL DATA DEFINITION LANGUAGE

SQL Introduction. CS 377: Database Systems

Database Management Systems. Chapter 3 Part 1

Database System Concepts

Database Programming. Week 9. *Some of the slides in this lecture are created by Prof. Ian Horrocks from University of Oxford

ORACLE DATABASE 12C INTRODUCTION

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

Databases. Jörg Endrullis. VU University Amsterdam

Transcription:

Faloutsos 15-415 Carnegie Mellon Univ. Dept. of Computer Science 15-415 - Database Applications Lecture#8 (cont d): SQL, Part 2 General Overview - rel. model Formal query languages rel algebra and calculi Commercial query languages SQL QBE, (QUEL) Faloutsos 15-415 #2 embedded SQL Reminder: our Mini-U db STUDENT Ssn Name Address 123 smith main str 234 jones forbes ave TAKES SSN c-id grade 123 15-413 A 234 15-413 B CLASS c-id c-name units 15-413 s.e. 2 15-412 o.s. 2 Faloutsos 15-415 #3 Faloutsos 15-415 #4 DML - insertions etc insert into student values ( 123, smith, main ) DML - insertions etc bulk insertion: how to insert, say, a table of foreign-student s, in bulk? insert into student(ssn, name, address) values ( 123, smith, main ) Faloutsos 15-415 #5 Faloutsos 15-415 #6 1

Faloutsos 15-415 DML - insertions etc bulk insertion: DML - deletion etc delete the record of smith insert into student select ssn, name, address from foreign-student Faloutsos 15-415 #7 Faloutsos 15-415 #8 DML - deletion etc delete the record of smith : delete from student where name= smith (careful - it deletes ALL the smith s!) DML - update etc record the grade A for ssn=123 and course 15-415 update takes set grade= A where ssn= 123 and c-id= 15-415 (will set to A ALL such records) Faloutsos 15-415 #9 Faloutsos 15-415 #10 DML - view update consider the db-takes view: create view db-takes as (select * from takes where c-id= 15-415 ) view updates are tricky - typically, we can only update views that have no joins, nor aggregates even so, consider changing a c-id to 15-222... DML - joins so far: INNER joins, eg: select ssn, c-name from takes, class where takes.c-id = class.c-id Faloutsos 15-415 #11 Faloutsos 15-415 #12 2

Faloutsos 15-415 Equivalently: DML - joins select ssn, c-name from takes join class on takes.c-id = class.c-id Joins select [column list] from table_name [inner {left right full} outer ] join table_name on qualification_list where Faloutsos 15-415 #13 Faloutsos 15-415 #14 Reminder: our Mini-U db Inner join STUDENT Ssn Name Address 123 smith main str 234 jones forbes ave CLASS c-id c-name units 15-413 s.e. 2 15-412 o.s. 2 TAKES SSN c-id grade 123 15-413 A 234 15-413 B CLASS c-id c-name units 15-413 s.e. 2 15-412 o.s. 2 TAKES SSN c-id grade 123 15-413 A 234 15-413 B SSN c-name 123 s.e 234 s.e o.s.: gone! Faloutsos 15-415 #15 Faloutsos 15-415 #16 Outer join Outer join TAKES SSN c-id grade 123 15-413 A 234 15-413 B CLASS c-id c-name units 15-413 s.e. 2 15-412 o.s. 2 select ssn, c-name from takes right outer join class on takes.cid=class.c-id SSN c-name 123 s.e 234 s.e. null o.s. SSN c-name 123 s.e 234 s.e. null o.s. Faloutsos 15-415 #17 Faloutsos 15-415 #18 3

Faloutsos 15-415 Outer join Null Values left outer join right outer join full outer join natural join null -> unknown, or inapplicable, (or ) Complications: 3-valued logic (true, false and unknown). null = null : false!! Faloutsos 15-415 #19 Faloutsos 15-415 #20 embedded SQL create table student (ssn char(9) not null, name char(30), address char(50), primary key (ssn) ) Faloutsos 15-415 #21 Faloutsos 15-415 #22 create table r( A1 D1,, An Dn, integrity-constraint1, integrity-constraint-n) Domains: char(n), varchar(n) int, numeric(p,d), real, double precision float, smallint date, time Faloutsos 15-415 #23 Faloutsos 15-415 #24 4

Faloutsos 15-415 delete a table: difference between drop table student modify a table: alter table student drop address delete from student alter table student add major char(10) Faloutsos 15-415 #25 Faloutsos 15-415 #26 integrity constraints: primary key foreign key check(p) create table takes (ssn char(9) not null, c-id char(5) not null, grade char(1), primary key (ssn, c-id), check grade in ( A, B, C, D, F )) Faloutsos 15-415 #27 Faloutsos 15-415 #28 Referential Integrity constraints foreign keys - eg: create table takes( ssn char(9) not null, c-id char(5) not null, grade integer, primary key(ssn, c-id), foreign key ssn references student, foreign key c-id references class) Faloutsos 15-415 #29 Referential Integrity constraints foreign key ssn references student, foreign key c-id references class) Effect: expects that ssn to exist in student table blocks ops that violate that - how?? insertion? deletion/update? Faloutsos 15-415 #30 5

Faloutsos 15-415 Referential Integrity constraints foreign key ssn references student on delete cascade on update cascade,... -> eliminate all student enrollments other options (set to null, to default etc) embedded SQL Faloutsos 15-415 #31 Faloutsos 15-415 #32 Weapons for IC: assertions create assertion <assertion-name> check <predicate> triggers (~ assertions with teeth ) on operation, if condition, then action Triggers - example define trigger zerograde on update takes (if new takes.grade < 0 then takes.grade = 0) Faloutsos 15-415 #33 Faloutsos 15-415 #34 Triggers - discussion more complicated: managers have higher salaries than their subordinates - a trigger can automatically boost mgrs salaries triggers: tricky (infinite loops) embedded SQL Faloutsos 15-415 #35 Faloutsos 15-415 #36 6

Faloutsos 15-415 Authorization grant <priv.-list> on <table-name> to <user-list> privileges for tuples: read / insert / delete / update privileges for tables: create, drop, index Authorization cont d variations: with grant option revoke <priv.-list> on <t-name> from <user_ids> Faloutsos 15-415 #37 Faloutsos 15-415 #38 embedded SQL; application development Embedded SQL from within a host language (eg., C, VB ) EXEC SQL <emb. SQL stmnt> END-EXEC Q: why do we need embedded SQL?? Faloutsos 15-415 #39 Faloutsos 15-415 #40 Embedded SQL SQL returns sets; host language expects a tuple - impedance mismatch! solution: cursor, ie., a pointer over the set of tuples. example: main(){ EXEC SQL declare c cursor for select * from student END-EXEC Embedded SQL Faloutsos 15-415 #41 Faloutsos 15-415 #42 7

Faloutsos 15-415 Embedded SQL - ctn d EXEC SQL open c END-EXEC while(!sqlerror ){ EXEC SQL fetch c into :cssn, :cname, :cad END-EXEC fprintf(, cssn, cname, cad); } Faloutsos 15-415 #43 Embedded SQL - ctn d EXEC SQL close c END-EXEC } /* end main() */ Faloutsos 15-415 #44 Dynamic SQL main(){ /* set all grades to user s input */ char *sqlcmd= update takes set grade =? ; EXEC SQL prepare dynsql from :sqlcmd ; char inputgrade[5]= a ; EXEC SQL execute dynsql using :inputgrade; } /* end main() */ Faloutsos 15-415 #45 embedded SQL; application development Faloutsos 15-415 #46 Overview concepts of SQL programs walkthrough of Create.java walkthrough of showall.java Outline of an SQL application establish connection with db server authenticate (user/password) execute SQL statement(s) process results close connection Faloutsos 15-415 #47 Faloutsos 15-415 #48 8

Faloutsos 15-415 andrew machine eg., sun4.andrew Pictorially: JDBC/ODBC dbclass.intro.cs.cmu.edu Create.java Purpose: to load the parent-child table legend: Create.java Create.class Windows NT box; With, say, ORACLE Server interesting observation very important point Faloutsos 15-415 #49 Faloutsos 15-415 #50 import java.io.*; import java.util.*; import java.sql.*; public class Create { Walk-through Create.java static final String DbURL = "jdbc:oracle:thin:@dbclass.intro.cs.cmu.edu:1521:dbintro"; //Oracel server at cs.cmu static final String OraDriver = "oracle.jdbc.driver.oracledriver"; //Oracle driver static final String User = "your-andrew-id"; static final String Passwd = "your-oracle-password"; Walk-through Create.java static final String Passwd = "your-oracle-password"; static final String filename="pc.txt"; //file name for text data public static void main(string[] args) { Connection con = null; try { // Load the Oracle Driver Class.forName(OraDriver); // Get a Connection to the database con = DriverManager.getConnection(DbURL, User, Passwd); // Create a Statement object Statement stmt = con.createstatement(); Faloutsos 15-415 #51 Faloutsos 15-415 #52 Walk-through Create.java Walk-through Create.java // Create a table named as PC (varchar2(10), varchar2(10)); String sqlst = "CREATE TABLE PC (parent varchar2(10), child varchar2(10))"; stmt.executequery(sqlst); rest of program: read input file insert one tuple at a time close connection Faloutsos 15-415 #53 Faloutsos 15-415 #54 9

Faloutsos 15-415 Walk-through Create.java Overview } while ((line = in.readline())!= null) { // read in the names into parent and child // Execute a SQL - insert statement sqlst = "INSERT INTO PC (parent, child) VALUES ('" + parent + "', '" + child + "')"; System.out.println("===" + (i++) + "===>" + sqlst); stmt.executequery(sqlst); } in.close(); con.commit(); concepts of SQL programs walkthrough of Create.java walkthrough of showall.java Faloutsos 15-415 #55 Faloutsos 15-415 #56 Walk-through showall.java Walk-through showall.java purpose: print all (parent, child) pairs // after opening the connection String sqlst = "SELECT * FROM PC"; Faloutsos 15-415 #57 Faloutsos 15-415 #58 Walk-through showall.java ResultSet rs = stmt.executequery(sqlst); while (rs.next()) { System.out.println( rs.getstring("parent") + ";" + rs.getstring("child") ); } Conclusions Outline of an SQL application: establish connection with db server authenticate (user/password) execute SQL statement(s) process results close connection Faloutsos 15-415 #59 Faloutsos 15-415 #60 10