Introduction to SQL J.-S. Chou Assistant Professor.

Slides:



Advertisements
Similar presentations
Introduction to Structured Query Language (SQL)
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.
Structure Query Language (SQL) COMSATS INSTITUTE OF INFORMATION TECHNOLOGY, VEHARI.
Introduction to Structured Query Language (SQL)
5 Chapter 5 Structured Query Language (SQL2) Revision.
Introduction to Structured Query Language (SQL)
Structured Query Language (SQL)
1 Basic SQL Prof. Sin-Min Lee Department of Computer Science.
3 3 Chapter 3 Structured Query Language (SQL) Database Systems: Design, Implementation, and Management 4th Edition Peter Rob & Carlos Coronel.
Fundamentals, Design, and Implementation, 9/e Chapter 6 Introduction to Structured Query Language (SQL)
Structured Query Language Part I Chapter Three CIS 218.
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)
S511 Session 10, IU-SLIS 1 Structured Query Language.
ITEC 3220A Using and Designing Database Systems Instructor: Gordon Turpin Course Website: Office: CSEB3020.
Chapter 5 Introduction to SQL. Structured Query Language = the “programming language” for relational databases SQL is a nonprocedural language = the user.
Chapter 7 Introduction to Structured Query Language (SQL)
Relational DBs and SQL Designing Your Web Database (Ch. 8) → Creating and Working with a MySQL Database (Ch. 9, 10) 1.
Chapter 3 Single-Table Queries
Structured Query Language Chapter Three DAVID M. KROENKE and DAVID J. AUER DATABASE CONCEPTS, 4 th Edition.
PHP Programming with MySQL Slide 8-1 CHAPTER 8 Working with Databases and MySQL.
AL-MAAREFA COLLEGE FOR SCIENCE AND TECHNOLOGY INFO 232: DATABASE SYSTEMS CHAPTER 7 INTRODUCTION TO STRUCTURED QUERY LANGUAGE (SQL) Instructor Ms. Arwa.
Database Systems: Design, Implementation, and Management Tenth Edition Chapter 8 Advanced SQL.
6 1 Chapter 6 Introduction to Structured Query Language (SQL) Database Systems: Design, Implementation, and Management, Sixth Edition, Rob and Coronel.
1 Single Table Queries. 2 Objectives  SELECT, WHERE  AND / OR / NOT conditions  Computed columns  LIKE, IN, BETWEEN operators  ORDER BY, GROUP BY,
Structure Query Language SQL. Database Terminology Employee ID 3 3 Last name Small First name Tony 5 5 Smith James
SQL SQL Server : Overview SQL : Overview Types of SQL Database : Creation Tables : Creation & Manipulation Data : Creation & Manipulation Data : Retrieving.
Chapter 5 Structured Query Language (SQL) Database Systems: Design, Implementation, and Management Peter Rob & Carlos Coronel.
Using Special Operators (LIKE and IN)
Database Systems: Design, Implementation, and Management Tenth Edition
Advanced SQL Advanced SQL Complex Queries, Joining Tables.
7 1 Chapter 7 Introduction to Structured Query Language (SQL) Database Systems: Design, Implementation, and Management, Seventh Edition, Rob and Coronel.
7 1 Chapter 7 Introduction to Structured Query Language (SQL) Database Systems: Design, Implementation, and Management, Seventh Edition, Rob and Coronel.
Chapter 7 Introduction to Structured Query Language (SQL)
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.
3 3 Chapter 3 Structured Query Language (SQL) Database Systems: Design, Implementation, and Management 4th Edition Peter Rob & Carlos Coronel.
Concepts of Database Management Eighth Edition Chapter 3 The Relational Model 2: SQL.
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 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)
Chapter 6 Structured Query Language (SQL) Database Systems: Design, Implementation, and Management Peter Rob & Carlos Coronel.
7 1 Chapter 7 Introduction to Structured Query Language (SQL) Database Systems: Design, Implementation, and Management, Seventh Edition, Rob and Coronel.
1 Chapter 6 Structured Query Language (SQL) DATABASE MANAGEMENT SYSTEM.
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
Week 4 Lecture Part 2 of 3 Structured Query Language (SQL) Samuel ConnSamuel Conn, Faculty Suggestions for using the Lecture Slides.
© 2002 by Prentice Hall 1 Structured Query Language David M. Kroenke Database Concepts 1e Chapter 3 3.
7 1 Database Systems: Design, Implementation, & Management, 7 th Edition, Rob & Coronel 7.6 Advanced Select Queries SQL provides useful functions that.
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 Query Getting to the data ……..
Structured Query Language
Chapter 5 Introduction to SQL.
Advanced SQL Advanced SQL Week 8 Comparison Operators
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
Chapter # 7 Introduction to Structured Query Language (SQL) Part I.
Presentation transcript:

Introduction to SQL J.-S. Chou Assistant Professor

Introduction to SQL  SQL is relatively easy to learn  Basic command set has a vocabulary of less than 100 words  Nonprocedural language  American National Standards Institute (ANSI) prescribes a standard SQL  Several SQL dialects exist

SQL Data Definition Commands

Data Manipulation Commands

Creating the Database  Two tasks must be completed –create the database structure –create the tables that will hold the end-user data  First task –RDBMS creates the physical files that will hold the database –Tends to differ substantially from one RDBMS to another

The Database Schema  Authentication –Process through which the DBMS verifies that only registered users are able to access the database –Log on to the RDBMS using a user ID and a password created by the database administrator  Schema –Group of database objects—such as tables and indexes—that are related to each other

Creating a Table

Data Manipulation Commands  Adding table rows  Saving table changes  Listing table rows  Updating table rows  Restoring table contents  Deleting table rows  Inserting table rows with a select subquery

Common SQL Data Manipulation Commands

Listing Table Rows  SELECT –Used to list contents of table  Syntax –SELECT columnlist FROM tablename  Columnlist represents one or more attributes, separated by commas  Asterisk can be used as wildcard character to list all attributes

Updating Table Rows  UPDATE –Modify data in a table  Syntax –UPDATE tablename SET columnname = expression [, columname = expression] [WHERE conditionlist];  If more than one attribute is to be updated in the row, separate corrections with commas

Restoring Table Contents  ROLLBACK –Used restore the database to its previous condition –Only applicable if COMMIT command has not been used to permanently store the changes in the database  Syntax –ROLLBACK;  COMMIT and ROLLBACK only work with data manipulation commands that are used to add, modify, or delete table rows

Deleting Table Rows  DELETE –Deletes a table row  Syntax –DELETE FROM tablename [WHERE conditionlist ];  WHERE condition is optional  If WHERE condition is not specified, all rows from the specified table will be deleted

Inserting Table Rows with a Select Subquery  INSERT –Inserts multiple rows from another table (source) –Uses SELECT subquery – Query that is embedded (or nested) inside another query – Executed first  Syntax –INSERT INTO tablename SELECT columnlist FROM tablename

Selecting Rows with Conditional Restrictions  Select partial table contents by placing restrictions on rows to be included in output –Add conditional restrictions to the SELECT statement, using WHERE clause  Syntax –SELECT columnlist FROM tablelist [ WHERE conditionlist ] ;

Restoring Table Contents  ROLLBACK –Used restore the database to its previous condition –Only applicable if COMMIT command has not been used to permanently store the changes in the database  Syntax –ROLLBACK;  COMMIT and ROLLBACK only work with data manipulation commands that are used to add, modify, or delete table rows

Deleting Table Rows  DELETE –Deletes a table row  Syntax –DELETE FROM tablename [WHERE conditionlist ];  WHERE condition is optional  If WHERE condition is not specified, all rows from the specified table will be deleted

Inserting Table Rows with a Select Subquery  INSERT –Inserts multiple rows from another table (source) –Uses SELECT subquery – Query that is embedded (or nested) inside another query – Executed first  Syntax –INSERT INTO tablename SELECT columnlist FROM tablename

Selecting Rows with Conditional Restrictions  Select partial table contents by placing restrictions on rows to be included in output –Add conditional restrictions to the SELECT statement, using WHERE clause  Syntax –SELECT columnlist FROM tablelist [ WHERE conditionlist ] ;  Example: qryFig6-5

Logical Operators LOGICAL “OR” (qryFig 6-13) SELECT P_DESCRIPT, P_INDATE, P_PRICE, V_CODE FROM PRODUCT WHERE V_CODE=21344 OR V_CODE=24288;

Logical Operators (2) LOGICAL “AND” (qryFig 6-14) SELECT P_DESCRIPT, P_INDATE, P_PRICE, V_CODE FROM PRODUCT WHERE P_INDATE>#15-JAN-2004# AND PRODUCT.P_PRICE<50;

Special Operators  BETWEEN –Used to check whether attribute value is within a range  IS NULL –Used to check whether attribute value is null  LIKE –Used to check whether attribute value matches a given string pattern  IN –Used to check whether attribute value matches any value within a value list  EXISTS –Used to check if a subquery returns any rows

Advanced Data Definition Commands  All changes in the table structure are made by using the ALTER command –Followed by a keyword that produces specific change –Three options are available – ADD – MODIFY – DROP

Copying Parts of Tables  SQL permits copying contents of selected table columns so that the data need not be reentered manually into newly created table(s)  First create the PART table structure  Next add rows to new PART table using table rows from the existing table

Advanced Select Queries  SQL provides useful functions –Count –Find minimum and maximum values –Calculate averages  SQL allows the user to limit queries to only those entries having no duplicates or entries whose duplicates may be grouped

Ordering a Listing ORDER BY (qryFig 6-20) SELECT P_CODE, P_DESCRIPT, P_INDATE, P_PRICE FROM PRODUCT ORDER BY P_PRICE;

Ordering a Listing (2) ORDER BY (qryFig 6-21) SELECT EMP_LNAME, EMP_FNAME, EMP_INITIAL, EMP_AREACODE, EMP_PHONE FROM EMPLOYEE ORDER BY EMP_LNAME, EMP_FNAME, EMP_INITIAL;

A Query Based on Multiple Restrictions qryFIG 6-23: SELECT P_DESCRIPT, V_CODE, P_INDATE, P_PRICE FROM PRODUCT WHERE P_INDATE<#21-JAN-2004# AND P_PRICE<=50 ORDER BY V_CODE, P_PRICE DESC;

Listing Unique Values qryFIG 6-24: SELECT DISTINCT V_CODE FROM PRODUCT;

Some Basic SQL Aggregate Functions

COUNT Function qryFIG 6-25C: SELECT COUNT(*) FROM PRODUCT WHERE P_PRICE <= 10.00;

MAX and MIN Function qryFig6-26C: SELECT P_CODE, P_DESCRIPT, P_PRICE FROM PRODUCT WHERE P_PRICE = (SELECT MAX(P_PRICE) FROM PRODUCT);

SUM Function qryFig6-27A: SELECT SUM(CUS_BALANCE) AS TOTBALANCE FROM CUSTOMER;

AVG Function qryFig6-28B: SELECT P_DESCRIPT, P_ONHAND, P_PRICE, V_CODE FROM PRODUCT WHERE P_PRICE > (SELECT AVG(P_PRICE) FROM PRODUCT) ORDER BY P_PRICE DESC;

Joining Database Tables  Ability to combine (join) tables on common attributes is most important distinction between a relational database and other databases  Join is performed when data are retrieved from more than one table at a time  Join is generally composed of an equality comparison between the foreign key and the primary key of related tables

Creating Links Through Foreign Keys

Joining Tables qryFig6-33: SELECT PRODUCT.P_DESCRIPT, PRODUCT.P_PRICE, VENDOR.V_NAME, VENDOR.V_CONTACT, VENDOR.V_AREACODE, VENDOR.V_PHONE FROM PRODUCT, VENDOR WHERE PRODUCT.V_CODE = VENDOR.V_CODE;

An Ordered and Limited Listing After a JOIN qryFig6-34: SELECT P_DESCRIPT, P_PRICE, V_NAME, V_CONTACT, V_AREACODE, V_PHONE FROM PRODUCT, VENDOR WHERE PRODUCT.V_CODE = VENDOR.V_CODE AND P_INDATE > ‘15-JAN-2004’;

Left Outer Join qryFig6-37: SELECT P_CODE, VENDOR.V_CODE, V_NAME FROM VENDOR LEFT JOIN PRODUCT ON VENDOR.V_CODE = PRODUCT.V_CODE;

Right Outer Join qryFig6-38: SELECT P_CODE, VENDOR.V_CODE, V_NAME FROM VENDOR RIGHT JOIN PRODUCT ON VENDOR.V_CODE = PRODUCT.V_CODE;