STRUCTURED QUERY LANGUAGE (SQL)
FERRY KEMPERMAN
NANJING FOREIGN LANGUAGE SCHOOL
NOV 2019
CAN YOU TELL ME…….
✓ What the name is of this diagram?
✓ What the official name is for rectangles, lines and symbols used on the lines?
✓ What is the purpose of this diagram?
✓ Who designs this diagram in reality?
✓ In how many warehouses a specific product can be stored?
✓ How many orders a customer can actually place?
✓ If a customer calls with a complaint about an order, can you track the responsible sales rep?
✓ There is something wrong with this design. What? What would the solution be?
TODAY WE ARE GOING TO LEARN…..
➢How to ask questions and invoke tasks to the database using Structured Query Language
(SQL)
➢How to retrieve data from the database by writing a SELECT statement
➢How to manipulate data (add,modify,delete) using in a database using DML-statements
➢How to create a database and tables with keys using DDL-statements
DATABASE CLIENT / SERVER ARCHITECTURE
❑ Database servers are centralized in a LAN
❑ Clients send their request to the server
❑ Server responds by sending selected data
back to client
General procedure
1. Request: Sales rep wants a list of all orders of customer Xu
2. Database server (DBMS) receives request from client
3. Database server prioritizes, validates and queues request
4. Database server collects selected data from database
5. Database server sends selected data back to client
A request to the database is called a query.
The selected data returned to client is called a result set.
STRUCTURED QUERY LANGUAGE (SQL)
• SQL is the industry wide language in which queries to the database are written. It
was developed way back in the 1970-ies by IBM and adopted by the entire IT-
community.
• SQL is pronounced as Sequel, no S-Q-L
• SQL is a retrieval language, not a programming language!
• SQL has a fixed syntax, so it works on every database!
• SQL has three parts with different goals:
• SQL – DML : Part of the language that allows users to select and modify data
• SQL – DDL : Part of the language that allows designers to create/alter a database
and tables with keys
• SQL – DCL : Part of the language that allow DBA-ers to set permissions for users
to access certain parts of the database
LET’S WRITE A QUERY IN SQL!
A query to request data from a
database is called a SELECT
statement. A few examples…..
SELECT *
FROM tblCustomer
SELECT CustomerName, CustomerPhone
FROM tblCustomer
SELECT CustomerName
FROM tblCustomer
WHERE CustomerId = 24
Result set:
CustomerName
Brown
Result set:
CustomerID CustomerName CustomerPhone
23 Ronchi 987654
24 Brown 017288
25 William 837392
26 Dev 792749
Result set:
CustomerID CustomerName CustomerPhone
23 Ronchi 987654
24 Brown 017288
25 William 837392
26 Dev 792749
Break activity 1:
Design a query that shows all the products that were ordered.
✓ Discuss for 3 minutes with partner
✓ Write your query on the mini whiteboard
SELECT CustomerPhone
FROM tblCustomer
WHERE CustomerName = “William” OR CustomerName = “Ronchi”
ORDER BY CustomerPhone
Result set:
CustomerPhone
837392
987654
Is this result set useful for the end-user?
SELECT <attributes you want to see>
FROM <source tables>
WHERE <booleans to select records>
ORDER BY <attributes you want to sort by>
Break activity 2:
Design a query that lists only the productnames alphabetically that were
ordered by customer 23.
✓ Discuss for 3 minutes with partner
✓ Write your query on the mini whiteboard
BUT SOMETHING IS WRONG….
• List all the products ordered by
customer 23?
• Customer need to be referred to by
their name, not id!
• The request should be:
• List all products ordered by Customer
Ronchi!
• But….how to do this?
• CustomerName is not in the same table
as OrderDescription.
• This query wants to filter on
CustomerName, but wants to display
OrderDescription.
• This query requires two tables instead
of one!
• We use an INNER JOIN to do this.
SELECT O.OrderDesc
FROM tblCustomer C INNER JOIN tblOrder ON C.CustomerID=O.CustomerID
WHERE C.CustomerName = “Ronchi”
ORDER BY O.OrderDesc
Break activity 3:
Design a query that lists all the product descriptions alphabetically ordered
by customer William and Brown!
✓ Discuss for 3 minutes with partner
✓ Write your query on the mini whiteboard
DATA MANIPULATION LANGUAGE (DML)
• DML is part of SQL that include statements that can manipulate data:
• INSERT statement to insert new data into a table
• UPDATE statement to modify existing data in a table
• DELETE statement to delete existing record(s) from a table
INSERT INTO Customer (CustID,CustName,CustPhone) UPDATE Customer DELETE FROM Customer
VALUES (1,”Kemperman”,”0018013”), SET CustName = “Smith” WHERE CustName = ‘Smith’
(2,”Zhang”,”123483”) WHERE CustID = 1
DATA DEFINITION LANGUAGE (DDL)
• Data definition language (DDL) is a part of SQL that is used by database designers. With
DDL you can
• Create a database
• Create new tables in a database
• Alter a table in a database
• Dropping (deleting) tables in a database
• Add primary and foreign keys to tables
• Database users do NOT have the permissions to use DDL, only designers have.
• Why? Deleting a customer table by accident?  Really?!
• Database users need to able to access the DATA only, not the DATABASE itself!
DDL STATEMENTS, AN EXAMPLE.
BUSINESS CASE : WRITING QUERIES
• Sample database (ERD – Populated tables)
• Write 10 queries based on the sample database
• Groups 2 of two students. Choose yourself.
• Write a report with the queries in SQL.
• Business case published on Schoology.

Structured Query Language introduction..

  • 1.
    STRUCTURED QUERY LANGUAGE(SQL) FERRY KEMPERMAN NANJING FOREIGN LANGUAGE SCHOOL NOV 2019
  • 2.
    CAN YOU TELLME……. ✓ What the name is of this diagram? ✓ What the official name is for rectangles, lines and symbols used on the lines? ✓ What is the purpose of this diagram? ✓ Who designs this diagram in reality? ✓ In how many warehouses a specific product can be stored? ✓ How many orders a customer can actually place? ✓ If a customer calls with a complaint about an order, can you track the responsible sales rep? ✓ There is something wrong with this design. What? What would the solution be?
  • 3.
    TODAY WE AREGOING TO LEARN….. ➢How to ask questions and invoke tasks to the database using Structured Query Language (SQL) ➢How to retrieve data from the database by writing a SELECT statement ➢How to manipulate data (add,modify,delete) using in a database using DML-statements ➢How to create a database and tables with keys using DDL-statements
  • 4.
    DATABASE CLIENT /SERVER ARCHITECTURE ❑ Database servers are centralized in a LAN ❑ Clients send their request to the server ❑ Server responds by sending selected data back to client General procedure 1. Request: Sales rep wants a list of all orders of customer Xu 2. Database server (DBMS) receives request from client 3. Database server prioritizes, validates and queues request 4. Database server collects selected data from database 5. Database server sends selected data back to client A request to the database is called a query. The selected data returned to client is called a result set.
  • 5.
    STRUCTURED QUERY LANGUAGE(SQL) • SQL is the industry wide language in which queries to the database are written. It was developed way back in the 1970-ies by IBM and adopted by the entire IT- community. • SQL is pronounced as Sequel, no S-Q-L • SQL is a retrieval language, not a programming language! • SQL has a fixed syntax, so it works on every database! • SQL has three parts with different goals: • SQL – DML : Part of the language that allows users to select and modify data • SQL – DDL : Part of the language that allows designers to create/alter a database and tables with keys • SQL – DCL : Part of the language that allow DBA-ers to set permissions for users to access certain parts of the database
  • 6.
    LET’S WRITE AQUERY IN SQL! A query to request data from a database is called a SELECT statement. A few examples….. SELECT * FROM tblCustomer SELECT CustomerName, CustomerPhone FROM tblCustomer SELECT CustomerName FROM tblCustomer WHERE CustomerId = 24 Result set: CustomerName Brown Result set: CustomerID CustomerName CustomerPhone 23 Ronchi 987654 24 Brown 017288 25 William 837392 26 Dev 792749 Result set: CustomerID CustomerName CustomerPhone 23 Ronchi 987654 24 Brown 017288 25 William 837392 26 Dev 792749
  • 7.
    Break activity 1: Designa query that shows all the products that were ordered. ✓ Discuss for 3 minutes with partner ✓ Write your query on the mini whiteboard
  • 8.
    SELECT CustomerPhone FROM tblCustomer WHERECustomerName = “William” OR CustomerName = “Ronchi” ORDER BY CustomerPhone Result set: CustomerPhone 837392 987654 Is this result set useful for the end-user? SELECT <attributes you want to see> FROM <source tables> WHERE <booleans to select records> ORDER BY <attributes you want to sort by>
  • 9.
    Break activity 2: Designa query that lists only the productnames alphabetically that were ordered by customer 23. ✓ Discuss for 3 minutes with partner ✓ Write your query on the mini whiteboard
  • 10.
    BUT SOMETHING ISWRONG…. • List all the products ordered by customer 23? • Customer need to be referred to by their name, not id! • The request should be: • List all products ordered by Customer Ronchi! • But….how to do this? • CustomerName is not in the same table as OrderDescription. • This query wants to filter on CustomerName, but wants to display OrderDescription. • This query requires two tables instead of one! • We use an INNER JOIN to do this. SELECT O.OrderDesc FROM tblCustomer C INNER JOIN tblOrder ON C.CustomerID=O.CustomerID WHERE C.CustomerName = “Ronchi” ORDER BY O.OrderDesc
  • 11.
    Break activity 3: Designa query that lists all the product descriptions alphabetically ordered by customer William and Brown! ✓ Discuss for 3 minutes with partner ✓ Write your query on the mini whiteboard
  • 12.
    DATA MANIPULATION LANGUAGE(DML) • DML is part of SQL that include statements that can manipulate data: • INSERT statement to insert new data into a table • UPDATE statement to modify existing data in a table • DELETE statement to delete existing record(s) from a table INSERT INTO Customer (CustID,CustName,CustPhone) UPDATE Customer DELETE FROM Customer VALUES (1,”Kemperman”,”0018013”), SET CustName = “Smith” WHERE CustName = ‘Smith’ (2,”Zhang”,”123483”) WHERE CustID = 1
  • 13.
    DATA DEFINITION LANGUAGE(DDL) • Data definition language (DDL) is a part of SQL that is used by database designers. With DDL you can • Create a database • Create new tables in a database • Alter a table in a database • Dropping (deleting) tables in a database • Add primary and foreign keys to tables • Database users do NOT have the permissions to use DDL, only designers have. • Why? Deleting a customer table by accident?  Really?! • Database users need to able to access the DATA only, not the DATABASE itself!
  • 14.
  • 15.
    BUSINESS CASE :WRITING QUERIES • Sample database (ERD – Populated tables) • Write 10 queries based on the sample database • Groups 2 of two students. Choose yourself. • Write a report with the queries in SQL. • Business case published on Schoology.