COMP 3715 Spring 05. Working with data in a DBMS Any database system must allow user to  Define data Relations Attributes Constraints  Manipulate data.

Slides:



Advertisements
Similar presentations
Relational Algebra Relational algebra consists of a set of relational operators Each operator has one or more relations as input and creates a new relation.
Advertisements

SQL Introduction Standard language for querying and manipulating data Structured Query Language Many standards out there: SQL92, SQL2, SQL3. Vendors support.
TURKISH STATISTICAL INSTITUTE 1 /34 SQL FUNDEMANTALS (Muscat, Oman)
Basic SQL Introduction Presented by: Madhuri Bhogadi.
Relational Database. Relational database: a set of relations Relation: made up of 2 parts: − Schema : specifies the name of relations, plus name and type.
1 Query-by-Example (QBE). 2 v A “GUI” for expressing queries. –Based on the Domain Relational Calulus (DRC)! –Actually invented before GUIs. –Very convenient.
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.
CS411 Database Systems Kazuhiro Minami 06: SQL. Join Expressions.
Midterm Review Lecture 14b. 14 Lectures So Far 1.Introduction 2.The Relational Model 3.Disks and Files 4.Relational Algebra 5.File Org, Indexes 6.Relational.
Introduction to Structured Query Language (SQL)
1 Relational Model. 2 Relational Database: Definitions  Relational database: a set of relations  Relation: made up of 2 parts: – Instance : a table,
Fundamentals, Design, and Implementation, 9/e Chapter 6 Introduction to Structured Query Language (SQL)
--The SQL Query Language DML--1 LIKE  LIKE allows to select character strings which have some element in common by using wild cards:  Wild cards:  “%”
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.
SQL SQL stands for Structured Query Language SQL allows you to access a database SQL is an ANSI standard computer language SQL can execute queries against.
5 Chapter 5 Structured Query Language (SQL1) Revision.
Structured Query Language Chapter Three (Excerpts) DAVID M. KROENKE’S DATABASE CONCEPTS, 2 nd Edition.
Structured Query Language Chapter Three DAVID M. KROENKE’S DATABASE CONCEPTS, 2 nd Edition.
Chapter 4 Relational Databases Copyright © 2012 Pearson Education, Inc. publishing as Prentice Hall 4-1.
SQL Tutorial Introduction to Database. Learning Objectives  Read and write Data Definition grammar of SQL  Read and write data modification statements.
MySQL Tutorial Introduction to Database. Learning Objectives  Read and write Data Definition grammar of SQL  Read and write data modification statements.
Chapter 4 Relational Databases Copyright © 2012 Pearson Education 4-1.
Exercises Product ( pname, price, category, maker) Purchase (buyer, seller, store, product) Company (cname, stock price, country) Person( per-name, phone.
DATABASES AND SQL. Introduction Relation: Relation means table(data is arranged in rows and columns) Domain : A domain is a pool of values appearing in.
Chapter 3: SQL Data Definition Language Data Definition Language Basic Structure of SQL Basic Structure of SQL Set Operations Set Operations Aggregate.
INFORMATION TECHNOLOGY IN BUSINESS AND SOCIETY SESSION 16 – SQL SEAN J. TAYLOR.
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.
Database Lecture # 1 By Ubaid Ullah.
Relational DBs and SQL Designing Your Web Database (Ch. 8) → Creating and Working with a MySQL Database (Ch. 9, 10) 1.
Fundamentals, Design, and Implementation, 9/e CPE 481 Database Processing Chapter 6 Structured Query Language (SQL) Instructor:Suthep Madarasmi, Ph.D.
Chapter 5: Part 1: DDL STRUCTURED QUERY LANGUAGE (SQL)
Structured Query Language. Group Functions What are group functions ? Group Functions Group functions operate on sets of rows to give one result per group.
Nitin Singh/AAO RTI ALLAHABAD 1 SQL Nitin Singh/AAO RTI ALLAHABAD 2 OBJECTIVES §What is SQL? §Types of SQL commands and their function §Query §Index.
Relational Database. Database Management System (DBMS)
6 1 Lecture 8: Introduction to Structured Query Language (SQL) J. S. Chou, P.E., Ph.D.
BIS Database Systems School of Management, Business Information Systems, Assumption University A.Thanop Somprasong Chapter # 7 Introduction to Structured.
Chapter 8: SQL. Data Definition Modification of the Database Basic Query Structure Aggregate Functions.
Database Management COP4540, SCS, FIU Structured Query Language (Chapter 8)
Database Systems: Design, Implementation, and Management Eighth Edition Chapter 7 Introduction to Structured Query Language (SQL)
Advanced Database CS-426 Week 1 - Introduction. Database Management System DBMS contains information about a particular enterprise Collection of interrelated.
IS 230Lecture 6Slide 1 Lecture 7 Advanced SQL Introduction to Database Systems IS 230 This is the instructor’s notes and student has to read the textbook.
Carnegie Mellon Carnegie Mellon Univ. Dept. of Computer Science Database Applications C. Faloutsos Rel. model - SQL part3.
7 1 Constraints & Triggers Chapter Constraints and triggers? Constraints: Certain properties that the DBMS is required to enforce –E.g. primary.
DBSQL 5-1 Copyright © Genetic Computer School 2009 Chapter 5 Structured Query Language.
Database Fundamental & Design by A.Surasit Samaisut Copyrights : All Rights Reserved.
Visual Programing SQL Overview Section 1.
SQL Jan 20,2014. DBMS Stores data as records, tables etc. Accepts data and stores that data for later use Uses query languages for searching, sorting,
AL-MAAREFA COLLEGE FOR SCIENCE AND TECHNOLOGY INFO 232: DATABASE SYSTEMS CHAPTER 7 (Part II) INTRODUCTION TO STRUCTURED QUERY LANGUAGE (SQL) Instructor.
Mining real world data RDBMS and SQL. Index RDBMS introduction SQL (Structured Query language)
ITS232 Introduction To Database Management Systems Siti Nurbaya Ismail Faculty of Computer Science & Mathematics, Universiti Teknologi MARA (UiTM), Kedah.
1 MySQL and SQL. 2 Topics  Introducing Relational Databases  Terminology  Managing Databases MySQL and SQL.
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)
April 2002 Information Systems Design John Ogden & John Wordsworth 1 Database Design SQL (1) John Wordsworth Department of Computer Science The University.
Aggregation SELECT Sum(price) FROM Product WHERE manufacturer=“Toyota” SQL supports several aggregation operations: SUM, MIN, MAX, AVG, COUNT Except COUNT,
7 1 Database Systems: Design, Implementation, & Management, 7 th Edition, Rob & Coronel 7.6 Advanced Select Queries SQL provides useful functions that.
SQL: Structured Query Language It enables to create and operate on relational databases, which are sets of related information stored in tables. It is.
Database Design lecture 3_2 Slide 1 Database Design Lecture 3_2 Data Manipulation in SQL Simple SQL queries References: Text Chapter 8 Oracle SQL Manual.
Concepts of Database Management, Fifth Edition Chapter 3: The Relational Model 2: SQL.
Big Data Yuan Xue CS 292 Special topics on.
SQL SQL Ayshah I. Almugahwi Maryam J. Alkhalifa
More SQL: Complex Queries,
Chapter 3 Introduction to SQL(3)
SQL: Advanced Options, Updates and Views Lecturer: Dr Pavle Mogin
CS 440 Database Management Systems
Chapter # 7 Introduction to Structured Query Language (SQL) Part II.
Lecture 12: SQL Friday, October 20, 2000.
Query Functions.
Lecture 14: SQL Wednesday, October 31, 2001.
Presentation transcript:

COMP 3715 Spring 05

Working with data in a DBMS Any database system must allow user to  Define data Relations Attributes Constraints  Manipulate data Insert Delete Retrieval

Working with data in DBMS Thus a DBMS must provide  Data Definition Language (DDL) Define tables structures Define attributes and domains Define constraints  Data Manipulation Language (DML) To insert, delete and update data To query and retrieve data

Structured Query Language -- SQL The “standard” query language for DBMS Contains both DDL & DML Supported by every DBMS on the market Some system may have GUI  But SQL is still the standard way for computer programs interact with the DBMS

Data Definition – Create table Need to specify  Table name  Attribute names  Attribute domains  Primary keys  Constraints

Data Definition – Create table CREATE TABLE Customer (ssn CHAR(9); name CHAR(30); address CHAR(50); age INTEGER; income INTEGER; PRIMARY KEY (ssn))

Data Definition – Create table CREATE TABLE Book (title CHAR(30); isbn CHAR(20); author CHAR(30); quantity INTEGER; price REAL; PRIMARY KEY (isbn))

Data Definition – Create table CREATE TABLE Buy (isbn CHAR(20); customer CHAR(9); quantity INTEGER NOT NULL; date DATE; PRIMARY KEY (isbn, customer, date); FOREIGN KEY (isbn) REFERENCES Books FOREIGN KEY (customer) REFERENCES Customer(ssn))

Data Manipulation -- Query The standard query for SQL SELECT FROM WHERE ORDER BY Other parameters (to be discuss in senior class…)

Data Manipulation – Query: Selection (single table) “Find the ssn of the customer name John Doe” SELECT ssn FROM Customer WHERE Name = “John Doe”

Data Manipulation – Query: Selection (single table) “Find the quantity of books in the bookstore for “Tax break 2005” written by “Mike Rich” SELECT quantity FROM Book WHERE title = “Tax break 2005” AND author = “Mike Rich”

Data Manipulation – Query: Selection (single table) “Find the isbn of all books that is sold on 2/14/2005 and also return how many of each book is sold” SELECT isbn, quantity FROM Buy WHERE date = “2/14/2005”

Data Manipulation – Query: Selection (single table) “Find the ssn of the customers who bought the book with isbn = ‘A ’” SELECT ssn FROM Buy WHERE isbn = “A ”

Data Manipulation – Query: Join (Multiple tables) “Find the name of the customers who bought the book with isbn = ‘A ’” SELECT name FROM Buy, Customer WHERE Buy.isbn = “A ” and Buy.customer = Customer.ssn

Data Manipulation – Query: Join (Multiple tables) Join  Combining information of multiple tables  Consider the Cartesian Product of the tuples from the table  i.e. consider ALL pairs of tuples from the two tables  For each possible pair, the condition is checked

Data Manipulation – Query: Join (Multiple tables) “Find the name of the customers who bought the book with isbn = ‘A ’”, also return the name of the book SELECT Customer.name, Book.title FROM Buy, Customer, Book WHERE Buy.isbn = “A ” and Buy.customer = Customer.ssn and Book.isbn = Buy.isbn

Data Manipulation – Query: negative information “Find the customer who buy a book on a date other then “2/14/2005” SELECT customer FROM Buy WHERE Date ≠ “2/14/2005”

Data Manipulation – Query: negative information “Find the customer who does not buy a book on “2/14/2005” SELECT customer FROM Buy WHERE Date ≠ “2/14/2005” Does NOT work! Why?  Each tuple is examined separately  Whenever the condition is satisfied, the tuple is returned

Data Manipulation – Query: Aggregate queries Functions available for counting, averaging etc. AVG(), COUNT(), SUM (), MIN (), MAX() Example, find the most expensive book SELECT MAX(price) FROM Book

Data Manipulation – Query: Aggregate queries Example, find the most expensive book written by “J. K. Roaming” SELECT MAX(price) FROM Book WHERE author = “J. K. Roaming”

Data Manipulation – Query: Aggregate queries Example, find the most expensive book written by “J. K. Roaming” and return its title SELECT title, MAX(price) FROM Book WHERE author = “J. K. Roaming” This does NOT work. Convention in SQL, you cannot max aggregate and non-aggregate values (except using Group by)

Data Manipulation – Query: Aggregate queries Example, find the number of authors that has book in the bookstore SELECT COUNT(author) FROM Book This does NOT work. It will count duplicates as separate

Data Manipulation – Query: Aggregate queries Example, find the number of authors that has book in the bookstore SELECT COUNT(DISTINCT author) FROM Book

Data Manipulation – Query: Group By For reporting, we may want to separate data into groups For example, I may want to list the number of customers based on age Using “Group By” enable it

Data Manipulation – Query: Group By List the number of customer for each age SELECT age, COUNT(*) FROM Customer GROUP BY age

Data Manipulation – Query: Group By List the number of rich customers (with income > 100,000) for each age SELECT age, COUNT(*) FROM Customer WHERE income > 100,000 GROUP BY age

Data Manipulation – Query: Group By List the number of rich customers (with income > 100,000) for each age, also list the maximum salary for each group SELECT age, COUNT(*), MAX(salary) FROM Customer WHERE income > 100,000 GROUP BY age

Data Manipulation – Insertion, Deletion, Inserting a tuple INSERT INTO Books VALUES (“How to cut taxes”, “A ”, “R. Rich”, 3, 19.95) Deleting a tuple DELETE FROM Books WHERE author = “R. Rich” Delete all books from “R. Rich”

Data Manipulation – Update Update tuple Update Books SET price = price * 1.1 WHERE author = “R. Rich” Increase the price of all books from R. Rich for 10%