CMU SCS Carnegie Mellon Univ. Dept. of Computer Science 15-415 - Database Applications C. Faloutsos Lecture#7 (cont’d): Rel. model - SQL part3.

Slides:



Advertisements
Similar presentations
Copyright © 2011 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Chapter 5 More SQL: Complex Queries, Triggers, Views, and Schema Modification.
Advertisements

Relational Database. Relational database: a set of relations Relation: made up of 2 parts: − Schema : specifies the name of relations, plus name and type.
CMU SCS Carnegie Mellon Univ. Dept. of Computer Science /615 - DB Applications C. Faloutsos & A. Pavlo Lecture#6: Rel. model - SQL part1 (R&G, chapter.
Database Management Systems, R. Ramakrishnan and J. Gehrke1 The Relational Model Chapter 3.
Carnegie Mellon Carnegie Mellon Univ. Dept. of Computer Science Database Applications C. Faloutsos Integrity Constraints.
SQL Lecture 10 Inst: Haya Sammaneh. Example Instance of Students Relation  Cardinality = 3, degree = 5, all rows distinct.
Fall 2005 ICS184/EECS116 – Notes 08 1 ICS 184/EECS116: Introduction to Data Management Lecture Note 8 SQL: Structured Query Language -- DDL.
Overview Begin 6:00 Quiz15 mins6:15 Review Table Terms25 mins6:40 Short Break10 mins6:50 SQL: Creating Tables60 mins7:50 Break10 mins8:00 Lab – Creating.
Database Design -- Basic SQL
The Relational Model CMU SCS /615 C. Faloutsos – A. Pavlo Lecture #3 R & G, Chap. 3.
The Relational Model CMU SCS /615 C. Faloutsos – A. Pavlo Lecture #3 R & G, Chap. 3.
Temple University – CIS Dept. CIS616– Principles of Data Management V. Megalooikonomou Integrity Constraints (based on notes by Silberchatz,Korth, and.
CMPT 354, Simon Fraser University, Fall 2008, Martin Ester 28 Database Systems I The Relational Data Model.
Copyright © 2011 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Chapter 4 SQL: Data Definition, Constraints, and Basic Queries and Updates.
SQL 2 – The Sequel R&G, Chapter 5 Lecture 10. Administrivia Homework 2 assignment now available –Due a week from Sunday Midterm exam will be evening of.
Temple University – CIS Dept. CIS331– Principles of Database Systems V. Megalooikonomou Query by example (based on notes by Silberchatz,Korth, and Sudarshan.
1 Relational Model. 2 Relational Database: Definitions  Relational database: a set of relations  Relation: made up of 2 parts: – Instance : a table,
The Relational Model Lecture 3 Book Chapter 3 Relational Data Model Relational Query Language (DDL + DML) Integrity Constraints (IC) From ER to Relational.
Murali Mani SQL DDL and Oracle utilities. Murali Mani Datatypes in SQL INT (or) INTEGER FLOAT (or) REAL DECIMAL (n, m) CHAR (n) VARCHAR (n) DATE, TIME.
Structured Query Language. Brief History Developed in early 1970 for relational data model: –Structured English Query Language (SEQUEL) –Implemented with.
CMU SCS Carnegie Mellon Univ. Dept. of Computer Science /615 - DB Applications C. Faloutsos & A. Pavlo Lecture#7 : Rel. model - SQL part2 (R&G, chapters.
DBMS 3. course. Reminder Data independence: logical and physical Concurrent processing – Transaction – Deadlock – Rollback – Logging ER Diagrams.
Carnegie Mellon Carnegie Mellon Univ. Dept. of Computer Science Database Applications C. Faloutsos Rel. model - SQL part1.
Temple University – CIS Dept. CIS331– Principles of Database Systems V. Megalooikonomou Relational Model – SQL Part I (based on notes by Silberchatz,Korth,
Structured Query Language (SQL) A2 Teacher Up skilling LECTURE 2.
The Relational Model These slides are based on the slides of your text book.
CSE314 Database Systems Lecture 4 Basic SQL Doç. Dr. Mehmet Göktürk src: Elmasri & Navanthe 6E Pearson Ed Slide Set.
The Relational Model. Review Why use a DBMS? OS provides RAM and disk.
Structured Query Language. Brief History Developed in early 1970 for relational data model: –Structured English Query Language (SEQUEL) –Implemented with.
Chapter 8 Part 1 SQL-99 Schema Definition, Constraints, Queries, and Views.
Chapter 7 SQL HUANG XUEHUA. SQL SQL server2005 introduction Install components  management studio.
©Silberschatz, Korth and Sudarshan4.1Database System Concepts Modification of the Database – Deletion Delete all tuples from the loan relation. delete.
Temple University – CIS Dept. CIS331– Principles of Database Systems V. Megalooikonomou Relational Model – SQL Part II (based on notes by Silberchatz,Korth,
1 The Relational Model. 2 Why Study the Relational Model? v Most widely used model. – Vendors: IBM, Informix, Microsoft, Oracle, Sybase, etc. v “Legacy.
FALL 2004CENG 351 File Structures and Data Management1 Relational Model Chapter 3.
SQL: DDL John Ortiz Cs.utsa.edu.
CMU SCS Carnegie Mellon Univ. Dept. of Computer Science Database Applications Lecture#6: Relational calculus.
SQL: DDL. SQL Statements DDL - data definition language –Defining and modifying data structures (metadata): database, tables, views, etc. DML - data manipulation.
SQL Basics. What is SQL? SQL stands for Structured Query Language. SQL lets you access and manipulate databases.
Carnegie Mellon Carnegie Mellon Univ. Dept. of Computer Science Database Applications C. Faloutsos Rel. model - SQL part3.
1 Dept. of CIS, Temple Univ. CIS616/661 – Principles of Data Management V. Megalooikonomou Integrity Constraints (based on slides by C. Faloutsos at CMU)
Prince Sultan University Dept. of Computer & Information Sciences CS 340 Introduction to Database Systems.
The Relational Model Content based on Chapter 3 Database Management Systems, (Third Edition), by Raghu Ramakrishnan and Johannes Gehrke. McGraw Hill, 2003.
Carnegie Mellon Carnegie Mellon Univ. Dept. of Computer Science Database Applications C. Faloutsos Relational model.
CMU SCS Carnegie Mellon Univ. Dept. of Computer Science /615 – DB Applications C. Faloutsos & A. Pavlo Lecture#5: Relational calculus.
CMPT 258 Database Systems The Relationship Model (Chapter 3)
Carnegie Mellon Carnegie Mellon Univ. Dept. of Computer Science Database Applications C. Faloutsos Rel. model - SQL part2.
Week 8-9 SQL-1. SQL Components: DDL, DCL, & DML SQL is a very large and powerful language, but every type of SQL statement falls within one of three main.
©Silberschatz, Korth and Sudarshan1 Structured Query Language (SQL) Data Definition Language Domains Integrity Constraints.
DBMS 3. course. Reminder Data independence: logical and physical Concurrent processing – Transaction – Deadlock – Rollback – Logging ER Diagrams.
1 CS 430 Database Theory Winter 2005 Lecture 11: SQL DDL.
CS34311 The Relational Model. cs34312 Why Relational Model? Currently the most widely used Vendors: Oracle, Microsoft, IBM Older models still used IBM’s.
Temple University – CIS Dept. CIS616– Principles of Data Management V. Megalooikonomou Relational Model – SQL (based on notes by Silberchatz,Korth, and.
1 Dept. of CIS, Temple Univ. CIS616/661 – Principles of Data Management V. Megalooikonomou SQL (part 2) (based on slides by C. Faloutsos at CMU)
Carnegie Mellon Carnegie Mellon Univ. Dept. of Computer Science Database Applications C. Faloutsos Relational tuple calculus.
Constraints and Views Chap. 3-5 continued (7 th ed. 5-7)
Database Management Systems 3ed, R. Ramakrishnan and J. Gehrke1 The Relational Model Chapter 3.
1 CS122A: Introduction to Data Management Lecture #4 (E-R  Relational Translation) Instructor: Chen Li.
CENG 351 File Structures and Data Management1 Relational Model Chapter 3.
More SQL: Complex Queries, Triggers, Views, and Schema Modification
CS 480: Database Systems Lecture 13 February 13,2013.
SQL: Advanced Options, Updates and Views Lecturer: Dr Pavle Mogin
Database Management Systems (CS 564)
Lecture #4: Relational Algebra
Database Applications (15-415) SQL-Part II Lecture 9, February 04, 2018 Mohammad Hammoud.
The Relational Model Relational Data Model
More SQL: Complex Queries, Triggers, Views, and Schema Modification
CMPT 354: Database System I
SQL-1 Week 8-9.
Session - 6 Sequence - 1 SQL: The Structured Query Language:
Presentation transcript:

CMU SCS Carnegie Mellon Univ. Dept. of Computer Science Database Applications C. Faloutsos Lecture#7 (cont’d): Rel. model - SQL part3

CMU SCS FaloutsosCMU SCS #2 General Overview - rel. model Formal query languages –rel algebra and calculi Commercial query languages –SQL –QBE, (QUEL)

CMU SCS FaloutsosCMU SCS #3 Overview - detailed - SQL DML –select, from, where, renaming, ordering, – aggregate functions, nested subqueries –insertion, deletion, update other parts: DDL, authorization, triggers embedded SQL

CMU SCS FaloutsosCMU SCS #4 Reminder: our Mini-U db

CMU SCS FaloutsosCMU SCS #5 DML - insertions etc insert into student values (“123”, “smith”, “main”) insert into student(ssn, name, address) values (“123”, “smith”, “main”)

CMU SCS FaloutsosCMU SCS #6 DML - insertions etc bulk insertion: how to insert, say, a table of ‘foreign-student’s, in bulk?

CMU SCS FaloutsosCMU SCS #7 DML - insertions etc bulk insertion: insert into student select ssn, name, address from foreign-student

CMU SCS FaloutsosCMU SCS #8 DML - deletion etc delete the record of ‘smith’

CMU SCS FaloutsosCMU SCS #9 DML - deletion etc delete the record of ‘smith’: delete from student where name=‘smith’ (careful - it deletes ALL the ‘smith’s!)

CMU SCS FaloutsosCMU SCS #10 DML - update etc record the grade ‘A’ for ssn=123 and course update takes set grade=“A” where ssn=“123” and c-id=“15-415” (will set to “A” ALL such records)

CMU SCS FaloutsosCMU SCS #11 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

CMU SCS FaloutsosCMU SCS #12 DML - joins so far: ‘INNER’ joins, eg: select ssn, c-name from takes, class where takes.c-id = class.c-id

CMU SCS FaloutsosCMU SCS #13 DML - joins Equivalently: select ssn, c-name from takes join class on takes.c-id = class.c-id

CMU SCS FaloutsosCMU SCS #14 Joins select [column list] from table_name [inner | {left | right | full} outer ] join table_name on qualification_list where…

CMU SCS FaloutsosCMU SCS #15 Reminder: our Mini-U db

CMU SCS FaloutsosCMU SCS #16 Inner join o.s.: gone!

CMU SCS FaloutsosCMU SCS #17 Outer join

CMU SCS FaloutsosCMU SCS #18 Outer join select ssn, c-name from takes right outer join class on takes.c- id=class.c-id

CMU SCS FaloutsosCMU SCS #19 Outer join left outer join right outer join full outer join natural join

CMU SCS FaloutsosCMU SCS #20 Null Values null -> unknown, or inapplicable, (or …) Complications: –3-valued logic (true, false and unknown). –null = null : false!!

CMU SCS FaloutsosCMU SCS #21 Overview - detailed - SQL DML –select, from, where, renaming, ordering, – aggregate functions, nested subqueries –insertion, deletion, update other parts: DDL, authorization, triggers embedded SQL

CMU SCS FaloutsosCMU SCS #22 Data Definition Language create table student (ssn char(9) not null, name char(30), address char(50), primary key (ssn) )

CMU SCS FaloutsosCMU SCS #23 Data Definition Language create table r( A1 D1, …, An Dn, integrity-constraint1, … integrity-constraint-n)

CMU SCS FaloutsosCMU SCS #24 Data Definition Language Domains: char(n), varchar(n) int, numeric(p,d), real, double precision float, smallint date, time

CMU SCS FaloutsosCMU SCS #25 Data Definition Language delete a table: difference between drop table student delete from student

CMU SCS FaloutsosCMU SCS #26 Data Definition Language modify a table: alter table student drop address alter table student add major char(10)

CMU SCS FaloutsosCMU SCS #27 Data Definition Language integrity constraints: primary key foreign key check(P)

CMU SCS FaloutsosCMU SCS #28 Data Definition Language 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”))

CMU SCS FaloutsosCMU SCS #29 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)

CMU SCS FaloutsosCMU SCS #30 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?

CMU SCS FaloutsosCMU SCS #31 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)

CMU SCS FaloutsosCMU SCS #32 Overview - detailed - SQL DML –select, from, where, renaming, ordering, – aggregate functions, nested subqueries –insertion, deletion, update other parts: DDL, authorization, triggers embedded SQL

CMU SCS FaloutsosCMU SCS #33 Weapons for IC: assertions –create assertion check triggers (~ assertions with ‘teeth’) –on operation, if condition, then action

CMU SCS FaloutsosCMU SCS #34 Triggers - example define trigger zerograde on update takes (if new takes.grade < 0 then takes.grade = 0)

CMU SCS FaloutsosCMU SCS #35 Triggers - discussion more complicated: “managers have higher salaries than their subordinates” - a trigger can automatically boost mgrs salaries triggers: tricky (infinite loops…)

CMU SCS FaloutsosCMU SCS #36 Overview - detailed - SQL DML –select, from, where, renaming, ordering, – aggregate functions, nested subqueries –insertion, deletion, update other parts: DDL, authorization, triggers embedded SQL

CMU SCS FaloutsosCMU SCS #37 Authorization grant on to privileges for tuples: read / insert / delete / update privileges for tables: create, drop, index

CMU SCS FaloutsosCMU SCS #38 Authorization – cont’d variations: –with grant option –revoke on from

CMU SCS FaloutsosCMU SCS #39 Overview - detailed - SQL DML –select, from, where, renaming, ordering, – aggregate functions, nested subqueries –insertion, deletion, update other parts: DDL, authorization, triggers embedded SQL; application development

CMU SCS FaloutsosCMU SCS #40 Embedded SQL from within a ‘host’ language (eg., ‘C’, ‘VB’) EXEC SQL END-EXEC Q: why do we need embedded SQL??

CMU SCS FaloutsosCMU SCS #41 Embedded SQL SQL returns sets; host language expects a tuple - impedance mismatch! solution: ‘cursor’, ie., a ‘pointer’ over the set of tuples. example:

CMU SCS FaloutsosCMU SCS #42 Embedded SQL main(){ … EXEC SQL declare c cursor for select * from student END-EXEC …

CMU SCS FaloutsosCMU SCS #43 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); }

CMU SCS FaloutsosCMU SCS #44 Embedded SQL - ctn’d … EXEC SQL close c END-EXEC … } /* end main() */

CMU SCS FaloutsosCMU SCS #45 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() */

CMU SCS FaloutsosCMU SCS #46 Overview - detailed - SQL DML –select, from, where, renaming, ordering, – aggregate functions, nested subqueries –insertion, deletion, update other parts: DDL, authorization, triggers embedded SQL; application development

CMU SCS FaloutsosCMU SCS #47 Overview concepts of SQL programs walkthrough of Create.java walkthrough of showAll.java

CMU SCS FaloutsosCMU SCS #48 Outline of an SQL application establish connection with db server authenticate (user/password) execute SQL statement(s) process results close connection

CMU SCS FaloutsosCMU SCS #49 Pictorially: andrew machine eg., sun4.andrew Create.java Create.class dbclass.intro.cs.cmu.edu Windows NT box; With, say, ORACLE Server JDBC/ODBC

CMU SCS FaloutsosCMU SCS #50 Create.java Purpose: to load the parent-child table interesting observation very important point legend:

CMU SCS FaloutsosCMU SCS #51 Walk-through Create.java

CMU SCS FaloutsosCMU SCS #52 Walk-through Create.java

CMU SCS FaloutsosCMU SCS #53 Walk-through Create.java

CMU SCS FaloutsosCMU SCS #54 Walk-through Create.java rest of program: read input file insert one tuple at a time close connection

CMU SCS FaloutsosCMU SCS #55 Walk-through Create.java

CMU SCS FaloutsosCMU SCS #56 Overview concepts of SQL programs walkthrough of Create.java walkthrough of showAll.java

CMU SCS FaloutsosCMU SCS #57 Walk-through showAll.java purpose: print all (parent, child) pairs

CMU SCS FaloutsosCMU SCS #58 Walk-through showAll.java

CMU SCS FaloutsosCMU SCS #59 Walk-through showAll.java

CMU SCS FaloutsosCMU SCS #60 Conclusions Outline of an SQL application: establish connection with db server authenticate (user/password) execute SQL statement(s) process results close connection