IMS 4212: Intro to SQL 1 Dr. Lawrence West, Management Dept., University of Central Florida Introduction to SQL—Topics Introduction to.

Slides:



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

Introduction to Structured Query Language (SQL)
A Guide to SQL, Seventh Edition. Objectives Understand the concepts and terminology associated with relational databases Create and run SQL commands in.
Introduction to Structured Query Language (SQL)
Database Systems: Design, Implementation, and Management Eighth Edition Chapter 7 Introduction to Structured Query Language (SQL)
Concepts of Database Management Sixth Edition
Tutorial 5 Multi-table queries. Tutorial 5 objectives Displaying Data from Multiple Tables –[ ]Write SELECT statements to access data from more than one.
Structured Query Language SQL: An Introduction. SQL (Pronounced S.Q.L) The standard user and application program interface to a relational database is.
WRITING BASIC SQL SELECT STATEMENTS Lecture 7 1. Outlines  SQL SELECT statement  Capabilities of SELECT statements  Basic SELECT statement  Selecting.
DAY 21: MICROSOFT ACCESS – CHAPTER 5 MICROSOFT ACCESS – CHAPTER 6 MICROSOFT ACCESS – CHAPTER 7 Akhila Kondai October 30, 2013.
Session 5: Working with MySQL iNET Academy Open Source Web Development.
Chapter 5 Introduction to SQL. Structured Query Language = the “programming language” for relational databases SQL is a nonprocedural language = the user.
CPS120: Introduction to Computer Science Information Systems: Database Management Nell Dale John Lewis.
ASP.NET Programming with C# and SQL Server First Edition
IMS 4212: Application Architecture and Intro to Stored Procedures 1 Dr. Lawrence West, Management Dept., University of Central Florida
AL-MAAREFA COLLEGE FOR SCIENCE AND TECHNOLOGY INFO 232: DATABASE SYSTEMS CHAPTER 7 INTRODUCTION TO STRUCTURED QUERY LANGUAGE (SQL) Instructor Ms. Arwa.
Concepts of Database Management Seventh Edition
Introduction to databases and SQL. What is a database?  A database is an organized way of holding together pieces of information  A database refers.
Module 8: Implementing Stored Procedures. Introducing Stored Procedures Creating, Modifying, Dropping, and Executing Stored Procedures Using Parameters.
Module 1: Introduction to Transact-SQL
1 Joining Tables in Queries. 2 Objectives You will be able to Write SQL queries that use Join operations to retrieve information from multiple tables.
CPS120: Introduction to Computer Science Lecture 19 Introduction to SQL.
ISM 4212 Lab Creating DB Tables 02 copyright Lars Paul Linden 2007.
SQL (DDL & DML Commands)
Structure Query Language SQL. Database Terminology Employee ID 3 3 Last name Small First name Tony 5 5 Smith James
Concepts of Database Management Seventh Edition
7 1 Chapter 7 Introduction to Structured Query Language (SQL) Database Systems: Design, Implementation, and Management, Seventh Edition, Rob and Coronel.
Oracle 11g DATABASE DEVELOPMENT LAB1. Introduction  Oracle 11g Database:-  Oracle 11g database is designed for some features, which helps to the organizations.
Databases MIS 21. Some database terminology  Database: integrated collection of data  Database Management System (DBMS): environment that provides mechanisms.
6 1 Lecture 8: Introduction to Structured Query Language (SQL) J. S. Chou, P.E., Ph.D.
Database Systems Design, Implementation, and Management Coronel | Morris 11e ©2015 Cengage Learning. All Rights Reserved. May not be scanned, copied or.
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.
IMS 4212: Data Manipulation 1 Dr. Lawrence West, MIS Dept., University of Central Florida Additional Data Manipulation Statements INSERT.
SQL Jan 20,2014. DBMS Stores data as records, tables etc. Accepts data and stores that data for later use Uses query languages for searching, sorting,
AL-MAAREFA COLLEGE FOR SCIENCE AND TECHNOLOGY INFO 232: DATABASE SYSTEMS CHAPTER 7 (Part II) INTRODUCTION TO STRUCTURED QUERY LANGUAGE (SQL) Instructor.
1 Chapter 4: Creating Simple Queries 4.1 Introduction to the Query Task 4.2 Selecting Columns and Filtering Rows 4.3 Creating New Columns with an Expression.
Concepts of Database Management Seventh Edition Chapter 3 The Relational Model 2: SQL.
Query Lab CSC 240 Blum1. Log on to PMA (PHPMyAdmin) and click on the Northwind database CSC 240 Blum2.
1 Copyright © Oracle Corporation, All rights reserved. Writing Basic SQL SELECT Statements.
SQL. Originally developed by IBM Standardized in 80’s by ANSI and ISO Language to access relational database and English-like non-procedural Predominant.
IMS 4212: Application Architecture and Intro to Stored Procedures 1 Dr. Lawrence West, Management Dept., University of Central Florida
IMS 4212: Intro to Multi-Table SELECT Statements 1 Dr. Lawrence West, MIS Dept., University of Central Florida Multi-Table SELECT Statements—Topics.
IMS 4212: Constraints & Triggers 1 Dr. Lawrence West, Management Dept., University of Central Florida Stored Procedures in SQL Server.
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.
MICROSOFT ACCESS – CHAPTER 5 MICROSOFT ACCESS – CHAPTER 6 MICROSOFT ACCESS – CHAPTER 7 Sravanthi Lakkimsety Mar 14,2016.
 CONACT UC:  Magnific training   
Lec-7. The IN Operator The IN operator allows you to specify multiple values in a WHERE clause. SQL IN Syntax SELECT column_name(s) FROM table_name WHERE.
1 Section 1 - Introduction to SQL u SQL is an abbreviation for Structured Query Language. u It is generally pronounced “Sequel” u SQL is a unified language.
Concepts of Database Management, Fifth Edition Chapter 3: The Relational Model 2: SQL.
COM621: Advanced Interactive Web Development Lecture 11 MySQL – Data Manipulation Language.
SQL SQL Ayshah I. Almugahwi Maryam J. Alkhalifa
CHAPTER 7 DATABASE ACCESS THROUGH WEB
SQL Query Getting to the data ……..
Chapter 5 Introduction to SQL.
Writing Basic SQL SELECT Statements
Structured Query Language – The Basics
20761A 10: Using Subqueries Module 10   Using Subqueries.
Writing Basic SQL SELECT Statements
Introduction To Structured Query Language (SQL)
Copyright © 2012 Pearson Education, Inc. Publishing as Prentice Hall
Writing Basic SQL SELECT Statements
Introduction To Structured Query Language (SQL)
Chapter 8 Advanced SQL.
Advanced Joins IN ( ) Expression Subqueries with IN ( ) Expression
Presentation transcript:

IMS 4212: Intro to SQL 1 Dr. Lawrence West, Management Dept., University of Central Florida Introduction to SQL—Topics Introduction to SQL SQL & Programming Categories of SQL Statements The SELECT Query –Limiting columns –Limiting rows with WHERE –Sorting (ordering) results –Renaming the output columns with AS –Computed columns –Functions in SQL

IMS 4212: Intro to SQL 2 Dr. Lawrence West, Management Dept., University of Central Florida What is SQL? SQL stands for "Structured Query Language" and is a language for manipulating relational databases Pronounced "S"-"Q"-"L" or "SEQUEL" ANSI SQL is the 'base' version of SQL Each manufacturer has their own dialect of SQL Most differences are in special purpose corners of the language

IMS 4212: Intro to SQL 3 Dr. Lawrence West, Management Dept., University of Central Florida What is SQL (cont.) SQL is not a programming language –SQL comes with the database –Database engine interprets SQL statements, acts on them, and (if pertinent) returns a result SQL can be entered directly in most DBMS SQL can be embedded in a programming language and passed to the database engine We will learn SQL and then use it in our applications

IMS 4212: Intro to SQL 4 Dr. Lawrence West, Management Dept., University of Central Florida SQL and Programming SQL represents a division of labor between the database and the program you write –You create the interface for input and output –You translate user needs into SQL 'behind the scenes' and pass SQL to the DBMS –DBMS executes commands –You take results (including errors!) and display them to the user through your interface SQL can contain complex procedural code in addition to data manipulation statements

IMS 4212: Intro to SQL 5 Dr. Lawrence West, Management Dept., University of Central Florida SQL & Programming (cont.) You will only rarely enter SQL directly at the DBMS level except for instructional needs and testing –Most have front ends for creating and modifying database structures –In large systems the database administrator (DBA) will do the database structure creation You will constantly use SQL in programs, though and need to be expert in its use

IMS 4212: Intro to SQL 6 Dr. Lawrence West, Management Dept., University of Central Florida SQL & Programming (cont.) SQL follows an 80/20 rule –You will use 20% of the language to do 80% of the necessary tasks –You need to be aware of what exists in the other 80% of the language Sometimes you're going to have one of those 20% tasks Many employers ask about SQL skills in interviews

IMS 4212: Intro to SQL 7 Dr. Lawrence West, Management Dept., University of Central Florida Categories of SQL Statements SQL statements are divided into two groups –Data definition language: Create and modify the structure of the database Tables Relationships Rules Indices –Data manipulation language Add and delete records Retrieve records Update records

IMS 4212: Intro to SQL 8 Dr. Lawrence West, Management Dept., University of Central Florida Categories of SQL Statements (cont) Data Definition Language –CREATE TABLE statement –CREATE INDEX statement –ALTER TABLE statement –CONSTRAINT clauses –DROP statement

IMS 4212: Intro to SQL 9 Dr. Lawrence West, Management Dept., University of Central Florida Categories of SQL Statements (cont.) Data Manipulation Language –SELECT statement –INSERT INTO statement –UPDATE statement –DELETE statement –JOIN operations INNER JOIN, LEFT JOIN, RIGHT JOIN, OUTER JOIN –WHERE, ORDER BY, & HAVING clauses

IMS 4212: Intro to SQL 10 Dr. Lawrence West, Management Dept., University of Central Florida The SELECT Statement (Query) SELECT provides the mechanism for retrieving data from the database –Can assemble data together from multiple tables into logical records –Can limit which records you retrieve –Can limit which fields you retrieve –Can perform calculations and display the results along with 'raw' data values SELECT will be a workhorse in your database programming and you should understand it thoroughly

IMS 4212: Intro to SQL 11 Dr. Lawrence West, Management Dept., University of Central Florida Introduction To the Query Editor

IMS 4212: Intro to SQL 12 Dr. Lawrence West, Management Dept., University of Central Florida SELECT Statement—Simple Example SELECT * FROM Suppliers; SELECT SQL keyword Return all fields (columns) FROM SQL keyword indicates tables involved in the query Indicates only Suppliers table is used This query returns all fields from all records in the Suppliers table Note that SQL keywords are in all capital letters

IMS 4212: Intro to SQL 13 Dr. Lawrence West, Management Dept., University of Central Florida SELECT Statement—Exercise Change active database to NorthWind Enter the query below and click Execute or press "F5" SELECT * FROM Products

IMS 4212: Intro to SQL 14 Dr. Lawrence West, Management Dept., University of Central Florida Query Results Pane Editing window Results Pane Records returned (Ctrl-R will eliminate the results pane after a query has run)

IMS 4212: Intro to SQL 15 Dr. Lawrence West, Management Dept., University of Central Florida The SELECT Clause—Limiting Output Columns SELECT keyword may be followed by a list of fields or other expressions Only these fields will be returned in the result set! Fields may be specified in any order (very important) SELECT CompanyName, Phone, Fax FROM Suppliers;

IMS 4212: Intro to SQL 16 Dr. Lawrence West, Management Dept., University of Central Florida Exercises Enter and run the following queries Write the query to return just Customer company names and countries SELECT ProductID, ProductName, UnitsInStock FROM Products SELECT ProductName, ProductID, UnitsInStock FROM Products

IMS 4212: Intro to SQL 17 Dr. Lawrence West, Management Dept., University of Central Florida The WHERE Clause—Limiting Records The WHERE clause establishes a condition that can be tested to be either True or False for any record –Usually tests value of one or more fields Only the records whose values are TRUE for the specified WHERE clause criteria are returned in the result set SELECT CompanyName, Phone, Fax FROM Suppliers WHERE Country = 'USA';

IMS 4212: Intro to SQL 18 Dr. Lawrence West, Management Dept., University of Central Florida The WHERE Clause—Limiting Records (cont.) String and Date literal values must be delimited –SQL Server uses single quotes as delimiters for all data types needing delimiters –Some databases use different delimiters String values in single or double quotes Date values in pound signs (#) or single quotes Numeric literals do not use delimiters

IMS 4212: Intro to SQL 19 Dr. Lawrence West, Management Dept., University of Central Florida The WHERE Clause—Compound Tests The WHERE Clause can contain complex tests Entire test must evaluate to True for record to be displayed Use logical operators –NOT –AND or OR –Parenthetical grouping –<> (notequal), =,, >= Follows the same precedence rules as compound logical tests in VB programming SELECT * FROM Suppliers WHERE Country = 'USA' OR Country = 'Germany';

IMS 4212: Intro to SQL 20 Dr. Lawrence West, Management Dept., University of Central Florida Exercises List all of the customers in Germany List all of the products where the units in stock are less than the reorder point List all products in Category 2 List all products in Category 2 where the units in stock is less than 20 but do not list the product if it has been discontinued (Discontinued = 1 indicates the product has been discontinued)

IMS 4212: Intro to SQL 21 Dr. Lawrence West, Management Dept., University of Central Florida Some Interesting Tricks Run each of these queries: SELECT 4 SELECT 4, 'Test Value', ProductName FROM Products SELECT * FROM Products WHERE 4 = 4

IMS 4212: Intro to SQL 22 Dr. Lawrence West, Management Dept., University of Central Florida Reordering Query Output with ORDER BY Run these four queries: SELECT ProductID, ProductName, UnitPrice FROM Products SELECT ProductID, ProductName, UnitPrice FROM Products ORDER BY ProductName SELECT ProductID, ProductName, UnitPrice FROM Products ORDER BY ProductName DESC SELECT ProductID, ProductName, UnitPrice FROM Products ORDER BY SupplierID

IMS 4212: Intro to SQL 23 Dr. Lawrence West, Management Dept., University of Central Florida Reordering Query Output with ORDER BY (cont) SELECT ProductID, ProductName, SupplierID FROM Products SELECT ProductID, ProductName, SupplierID FROM Products ORDER BY SupplierID SELECT ProductID, ProductName, SupplierID FROM Products ORDER BY SupplierID, ProductName

IMS 4212: Intro to SQL 24 Dr. Lawrence West, Management Dept., University of Central Florida Aliasing Column Names with AS SELECT CustomerID, CompanyName FROM Customers SELECT CustomerID, CompanyName AS 'Company Name' FROM Customers SELECT CustomerID, CompanyName AS Company FROM Customers SELECT CustomerID, CompanyName AS Company Name FROM Customers Msg 102, Level 15, State 1, Line 1 Incorrect syntax near 'Name'.

IMS 4212: Intro to SQL 25 Dr. Lawrence West, Management Dept., University of Central Florida Calculated Columns SELECT ProductID, ProductName, UnitsInStock * UnitPrice AS 'Value' FROM Products ORDER BY Value SELECT LastName, FirstName, LastName + ', ' + FirstName AS 'Employee Name' FROM Employees Always provide a column name to a calculated column with the AS expression

IMS 4212: Intro to SQL 26 Dr. Lawrence West, Management Dept., University of Central Florida Functions SQL Server has many intrinsic functions that may be used in SQL statements We will be mixing these in with examples throughout the lab Browsing these on your own can pay off for you Pay special attention to date and time functions!!!

IMS 4212: Intro to SQL 27 Dr. Lawrence West, Management Dept., University of Central Florida Some Date Functions in Action List all of the order information where the orders took more than five days to ship SELECT GetDate() SELECT DatePart(mm, GetDate()) SELECT DateName(mm, GetDate()) SELECT OrderID, OrderDate, ShippedDate, DateDiff(dd, OrderDate, ShippedDate) AS 'Fulfillment Time' FROM Orders WHERE ShippedDate IS NOT NULL

IMS 4212: Intro to SQL 28 Dr. Lawrence West, Management Dept., University of Central Florida Commenting SQL Statements CREATE PROCEDURE [dbo].[up_Order_Invoice_OrderInfo] /********************************************************** SP: up_Order_Invoice Takes an OrderID as a parameter and returns all data needed to create an invoice for the order except order details Must work with the SP up_Order_Invoice_Details to create all order invoice information. Created by Dr. Larry West, MIS Department, UCF Last Modified: 9 January 2007 **********************************************************/ --Create int AS --Execute the query SELECT CUSTOMERS.*, ORDERS.*, EMPLOYEES.LastName, EMPLOYEES.FirstName FROM CUSTOMERS, ORDERS, EMPLOYEES WHERE ORDERS.OrderID AND ORDERS.CustomerID = CUSTOMERS.CustomerID AND ORDERS.EmployeeID = EMPLOYEES.EmployeeID Block comment /* … */ Single line comment -- (two hyphens)