How to: SQL By: Sam Loch.

Slides:



Advertisements
Similar presentations
Structured Query Language (SQL)
Advertisements

Copyright © by Royal Institute of Information Technology Introduction To Structured Query Language (SQL) 1.
1 SQL-Structured Query Language SQL is the most common language used for creating and querying relational databases. Many users can access a database applications.
Structured Query Language Part I Chapter Three CIS 218.
Concepts of Database Management Sixth Edition
SQL Tutorials To understand some of the topics please analyze the following tutorials: The following tutorials will help:
DATABASES AND SQL. Introduction Relation: Relation means table(data is arranged in rows and columns) Domain : A domain is a pool of values appearing in.
INFORMATION TECHNOLOGY IN BUSINESS AND SOCIETY SESSION 16 – SQL SEAN J. TAYLOR.
Chapter 5 Introduction to SQL. Structured Query Language = the “programming language” for relational databases SQL is a nonprocedural language = the user.
Relational DBs and SQL Designing Your Web Database (Ch. 8) → Creating and Working with a MySQL Database (Ch. 9, 10) 1.
 SQL stands for Structured Query Language.  SQL lets you access and manipulate databases.  SQL is an ANSI (American National Standards Institute) standard.
HAP 709 – Healthcare Databases SQL Data Manipulation Language (DML) Updated Fall, 2009.
15/10/20151 PHP & MySQL 'Slide materials are based on W3Schools PHP tutorial, 'PHP website 'MySQL website.
SQL 1: GETTING INFORMATION OUT OF A DATABASE MIS2502 Data Analytics.
SQL SQL Server : Overview SQL : Overview Types of SQL Database : Creation Tables : Creation & Manipulation Data : Creation & Manipulation Data : Retrieving.
Using Special Operators (LIKE and IN)
Concepts of Database Management Seventh Edition
1 Structured Query Language (SQL). 2 Contents SQL – I SQL – II SQL – III SQL – IV.
MIS2502: Data Analytics SQL – Getting Information Out of a Database David Schuff
Topic 1: Introduction to SQL. SQL stands for Structured Query Language. SQL is a standard computer language for accessing and manipulating databases SQL.
Tables and Constraints Oracle PL/SQL. Datatypes The SQL Data Definition Language Commands (or DDL) enable us to create, modify and remove database data.
IMS 4212: Data Manipulation 1 Dr. Lawrence West, MIS Dept., University of Central Florida Additional Data Manipulation Statements INSERT.
Concepts of Database Management Seventh Edition Chapter 3 The Relational Model 2: SQL.
ITEC 3220A Using and Designing Database Systems Instructor: Prof. Z. Yang Course Website: 3220a.htm
MIS2502: Data Analytics SQL – Putting Information Into a Database David Schuff
>> Introduction to MySQL. Introduction Structured Query Language (SQL) – Standard Database Language – Manage Data in a DBMS (Database Management System)
SQL SELECT Getting Data from the Database. Basic Format SELECT, FROM WHERE (=, >, LIKE, IN) ORDER BY ; SELECT LastName, FirstName, Phone, City FROM Customer.
IS6146 Databases for Management Information Systems Lecture 4: SQL IV – SQL Functions and Procedures Rob Gleasure robgleasure.com.
MIS2502: Data Analytics SQL – Getting Information Out of a Database.
Introduction to Database SEM I, AY Department of Information Technology Salalah College of Technology Chapter No.3 SQL.
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.
Concepts of Database Management, Fifth Edition Chapter 3: The Relational Model 2: SQL.
COM621: Advanced Interactive Web Development Lecture 11 MySQL – Data Manipulation Language.
1 ORACLE I 3 – SQL 1 Salim Phone: YM: talim_bansal.
Standard language for querying and manipulating data Structured Query Language Many standards out there: ANSI SQL, SQL92 (a.k.a. SQL2), SQL99 (a.k.a. SQL3),
SQL SQL Ayshah I. Almugahwi Maryam J. Alkhalifa
Order Database – ER Diagram
Web Systems & Technologies
SQL Query Getting to the data ……..
Structured Query Language
Chapter 5 Introduction to SQL.
Insert, Update and the rest…
Prepared by : Moshira M. Ali CS490 Coordinator Arab Open University
MIS2502: Data Analytics SQL – Putting Information Into a Database
MIS2502: Data Analytics SQL – Putting Information Into a Database
SQL FUNDAMENTALS CDSE Days 2018.
MENAMPILKAN DATA DARI SATU TABEL (Chap 2)
MIS2502: Data Analytics SQL – Getting Information Out of a Database
MIS2502: Data Analytics SQL – Putting Information Into a Database
Chapter # 7 Introduction to Structured Query Language (SQL) Part II.
MIS2502: Review for Exam 1 JaeHwuen Jung
MIS2502: Data Analytics SQL – Getting Information Out of a Database
Introduction To Structured Query Language (SQL)
Copyright © 2012 Pearson Education, Inc. Publishing as Prentice Hall
Database systems Lecture 3 – SQL + CRUD
Access: SQL Participation Project
Exam 2 Exam 2 Study Guide is posted on the Course Site
MIS2502: Data Analytics SQL – Putting Information Into a Database
MIS2502: Review for Exam 1 Aaron Zhi Cheng
MIS2502: Data Analytics SQL – Putting Information Into a Database
Lesson Plan Instructional Objective Learning Objective
Structured Query Language – The Fundamentals
MIS2502: Data Analytics SQL – Putting Information Into a Database
Introduction To Structured Query Language (SQL)
Section 4 - Sorting/Functions
MIS2502: Data Analytics SQL 4– Putting Information Into a Database
The University of Akron College of Applied Science & Technology Dept
MIS2502: Data Analytics SQL – Getting Information Out of a Database Part 1: Basic Queries Aaron Zhi Cheng
MIS2502: Data Analytics SQL – Getting Information Out of a Database Part 2: Advanced Queries Zhe (Joe) Deng
Presentation transcript:

How to: SQL By: Sam Loch

Goal To retrieve data from and insert data into a relational database Product ProductID ProductName Price InventoryLevel Order-Product OrderProductID OrderID ProductID Quantity Order OrderID CustomerID OrderDate ShippingInstructions Customer CustomerID FirstName LastName Email Phone Address

How to Accomplish Our Goal Structured Query Language (SQL) Using SQL we can Retrieve data from a database Insert data into a database Update data in a database Delete data in a database Add and delete tables form a database Combine tables in a database

Creating a Database Define the database Create tables Define columns within these tables and choose their data types Each table needs a Primary Key Some tables will need Foreign Key’s to communicate with other tables in the database

Creating a Database (2) CREATE statements Text Description These can be used to create a table within a database Example: CREATE TABLE db_name.table_name ( ColumnName1 datatype NOT NULL, ColumnName2 datatype NULL, PRIMARY KEY (KeyName)); Text Description NULL/NOT NULL Defines whether the field can be empty (NULL) or whether it cannot (NOT NULL) Primary Key’s cannot be NULL datatype The data-type of the column.

Creating a Database (3) Order OrderID CustomerID OrderDate Another Example: CREATE TABLE salesdb.Order ( OrderID INT NOT NULL, CustomerID INT NOT NULL, OrderDate DATE NULL, ShippingInstructions VARCHAR(45) NULL, PRIMARY KEY (OrderID)); Order OrderID CustomerID OrderDate ShippingInstructions

Data Types Each column can contain a different type of data The data type of each column must be specified when it is created A few data types: Data type Description Examples INT Integer 4, 62, -20 DECIMAL(p,s) Decimal. Example: decimal(6,3) is a number that has 3 digits after the decimal and 6 digits total (123.456) 6.5, 0.9987, 123.456 VARCHAR(x) String of any characters with a maximum length of x ‘Howdy’, ‘She sells seashells by the sea shore’ DATETIME, DATE Date and time, or just Date '2017-05-03 20:43:00', '2017-05-03' BOOLEAN Boolean value 0,1

Foreign Keys Product ProductID ProductName Price InventoryLevel The Foreign Key in one table connects that table to a Primary key in another table. CREATE TABLE salesdb.`Order-Product` ( OrderProductID INT NOT NULL, OrderID INT NULL, ProductID INT NULL, Quantity INT NULL, PRIMARY KEY (OrderProductID), FOREIGN KEY (OrderID) REFERENCES salesdb.Order(OrderID) FOREIGN KEY (ProductID) REFERENCES salesdb.Product(ProductID) Product ProductID ProductName Price InventoryLevel Order-Product OrderProductID OrderID ProductID Quantity ProductID is a foreign key in the Order-Product table, and the primary key in the Product table.

Deleting Tables DROP TABLE db_name.table_name; EX: DROP TABLE salesdb.Product;

Altering the Data in a Table Adding a column: ALTER TABLE db_name.table_name ADD COLUMN column_name datatype (NULL or NOT NULL); Deleting a column: DROP COLUMN column_name; Changing a column: CHANGE COLUMN old_column_name new_column_name datatype (NULL or NOT NULL);

Altering the Data in a Table (2) Examples: ALTER TABLE salesdb.Product ADD COLUMN Color VARCHAR(10) NULL; DROP COLUMN Color; CHANGE COLUMN Color Version VARCHAR(20) NULL; Adds “Color” column to Product table Deletes “Color” column to Product table Changes “Color” column from Product table to “Version” and changes character limit from 10 to 20

Adding a Row to a Table INSERT INTO db_name.table_name (ColumnName1, ColumnName2, ColumnName3) VALUES (Value1, Value2, Value3); Example: INSERT INTO salesdb.Customer (CustomerID, FirstName, LastName, Email, Phone, Address) VALUES (2003, ‘Kit’, ‘Fisto’, ‘KFisto@Gmail .com’, 717.555.1234, ‘560 Lois Lane Pittsburgh, PA’) CustomerID FirstName LastName Email Phone Address 1999 Johnny Bravo Jbravo@gmail.com 215.555.5678 1624 N. 18th Street Philadelphia, PA 2000 Dwight Shrute BeetBoi@Hotmail.com 717.555.5555 12 Farm Street Scranton, PA 2001 Patches O‘Houlihan Dballislife@gmail.com 717.555.0000 34 Legend Lane Lancaster, PA 2002 Eric Foreman Eforeman@yahoo.com 215.555.9876 1919 S. 19th Street Philadelphia, PA 2003 Kit Fisto Kfisto@gmail.com 717.555.1234 560 Lois Lane Pittsburgh, PA

The Price of the Shake Weight was changed to 99.99 Changing a Row UPDATE db_name.table_name SET ColumnName1 = Value1, ColumnName2 = Value2 WHERE condition; Example: UPDATE salesdb.Product SET ProductName = ‘Shake Weight’, Price = 99.99 WHERE ProductID = 1341; ProductID ProductName Price 1341 Shake Weight 99.99 1342 The Claw 29.99 1343 SHAM WOW 4.99 The Price of the Shake Weight was changed to 99.99

Deleting a Row DELETE FROM db_name.table_name WHERE condition; Example: DELETE FROM salesdb.Product WHERE ProductID = 1738;

Retrieving Data From a Database SELECT statements SELECT statements are used to retrieve data from a database EX: SELECT column_name(s) FROM db_name.table_name;

SELECT statements CustomerID FirstName LastName Email Phone Address 1999 Johnny Bravo Jbravo@gmail.com 215.555.5678 1624 N. 18th Street Philadelphia, PA 2000 Dwight Shrute BeetBoi@Hotmail.com 717.555.5555 12 Farm Street Scranton, PA 2001 Patches O‘Houlihan Dballislife@gmail.com 717.555.0000 34 Legend Lane Lancaster, PA 2002 Eric Foreman Eforeman@yahoo.com 215.555.9876 1919 S. 19th Street Philadelphia, PA 2003 Kit Fisto Kfisto@gmail.com 717.555.1234 560 Lois Lane Pittsburgh, PA If you had a customer table and wanted to see the first and last names of all of the customers you would type: SELECT FirstName, LastName FROM salesdb.Customer This would return: FirstName LastName Johnny Bravo Dwight Shrute Patches O‘Houlihan Eric Foreman Kit Fisto

Retrieving All Columns in a Table SELECT * FROM salesdb.Customer CustomerID FirstName LastName Email Phone Address 1999 Johnny Bravo Jbravo@gmail.com 215.555.5678 1624 N. 18th Street Philadelphia, PA 2000 Dwight Shrute BeetBoi@Hotmail.com 717.555.5555 12 Farm Street Scranton, PA 2001 Patches O‘Houlihan Dballislife@gmail.com 717.555.0000 34 Legend Lane Lancaster, PA 2002 Eric Foreman Eforeman@yahoo.com 215.555.9876 1919 S. 19th Street Philadelphia, PA 2003 Kit Fisto Kfisto@gmail.com 717.555.1234 560 Lois Lane Pittsburgh, PA

Retrieving Values To retrieve only unique values: SELECT DISTINCT Price FROM salesdb.Product; To retrieve certain records only SELECT * FROM salesdb.Product WHERE Price = 99.99; SELECT * FROM salesdb.Product WHERE Price > 5;

WHERE Clause Operator Description = Equal to > Greater than >= The following is a list of operators that can be used in the WHERE Clause: Operator Description = Equal to > Greater than >= Greater than or equal to < Less than <= Less than or equal to <> Not equal to

Sorting Results of a Select Statement SELECT * FROM salesdb.Customer WHERE CustomerID >= 2000 ORDER BY FirstName; CustomerID FirstName LastName Email Phone Address 2000 Dwight Shrute BeetBoi@Hotmail.com 717.555.5555 12 Farm Street Scranton, PA 2002 Eric Foreman Eforeman@yahoo.com 215.555.9876 1919 S. 19th Street Philadelphia, PA 2003 Kit Fisto Kfisto@gmail.com 717.555.1234 560 Lois Lane Pittsburgh, PA 2001 Patches O‘Houlihan Dballislife@gmail.com 717.555.0000 34 Legend Lane Lancaster, PA

ORDER BY Ascending (ASC) and Descending (DESC) SELECT * FROM salesdb.Product ORDER BY Price ASC; ORDER BY Price DESC; ProductID ProductName Price 1343 Shake Weight 4.99 1342 The Claw 29.99 1341 SHAM WOW 99.99 ProductID ProductName Price 1341 Shake Weight 99.99 1342 The Claw 29.99 1343 SHAM WOW 4.99

Other SQL Functions COUNT() – Returns the number of rows MAX() – Returns the largest value MIN() – Returns the smallest value SUM() – Returns the sum AVG() – Returns the average value

Fun With Functions ProductID ProductName Price 1341 Shake Weight 99.99 1342 The Claw 29.99 1343 SHAM WOW 4.99 Price 99.99 SELECT MAX(Price) FROM salesdb.Product; SELECT COUNT(ProductID) FROM salesdb.Product; SELECT MIN(Price) FROM salesdb.Product; SELECT SUM(Price) FROM salesdb.Product; SELECT AVG(Price) FROM salesdb.Product; 3 Price 4.99 Price 134.97 Price 44.99

SELECTing From Multiple Tables To SELECT from multiple tables you must join the tables with WHERE Example: SELECT * FROM salesdb.Customer, salesdb.Order WHERE Customer.CustomerID = Order.CustomerID; This Returns: Customer.CustomerID FirstName LastName Email Phone Address OrderID OrderDate Order.CustomerID 2000 Dwight Shrute BeetBoi@Hotmail.com 717.555.5555 12 Farm Street Scranton, PA 102 2017-4-15 1002 2002 Eric Foreman Eforeman@yahoo.com 215.555.9876 1919 S. 19th Street Philadelphia, PA 103 2017-4-25 1003 2003 Kit Fisto Kfisto@gmail.com 717.555.1234 560 Lois Lane Pittsburgh, PA 104 2017-4-30 1004 2001 Patches O‘Houlihan Dballislife@gmail.com 717.555.0000 34 Legend Lane Lancaster, PA 105 2017-5-3 1005

JOIN Syntax SELECT * Return all the columns from both tables SELECT * FROM salesdb.Customer, salesdb.Order WHERE Customer.CustomerID = Order.CustomerID; SELECT * Return all the columns from both tables FROM salesdb.Customer, salesdb.Order The two tables to be joined WHERE Customer.CustomerID = Order.CustomerID Only choose records where the CustomerID exists in both tables

Summary Text Description NULL/NOT NULL Defines whether the field can be empty (NULL) or whether it cannot (NOT NULL) Primary Key’s cannot be NULL datatype The data-type of the column. Use CREATE statements to create a table Use DROP TABLE to delete a table Use ALTER TABLE to change the data in a table ADD COLUMN DROP COLUMN CHANGE COLUMN Use INSERT INTO to add a row Use UPDATE and SET to change a row Use DELETE FROM to delete a row Data type Description Examples INT Integer 4, 62, -20 DECIMAL(p,s) Decimal. Example: decimal(6,3) is a number that has 3 digits after the decimal and 6 digits total (123.456) 6.5, 0.9987, 123.456 VARCHAR(x) String of any characters with a maximum length of x ‘Howdy’, ‘She sells seashells by the sea shore’ DATETIME, DATE Date and time, or just Date '2017-05-03 20:43:00', '2017-05-03' BOOLEAN Boolean value 0,1

Summary(2) Use SELECT statements to retrieve data from a database SELECT * selects all data from COUNT() – Returns the number of rows MAX() – Returns the largest value MIN() – Returns the smallest value SUM() – Returns the sum AVG() – Returns the average value