Presentation is loading. Please wait.

Presentation is loading. Please wait.

How to: SQL By: Sam Loch.

Similar presentations


Presentation on theme: "How to: SQL By: Sam Loch."— Presentation transcript:

1 How to: SQL By: Sam Loch

2 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 Phone Address

3 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

4 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

5 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.

6 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

7 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 ( ) 6.5, , 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 ' :43:00', ' ' BOOLEAN Boolean value 0,1

8 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.

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

10 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);

11 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

12 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, , Phone, Address) VALUES (2003, ‘Kit’, ‘Fisto’, .com’, , ‘560 Lois Lane Pittsburgh, PA’) CustomerID FirstName LastName Phone Address 1999 Johnny Bravo 1624 N. 18th Street Philadelphia, PA 2000 Dwight Shrute 12 Farm Street Scranton, PA 2001 Patches O‘Houlihan 34 Legend Lane Lancaster, PA 2002 Eric Foreman 1919 S. 19th Street Philadelphia, PA 2003 Kit Fisto 560 Lois Lane Pittsburgh, PA

13 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

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

15 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;

16 SELECT statements CustomerID FirstName LastName Phone Address 1999 Johnny Bravo 1624 N. 18th Street Philadelphia, PA 2000 Dwight Shrute 12 Farm Street Scranton, PA 2001 Patches O‘Houlihan 34 Legend Lane Lancaster, PA 2002 Eric Foreman 1919 S. 19th Street Philadelphia, PA 2003 Kit Fisto 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

17 Retrieving All Columns in a Table
SELECT * FROM salesdb.Customer CustomerID FirstName LastName Phone Address 1999 Johnny Bravo 1624 N. 18th Street Philadelphia, PA 2000 Dwight Shrute 12 Farm Street Scranton, PA 2001 Patches O‘Houlihan 34 Legend Lane Lancaster, PA 2002 Eric Foreman 1919 S. 19th Street Philadelphia, PA 2003 Kit Fisto 560 Lois Lane Pittsburgh, PA

18 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;

19 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

20 Sorting Results of a Select Statement
SELECT * FROM salesdb.Customer WHERE CustomerID >= 2000 ORDER BY FirstName; CustomerID FirstName LastName Phone Address 2000 Dwight Shrute 12 Farm Street Scranton, PA 2002 Eric Foreman 1919 S. 19th Street Philadelphia, PA 2003 Kit Fisto 560 Lois Lane Pittsburgh, PA 2001 Patches O‘Houlihan 34 Legend Lane Lancaster, PA

21 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

22 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

23 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

24 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 Phone Address OrderID OrderDate Order.CustomerID 2000 Dwight Shrute 12 Farm Street Scranton, PA 102 1002 2002 Eric Foreman 1919 S. 19th Street Philadelphia, PA 103 1003 2003 Kit Fisto 560 Lois Lane Pittsburgh, PA 104 1004 2001 Patches O‘Houlihan 34 Legend Lane Lancaster, PA 105 1005

25 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

26 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 ( ) 6.5, , 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 ' :43:00', ' ' BOOLEAN Boolean value 0,1

27 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


Download ppt "How to: SQL By: Sam Loch."

Similar presentations


Ads by Google