Carnegie Mellon Carnegie Mellon Univ. Dept. of Computer Science 15-415 - Database Applications C. Faloutsos Rel. model - SQL part3.

Slides:



Advertisements
Similar presentations
Basic SQL Introduction Presented by: Madhuri Bhogadi.
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.
COMP 3715 Spring 05. Working with data in a DBMS Any database system must allow user to  Define data Relations Attributes Constraints  Manipulate data.
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
CMSC424, Spring 2005 CMSC424: Database Design Lecture 5.
CMPT 354, Simon Fraser University, Fall 2008, Martin Ester 28 Database Systems I The Relational Data Model.
CMU SCS Carnegie Mellon Univ. Dept. of Computer Science Database Applications C. Faloutsos Lecture#7 (cont’d): Rel. model - SQL part3.
SQL Sangeeta Devadiga CS157A, Fall Outline Background Data Definition Basic Structure Set Operation.
Temple University – CIS Dept. CIS331– Principles of Database Systems V. Megalooikonomou Query by example (based on notes by Silberchatz,Korth, and Sudarshan.
©Silberschatz, Korth and Sudarshan4.1Database System Concepts Modification of the Database – Deletion Delete all account records at the Perryridge branch.
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.
SQL Overview Defining a Schema CPSC 315 – Programming Studio Spring 2008 Project 1, Lecture 3 Slides adapted from those used by Jeffrey Ullman, via Jennifer.
Database Management System LICT 3011 Eyad H. Elshami.
Chapter 3: SQL Data Definition Language Data Definition Language Basic Structure of SQL Basic Structure of SQL Set Operations Set Operations Aggregate.
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,
SQL Overview Defining a Schema CPSC 315 – Programming Studio Slides adapted from those used by Jeffrey Ullman, via Jennifer Welch Via Yoonsuck Choe.
SQL Basics. SQL SQL (Structured Query Language) is a special-purpose programming language designed from managing data in relational database management.
Structured Query Language (SQL) A2 Teacher Up skilling LECTURE 2.
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,
Carnegie Mellon Carnegie Mellon Univ. Dept. of Computer Science Database Applications C. Faloutsos Introduction.
Chapter 8: SQL. Data Definition Modification of the Database Basic Query Structure Aggregate Functions.
SQL Fundamentals  SQL: Structured Query Language is a simple and powerful language used to create, access, and manipulate data and structure in the database.
SQL Basics. What is SQL? SQL stands for Structured Query Language. SQL lets you access and manipulate databases.
Prince Sultan University Dept. of Computer & Information Sciences CS 340 Introduction to Database Systems.
DBSQL 5-1 Copyright © Genetic Computer School 2009 Chapter 5 Structured Query Language.
Chapter 4: SQL Complex Queries Complex Queries Views Views Modification of the Database Modification of the Database Joined Relations Joined Relations.
Carnegie Mellon Carnegie Mellon Univ. Dept. of Computer Science Database Applications C. Faloutsos Relational model.
CS 338The Relational Model2-1 The Relational Model Lecture Topics Overview of SQL Underlying relational model Relational database structure SQL DDL and.
Carnegie Mellon Carnegie Mellon Univ. Dept. of Computer Science Database Applications C. Faloutsos Relational domain calculus.
CMU SCS Carnegie Mellon Univ. Dept. of Computer Science /615 – DB Applications C. Faloutsos & A. Pavlo Lecture#5: Relational calculus.
Different Constraint Types Type Where Declared When activated Guaranteed to hold? Attribute with attribute on insertion not if CHECK or update subquery.
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.
1 CS 430 Database Theory Winter 2005 Lecture 10: Introduction to SQL.
DBMS 3. course. Reminder Data independence: logical and physical Concurrent processing – Transaction – Deadlock – Rollback – Logging ER Diagrams.
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 Database design and normalization.
Carnegie Mellon Carnegie Mellon Univ. Dept. of Computer Science Database Applications C. Faloutsos Relational tuple calculus.
الفصل السادس لغة Structured Query Language) SQL الفصل السادس لغة Structured Query Language) SQL.
CENG 351 File Structures and Data Management1 Relational Model Chapter 3.
Big Data Yuan Xue CS 292 Special topics on.
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
SQL OVERVIEW DEFINING A SCHEMA
The Relational Model Textbook /7/2018.
مقدمة في قواعد البيانات
Database systems Lecture 3 – SQL + CRUD
CMPT 354: Database System I
SQL-1 Week 8-9.
Session - 6 Sequence - 1 SQL: The Structured Query Language:
SQL (Structured Query Language)
Presentation transcript:

Carnegie Mellon Carnegie Mellon Univ. Dept. of Computer Science Database Applications C. Faloutsos Rel. model - SQL part3

Carnegie Mellon C. Faloutsos2 General Overview - rel. model Formal query languages –rel algebra and calculi Commercial query languages –SQL –QBE, (QUEL)

Carnegie Mellon C. Faloutsos3 Overview - detailed - SQL DML –select, from, where, renaming, ordering, – aggregate functions, nested subqueries –insertion, deletion, update other parts: DDL, embedded SQL, auth etc

Carnegie Mellon C. Faloutsos4 Reminder: our Mini-U db

Carnegie Mellon C. Faloutsos5 DML - insertions etc insert into student values (“123”, “smith”, “main”) insert into student(ssn, name, address) values (“123”, “smith”, “main”)

Carnegie Mellon C. Faloutsos6 DML - insertions etc bulk insertion: how to insert, say, a table of ‘foreign-student’s, in bulk?

Carnegie Mellon C. Faloutsos7 DML - insertions etc bulk insertion: insert into student select ssn, name, address from foreign-student

Carnegie Mellon C. Faloutsos8 DML - deletion etc delete the record of ‘smith’

Carnegie Mellon C. Faloutsos9 DML - deletion etc delete the record of ‘smith’: delete from student where name=‘smith’ (careful - it deletes ALL the ‘smith’s!)

Carnegie Mellon C. Faloutsos10 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)

Carnegie Mellon C. Faloutsos11 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

Carnegie Mellon C. Faloutsos12 DML - joins so far: ‘INNER’ joins, eg: select ssn, c-name from takes, class where takes.c-id = class.c-id

Carnegie Mellon C. Faloutsos13 Reminder: our Mini-U db

Carnegie Mellon C. Faloutsos14 inner join o.s.: gone!

Carnegie Mellon C. Faloutsos15 outer join

Carnegie Mellon C. Faloutsos16 outer join select ssn, c-name from takes outer join class on takes.c- id=class.c-id

Carnegie Mellon C. Faloutsos17 outer join left outer join right outer join full outer join natural join

Carnegie Mellon C. Faloutsos18 Overview - detailed - SQL DML –select, from, where, renaming, ordering, – aggregate functions, nested subqueries –insertion, deletion, update other parts: DDL, embedded SQL, auth etc

Carnegie Mellon C. Faloutsos19 Data Definition Language create table student (ssn char(9) not null, name char(30), address char(50), primary key (ssn) )

Carnegie Mellon C. Faloutsos20 Data Definition Language create table r( A1 D1, …, An Dn, integrity-constraint1, … integrity-constraint-n)

Carnegie Mellon C. Faloutsos21 Data Definition Language Domains: char(n), varchar(n) int, numeric(p,d), real, double precision float, smallint date, time

Carnegie Mellon C. Faloutsos22 Data Definition Language integrity constraints: primary key foreign key check(P)

Carnegie Mellon C. Faloutsos23 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”))

Carnegie Mellon C. Faloutsos24 Data Definition Language delete a table: difference between drop table student delete from student

Carnegie Mellon C. Faloutsos25 Data Definition Language modify a table: alter table student drop address alter table student add major char(10)

Carnegie Mellon C. Faloutsos26 Overview - detailed - SQL DML –select, from, where, renaming, ordering, – aggregate functions, nested subqueries –insertion, deletion, update other parts: DDL, embedded SQL, auth etc

Carnegie Mellon C. Faloutsos27 Embedded SQL from within a ‘host’ language (eg., ‘C’, ‘VB’) EXEC SQL END-EXEC Q: why do we need embedded SQL??

Carnegie Mellon C. Faloutsos28 Embedded SQL SQL returns sets; host language expects a tuple - impedance mismatch! solution: ‘cursor’, ie., a ‘pointer’ over the set of tuples. example:

Carnegie Mellon C. Faloutsos29 Embedded SQL main(){ … EXEC SQL declare c cursor for select * from student END-EXEC …

Carnegie Mellon C. Faloutsos30 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); }

Carnegie Mellon C. Faloutsos31 Embedded SQL - ctn’d … EXEC SQL close c END-EXEC … } /* end main() */

Carnegie Mellon C. Faloutsos32 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() */

Carnegie Mellon C. Faloutsos33 Overview - detailed - SQL DML –select, from, where, renaming, ordering, – aggregate functions, nested subqueries –insertion, deletion, update other parts: DDL, embedded SQL, auth etc

Carnegie Mellon C. Faloutsos34 SQL - misc Later, we’ll see authorization: grant select on student to transactions other features (triggers, assertions etc) see, e.g.:

Carnegie Mellon C. Faloutsos35 General Overview - rel. model Formal query languages –rel algebra and calculi Commercial query languages –SQL –QBE, (QUEL)

Carnegie Mellon C. Faloutsos36 Rel. model - QBE Inspired by the R.D.C. “P.” -> print (ie., ‘select’ of SQL) _x, _y: domain variables (ie., attribute names) Example: find names of students taking

Carnegie Mellon C. Faloutsos37 Rel. model - QBE

Carnegie Mellon C. Faloutsos38 Rel. model - QBE

Carnegie Mellon C. Faloutsos39 Rel. model - QBE names of students taking

Carnegie Mellon C. Faloutsos40 Rel. model - QBE condition box self-joins (Tom’s grandparents) ordering (AO., DO.) aggregation (SUM.ALL., COUNT.UNIQUE., …) group-by (G.)

Carnegie Mellon C. Faloutsos41 Rel. model - QBE aggregate: avg grade overall:

Carnegie Mellon C. Faloutsos42 Rel. model - QBE aggregate: avg grade per student:

Carnegie Mellon C. Faloutsos43 General Overview - rel. model Formal query languages –rel algebra and calculi Commercial query languages –SQL –QBE, (QUEL)

Carnegie Mellon C. Faloutsos44 Rel. model - QUEL Used in INGRES only - of historical interest. Eg.: find all ssn’s in mini-U: range of s is student; retrieve (s.ssn);

Carnegie Mellon C. Faloutsos45 Rel. model - QUEL general syntax: range of …. is t-name retrieve (attribute list) where condition SQL select attr. list from t-name where condition

Carnegie Mellon C. Faloutsos46 Rel. model - QUEL very similar to SQL also supports aggregates, ordering etc

Carnegie Mellon C. Faloutsos47 General Overview Formal query languages –rel algebra and calculi Commercial query languages –SQL –QBE, (QUEL) Integrity constraints Functional Dependencies Normalization - ‘good’ DB design