Introduction. Sample Database SQL-92. Sample Data. Sample Data. Chapter 6 Introduction to Structured Query Language (SQL)

Similar documents
Full file at

Test Bank for Database Processing Fundamentals Design and Implementation 13th Edition by Kroenke

2) SQL includes a data definition language, a data manipulation language, and SQL/Persistent stored modules. Answer: TRUE Diff: 2 Page Ref: 36

GlobAl EDITION. Database Concepts SEVENTH EDITION. David M. Kroenke David J. Auer

CSC Web Programming. Introduction to SQL

INDEX. 1 Basic SQL Statements. 2 Restricting and Sorting Data. 3 Single Row Functions. 4 Displaying data from multiple tables

Chapter 7. Introduction to Structured Query Language (SQL) Database Systems: Design, Implementation, and Management, Seventh Edition, Rob and Coronel

Querying Data with Transact SQL

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

SQL STRUCTURED QUERY LANGUAGE

Oracle Database 11g: SQL and PL/SQL Fundamentals

Database design process

SQL functions fit into two broad categories: Data definition language Data manipulation language

Oracle Database: SQL and PL/SQL Fundamentals NEW

1 Writing Basic SQL SELECT Statements 2 Restricting and Sorting Data

Oracle Syllabus Course code-r10605 SQL

Oracle Database: SQL and PL/SQL Fundamentals Ed 2

Sql Server Syllabus. Overview

5. Single-row function

Relational Database Management Systems for Epidemiologists: SQL Part I

Slides by: Ms. Shree Jaswal

Database Technology. Topic 3: SQL. Olaf Hartig.

Querying Data with Transact-SQL

Course Modules for MCSA: SQL Server 2016 Database Development Training & Certification Course:

MTA Database Administrator Fundamentals Course

COSC344 Database Theory and Applications. Lecture 6 SQL Data Manipulation Language (1)

Querying Data with Transact SQL Microsoft Official Curriculum (MOC 20761)

1) Introduction to SQL

Index. Bitmap Heap Scan, 156 Bitmap Index Scan, 156. Rahul Batra 2018 R. Batra, SQL Primer,

Structured Query Language (SQL) Part A. KSE 521 Topic 10 Mun Yi

Chapter 4. Basic SQL. Copyright 2011 Pearson Education, Inc. Publishing as Pearson Addison-Wesley

DB2 SQL Class Outline

1Z Oracle Database 11g - SQL Fundamentals I Exam Summary Syllabus Questions

Chapter 4: SQL Basics

SQL. Char (30) can store ram, ramji007 or 80- b

8) A top-to-bottom relationship among the items in a database is established by a


Table of Contents. PDF created with FinePrint pdffactory Pro trial version

COSC 304 Introduction to Database Systems SQL DDL. Dr. Ramon Lawrence University of British Columbia Okanagan

Oracle Database: Introduction to SQL Ed 2

Querying Data with Transact-SQL

Principles of Data Management

Oracle Database 10g: Introduction to SQL

T-SQL Training: T-SQL for SQL Server for Developers

1. SQL definition SQL is a declarative query language 2. Components DRL: Data Retrieval Language DML: Data Manipulation Language DDL: Data Definition

Chapter 4. Basic SQL. SQL Data Definition and Data Types. Basic SQL. SQL language SQL. Terminology: CREATE statement

20461: Querying Microsoft SQL Server 2014 Databases

Introduction to SQL. IT 5101 Introduction to Database Systems. J.G. Zheng Fall 2011

Querying Data with Transact-SQL (20761)

Introduction to Computer Science and Business

ORACLE TRAINING CURRICULUM. Relational Databases and Relational Database Management Systems

Course Outline and Objectives: Database Programming with SQL

COGS 121 HCI Programming Studio. Week 03 - Tech Lecture

Activant Solutions Inc. SQL 2005: Basic Data Manipulation

CGS 3066: Spring 2017 SQL Reference

12. MS Access Tables, Relationships, and Queries

Structured Query Language (SQL)

SQL Interview Questions

1. Data Definition Language.

RDBMS-Day3. SQL Basic DDL statements DML statements Aggregate functions

Greenplum SQL Class Outline

COURSE OUTLINE MOC 20461: QUERYING MICROSOFT SQL SERVER 2014

Outline. Textbook Chapter 6. Note 1. CSIE30600/CSIEB0290 Database Systems Basic SQL 2

After completing this course, participants will be able to:

SIT772 Database and Information Retrieval WEEK 6. RELATIONAL ALGEBRAS. The foundation of good database design

SQL: Concepts. Todd Bacastow IST 210: Organization of Data 2/17/ IST 210

Business Analytics. SQL PL SQL [Oracle 10 g] P r i n c e S e t h i w w w. x l m a c r o. w e b s. c o m

Chapter 4 SQL. Database Systems p. 121/567

SQL - Subqueries and. Schema. Chapter 3.4 V4.0. Napier University

Tables From Existing Tables

SQL (Structured Query Language)

UNIT-IV (Relational Database Language, PL/SQL)

Database Technology. Topic 2: Relational Databases and SQL. Olaf Hartig.

SQL Data Query Language

CSIE30600 Database Systems Basic SQL 2. Outline

Course Details Duration: 3 days Starting time: 9.00 am Finishing time: 4.30 pm Lunch and refreshments are provided.

Overview Relational data model

Chapter 1 SQL and Data

SQL Server 2008 Tutorial 3: Database Creation

Chapter 3: Introduction to SQL

Querying Microsoft SQL Server

IT360: Applied Database Systems. SQL: Structured Query Language DDL and DML (w/o SELECT) (Chapter 7 in Kroenke) SQL: Data Definition Language

Exam code: Exam name: Database Fundamentals. Version 16.0

COURSE OUTLINE: Querying Microsoft SQL Server

Advanced SQL Tribal Data Workshop Joe Nowinski

Copyright 2016 Ramez Elmasri and Shamkant B. Navathe

20461: Querying Microsoft SQL Server

EGCI 321: Database Systems. Dr. Tanasanee Phienthrakul

Midterm Review. Winter Lecture 13

Introduction to SQL. ECE 650 Systems Programming & Engineering Duke University, Spring 2018

Chapter 3: Introduction to SQL

Querying Data with Transact-SQL

Querying Microsoft SQL Server (MOC 20461C)

Chapter 8. Joined Relations. Joined Relations. SQL-99: Schema Definition, Basic Constraints, and Queries

Follow these steps to get started: o Launch MS Access from your start menu. The MS Access startup panel is displayed:

Lesson 2. Data Manipulation Language

The SQL data-definition language (DDL) allows defining :

Introduction SQL DRL. Parts of SQL. SQL: Structured Query Language Previous name was SEQUEL Standardized query language for relational DBMS:

Course 20461C: Querying Microsoft SQL Server

normalization are being violated o Apply the rule of Third Normal Form to resolve a violation in the model

Transcription:

Chapter 6 Introduction to Structured Query Language (SQL) Introduction Structured Query Language (SQL) is a data sublanguage that has constructs for defining and processing a database It can be Used stand-alone within a DBMS command Embedded in triggers and stored procedures Used in scripting or programming languages Fundamentals, Design, and Implementation, 9/e Chapter 6/2 SQL-92 Sample Database SQL was developed by IBM in late 1970s SQL-92 was endorsed as a national standard by ANSI in 1992 SQL3 incorporates some object-oriented concepts but has not gained acceptance in industry Data Definition Language (DDL) is used to define database structures Data Manipulation Language (DML) is used to query and update data SQL statement is terminated with a semicolon Chapter 6/3 Chapter 6/4 Sample Data Sample Data Chapter 6/5 Chapter 6/6

CREATE TABLE CREATE TABLE statement is used for creating relations Each column is described with three parts: column name, data type, and optional constraints CREATE TABLE PROJECT ( ProjectID Integer Primary Key, Name Char(25) Unique Not Null, Department VarChar(100) Null, MaxHours Numeric(6,1) Default 100); Data Types Standard data types Char for fixed- length character VarChar for variable- length character It requires additional processing than Char data types Integer for whole number Numeric There are many more data types in the SQL-92 standard Chapter 6/7 Chapter 6/8 Constraints ALTER Statement Constraints can be defined within the CREATE TABLE statement, or they can be added to the table after it is created using the ALTER table statement Five types of constraints: PRIMARY KEY may not have null values UNIQUE may have null values NULL/NOT NULL FOREIGN KEY CHECK ALTER statement changes table structure, properties, or constraints after it has been created ALTER TABLE ASSIGNMENT ADD CONSTRAINT EmployeeFK FOREIGN KEY (EmployeeNum) REFERENCES EMPLOYEE (EmployeeNumber) ON UPDATE CASCADE ON DELETE NO ACTION; Chapter 6/9 Chapter 6/10 DROP Statements SELECT Statement DROP TABLE statement removes tables and their data from the database A table cannot be dropped if it contains foreign key values needed by other tables Use ALTER TABLE DROP CONSTRAINT to remove integrity constraints in the other table first : DROP TABLE CUSTOMER; ALTER TABLE ASSIGNMENT DROP CONSTRAINT ProjectFK; SELECT can be used to obtain values of specific columns, specific rows, or both Basic format: SELECT (column names or *) FROM (table name(s)) [WHERE (conditions)]; Chapter 6/11 Chapter 6/12

WHERE Clause Conditions Require quotes around values for Char and VarChar columns, but no quotes for Integer and Numeric columns AND may be used for compound conditions IN and NOT IN indicate match any and match all sets of values, respectively Wildcards _ and % can be used with LIKE to specify a single or multiple unknown characters, respectively IS NULL can be used to test for null values Example: SELECT Statement, MaxHours FROM PROJECT; Insert Figure 6-2 (PROJECT Table only) Chapter 6/13 Chapter 6/14 Example: SELECT DISTINCT SELECT DISTINCT Department FROM PROJECT; Insert Figure 6-2 (PROJECT Table only) Example: SELECT Statement SELECT * FROM PROJECT WHERE Department = Finance AND MaxHours > 100; Insert Figure 6-2 (PROJECT Table only) Chapter 6/15 Chapter 6/16 Example: IN/NOT IN Example: BETWEEN SELECT Name, Phone, Department WHERE Department IN ( Accounting, Finance, Marketing ); Insert Figure 6-2 (EMPLOYEE Table only) SELECT Name, Phone, Department WHERE Department NOT IN ( Accounting, Finance, Marketing ); Insert Figure 6-2 (EMPLOYEE Table only) WHERE EmployeeNumber BETWEEN 200 AND 500; Or WHERE EmployeeNumber >= 200 AND EmployeeNumber <= 500; Insert Figure 6-2 (EMPLOYEE table only) Chapter 6/17 Chapter 6/18

Example: LIKE SELECT * WHERE Phone LIKE 285- ; SELECT * WHERE Phone LIKE 285% ; Insert Figure 6-2 (EMPLOYEE Table only) Example: IS NULL WHERE Phone IS NULL; Insert Figure 6-2 (EMPLOYEE Table only) Chapter 6/19 Chapter 6/20 Sorting the Results Built-in Functions ORDER BY phrase can be used to sort rows from SELECT statement ORDER BY Department; Two or more columns may be used for sorting purposes ORDER BY Department DESC, Name ASC; Five built-in functions for SELECT statement: COUNT counts the number of rows in the result SUM totals the values in a numeric column AVG calculates an average value MAX retrieves a maximum value MIN retrieves a minimum value Result is a single number (relation with a single row and a single column) Column names cannot be mixed with built-in functions Built-in functions cannot be used in WHERE clauses Chapter 6/21 Chapter 6/22 Example: Built-in Functions SELECT COUNT (DISTINCT Department) FROM PROJECT; SELECT MIN(MaxHours), MAX(MaxHours), SUM(MaxHours) FROM PROJECT WHERE ProjectID < 1500; Built-in Functions and Grouping GROUP BY allows a column and a built-in function to be used together GROUP BY sorts the table by the named column and applies the built-in function to groups of rows having the same value of the named column WHERE condition must be applied before GROUP BY phrase SELECT Department, Count(*) WHERE EmployeeNumber < 600 GROUP BY Department HAVING COUNT(*) > 1; Chapter 6/23 Chapter 6/24

Querying Multiple Tables Multiple tables can be queried by using either subqueries or joins If all of the result data comes from a single table, subqueries can be used If results come from two or more tables, joins must be used Joins cannot substitute for correlated subqueries nor for queries that involve EXISTS and NOT EXISTS Chapter 6/25 Subqueries Subqueries can be extended to include many levels SELECT DISTINCT Name WHERE EmployeeNumber IN (SELECT EmployeeNum FROM ASSIGNMENT WHERE HoursWorked > 40 AND ProjectID IN (SELECT ProjectID FROM PROJECT WHERE Department = Accounting )); Chapter 6/26 Joins The basic idea of a join is to form a new relation by connecting the contents of two or more other relations This joined table can be processed like any other table SELECT PROJECT.Name, HoursWorked, EMPLOYEE.Name FROM PROJECT, ASSIGNMENT, EMPLOYEE WHERE PROJECT.ProjectID = ASSIGNMENT.ProjectID AND EMPLOYEE.EmployeeNumber = ASSIGNMENT.EmployeeNum; Alternate Join Syntax SQL- 92 s alternative join syntax substitutes the words JOIN and ON for WHERE Using aliases for table names improves the readability of a join : alias E is assigned to the EMPLOYEE table SELECT P.Name, HoursWorked, E.Name FROM PROJECT P JOIN ASSIGNMENT A ON P.ProjectID = A.ProjectID JOIN EMPLOYEE E ON A.EmployeeNum = E.EmployeeNumber; Chapter 6/27 Chapter 6/28 Outer Joins Example: Outer Join Outer joins can be used to ensure that all rows from a table appear in the result Left (right) outer join: every row on the table on the left (right) hand side is included in the results even though the row may not have a match Outer joins can be nested Left outer join SELECT Name, HoursWorked FROM PROJECT LEFT JOIN ASSIGNMENT ON PROJECT.ProjectID = ASSIGNMENT.ProjectID; Nested outer join SELECT PROJECT.Name, HoursWorked, EMPLOYEE.Name FROM ((PROJECT LEFT JOIN ASSIGNMENT ON PROJECT.ProjectID = ASSIGNMENT.ProjectID) LEFT JOIN EMPLOYEE ON EMPLOYEE.EmployeeNumber = Assignment.EmployeeNum); Chapter 6/29 Chapter 6/30

INSERT INTO Statement The order of the column names must match the order of the values Values for all NOT NULL columns must be provided No value needs to be provided for a surrogate primary key It is possible to use a select statement to provide the values for bulk inserts from a second table s: INSERT INTO PROJECT VALUES (1600, Q4 Tax Prep, Accounting, 100); INSERT INTO PROJECT (Name, ProjectID) VALUES ( Q1+ Tax Prep, 1700); UPDATE Statement UPDATE statement is used to modify values of existing data : UPDATE EMPLOYEE SET Phone = 287-1435 WHERE Name = James Nestor ; UPDATE can also be used to modify more than one column value at a time UPDATE EMPLOYEE SET Phone = 285-0091, Department = Production WHERE EmployeeNumber = 200; Chapter 6/31 Chapter 6/32 DELETE FROM Statement Delete statement eliminates rows from a table DELETE FROM PROJECT WHERE Department = Accounting ; ON DELETE CASCADE removes any related referential integrity constraint of a deleted row Chapter 6/33 Chapter 6 Introduction to Structured Query Language (SQL) Fundamentals, Design, and Implementation, 9/e