Database Management COP4540, SCS, FIU Structured Query Language (Chapter 8)

Slides:



Advertisements
Similar presentations
TURKISH STATISTICAL INSTITUTE 1 /34 SQL FUNDEMANTALS (Muscat, Oman)
Advertisements

Virtual training week 4 structured query language (SQL)
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)
© 2002 by Prentice Hall 1 David M. Kroenke Database Processing Eighth Edition Chapter 9 Structured Query Language.
1 Query Languages: How to build or interrogate a relational database Structured Query Language (SQL)
Fundamentals, Design, and Implementation, 9/e Chapter 6 Introduction to Structured Query Language (SQL)
Structured Query Language Chapter Three (Excerpts) DAVID M. KROENKE’S DATABASE CONCEPTS, 2 nd Edition.
Database Systems More SQL Database Design -- More SQL1.
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 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.
A Guide to SQL, Seventh Edition. Objectives Retrieve data from a database using SQL commands Use compound conditions Use computed columns Use the SQL.
Microsoft Access 2010 Chapter 7 Using SQL.
Chapter 7: SQL, the Structured Query Language Soid Quintero & Ervi Bongso CS157B.
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.
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.
©Silberschatz, Korth and Sudarshan4.1Database System Concepts Chapter 4: SQL Basic Structure Set Operations Aggregate Functions Null Values Nested Subqueries.
Chapter 3 Introduction to SQL Yonsei University 1 st Semester, 2015 Sanghyun Park.
Relational DBs and SQL Designing Your Web Database (Ch. 8) → Creating and Working with a MySQL Database (Ch. 9, 10) 1.
1 IT420: Database Management and Organization SQL - Data Manipulation Language 27 January 2006 Adina Crăiniceanu
Chapter 3 Single-Table Queries
Structured Query Language Chapter Three DAVID M. KROENKE and DAVID J. AUER DATABASE CONCEPTS, 4 th Edition.
AL-MAAREFA COLLEGE FOR SCIENCE AND TECHNOLOGY INFO 232: DATABASE SYSTEMS CHAPTER 7 INTRODUCTION TO STRUCTURED QUERY LANGUAGE (SQL) Instructor Ms. Arwa.
 SQL stands for Structured Query Language.  SQL lets you access and manipulate databases.  SQL is an ANSI (American National Standards Institute) standard.
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.
1 Single Table Queries. 2 Objectives  SELECT, WHERE  AND / OR / NOT conditions  Computed columns  LIKE, IN, BETWEEN operators  ORDER BY, GROUP BY,
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)
7 1 Chapter 7 Introduction to Structured Query Language (SQL) Database Systems: Design, Implementation, and Management, Seventh Edition, Rob and Coronel.
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.
1 DBS201: Introduction to Structure Query Language (SQL) Lecture 1.
Database Systems: Design, Implementation, and Management Eighth Edition Chapter 7 Introduction to Structured Query Language (SQL)
SQL “Structured Query Language; standard language for relational data manipulation” DB2, SQL/DS, Oracle, INGRES, SYBASE, SQL Server, dBase/Win, Paradox,
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.
1/18/00CSE 711 data mining1 What is SQL? Query language for structural databases (esp. RDB) Structured Query Language Originated from Sequel 2 by Chamberlin.
ITEC 3220A Using and Designing Database Systems Instructor: Prof. Z. Yang Course Website: 3220a.htm
ITS232 Introduction To Database Management Systems Siti Nurbaya Ismail Faculty of Computer Science & Mathematics, Universiti Teknologi MARA (UiTM), Kedah.
© 2002 by Prentice Hall 1 Structured Query Language David M. Kroenke Database Concepts 1e Chapter 3 3.
A Guide to SQL, Eighth Edition Chapter Four Single-Table Queries.
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.
SQL, the Structured Query Language
SQL Query Getting to the data ……..
Structured Query Language
Chapter 5 Introduction to SQL.
CH 9 SQL 9.1 Querying A Single Table 9.2 Querying Multiple Tables
Server-Side Application and Data Management IT IS 3105 (FALL 2009)
Chapter # 7 Introduction to Structured Query Language (SQL) Part II.
Chapter 7 Introduction to Structured Query Language (SQL)
Contents Preface I Introduction Lesson Objectives I-2
CSC 453 Database Systems Lecture
Presentation transcript:

Database Management COP4540, SCS, FIU Structured Query Language (Chapter 8)

Database Management COP4540, SCS, FIU SQL Introduction Most important relational data manipulation language endorsed by American National Standard Institute (ANSI) SEQUEL developed by IBM in mid 70’s, renamed SQL in 80’s Input: one or more relations Output: a table Syntax may vary slightly among different DBMS. (The examples appearing in lecture notes comply with ANSI SQL2/SQL92)

Database Management COP4540, SCS, FIU Basic Components A standard relational database language –Data definition language (DDL) Define relation schemes, delete relations, create indices, and Modify schemes. –Data manipulation language (DML) Insert, delete, and modify tuples. –Query language (QL) select

Database Management COP4540, SCS, FIU SQL as DDL

Database Management COP4540, SCS, FIU Example of DDL Commands (1) Defining a database CREATE DATABASE UNIVERSITY Creating tables: CREATE TABLE CLASS ( NameVARCHAR(40)NOT NULL, TimeDATENOT NULL, CONSTRAINT CLASS_PK PRIMARY KEY (Name) );

Database Management COP4540, SCS, FIU Example of DDL Commands (2) CREATE TABLE ENROLLMENT ( SNo.INTEGERNOT NULL, CNameVARCHAR(40)NOT NULL, PositionNoINTEGERNOT NULL, CONSTRAINT ENROLLMENT_PK PRIMARY KEY (SNo, Cname), CONSTRAINT ENROLLMENT_FK1 FOREIGN KEY (SNo) REFERENCES STUDENT(SID) ON DELETE CASCADE ON UPDATE CASCADE, CONSTRAINT ENROLLMENT_FK2 FOREIGN KEY (CName) REFERENCES CLASS(Name) ON DELETE CASCADE ON UPDATE CASCADE, );

Database Management COP4540, SCS, FIU Example of DDL Commands (3) Changing table definitions ALTER TABLE CLASS ADD (Room VARCHAR(5)); Removing tables: DROP TABLE CLASS; DROP TALBE CLASS CASCADE; DROP TABLE CLASS RESTRICT;

Database Management COP4540, SCS, FIU SQL as DML Relations Used for SQL Statements: JUNIOR(Snum, Name, Major) HONOR_STU(Number, Name, Interest) STUDENT(SID, Name, Major, Year, Age) CLASS(CID, Name, Time, Room) ENROLLMENT(SNo, CName, PositionNo) FACULTY(FID, Name, Dept, Salary)

Database Management COP4540, SCS, FIU Insert One Row Inserting one row INSERT INTO ENROLLMENT VALUES ( 400, ‘COP4540’, 50); INSERT INTO CLASS (CID, Name, Room) VALUES ( COP3338, ‘Programming III’, ‘ECS134’);

Database Management COP4540, SCS, FIU Inserting Rows in Groups INSERT INTO JUNIOR VALUES (SELECT SID, Name, Major FROM STUDENT WHERE Year = ‘JR’ );

Database Management COP4540, SCS, FIU Deleting Database Contents Deleting rows that meet a certain criterion DELETE FROM STUDENT WHERE SID = 100; DELETE FROM STUDENT WHERE Major = ‘Biochemistry’; Deleting all rows from a table DELETE FROM STUDENT;

Database Management COP4540, SCS, FIU Changing Database Contents UPDATE CLASS SET Room = ‘PC 213’ WHERE Name = ‘Database’; UPDATE FACULTY SET Salary = Salary * 1.1 WHERE Dept = ‘Computer Science’;

Database Management COP4540, SCS, FIU Querying with SQL General Syntax of SELECT Statement SELECT [ALL| DISTINCT] column_list FROMtable_list [WHERE conditional expression] [GROUP BY group_by_column_list] [HAVING condition expression] [ORDER BY order_by_column_list];

Database Management COP4540, SCS, FIU Simple Queries in SQL Basic structure –select clause lists attributes to be copied Corresponds to projection in relational algebra. –from clause lists relations to be used. –where clause Corresponds to selection in relational algebra. –Typical simple query has the form: SELECTA 1, A 2, …, A n FROM R WHERE Condi where each A i represents an attribute in R, R is a relation, and Condi is a condition.

Database Management COP4540, SCS, FIU Selecting Columns (1) 1. Name the relation to be projected 2. List the columns to be shown SELECT SID, Name, Major FROM STUDENT; SELECT Name, SID, Major (attribute FROM STUDENT; order changed)

Database Management COP4540, SCS, FIU Selecting Columns (2) Changing column headers SELECT SID AS Student_ID, Name FROM STUDENT; Duplicates not eliminated automatically. If duplicate rows must be removed, do: SELECT DISTINCT Major FROM STUDENT;

Database Management COP4540, SCS, FIU Using Expressions and Constants Expressions are mathematical manipulations of data in the table SELECT Product_Name, Unit_Price, On_Hand, Unit_Price * On_Hand AS Value FROM PRODUCT; SELECT name, price/1.6 as price_in_US, ‘US$’ as unit FROM Merchandise WHERE type = ‘laptop’;

Database Management COP4540, SCS, FIU Using Wildcards Star (*) as lists of All attributes SELECT SID, Name, Major, Year, Age FROM STUDENT WHERE Major = ‘MATH’; SELECT * FROM STUDENT WHERE Major = ‘MATH’;

Database Management COP4540, SCS, FIU The where clause The predicates can be more complicated, and can involve –Logical connectives: AND, OR and NOT. –Common comparison operators: =, <>,, = –String comparison operators: LIKE –Arithmetic expressions on constant or tuple values. –The BETWEEN operator for ranges of values. –The IN operator for set/bag.

Database Management COP4540, SCS, FIU Comparison of strings The strings are compared in lexicographic order. –Given two strings A = a 1 a 2 …a n and B = b 1 b 2 …b m, we say A < B if: a 1 < b 1, or a 1 = b 1 and a 2 < b 2, or a 1 = b 1 a 2 = b 2 and a 3 < b 3, and so on. If n < m and a 1 a 2 …a n = b 1 b 2 …b n. Pattern Match –Format: sting LIKE pattern –Two special characters in the pattern %any sequence of 0 or more characters in the string. _any one character in the string

Database Management COP4540, SCS, FIU LIKE SELECT* FROM STUDENT WHERE SID LIKE ‘___-__-5678’; SELECT* FROM STUDENT WHERE SID LIKE ‘%5678’;

Database Management COP4540, SCS, FIU Ranges SELECTName, Age FROM STUDENT WHERE Age >= 20 AND Age <= 31; SELECT Name, Age FROMSTUDENT WHERE Age BETWEEN 21 AND 31

Database Management COP4540, SCS, FIU IN and NOT IN Lists SELECTName FROMSTUDENT WHEREMajor IN [‘MATH’, ‘ACCOUNTING’]; IN is particular useful in SQL Statements that use sub-queries.

Database Management COP4540, SCS, FIU SQL Built-in (Aggregation) Functions COUNT: count number of rows in a table SUM: sum up numeric columns AVG: compute average MAX: obtain maximum value of a column MIN: obtain minimum value of a column

Database Management COP4540, SCS, FIU Using Built-in Functions SELECT COUNT(*) FROM STUDENT; Cannot be mixed with column names in SELECT Cannot be used in WHERE clause  SELECT Name, COUNT(*) FROM STUDENT; unless a GROUP BY clause is used.  SELECT Name, COUNT(*) FROM STUDENT GROUP BY Name;

Database Management COP4540, SCS, FIU Categorizing Results: GROUP BY Count the number of students in each major SELECTMajor, COUNT(*) FROMSTUDENT GROUP BYMajor; 1. Divides a table into subsets 2. An aggregate function can be used to provide summary information for that group.

Database Management COP4540, SCS, FIU Qualifying Results by Categories HAVING Find only department with more than 10 student SELECTMajor FROMSTUDENT GROUP BYMajor HAVINGCOUNT(*) > 10;

Database Management COP4540, SCS, FIU Ordering the output Order by clause –ORDER BY [ASC | DESC] The order is based on the value of attributes specified in the The order is by default ascending. SELECT Name, Major, Age FROM STUDENT ORDER BY Major ASC, Age DESC;