Introduction to SQL Mike Burr

Slides:



Advertisements
Similar presentations
Copyright © 2006 by The McGraw-Hill Companies, Inc. All rights reserved. McGraw-Hill Technology Education Copyright © 2006 by The McGraw-Hill Companies,
Advertisements

Database Basics. What is Access? Database management system Computer-based equivalent of a manual database Makes it easy to organize and update information.
Concepts of Database Management Sixth Edition
Access Lesson 2 Creating a Database
Introduction to Structured Query Language (SQL)
CSE 190: Internet E-Commerce Lecture 10: Data Tier.
A Guide to SQL, Seventh Edition. Objectives Understand the concepts and terminology associated with relational databases Create and run SQL commands in.
Concepts of Database Management Sixth Edition
CSCI 3328 Object Oriented Programming in C# Chapter 12: Databases and LINQ 1 Xiang Lian The University of Texas – Pan American Edinburg, TX 78539
DATA, DATABASES, AND QUERIES Managing Data in Relational Databases CS1100Microsoft Access - Introduction1.
DAY 21: MICROSOFT ACCESS – CHAPTER 5 MICROSOFT ACCESS – CHAPTER 6 MICROSOFT ACCESS – CHAPTER 7 Akhila Kondai October 30, 2013.
DATA, DATABASES, AND QUERIES Managing Data in Relational Databases CS1100Microsoft Access - Introduction1 Created By Martin Schedlbauer
Session 5: Working with MySQL iNET Academy Open Source Web Development.
1 Microsoft Access 2002 Tutorial 3 – Querying a Database.
Database Lecture # 1 By Ubaid Ullah.
CPS120: Introduction to Computer Science Information Systems: Database Management Nell Dale John Lewis.
ASP.NET Programming with C# and SQL Server First Edition
DAY 14: ACCESS CHAPTER 1 Tazin Afrin October 03,
Database A collection of related data. Database Applications Banking: all transactions Airlines: reservations, schedules Universities: registration, grades.
Learningcomputer.com SQL Server 2008 – Entity Relationships in a Database.
Introduction to SQL Steve Perry
Data-mining & Data As we used Excel that has capability to analyze data to find important information, the data-mining helps us to extract information.
1 INTRODUCTION TO DATABASE MANAGEMENT SYSTEM L E C T U R E
Mr. Justin “JET” Turner CSCI 3000 – Fall 2015 CRN Section A – TR 9:30-10:45 CRN – Section B – TR 5:30-6:45.
Copyright  Oracle Corporation, All rights reserved. 4 CMIS Powell Oracle Designer: Creating the Database Design CMIS Powell.
Microsoft Access 2003 Define some key Access terminology: Field – A single characteristic or attribute of a person, place, object, event, or idea. Record.
Access Queries Office 2013/ Queries Most common type of Query is selection(projection) Specify sources for data retrieval table(s) and/or query(ies)
1 By: Nour Hilal. Microsoft Access is a database software where data is stored in one or more Tables. A Database is a group of related Tables. Access.
CPS120: Introduction to Computer Science Lecture 19 Introduction to SQL.
Relational Databases Database Driven Applications Retrieving Data Changing Data Analysing Data What is a DBMS An application that holds the data manages.
Concepts of Database Management Seventh Edition
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
CS1100: Microsoft Access Managing Data in Relational Databases Created By Martin Schedlbauer CS11001Microsoft Access - Introduction.
M1G Introduction to Database Development 5. Doing more with queries.
6 1 Lecture 8: Introduction to Structured Query Language (SQL) J. S. Chou, P.E., Ph.D.
Microsoft Office Illustrated Introductory, Premium Edition Using Tables and Queries.
XP New Perspectives on Microsoft Access 2002 Tutorial 1 1 Microsoft Access 2002 Tutorial 1 – Introduction To Microsoft Access 2002.
Database Management Systems.  Database management system (DBMS)  Store large collections of data  Organize the data  Becomes a data storage system.
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.
CIS 375—Web App Dev II SQL. 2 Introduction SQL (Structured _______ Language) is an ANSI standard language for accessing databases.ANSI SQL can execute.
Database Fundamental & Design by A.Surasit Samaisut Copyrights : All Rights Reserved.
Visual Programing SQL Overview Section 1.
CIS 375—Web App Dev II SQL. 2 Introduction SQL (Structured _______ Language) is an ANSI standard language for accessing databases.ANSI SQL can execute.
AL-MAAREFA COLLEGE FOR SCIENCE AND TECHNOLOGY INFO 232: DATABASE SYSTEMS CHAPTER 7 (Part II) INTRODUCTION TO STRUCTURED QUERY LANGUAGE (SQL) Instructor.
Planning & Creating a Database By Ms. Naira Microsoft Access.
DAY 21: MICROSOFT ACCESS – CHAPTER 5 MICROSOFT ACCESS – CHAPTER 6 MICROSOFT ACCESS – CHAPTER 7 Aliya Farheen October 29,2015.
Lesson 13 Databases Unit 2—Using the Computer. Computer Concepts BASICS - 22 Objectives Define the purpose and function of database software. Identify.
Constraints Lesson 8. Skills Matrix Constraints Domain Integrity: A domain refers to a column in a table. Domain integrity includes data types, rules,
Microsoft Access Database Creation and Management.
MSOffice Access Microsoft® Office 2010: Illustrated Introductory 1 Part 1 ® Database & Table.
Distribution of Marks For Second Semester Internal Sessional Evaluation External Evaluation Assignment /Project QuizzesClass Attendance Mid-Term Test Total.
QUERY CONSTRUCTION CS1100: Data, Databases, and Queries CS1100Microsoft Access1.
CSCI 3327 Visual Basic Chapter 13: Databases and LINQ UTPA – Fall 2011.
7 1 Database Systems: Design, Implementation, & Management, 7 th Edition, Rob & Coronel 7.6 Advanced Select Queries SQL provides useful functions that.
Introduction to Core Database Concepts Getting started with Databases and Structure Query Language (SQL)
MICROSOFT ACCESS – CHAPTER 5 MICROSOFT ACCESS – CHAPTER 6 MICROSOFT ACCESS – CHAPTER 7 Sravanthi Lakkimsety Mar 14,2016.
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.
LEC-8 SQL. Indexes The CREATE INDEX statement is used to create indexes in tables. Indexes allow the database application to find data fast; without reading.
CompSci 280 S Introduction to Software Development
How to: SQL By: Sam Loch.
Web Systems & Technologies
Prepared by : Moshira M. Ali CS490 Coordinator Arab Open University
Databases and Information Management
Structured Query Language (SQL) William Klingelsmith
Databases and Information Management
Introduction to Access
Presentation transcript:

Introduction to SQL Mike Burr

Rough Outline A Few Resources Differentiating Yourself Creating Tables and Keys Single Table Queries Multiple Table Queries Aggregation Appendices: – Tutorials for Access Create a table using the table designer Create a foreign key constraint using the relationships view Create a foreign key constraint using the relationships view Create tables and FK constraint using Access query Create tables and FK constraint using Access query Insert Data (Datasheet View) Insert Data (Query) Using Cartesian Products to generate test data Using Cartesian Products to generate test data Update Data (Datasheet View) Update Data (Query) Install NW Traders DB Create Query (Access Designer) Create Query (SQL) – Non-DB Tutorial (For your benefit, but not required) Create a data model with StarUML

A Few Resources Free Software: Access 2007 SQL Reference W3Schools SQL Tutorial/Reference Microsoft SQL Server T-SQL Reference SQL Server Books Online

Differentiating Yourself (Database Certifications) Oracle Microsoft – IT Professional Microsoft – Office Professional IBM Sybase (SAP) Many others exist inside and outside the database world…

Concept Review Processes give rise to data that needs to be stored and queried. Actors in the process define the data that needs to be collected and maintained (views). Each view has multiple entities, attributes, and relationships. All views are combined to form a single data model. Entities are transformed into database tables, attributes become columns, and relationships usually become foreign keys. Data is extracted from the database and presented in a meaningful format.

Introductory Example: Items/Orders Represents minimalistic view from order taker 4 tables, 15 columns, 3 foreign key constraints 1 attribute can be blank (null) for each customer row Goal: Create the tables using Access Designer and SQL Data Definition Language (DDL)

Data Type Selection Access 2007 Age:tinyint First Name:text/character Birthday:datetime Price:money Sale Time:datetime "Exact" Weight:float/decimal Image:image/binary Unique ID:AutoNumber/AutoIncrement

Customers Table Access SQL: CREATE TABLE Customers( ID AUTOINCREMENT, FirstName text NOT NULL, LastName text NOT NULL, Phone text, PRIMARY KEY(ID)); Access 2010:

Items Table Access SQL: CREATE TABLE Items( ID AUTOINCREMENT, ItemName text NOT NULL, Description text NOT NULL, PRIMARY KEY(ID)); Access 2010:

Orders Table Access SQL: CREATE TABLE PurchaseOrders( ID AUTOINCREMENT, OrderTime DateTime NOT NULL, CustomerId Integer NOT NULL, PRIMARY KEY(ID), FOREIGN KEY (CustomerId) REFERENCES Customers(ID)); Access 2010:

Line Items Table CREATE TABLE LineItems( ID AUTOINCREMENT, OrderId Integer NOT NULL, ItemId Integer NOT NULL, Quantity Integer NOT NULL, Price Money NOT NULL, PRIMARY KEY(ID), FOREIGN KEY(OrderId) REFERENCES PurchaseOrders(ID), FOREIGN KEY(ItemId) REFERENCES Items(ID)); Access 2010:

Final Models

Notes Access tables can be modified in the table designer view (see tutorial appendix) or by creating a query and using an alter table statement.alter table

Now: Querying Data We will be using the example databases provided by Microsoft – Access: Northwind Traders In the tutorials (not required): – SQL Server 2008: AdventureWorksAdventureWorks – Comparing AdventureWorks and Northwind Comparing AdventureWorks and Northwind

The Northwind Model

Customers Table Get Everything from the Customers table: Select * from Customers;

Get a few columns from Customers SQL: SELECT Customers.ID, Customers.[Last Name], Customers.[First Name] FROM Customers; Query Designer: Result Set:

Narrow Query to Certain Customers Get all customers with last name of ‘Bedecs’ SELECT Customers.ID, Customers.[Last Name], Customers.[First Name] FROM Customers WHERE Customers.[Last Name] = 'Bedecs'; Get all customers with last name starting with B SELECT Customers.ID, Customers.[Last Name], Customers.[First Name] FROM Customers WHERE Customers.[Last Name] like 'B*';

Operators in Where/Having Clauses OperatorMeaning =Equals <>Not equal <Less than <=Less than or equal to >Greater than >=Greater than or equal to in (...)Contained in a set of items likeWildcard match (…)Order of Operations andTrue if left side and right side are true orTrue if left side or right side are true notNegates condition

More Examples Using multiple conditions with and/or SELECT Customers.ID, Customers.[Last Name], Customers.[First Name] FROM Customers WHERE Customers.[Last Name] in ('Bedecs', 'Gratacos Solsona', 'Axen') and Customers.[First Name] in ('Thomas', 'Christina', 'Martin'); SELECT Customers.ID, Customers.[Last Name], Customers.[First Name] FROM Customers WHERE Customers.[Last Name] in ('Bedecs', 'Gratacos Solsona', 'Axen') or Customers.[First Name] in ('Thomas', 'Christina', 'Martin');

Order By Ascending: SELECT Customers.ID, Customers.[Last Name], Customers.[First Name] FROM Customers ORDER BY Customers.[Last Name] Descending: SELECT Customers.ID, Customers.[Last Name], Customers.[First Name] FROM Customers ORDER BY Customers.[Last Name] DESC

JoiningJoining Tables Look at the model: Want to use foreign keys to get all orders, line items, and products for customer “Anna Bedecs”

The Query Select Customers.[First Name], Customers.[Last Name], Orders.[Order Date], [Order Details].[Quantity], [Order Details].[Unit Price], [Order Details].[Quantity] * [Order Details].[Unit Price] as "Line Total", Products.[Product Name] FROM Customers INNER QUERY ( Orders INNER JOIN (Products INNER JOIN [Order Details] on ([Order Details].[Product ID] = Products.ID) ) on (Orders.[Order ID] = [Order Details].[Order ID]) ) on (Customers.ID = Orders.[Customer ID]) WHERE Customers.[First Name] = 'Anna' and Customers.[Last Name] = 'Bedecs';

A Cleaner Version (same result) Select Customers.[First Name], Customers.[Last Name], Orders.[Order Date], [Order Details].[Quantity], [Order Details].[Unit Price], [Order Details].[Quantity] * [Order Details].[Unit Price] as "Line Total", Products.[Product Name] FROM Customers, Orders, Products, [Order Details] WHERE Customers.ID = Orders.[Customer ID] and Orders.[Order ID] = [Order Details].[Order ID] and [Order Details].[Product ID] = Products.ID and Customers.[First Name] = 'Anna' and Customers.[Last Name] = 'Bedecs';

Counting Orders for Customers Select Orders.[Customer ID], count(Orders.[Order ID]) FROM Orders WHERE Customers.ID = Orders.[Customer ID] This query crashes and burns. We need another tool, aggregation with the group by clause.

Aggregation: Group By Group By: Orders per Customer SELECT Orders.[Customer ID], Count(Orders.[Order ID]) FROM Orders GROUP BY Orders.[Customer ID]; No Group By: Total Orders SELECT Count(Orders.[Order ID]) FROM Orders; Other Aggregating Functions: Microsoft Access Optional: Transact-SQL (SQL Server)

Aggregation: Having Group By: Orders per Customer SELECT Orders.[Customer ID], Count(Orders.[Order ID]) FROM Orders GROUP BY Orders.[Customer ID] HAVING Count(Orders.[Order ID]) > 5

Meaningful Data: Subqueries One possible use is to use aggregation to create a “table” to use in a join Select Customers.[First Name], Customers.[Last Name], counted.aggcount From Customers INNER JOIN (SELECT Orders.[Customer Id], Count(Orders.[Order ID]) as aggcount FROM Orders GROUP BY Orders.[Customer ID] HAVING Count(Orders.[Order ID]) > 5) as counted on (counted.[Customer ID] = Customers.ID)

Questions?

Appendix: Create Tables Using Table Designer Create a blank database and create a new table in design view

Add Fields and Create Primary Key Constraint

When Done, Save and Close Table

Appendix: Create Foreign Key Constraints using Access Designer After Creating the Tables, Open the Relationships View

Show Desired Tables

Drag Customer ID to PurchaseOrders CustomerId field

Done

Appendix: Create Tables and Foreign Key Constraints in Access 2010 SQL Create a blank database and create a new query:

Change to SQL View

Enter DDL for a Table and Run Query

Verify Table was Created

Create Other Tables and Constraints

Done

Appendix: Insert Data (Access Datasheet View) Open Desired Table

Add Desired Data and Save

Appendix: Insert Data (Access Query)

Change to SQL View and Create INSERT statement

Run the Query to Insert the Data

Appendix: Getting Test Data with Cartesian Products The Basics: – A cartesian product results from a select statement using 2 or more tables without a join condition. – This causes the RDBMS to return all of the combinations of the rows in the 2 (or more tables) – This can be combined with an INSERT INTO statement to populate test data for queries – I will be generating test first names and last names for the customers table

First Step I have created 2 tables with 1 column each (matching the data type on one of the columns the data type of the column that I want to populate with test data)

Verify the Cartesian product

Insert the Test Data

Done Test Data is ready to go, other Cartesian products can be used to populate the other tables Using existing tables in Cartesian products can be used to satisfy foreign key constraints Example, use a Cartesian product between the PurchaseOrders table and the Items table (in addition to a couple of others holding test data) to fill in the LineItems table

Appendix: Update Data (Datasheet View) Open desired Table and click fields to modify

Appendix: Update Data (SQL) Create a Query

Change to SQL View and Enter Query Run the Query:

Appendix: Install Northwind Traders

Appendix: Create a Query (Designer) Create a new query

Select Table(s)

Choose Field(s) and Specify Criteria

Run Query

Review Result

Appendix: Create a Query (SQL) Create a new query

Open SQL View

Enter Query and Execute

Review Result

Appendix: Creating an ERD with StarUML Download and install StarUML Download staruml-erd and extract to C:\Program Files (x86)\StarUML\modulesstaruml-erd

Launch StarUML

Create a New Project

Enable the ER Profile

Add a Model

Add an ER Diagram

Create Entities, Attributes, and Relationships

Modify Relationship Properties

Modify Attribute Properties