1.264 Lecture 11. SQL: Basics, SELECT. Please start SQL Server before each class



Similar documents
1.204 Lecture 3. SQL: Basics, Joins SQL

1.264 Lecture 15. SQL transactions, security, indexes

Oracle Database 10g Express

1.264 Lecture 10. Data normalization

Exploring Microsoft Office Access Chapter 2: Relational Databases and Multi-Table Queries

1.264 Lecture 19 Web database: Forms and controls

SQL Simple Queries. Chapter 3.1 V3.0. Napier University Dr Gordon Russell

David Dye. Extract, Transform, Load

Creating QBE Queries in Microsoft SQL Server

Instant SQL Programming

D61830GC30. MySQL for Developers. Summary. Introduction. Prerequisites. At Course completion After completing this course, students will be able to:

SQL. Short introduction

CSC 443 Data Base Management Systems. Basic SQL

Database Query 1: SQL Basics

Oracle 10g PL/SQL Training

COMP 5138 Relational Database Management Systems. Week 5 : Basic SQL. Today s Agenda. Overview. Basic SQL Queries. Joins Queries

Using AND in a Query: Step 1: Open Query Design

Creating Database Tables in Microsoft SQL Server

A basic create statement for a simple student table would look like the following.

SQL Server for developers. murach's TRAINING & REFERENCE. Bryan Syverson. Mike Murach & Associates, Inc. Joel Murach

Setting Up ALERE with Client/Server Data

DIPLOMA IN WEBDEVELOPMENT

Using SQL Server Management Studio

How To Create A Table In Sql (Ahem)

Introduction to Databases

5. CHANGING STRUCTURE AND DATA

Introduction to Microsoft Jet SQL

Oracle Database 10g: Introduction to SQL

ATTACHMENT 6 SQL Server 2012 Programming Standards

Testing of the data access layer and the database itself

Foreign and Primary Keys in RDM Embedded SQL: Efficiently Implemented Using the Network Model

Netezza Workbench Documentation

Databases in Engineering / Lab-1 (MS-Access/SQL)

1. INTRODUCTION TO RDBMS

Using Multiple Operations. Implementing Table Operations Using Structured Query Language (SQL)

Structured Query Language. Telemark University College Department of Electrical Engineering, Information Technology and Cybernetics

Introduction to Microsoft Access 2010

Basic Concepts of Database Systems

Microsoft Office 2010

How to Backup Your Eclipse.Net Database Automatically. To clearly document a specific automatic SQL database backup method for Eclipse.net.

IENG2004 Industrial Database and Systems Design. Microsoft Access I. What is Microsoft Access? Architecture of Microsoft Access

Introduction to Microsoft Access 2013

Duration Vendor Audience 5 Days Oracle End Users, Developers, Technical Consultants and Support Staff

Access Tutorial 1 Creating a Database

Relational Databases. Christopher Simpkins

1 File Processing Systems

ICAB4136B Use structured query language to create database structures and manipulate data

14 Triggers / Embedded SQL

DISTRIBUTED DATABASES MANAGEMENT USING REMOTE ACCESS METHOD

ITS410: Database Management

Course 20464: Developing Microsoft SQL Server Databases

Course 20461C: Querying Microsoft SQL Server Duration: 35 hours

Data Transfer Tips and Techniques

Talking to Databases: SQL for Designers

Beginning C# 5.0. Databases. Vidya Vrat Agarwal. Second Edition

Deleting A Record Updating the Database Binding Data Tables to Controls Binding the Data Table to the Data Grid View...

- Eliminating redundant data - Ensuring data dependencies makes sense. ie:- data is stored logically

Database Programming with PL/SQL: Learning Objectives

Relational Database: Additional Operations on Relations; SQL

Developing Web Applications for Microsoft SQL Server Databases - What you need to know

David M. Kroenke and David J. Auer Database Processing: Fundamentals, Design and Implementation

BCA. Database Management System

Creating tables in Microsoft Access 2007

Oracle Database: SQL and PL/SQL Fundamentals

SQL - QUICK GUIDE. Allows users to access data in relational database management systems.

Developing Microsoft SQL Server Databases 20464C; 5 Days

A Brief Introduction to MySQL

4. The Third Stage In Designing A Database Is When We Analyze Our Tables More Closely And Create A Between Tables

Introduction to SQL ( )

Creating a Participants Mailing and/or Contact List:

2874CD1EssentialSQL.qxd 6/25/01 3:06 PM Page 1 Essential SQL Copyright 2001 SYBEX, Inc., Alameda, CA

Access Tutorial 2 Building a Database and Defining Table Relationships

There are five fields or columns, with names and types as shown above.

T-SQL STANDARD ELEMENTS

Information Systems SQL. Nikolaj Popov

Basic SQL Server operations

Teach Yourself InterBase

20464C: Developing Microsoft SQL Server Databases

Chapter 9 Java and SQL. Wang Yang wyang@njnet.edu.cn

Lab 2: PostgreSQL Tutorial II: Command Line

Data Tool Platform SQL Development Tools

SQL Databases Course. by Applied Technology Research Center. This course provides training for MySQL, Oracle, SQL Server and PostgreSQL databases.

Utility Software II lab 1 Jacek Wiślicki, jacenty@kis.p.lodz.pl original material by Hubert Kołodziejski

Oracle Database: SQL and PL/SQL Fundamentals NEW

CSE 530A Database Management Systems. Introduction. Washington University Fall 2013

TIM 50 - Business Information Systems

Access Queries (Office 2003)

Databases What the Specification Says

Course ID#: W 35 Hrs. Course Content

Chapter 13. Introduction to SQL Programming Techniques. Database Programming: Techniques and Issues. SQL Programming. Database applications

SQL DATA DEFINITION: KEY CONSTRAINTS. CS121: Introduction to Relational Database Systems Fall 2015 Lecture 7

MOC 20461C: Querying Microsoft SQL Server. Course Overview

2. Oracle SQL*PLUS Winter Some SQL Commands. To connect to a CS server, do:

Microsoft Access Lesson 5: Structured Query Language (SQL)

Introduction to Computing. Lectured by: Dr. Pham Tran Vu

Developing Microsoft SQL Server Databases

MySQL for Beginners Ed 3

Transcription:

1.264 Lecture 11 SQL: Basics, SELECT Please start SQL Server before each class Download Lecture11CreateDB.sql from Web site; open it in SQL Svr Mgt Studio This class: Upload your.sql file from the exercises after class Next class: Murach chapter 7. Exercises due after class Reminder: Solutions to exercises are posted in the evening after each class 1

Building a database When building a database by loading data based on a data model: It may build well, which usually means you found the real business rules It may build with some errors, which usually means you have the real business rules but the data is sloppy It may build with many errors, which usually means that you were told the business rules people wish to have or think they have, not the ones they actually use It s often useful to get some sample data and browse it while building the data model 2

SQL Structured query language (SQL) used for Data definition (DDL): tables and views (virtual tables). These are the basic operations to convert a data model to a database Data manipulation (DML): user or program can INSERT, DELETE, UPDATE or retrieve (SELECT) data. Data integrity: referential integrity and transactions. Enforces keys (primary and foreign) Access control: security Data sharing: by concurrent users Not a complete language like Java, Visual Basic or C++ SQL is sub-language of about 30 statements Embedded in another language or tool for database access SQL has several inconsistencies; NULLs are problematic Portable across operating systems and somewhat among vendors Declarative language, not procedural 3

Things that vary among SQL implementations Error codes Data types supported (dates/times, currency, string/text variations) System tables, about the structure of the database itself Interactive SQL Programming interface: no vendor follows the standard Dynamic SQL, used for report writers and query tools Implementer-defined variations within the standard Database initialization, opening and connection Whether case matters (upper, lower case) 4

SQL SELECT SELECT constructed of clauses to get columns and rows from one or more tables or views. Clauses must be in order: SELECT columns/attributes INTO new table FROM table or view WHERE specific rows or a join is created GROUP BY grouping conditions (columns) HAVING group-property (specific rows) ORDER BY ordering criterion ASC DESC 5

Example tables Orders Customers OrderNbr Cust Prod Qty Amt Disc 1 211 Bulldozer 7 $31,000.00 0.2 2 522 Riveter 2 $4,000.00 0.3 3 522 Crane 1 $500,000.00 0.4 CustNbr Company CustRep CreditLimit 211 Connor Co 89 $50,000.00 522 AmaratungaEnterprises 89 $40,000.00 890 Feni Fabricators 53 $1,000,000.00 SalesReps Offices RepNbr Name RepOffice Quota Sales 53 Bill Smith 1 $100,000.00 $0.00 89 Jen Jones 2 $50,000.00 $130,000.00 OfficeNbr City State Region Target Sales Phone 1 Denver CO West $3,000,000.00 $130,000.00 970.586.3341 2 New York NY East $200,000.00 $300,000.00 212.942.5574 57 Dallas TX West $0.00 $0.00 214.781.5342 6

Example data model Offices SalesReps Customers Orders OfficeNbr City Region Target State RepNbr Name Quota Sales RepOffice CustNbr Company CreditLimit CustRep OrderNbr Prod Qty Amt Disc Phone Cust Image by MIT OpenCourseWare. Columns with the same meaning usually have the same name in different tables. We give them different names here so we don t have to say tablename.columnname, e.g. Orders.OrderNbr 7

Using SQL Server and Management Studio Your SQL Server database engine should start by default when your system starts Start SQL Server Management Studio (SSMS) from Start->Programs->MS SQL Server 2012 Open Lecture11CreateDB.sql with SSMS in Windows Explorer Download the.sql file from the web site first Select Execute from toolbar Database MIT1264 will be created and data inserted for exercises during this class Review Lecture11CreateDB.sql Creates tables, relationships, keys, inserts data IDENTITY means auto-number 8

Exercise 1 SQL queries: SELECT Click New Query in SSMS; type these statements: List the sales reps SELECT Name, Sales, Quota FROM SalesReps Find the amount each rep is over or under quota SELECT Name, Sales, Quota, (Sales-Quota) FROM SalesReps Find the slackers SELECT Name, Sales, Quota, (Sales-Quota) FROM SalesReps WHERE Sales < Quota RepNbr Name RepOffice Quota Sales 53 Bill Smith 1 $100,000.00 $0.00 89 Jen Jones 2 $50,000.00 $130,000.00 9

Exercise 2 SQL queries: insert, delete, update Find the average sale SELECT AVG(Amt) FROM Orders; Find the average sale for a customer SELECT AVG(Amt) FROM Orders WHERE Cust = 211; Add an office INSERT INTO Offices (OfficeNbr, City, State, Region, Target, Sales, Phone) VALUES ( 55, Dallas, TX, West, 200000, 0, 214.333.2222 ); Delete a customer DELETE FROM Customers WHERE Company = Connor Co ; (Syntax is valid but command will fail due to referential integrity) Exercise: Create (insert) a new order and delete it Omit OrderNbr in INSERT because it s auto-generated Raise a credit limit UPDATE Customers SET CreditLimit = 75000 WHERE Company = Amaratunga 10

Exercise 3: SELECT * and duplicates Select all columns (fields) SELECT * FROM Offices; Duplicate rows: query will get two instances of West SELECT Region FROM Offices; Select, but eliminate duplicates: SELECT DISTINCT Region FROM Offices; Exercise: Find the greatest positive difference between sales and target 11

MIT OpenCourseWare http://ocw.mit.edu 1.264J / ESD.264J Database, Internet, and Systems Integration Technologies Fall 2013 For information about citing these materials or our Terms of Use, visit: http://ocw.mit.edu/terms.