Download presentation
Presentation is loading. Please wait.
1
Introduction to SQL Wenhao Zhang October 5, 2018
2
What is a Database(DB)? Any collection of related information
Shopping list To do list Database can be stored in different ways On paper or in your mind On a computer (Spreedsheet) Computer is good at keeping track of large amounts of information
3
Database Management System(DBMS)
A special software program that helps users create and maintain a database(MySQL, Oracle, PostgreSQL, MariaDB, etc) Make it easy to manage large amounts of information Handles security Backups Importing/exporting data Concurrency Interacts with software apps(Programming language)
4
Types of Databases Relational Databases(SQL)
Organize data into one or more tables Each tables has columns and rows Non-Relational(noSQL/ not just SQL) Organize data is anything but a traditional table Graphs, Documents, etc
5
Structured Query Language(SQL)
SQL is a language used for interacting with Relational Database Management Systems Create, retrieve, update & delete (C.R.U.D) Create & manage databases Design & create database tables Perform administration tasks(security, import/export, user management, etc)
6
Structured Query Language(SQL)
Data Query Language (DQL) Query the database for information Data Definition Language (DDL) Define database schemas Data Control Language (DCL) Control access to the data in the database Data Manipulation Language (DML) Insert, update and delete data from the database
7
Tables in SQL Table name Attribute names Product PName Price Category
Manufacturer Gizmo $19.99 Gadgets GizmoWorks Powergizmo $29.99 SingleTouch $149.99 Photography Canon MultiTouch $203.99 Household Hitachi rows
8
Keys and Foreign Keys Company Key Product Foreign key CName StockPrice
Country GizmoWorks 25 USA Canon 65 Japan Hitachi 15 Key Product Foreign key PName Price Category Manufacturer Gizmo $19.99 Gadgets GizmoWorks Powergizmo $29.99 SingleTouch $149.99 Photography Canon MultiTouch $203.99 Household Hitachi
9
Tables Explained The schema of a table is the table name and its attributes: Product(PName, Price, Category, Manfacturer) A key is an attribute whose values are unique; we underline a key
10
Data Types in SQL SQL supports a large number of different formats
Numeric: INT, BIGINT, SMALLINT Character: CHAR(L) : a fixed length character of length L VARCHAR(L): a variable length character of length L Date: DATE(), TIME()
11
Important SQL commands
SELECT - extracts data from a database UPDATE - updates data in a database DELETE - delete data from a database INSERT INTO - insert new data into a database CREATE DATABASE - creates a new database ALTER DATABASE - modifies a database CREATE TABLE - creates a new table ALTER TABLE - modifies a table DROP TABLE - deletes a table CREATE INDEX - creates an index (search key) DROP INDEX - deletes an index
12
SQL Query Basic form: (plus many more bells and whistles)
SELECT <attributes> FROM <one or more relations> WHERE <conditions>
13
Simple SQL Query SELECT * FROM Product WHERE category=‘Gadgets’
PName Price Category Manufacturer Gizmo $19.99 Gadgets GizmoWorks Powergizmo $29.99 SingleTouch $149.99 Photography Canon MultiTouch $203.99 Household Hitachi SELECT * FROM Product WHERE category=‘Gadgets’ PName Price Category Manufacturer Gizmo $19.99 Gadgets GizmoWorks Powergizmo $29.99
14
Simple SQL Query Product PName Price Category Manufacturer Gizmo $19.99 Gadgets GizmoWorks Powergizmo $29.99 SingleTouch $149.99 Photography Canon MultiTouch $203.99 Household Hitachi SELECT PName, Price, Manufacturer FROM Product WHERE Price > 100 PName Price Manufacturer SingleTouch $149.99 Canon MultiTouch $203.99 Hitachi
15
Notation Input Schema Product(PName, Price, Category, Manfacturer) SELECT PName, Price, Manufacturer FROM Product WHERE Price > 100 Answer(PName, Price, Manfacturer) Output Schema
16
Eliminating Duplicates
Category Gadgets Photography Household SELECT DISTINCT category FROM Product Compare to: Category Gadgets Photography Household SELECT category FROM Product
17
Ordering the Results SELECT pname, price, manufacturer FROM Product
WHERE price > 100 ORDER BY pname Ordering is ascending, unless you specify the DESC keyword.
18
And Product (pname, price, category, manufacturer)
Company (cname, stockPrice, country) Find all products under $200 manufactured in Japan; return their names and prices. SELECT PName, Price FROM Product, Company WHERE Manufacturer=CName AND Country=‘Japan’ AND Price <= 200
19
And Product Company PName Price Category Manufacturer Gizmo $19.99 Gadgets GizmoWorks Powergizmo $29.99 SingleTouch $149.99 Photography Canon MultiTouch $203.99 Household Hitachi Cname StockPrice Country GizmoWorks 25 USA Canon 65 Japan Hitachi 15 SELECT PName, Price FROM Product, Company WHERE Manufacturer=CName AND Country=‘Japan’ AND Price <= 200 PName Price SingleTouch $149.99
20
Aggregation SELECT avg(price) FROM Product WHERE maker=“Toyota”
SELECT count(*) FROM Product WHERE year > 1995 SQL supports several aggregation operations: sum, count, min, max, avg Except count, all aggregations apply to a single attribute
21
Aggregation: Count COUNT applies to duplicates, unless otherwise stated: same as Count(*) SELECT Count(category) FROM Product WHERE year > 1995 We probably want: SELECT Count(DISTINCT category) FROM Product WHERE year > 1995
22
Simple Aggregations Purchase Product Date Price Quantity Bagel 10/21 1
20 Banana 10/3 0.5 10 10/10 10/25 1.50 SELECT Sum(price * quantity) FROM Purchase WHERE product = ‘bagel’ 50 (= 20+30)
23
Grouping and Aggregation
Purchase(product, date, price, quantity) Find total sales after 10/1/2005 per product. SELECT product, Sum(price*quantity) AS TotalSales FROM Purchase WHERE date > ‘10/1/2005’ GROUP BY product
24
1&2. FROM-WHERE-GROUPBY Product Date Price Quantity Bagel 10/21 1 20
10/25 1.50 Banana 10/3 0.5 10 10/10
25
3. SELECT Product TotalSales Bagel 50 Banana 15
Date Price Quantity Bagel 10/21 1 20 10/25 1.50 Banana 10/3 0.5 10 10/10 SELECT product, Sum(price*quantity) AS TotalSales FROM Purchase WHERE date > ‘10/1/2005’ GROUP BY product
26
HAVING Clause Same query, except that we consider only products that had at least 100 buyers. SELECT product, Sum(price * quantity) FROM Purchase WHERE date > ‘10/1/2005’ GROUP BY product HAVING Sum(quantity) > 30 HAVING clause contains conditions on aggregates.
27
NULLS in SQL Whenever we don’t have a value, we can put a NULL
Can mean many things: Value does not exists; Value exists but is unknown; Value not applicable, etc The schema specifies for each attribute if can be null (nullable attribute) or not SELECT PName, Price, Category FROM Product WHERE Category IS NULL
Similar presentations
© 2025 SlidePlayer.com. Inc.
All rights reserved.