Chapter 7: SQL, the Structured Query Language Soid Quintero & Ervi Bongso CS157B.

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

Greg Riccardi Florida State University. Using SQL to Manipulate Database Content and Structure How to create queries in SQL –Simple select statements.
Database Systems: Design, Implementation, and Management Tenth Edition
Copyright © by Royal Institute of Information Technology Introduction To Structured Query Language (SQL) 1.
Introduction to Structured Query Language (SQL)
Fundamentals, Design, and Implementation, 9/e COS 346 Day 11.
Introduction to Structured Query Language (SQL)
Structured query language This is a presentation by JOSEPH ESTRada on the beauty of Structured Query Language.
Fundamentals, Design, and Implementation, 9/e Chapter 6 Introduction to Structured Query Language (SQL)
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 Chapter Three (Excerpts) DAVID M. KROENKE’S DATABASE CONCEPTS, 2 nd Edition.
Introduction to Structured Query Language (SQL)
Structured Query Language Chapter Three DAVID M. KROENKE’S DATABASE CONCEPTS, 2 nd Edition.
Database Systems: Design, Implementation, and Management Eighth Edition Chapter 7 Introduction to Structured Query Language (SQL)
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.
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.
SQL Basics. SQL SQL (Structured Query Language) is a special-purpose programming language designed from managing data in relational database management.
Introduction to SQL J.-S. Chou Assistant Professor.
Chapter 5 Introduction to SQL. Structured Query Language = the “programming language” for relational databases SQL is a nonprocedural language = the user.
Structured Query Language Chapter Three DAVID M. KROENKE and DAVID J. AUER DATABASE CONCEPTS, 4 th Edition.
 SQL stands for Structured Query Language.  SQL lets you access and manipulate databases.  SQL is an ANSI (American National Standards Institute) standard.
Practical Session 13 Structured Data Bases Structured Query Language Exam Questions Factory Method Pattern Abstract Factory Pattern.
Fundamentals, Design, and Implementation, 9/e CPE 481 Database Processing Chapter 6 Structured Query Language (SQL) Instructor:Suthep Madarasmi, Ph.D.
SQL: Data Manipulation Presented by Mary Choi For CS157B Dr. Sin Min Lee.
Structure Query Language SQL. Database Terminology Employee ID 3 3 Last name Small First name Tony 5 5 Smith James
Structured Query Language Chris Nelson CS 157B Spring 2008.
SQL SQL Server : Overview SQL : Overview Types of SQL Database : Creation Tables : Creation & Manipulation Data : Creation & Manipulation Data : Retrieving.
Using Special Operators (LIKE and IN)
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.
Database Processing: Fundamentals, Design, and Implementation, 9/e by David M. KroenkeChapter 6/1 Copyright © 2004 Please……. No Food Or Drink in the class.
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.
Topic 1: Introduction to SQL. SQL stands for Structured Query Language. SQL is a standard computer language for accessing and manipulating databases SQL.
Chapter 8: SQL. Data Definition Modification of the Database Basic Query Structure Aggregate Functions.
Database Systems Design, Implementation, and Management Coronel | Morris 11e ©2015 Cengage Learning. All Rights Reserved. May not be scanned, copied or.
SQL Basics. What is SQL? SQL stands for Structured Query Language. SQL lets you access and manipulate databases.
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)
DBSQL 5-1 Copyright © Genetic Computer School 2009 Chapter 5 Structured Query Language.
Database Fundamental & Design by A.Surasit Samaisut Copyrights : All Rights Reserved.
SQL – Structured Query Language
AL-MAAREFA COLLEGE FOR SCIENCE AND TECHNOLOGY INFO 232: DATABASE SYSTEMS CHAPTER 7 (Part II) INTRODUCTION TO STRUCTURED QUERY LANGUAGE (SQL) Instructor.
ITEC 3220A Using and Designing Database Systems Instructor: Prof. Z. Yang Course Website: 3220a.htm
An Introduction To SQL Part 2 (Special thanks to Geoff Leese)
1 Information Retrieval and Use (IRU) An Introduction To SQL Part 2.
© 2002 by Prentice Hall 1 Structured Query Language David M. Kroenke Database Concepts 1e Chapter 3 3.
Distribution of Marks For Second Semester Internal Sessional Evaluation External Evaluation Assignment /Project QuizzesClass Attendance Mid-Term Test Total.
April 2002 Information Systems Design John Ogden & John Wordsworth 1 Database Design SQL (1) John Wordsworth Department of Computer Science The University.
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.
LM 5 Introduction to SQL MISM 4135 Instructor: Dr. Lei Li.
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
SQL, the Structured Query Language
More SQL: Complex Queries, Triggers, Views, and Schema Modification
Chapter 5 Introduction to SQL.
SQL: Advanced Options, Updates and Views Lecturer: Dr Pavle Mogin
Chapter # 7 Introduction to Structured Query Language (SQL) Part II.
Chapter 4 Summary Query.
Copyright © 2012 Pearson Education, Inc. Publishing as Prentice Hall
Database systems Lecture 3 – SQL + CRUD
Access: SQL Participation Project
Chapter 7 Introduction to Structured Query Language (SQL)
Structured Query Language – The Fundamentals
Session - 6 Sequence - 1 SQL: The Structured Query Language:
Contents Preface I Introduction Lesson Objectives I-2
Session - 6 Sequence - 1 SQL: The Structured Query Language:
Structured Query Language Path from Unorganized to Organized….
Presentation transcript:

Chapter 7: SQL, the Structured Query Language Soid Quintero & Ervi Bongso CS157B

Overview Introduction DDL Commands DML Commands SQL Statements, Operators, Clauses Aggregate Functions

The ANSI standard language for the definition and manipulation of relational database. Includes data definition language (DDL), statements that specify and modify database schemas. Includes a data manipulation language (DML), statements that manipulate database content. Structured Query Language ( SQL ) ‏

Some Facts on SQL SQL data is case-sensitive, SQL commands are not. First Version was developed at IBM by Donald D. Chamberlin and Raymond F. Boyce. [SQL] Developed using Dr. E.F. Codd's paper, “A Relational Model of Data for Large Shared Data Banks.” SQL query includes references to tuples variables and the attributes of those variables

SQL: DDL Commands CREATE TABLE : used to create a table. ALTER TABLE : modifies a table after it was created. DROP TABLE : removes a table from a database.

SQL: CREATE TABLE Statement Things to consider before you create your table are: The type of data the table name what column(s) will make up the primary key the names of the columns CREATE TABLE statement syntax: CREATE TABLE ( field1 datatype ( NOT NULL ), field2 datatype ( NOT NULL ) );

SQL: Attributes Types Table 7.6 pg.164

SQL: ALTER TABLE Statement To add or drop columns on existing tables. ALTER TABLE statement syntax: ALTER TABLE ADD attr datatype; or DROP COLUMN attr;

SQL: DROP TABLE Statement Has two options: CASCADE : Specifies that any foreign key constraint violations that are caused by dropping the table will cause the corresponding rows of the related table to be deleted. RESTRICT : blocks the deletion of the table of any foreign key constraint violations would be created. DROP TABLE statement syntax: DROP TABLE [ RESTRICT | CASCADE ];

Example: CREATE TABLE FoodCart ( date varchar (10), food varchar (20), profit float ); ALTER TABLE FoodCart ( ADD sold int ); ALTER TABLE FoodCart( DROP COLUMN profit ); DROP TABLE FoodCart; profitfooddate soldprofitfooddate soldfooddate FoodCart

SQL: DML Commands INSERT : adds new rows to a table. UPDATE : modifies one or more attributes. DELETE : deletes one or more rows from a table.

SQL: INSERT Statement To insert a row into a table, it is necessary to have a value for each attribute, and order matters. INSERT statement syntax: INSERT into VALUES ('value1', 'value2', NULL); Example: INSERT into FoodCart VALUES (’02/26/08', ‘pizza', 70 ); FoodCart 70pizza02/26/08 500hotdog02/26/08 350pizza02/25/08 soldfooddate 500hotdog02/26/08 350pizza02/25/08 soldfooddate

SQL: UPDATE Statement To update the content of the table: UPDATE statement syntax: UPDATE SET = WHERE ; Example: UPDATE FoodCart SET sold = 349 WHERE date = ’02/25/08’ AND food = ‘pizza’; FoodCart 70pizza02/26/08 500hotdog02/26/08 350pizza02/25/08 soldfooddate 70pizza02/26/08 500hotdog02/26/08 349pizza02/25/08 soldfooddate

SQL: DELETE Statement To delete rows from the table: DELETE statement syntax: DELETE FROM WHERE ; Example: DELETE FROM FoodCart WHERE food = ‘hotdog’; FoodCart Note: If the WHERE clause is omitted all rows of data are deleted from the table. 70pizza02/26/08 500hotdog02/26/08 349pizza02/25/08 soldfooddate 70pizza02/26/08 349pizza02/25/08 soldfooddate

SQL Statements, Operations, Clauses SQL Statements: Select SQL Operations: Join Left Join Right Join Like SQL Clauses: Order By Group By Having

SQL: SELECT Statement A basic SELECT statement includes 3 clauses SELECT FROM WHERE SELECT Specifies the attributes that are part of the resulting relation FROM Specifies the tables that serve as the input to the statement WHERE Specifies the selection condition, including the join condition. Note: that you don't need to use WHERE

Using a “*” in a select statement indicates that every attribute of the input table is to be selected. Example: SELECT * FROM … WHERE …; To get unique rows, type the keyword DISTINCT after SELECT. Example: SELECT DISTINCT * FROM … WHERE …; SQL: SELECT Statement (cont.) ‏

Example: Person 8034Peter 54 Helena 7029George 6428Sally 8034Harry WeightAgeName 8034Peter 54 Helena 8034Harry WeightAgeName Weight 1) SELECT * FROM person WHERE age > 30; 2) SELECT weight FROM person WHERE age > 30; 3) SELECT distinct weight FROM person WHERE age > 30; Weight

SQL: Join operation A join can be specified in the FROM clause which list the two input relations and the WHERE clause which lists the join condition. Example: Biotech1003 Sales1002 IT1001 DivisionID TN1002 MA1001 CA1000 StateID EmpDept

SQL: Join operation (cont.) ‏ Sales1002 IT1001 Dept.DivisionDept.ID TN1002 MA1001 Emp.StateEmp.ID inner join = join SELECT * FROM emp join dept (or FROM emp, dept) ‏ on emp.id = dept.id;

SQL: Join operation (cont.) ‏ IT1001 Sales1002 null Dept.DivisionDept.ID CA1000 TN1002 MA1001 Emp.StateEmp.ID left outer join = left join SELECT * FROM emp left join dept on emp.id = dept.id;

SQL: Join operation (cont.) ‏ Sales1002 Biotech1003 IT1001 Dept.DivisionDept.ID MA1001 null TN1002 Emp.StateEmp.ID right outer join = right join SELECT * FROM emp right join dept on emp.id = dept.id;

SQL: Like operation Pattern matching selection % (arbitrary string) ‏ SELECT * FROM emp WHERE ID like ‘%01’;  finds ID that ends with 01, e.g. 1001, 2001, etc _ (a single character) ‏ SELECT * FROM emp WHERE ID like ‘_01_’;  finds ID that has the second and third character as 01, e.g. 1010, 1011, 1012, 1013, etc

SQL: The ORDER BY Clause Ordered result selection desc (descending order) ‏ SELECT * FROM emp order by state desc  puts state in descending order, e.g. TN, MA, CA asc (ascending order) ‏ SELECT * FROM emp order by id asc  puts ID in ascending order, e.g. 1001, 1002, 1003

SQL: The GROUP BY Clause The function to divide the tuples into groups and returns an aggregate for each group. Usually, it is an aggregate function’s companion SELECT food, sum(sold) as totalSold FROM FoodCart group by food; FoodCart 419pizza 500hotdog totalSoldfood 70pizza02/26/08 500hotdog02/26/08 349pizza02/25/08 soldfooddate

SQL: The HAVING Clause The substitute of WHERE for aggregate functions Usually, it is an aggregate function’s companion SELECT food, sum(sold) as totalSold FROM FoodCart group by food having sum(sold) > 450; FoodCart 500hotdog totalSoldfood 70pizza02/26/08 500hotdog02/26/08 349pizza02/25/08 soldfooddate

SQL: Aggregate Functions Are used to provide summarization information for SQL statements, which return a single value. COUNT (attr) ‏ SUM (attr) ‏ MAX (attr) ‏ MIN (attr) ‏ AVG (attr) ‏ Note: when using aggregate functions, NULL values are not considered, except in COUNT (*).

SQL: Aggregate Functions (cont.) ‏ COUNT (attr) -> return # of rows that are not null Ex: COUNT(distinct food ) from FoodCart; -> 2 SUM (attr) -> return the sum of values in the attr Ex: SUM( sold ) from FoodCart; -> 919 MAX (attr) -> return the highest value from the attr Ex: MAX( sold ) from FoodCart; -> pizza02/26/08 500hotdog02/26/08 349pizza02/25/08 soldfooddate FoodCart

SQL: Aggregate Functions (cont.) ‏ MIN (attr) -> return the lowest value from the attr Ex: MIN( sold ) from FoodCart; -> 70 AVG (attr) -> return the average value from the attr Ex: AVG( sold ) from FoodCart; -> Note: value is rounded to the precision of the datatype 70pizza02/26/08 500hotdog02/26/08 349pizza02/25/08 soldfooddate FoodCart

Riccardi, Greg. Principles of Database Systems with Internet and Java Applications. Addison Wesley, Ronald R. Plew, Ryan K. Stephens. Teach Yourself SQL in 24 Hours 3rd Edition. Sams Publishing, SQL W3C Wikipedia - SQL Wikipedia - join ‏ ‏ References