Database Systems: Design, Implementation, and Management Eighth Edition Chapter 7 Introduction to Structured Query Language (SQL)

Slides:



Advertisements
Similar presentations
Introduction to Structured Query Language (SQL)
Advertisements

SQL DESIGN AND IMPLEMENTATION CONTENT SOURCES: ELAMSARI AND NAVATHE, FUNDAMENTALS OF DATABASE MANAGEMENT SYSTEMSELAMSARI AND NAVATHE, FUNDAMENTALS OF.
OUTLINE OF THE LECTURE PART I GOAL: Understand the Data Definition Statements in Fig 4.1 Step1: Columns of the Tables and Data types. Step2: Single column.
Copyright © 2007 Ramez Elmasri and Shamkant B. Navathe Slide 8- 1.
Database Systems: Design, Implementation, and Management Tenth Edition
Copyright © by Royal Institute of Information Technology Introduction To Structured Query Language (SQL) 1.
1 CSE 480: Database Systems Lecture 10: SQL - DML Reference: Read Chapter 4 of the textbook.
Introduction to Structured Query Language (SQL)
Copyright © 2007 Ramez Elmasri and Shamkant B. Navathe Chapter 8 SQL-99: SchemaDefinition, Constraints, and Queries and Views.
Introduction to Structured Query Language (SQL)
Structured Query Language (SQL)
Fundamentals, Design, and Implementation, 9/e Chapter 6 Introduction to Structured Query Language (SQL)
Structured Query Language Part I Chapter Three CIS 218.
Introduction to Structured Query Language (SQL)
Database Systems: Design, Implementation, and Management Eighth Edition Chapter 7 Introduction to Structured Query Language (SQL)
SQL The relational DB Standard CS-450 Dr. Ali Obaidi.
Introduction to SQL J.-S. Chou Assistant Professor.
ITEC 3220A Using and Designing Database Systems Instructor: Gordon Turpin Course Website: Office: CSEB3020.
Copyright © 2007 Ramez Elmasri and Shamkant B. Navathe Slide 8- 1 CREATE/DROP/ALTER TABLE Data types : char, varchar, decimal, date CREATE TABLE DEPARTMENT.
Chapter 5 Introduction to SQL. Structured Query Language = the “programming language” for relational databases SQL is a nonprocedural language = the user.
Relational DBs and SQL Designing Your Web Database (Ch. 8) → Creating and Working with a MySQL Database (Ch. 9, 10) 1.
15 Structured Query Language (SQL). 2 Objectives After completing this section, you should be able to: Understand Structured Query Language (SQL) and.
Chapter 3 Single-Table Queries
AL-MAAREFA COLLEGE FOR SCIENCE AND TECHNOLOGY INFO 232: DATABASE SYSTEMS CHAPTER 7 INTRODUCTION TO STRUCTURED QUERY LANGUAGE (SQL) Instructor Ms. Arwa.
6 1 Chapter 6 Introduction to Structured Query Language (SQL) Database Systems: Design, Implementation, and Management, Sixth Edition, Rob and Coronel.
SQL: Data Manipulation Presented by Mary Choi For CS157B Dr. Sin Min Lee.
Database Systems: Design, Implementation, and Management Tenth Edition
7 1 Chapter 7 Introduction to Structured Query Language (SQL) Database Systems: Design, Implementation, and Management, Seventh Edition, Rob and Coronel.
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.
Announcements Written Homework 1 due Friday –If you have fourth edition make sure you do the right problems Program 3 out today, due next Friday Nov 10.
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)
Chapter 7 SQL HUANG XUEHUA. AGGREGATE FUNCTIONS Include COUNT, SUM, MAX, MIN, and AVG Query 15: Find the maximum salary, the minimum salary, and the.
Chapter 8 Part 2 SQL-99 Schema Definition, Constraints, Queries, and Views.
Database Lab Lecture 1. Database Languages Data definition language ( DDL ) Data definition language –defines data types and the relationships among them.
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
Slide 8- 1 THE HAVING-CLAUSE Provides a condition on the summary information Sometimes we want to retrieve the values of these functions for only those.
Queries SELECT [DISTINCT] FROM ( { }| ),... [WHERE ] [GROUP BY [HAVING ]] [ORDER BY [ ],...]
DATA RETRIEVAL WITH SQL Goal: To issue a database query using the SELECT command.
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.
Riyadh Philanthropic Society For Science Prince Sultan College For Woman Dept. of Computer & Information Sciences CS 340 Introduction to Database Systems.
Introduction to Database Systems
A Guide to SQL, Eighth Edition Chapter Four Single-Table Queries.
Relational Database Management System(RDBMS) Structured Query Language(SQL)
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.
Copyright © 2004 Pearson Education, Inc. Schema Definition, Basic Constraints, and Queries.
SQL- Updates, Assertions and Views. Data Definition, Constraints, and Schema Changes Used to CREATE, DROP, and ALTER the descriptions of the tables (relations)
Concepts of Database Management, Fifth Edition Chapter 3: The Relational Model 2: SQL.
Structured Query Language (3) The main reference of this presentation is the textbook and PPT from : Elmasri & Navathe, Fundamental of Database Systems,
SQL SQL Ayshah I. Almugahwi Maryam J. Alkhalifa
CHAPTER 7 DATABASE ACCESS THROUGH WEB
Chapter 5 Introduction to SQL.
ATS Application Programming: Java Programming
Chapter 4 Basic SQL.
CS580 Advanced Database Topics
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:

Database Systems: Design, Implementation, and Management Eighth Edition Chapter 7 Introduction to Structured Query Language (SQL)

Database Systems, 8 th Edition 2 Objectives In this chapter, you will learn: –The basic commands and functions of SQL –How to use SQL for data administration (to create tables, indexes, and views) –How to use SQL for data manipulation (to add, modify, delete, and retrieve data) –How to use SQL to query a database for useful information

Database Systems, 8 th Edition 3 Introduction to SQL SQL functions fit into two broad categories: –Data definition language (DDL) –Data manipulation language (DML) Basic command set has vocabulary of less than 100 words Schema –Group of database objects that are related to each other (tables, indexes, views, …etc)

SQL Data Definition Commands

Database Systems, 8 th Edition 5 Data Types Data type selection is usually dictated by nature of data and by intended use

Database Systems, 8 th Edition 6 Creating Table Structures Use one line per column (attribute) definition Table and attribute names are capitalized Command sequence ends with a semicolon Example CREATE TABLE DEPT ( DNAMEVARCHAR(10)NOT NULL, DNUMBERINTEGERNOT NULL, MGRSSNCHAR(9), MGRSTARTDATECHAR(9));

Database Systems, 8 th Edition 7 SQL Constraints NOT NULL constraint –Ensures that column does not accept nulls UNIQUE constraint –Ensures that all values in column are unique DEFAULT constraint –Assigns value to attribute when a new row is added to table CHECK constraint –Validates data when attribute value is entered Primary key attributes contain both a NOT NULL and a UNIQUE specification

Database Systems, 8 th Edition 8 SQL Indexes When primary key is declared, DBMS automatically creates unique index Often need additional indexes Using CREATE INDEX command, SQL indexes can be created on basis of any selected attribute Composite index –Index based on two or more attributes –Often used to prevent data duplication

Database Systems, 8 th Edition 9 Deleting a Table from the Database DROP –Deletes table from database –Syntax: DROP TABLE tablename; Can drop a table only if it is not the “one” side of any relationship –Otherwise RDBMS generates an error message –Foreign key integrity violation Example: DROP TABLE DEPENDENT;

Database Systems, 8 th Edition 10 Advanced Data Definition Commands All changes in table structure are made by using ALTER command Three options –ADD adds a column –MODIFY changes column characteristics –DROP deletes a column Example: ALTER TABLE EMPLOYEE ADD JOB VARCHAR(12);

Database Systems, 8 th Edition 11 Data Manipulation Commands INSERT SELECT COMMIT UPDATE ROLLBACK DELETE

Database Systems, 8 th Edition 12 Adding Table Rows INSERT –Used to enter data into table –Syntax: INSERT INTO columnname VALUES (value1, value2, …, valueN); INSERT INTO EMPLOYEE (FNAME, LNAME, SSN) VALUES ('Richard', 'Marini', ' ')

Database Systems, 8 th Edition 13 Adding Table Rows (continued) When entering values, notice that: –Row contents are entered between parentheses –Character and date values are entered between apostrophes –Numerical entries are not enclosed in apostrophes –Attribute entries are separated by commas –A value is required for each column Use NULL for unknown values

Database Systems, 8 th Edition 14 Inserting Table Rows with a SELECT Subquery INSERT –Inserts multiple rows from another table (source) –Uses SELECT subquery –Subquery: query embedded (or nested) inside another query –Subquery executed first –Syntax: INSERT INTO tablename SELECT columnlist FROM tablename; INSERT INTODEPTS_INFO (DEPT_NAME, NO_OF_EMPS, TOTAL_SAL) SELECTDNAME, COUNT (*), SUM (SALARY) FROMDEPARTMENT, EMPLOYEE WHEREDNUMBER=DNO GROUP BYDNAME ;

Database Systems, 8 th Edition 15 Saving Table Changes Changes made to table contents are not physically saved on disk until: –Database is closed –Program is closed –COMMIT command is used Syntax: –COMMIT [WORK]; Will permanently save any changes made to any table in the database

Database Systems, 8 th Edition 16 Thu 13-6 Updating Table Rows UPDATE –Modify data in a table –Syntax: UPDATE tablename SET columnname = expression [, columnname = expression] [WHERE conditionlist]; If more than one attribute is to be updated in row, separate corrections with commas

Updating Table Rows UPDATE PROJECT SETPLOCATION = 'Bellaire', DNUM = 5 WHEREPNUMBER=10 UPDATE EMPLOYEE SETSALARY = SALARY *1.1 WHEREDNO IN (SELECTDNUMBER FROMDEPARTMENT WHEREDNAME='Research')

Database Systems, 8 th Edition 18 Restoring Table Contents ROLLBACK –Undoes changes since last COMMIT –Brings data back to pre-change values Syntax: –ROLLBACK; COMMIT and ROLLBACK only work with commands to add, modify, or delete table rows (with DML only NOT with DDL)

Database Systems, 8 th Edition 19 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 specified table will be deleted

Deleting Table Rows Examples: DELETE FROM EMPLOYEE WHERELNAME='Brown’; DELETE FROM EMPLOYEE WHERESSN=' ’; DELETE FROM EMPLOYEE WHEREDNO IN (SELECTDNUMBER FROMDEPARTMENT WHEREDNAME='Research'); DELETE FROM EMPLOYEE;

Database Systems, 8 th Edition 21 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 SELECT FNAME, LNAME, ADDRESS FROM EMPLOYEE, DEPARTMENT WHERE DNAME='Research' AND DNUMBER=DNO

Database Systems, 8 th Edition 22 SELECT Queries Fine-tune SELECT command by adding restrictions to search criteria using: –Conditional restrictions –Arithmetic operators –Logical operators –Special operators

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

Database Systems, 8 th Edition 24 Arithmetic Operators: The Rule of Precedence Perform operations within parentheses Perform power operations Perform multiplications and divisions Perform additions and subtractions

Database Systems, 8 th Edition 25 Logical Operators: AND, OR, and NOT Searching data involves multiple conditions Logical operators: AND, OR, and NOT Can be combined –Parentheses placed to enforce precedence order Conditions in parentheses always executed first NOT negates result of conditional expression

Database Systems, 8 th Edition 26 Special Operators BETWEEN: checks whether attribute value is within a range IS NULL: checks whether attribute value is null LIKE: checks whether attribute value matches given string pattern IN: checks whether attribute value matches any value within a value list EXISTS: checks if subquery returns any rows Comparison operators: >,, >=, <=

SELECT Queries - Examples SELECT BDATE, ADDRESS FROM EMPLOYEE WHEREFNAME='John' AND MINIT='B’ AND LNAME='Smith’; SELECTFNAME, LNAME, ADDRESS FROM EMPLOYEE, DEPARTMENT WHEREDNAME='Research' AND DNUMBER=DNO; SELECT PNUMBER, DNUM, LNAME, BDATE, ADDRESS FROMPROJECT, DEPARTMENT, EMPLOYEE WHERE DNUM=DNUMBER AND MGRSSN=SSN ANDPLOCATION='Stafford‘;

Database Systems, 8 th Edition 28 SUN 16-6 Advanced SELECT Queries Logical operators work well in the query environment SQL provides useful functions that: –Count –Find minimum and maximum values –Calculate averages, etc. SQL allows user to limit queries to: –Entries having no duplicates –Entries whose duplicates may be grouped

Database Systems, 8 th Edition 29 Ordering a Listing ORDER BY clause useful when listing order important Syntax: SELECT columnlist FROM tablelist [WHERE conditionlist] [ORDER BY columnlist [ASC | DESC]]; Ascending order by default

Database Systems, 8 th Edition 30 Grouping Data Frequency distributions created by GROUP BY clause within SELECT statement Syntax: SELECT columnlist FROM tablelist [WHEREconditionlist] [GROUP BY columnlist] [HAVINGconditionlist] [ORDER BYcolumnlist [ASC | DESC] ] ;

Database Systems, 8 th Edition 31 Listing Unique Values DISTINCT clause produces list of only values that are different from one another Example: SELECT DISTINCT V_CODE FROM PRODUCT; Access places nulls at the top of the list –Oracle places it at the bottom –Placement of nulls does not affect list contents

Database Systems, 8 th Edition 32 Aggregate Functions COUNT function tallies number of non-null values of an attribute –Takes one parameter: usually a column name MAX and MIN find highest (lowest) value in a table –Compute MAX value in inner query –Compare to each value returned by the query SUM computes total sum for any specified attribute AVG function format similar to MIN and MAX Example: SELECT MAX(SALARY), MIN(SALARY), AVG(SALARY) FROM EMPLOYEE

COUNT Function Output Examples

MAX and MIN Function Output Examples

The Total Value of All Items in the PRODUCT Table

AVG Function Output Examples

GROUP BY Clause Output Examples

An Application of the HAVING Clause

Database Systems, 8 th Edition 39 Virtual Tables: Creating a View View is virtual table based on SELECT query Create view by using CREATE VIEW command Special characteristics of relational view: –Name of view can be used anywhere a table name is expected –View dynamically updated –Restricts users to only specified columns and rows –Views may be used as basis for reports

Creating a View