Transcript
Page 1: Structured Query  Language(SQL)

Structured Query Language(SQL)

XU Yinqing

SEEM PHD YEAR 3

Page 2: Structured Query  Language(SQL)

Outline

SQL StatementsBasic statementsSQL functions

SQLite: a SQL engineSQLite browserSQLite Manager: add-on for Firefox

Page 3: Structured Query  Language(SQL)

What is SQL?

SQL stands for Structured Query Language SQL lets you access and manipulate

databases SQL is an ANSI (American National Standards

Institute) standard

Page 4: Structured Query  Language(SQL)

What Can SQL do?

SQL can execute queries against a database SQL can insert records in a database SQL can update records in a database SQL can delete records from a database SQL can create new databases SQL can create new tables in a database …

Page 5: Structured Query  Language(SQL)

SQL is a Standard - BUT....

Although SQL is an ANSI (American National Standards Institute) standard, there are different versions of the SQL language.

However, to be compliant with the ANSI standard, they all support at least the major commands (such as SELECT, UPDATE, DELETE, INSERT, WHERE) in a similar manner.

Page 6: Structured Query  Language(SQL)

Using SQL in Your Web Site

To build a web site that shows data from a database, you will need:An RDBMS database program (i.e. MS

Access, SQL Server, MySQL)To use a server-side scripting language, like

PHP or ASPTo use SQL to get the data you wantTo use HTML / CSS

Page 7: Structured Query  Language(SQL)

RDBMS

RDBMS stands for Relational Database Management System.

RDBMS is the basis for SQL, and for all modern database systems such as MS SQL Server, IBM DB2, Oracle, MySQL, and Microsoft Access.

The data in RDBMS is stored in database objects called tables.

A table is a collection of related data entries and it consists of columns and rows.

Page 8: Structured Query  Language(SQL)

Database TablesCustomerID

CustomerName

ContactName

Address City PostalCode

Country

1 Alfreds Futterkiste

Maria Anders

Obere Str. 57

Berlin 12209 Germany

2 Ana Trujillo Emparedados y helados

Ana Trujillo

Avda. de la Constitución 2222

México D.F.

05021 Mexico

3 Antonio Moreno Taquería

Antonio Moreno

Mataderos 2312

México D.F.

05023 Mexico

4 Around the Horn

Thomas Hardy

120 Hanover Sq.

London

WA1 1DP UK

5 Berglunds snabbköp

Christina Berglund

Berguvsvägen 8

Luleå S-958 22 Sweden

Page 9: Structured Query  Language(SQL)

Note

SQL is NOT case sensitive: SELECT is the same as select

Semicolon after SQL Statements? Depend on the database system

Semicolon is the standard way to separate each SQL statement in database systems that allow more than one SQL statement to be executed in the same call to the server.

Page 10: Structured Query  Language(SQL)

Some of The Most Important SQL Commands SELECT - extracts data from a database UPDATE - updates data in a database DELETE - deletes data from a database INSERT INTO - inserts 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

Page 11: Structured Query  Language(SQL)

SELECT

SQL SELECT SyntaxSELECT column_name,column_name

FROM table_name;

andSELECT * FROM table_name;

Page 12: Structured Query  Language(SQL)

Database TablesCustomerID

CustomerName

ContactName

Address City PostalCode

Country

1 Alfreds Futterkiste

Maria Anders

Obere Str. 57

Berlin 12209 Germany

2 Ana Trujillo Emparedados y helados

Ana Trujillo

Avda. de la Constitución 2222

México D.F.

05021 Mexico

3 Antonio Moreno Taquería

Antonio Moreno

Mataderos 2312

México D.F.

05023 Mexico

4 Around the Horn

Thomas Hardy

120 Hanover Sq.

London

WA1 1DP UK

5 Berglunds snabbköp

Christina Berglund

Berguvsvägen 8

Luleå S-958 22 Sweden

Page 13: Structured Query  Language(SQL)

SELECT

The following SQL statement selects the "CustomerName" and "City" columns from the "Customers" table: SELECT CustomerName,City FROM Customers;

The following SQL statement selects all the columns from the "Customers" table: SELECT * FROM Customers;

Page 14: Structured Query  Language(SQL)

SELECT DISTINCT

The DISTINCT keyword can be used to return only distinct (different) values.

SQL SELECT DISTINCT Syntax SELECT DISTINCT column_name,column_name

FROM table_name;

EXAMPLE: SELECT DISTINCT City FROM Customers;

Page 15: Structured Query  Language(SQL)

Database TablesCustomerID

CustomerName

ContactName

Address City PostalCode

Country

1 Alfreds Futterkiste

Maria Anders

Obere Str. 57

Berlin 12209 Germany

2 Ana Trujillo Emparedados y helados

Ana Trujillo

Avda. de la Constitución 2222

México D.F.

05021 Mexico

3 Antonio Moreno Taquería

Antonio Moreno

Mataderos 2312

México D.F.

05023 Mexico

4 Around the Horn

Thomas Hardy

120 Hanover Sq.

London

WA1 1DP UK

5 Berglunds snabbköp

Christina Berglund

Berguvsvägen 8

Luleå S-958 22 Sweden

Page 16: Structured Query  Language(SQL)

WHERE

The WHERE clause is used to filter records. The WHERE clause is used to extract only

those records that fulfill a specified criterion. SQL WHERE Syntax

SELECT column_name,column_nameFROM table_nameWHERE column_name operator value;

Page 17: Structured Query  Language(SQL)

Database TablesCustomerID

CustomerName

ContactName

Address City PostalCode

Country

1 Alfreds Futterkiste

Maria Anders

Obere Str. 57

Berlin 12209 Germany

2 Ana Trujillo Emparedados y helados

Ana Trujillo

Avda. de la Constitución 2222

México D.F.

05021 Mexico

3 Antonio Moreno Taquería

Antonio Moreno

Mataderos 2312

México D.F.

05023 Mexico

4 Around the Horn

Thomas Hardy

120 Hanover Sq.

London

WA1 1DP UK

5 Berglunds snabbköp

Christina Berglund

Berguvsvägen 8

Luleå S-958 22 Sweden

Page 18: Structured Query  Language(SQL)

WHERE

The following SQL statement selects all the customers from the country "Mexico", in the "Customers" table: SELECT * FROM Customers

WHERE Country='Mexico';

Page 19: Structured Query  Language(SQL)

Operators in The WHERE Clause

Operator Description

= Equal

<> Not equal. Note: In some versions of SQL this operator may be written as !=

> Greater than

< Less than

>= Greater than or equal

<= Less than or equal

BETWEEN Between an inclusive range

LIKE Search for a pattern

IN To specify multiple possible values for a column

Page 20: Structured Query  Language(SQL)

AND & OR

The AND operator displays a record if both the first condition AND the second condition are true.

The OR operator displays a record if either the first condition OR the second condition is true.

Page 21: Structured Query  Language(SQL)

AND & OR

AND Operator Example SELECT * FROM Customers

WHERE Country='Germany'AND City='Berlin';

OR Operator Example SELECT * FROM Customers

WHERE City='Berlin'OR City='München';

Combining AND & OR SELECT * FROM Customers

WHERE Country='Germany'AND (City='Berlin' OR City='München');

Page 22: Structured Query  Language(SQL)

Database TablesCustomerID

CustomerName

ContactName

Address City PostalCode

Country

1 Alfreds Futterkiste

Maria Anders

Obere Str. 57

Berlin 12209 Germany

2 Ana Trujillo Emparedados y helados

Ana Trujillo

Avda. de la Constitución 2222

México D.F.

05021 Mexico

3 Antonio Moreno Taquería

Antonio Moreno

Mataderos 2312

México D.F.

05023 Mexico

4 Around the Horn

Thomas Hardy

120 Hanover Sq.

London

WA1 1DP UK

5 Berglunds snabbköp

Christina Berglund

Berguvsvägen 8

Luleå S-958 22 Sweden

SELECT * FROM CustomersWHERE Country='Germany'AND City='Berlin';

Page 23: Structured Query  Language(SQL)

ORDER BY

The ORDER BY keyword is used to sort the result-set by one or more columns.

The ORDER BY keyword sorts the records in ascending order by default. To sort the records in a descending order, you can use the DESC keyword.

SQL ORDER BY Syntax SELECT column_name,column_name

FROM table_nameORDER BY column_name,column_name ASC|DESC;

Page 24: Structured Query  Language(SQL)

ORDER BY

ORDER BY Example

SELECT * FROM CustomersORDER BY Country;

ORDER BY DESC Example

SELECT * FROM CustomersORDER BY Country DESC;

Page 25: Structured Query  Language(SQL)

Database TablesCustomerID

CustomerName

ContactName

Address City PostalCode

Country

1 Alfreds Futterkiste

Maria Anders

Obere Str. 57

Berlin 12209 Germany

2 Ana Trujillo Emparedados y helados

Ana Trujillo

Avda. de la Constitución 2222

México D.F.

05021 Mexico

3 Antonio Moreno Taquería

Antonio Moreno

Mataderos 2312

México D.F.

05023 Mexico

4 Around the Horn

Thomas Hardy

120 Hanover Sq.

London

WA1 1DP UK

5 Berglunds snabbköp

Christina Berglund

Berguvsvägen 8

Luleå S-958 22 Sweden

Page 26: Structured Query  Language(SQL)

INSERT INTO The INSERT INTO statement is used to insert new

records in a table. SQL INSERT INTO Syntax It is possible to write the INSERT INTO statement in two

forms. The first form does not specify the column names

where the data will be inserted, only their values: INSERT INTO table_name

VALUES (value1,value2,value3,...);

The second form specifies both the column names and the values to be inserted: INSERT INTO table_name (column1,column2,column3,...)

VALUES (value1,value2,value3,...);

Page 27: Structured Query  Language(SQL)

INSERT INTO

Assume we wish to insert a new row in the "Customers" table.

We can use the following SQL statement: INSERT INTO Customers (CustomerName, ContactName,

Address, City, PostalCode, Country)VALUES ('Cardinal','Tom B. Erichsen','Skagen 21','Stavanger','4006','Norway');

Page 28: Structured Query  Language(SQL)

Database TablesCustomerID

CustomerName

ContactName

Address City PostalCode

Country

1 Alfreds Futterkiste

Maria Anders

Obere Str. 57

Berlin 12209 Germany

2 Ana Trujillo Emparedados y helados

Ana Trujillo

Avda. de la Constitución 2222

México D.F.

05021 Mexico

3 Antonio Moreno Taquería

Antonio Moreno

Mataderos 2312

México D.F.

05023 Mexico

4 Around the Horn

Thomas Hardy

120 Hanover Sq.

London

WA1 1DP UK

5 Berglunds snabbköp

Christina Berglund

Berguvsvägen 8

Luleå S-958 22 Sweden

INSERT INTO Customers (CustomerName, ContactName, Address, City, PostalCode, Country)VALUES ('Cardinal','Tom B. Erichsen','Skagen 21','Stavanger','4006','Norway');

Page 29: Structured Query  Language(SQL)

UPDATE The UPDATE statement is used to update existing

records in a table. SQL UPDATE Syntax

UPDATE table_nameSET column1=value1,column2=value2,...WHERE some_column=some_value;

Assume we wish to update the customer "Alfreds Futterkiste" with a new contact person and city.

We use the following SQL statement: UPDATE Customers

SET ContactName='Alfred Schmidt', City='Hamburg'WHERE CustomerName='Alfreds Futterkiste';

Page 30: Structured Query  Language(SQL)

Database TablesCustomerID

CustomerName

ContactName

Address City PostalCode

Country

1 Alfreds Futterkiste

Maria Anders

Obere Str. 57

Berlin 12209 Germany

2 Ana Trujillo Emparedados y helados

Ana Trujillo

Avda. de la Constitución 2222

México D.F.

05021 Mexico

3 Antonio Moreno Taquería

Antonio Moreno

Mataderos 2312

México D.F.

05023 Mexico

4 Around the Horn

Thomas Hardy

120 Hanover Sq.

London

WA1 1DP UK

5 Berglunds snabbköp

Christina Berglund

Berguvsvägen 8

Luleå S-958 22 Sweden

UPDATE CustomersSET ContactName='Alfred Schmidt', City='Hamburg'WHERE CustomerName='Alfreds Futterkiste';

Page 31: Structured Query  Language(SQL)

DELETE

The DELETE statement is used to delete rows in a table. SQL DELETE Syntax

DELETE FROM table_nameWHERE some_column=some_value;

Assume we wish to delete the customer "Alfreds Futterkiste" from the "Customers" table.

We use the following SQL statement: DELETE FROM Customers

WHERE CustomerName='Alfreds Futterkiste' AND ContactName='Maria Anders';

Page 32: Structured Query  Language(SQL)

DELETE

It is possible to delete all rows in a table without deleting the table. This means that the table structure, attributes, and indexes will be intact:DELETE FROM table_name;

or

DELETE * FROM table_name;

Be very careful when deleting records. You cannot undo this statement!

Page 33: Structured Query  Language(SQL)

LEFT JOIN The LEFT JOIN keyword returns all rows from the left

table (table1), with the matching rows in the right table (table2). The result is NULL in the right side when there is no match.

SQL LEFT JOIN Syntax SELECT column_name(s)

FROM table1LEFT JOIN table2ON table1.column_name=table2.column_name;

or: SELECT column_name(s)

FROM table1LEFT OUTER JOIN table2ON table1.column_name=table2.column_name;

Page 34: Structured Query  Language(SQL)

Demo Database

CustomerID CustomerName ContactName Address City PostalCode Country

1 Alfreds Futterkiste Maria Anders Obere Str. 57 Berlin 12209 Germany

2 Ana Trujillo Emparedados y helados

Ana Trujillo Avda. de la Constitución 2222

México D.F. 05021 Mexico

3 Antonio Moreno Taquería Antonio Moreno

Mataderos 2312

México D.F. 05023 Mexico

Customers

OrderID CustomerID EmployeeID OrderDate ShipperID

10308 2 7 1996-09-18 3

10309 37 3 1996-09-19 1

10310 77 8 1996-09-20 2

Orders

SELECT Customers.CustomerName, Orders.OrderIDFROM CustomersLEFT JOIN OrdersON Customers.CustomerID=Orders.CustomerIDORDER BY Customers.CustomerName;

Page 35: Structured Query  Language(SQL)

SQL FUNCTION

MAX

MIN

GROUP BY

Page 36: Structured Query  Language(SQL)

MAX

The MAX() function returns the largest value of the selected column.

SQL MAX() Syntax

SELECT MAX(column_name) FROM table_name;

Page 37: Structured Query  Language(SQL)

Demo database

ProductID ProductName

SupplierID CategoryID Unit Price

1 Chais 1 1 10 boxes x 20 bags

18

2 Chang 1 1 24 - 12 oz bottles

19

3 Aniseed Syrup

1 2 12 - 550 ml bottles

10

4 Chef Anton's Cajun Seasoning

2 2 48 - 6 oz jars

21.35

5 Chef Anton's Gumbo Mix

2 2 36 boxes 25

SELECT MAX(Price) AS HighestPrice FROM Products;

Page 38: Structured Query  Language(SQL)

MIN

The MIN() function returns the smallest value of the selected column.

SQL MIN() Syntax SELECT MIN(column_name) FROM

table_name;

Page 39: Structured Query  Language(SQL)

Demo database

ProductID ProductName

SupplierID CategoryID Unit Price

1 Chais 1 1 10 boxes x 20 bags

18

2 Chang 1 1 24 - 12 oz bottles

19

3 Aniseed Syrup

1 2 12 - 550 ml bottles

10

4 Chef Anton's Cajun Seasoning

2 2 48 - 6 oz jars

21.35

5 Chef Anton's Gumbo Mix

2 2 36 boxes 25

SELECT MIN(Price) AS SmallestOrderPrice FROM Products;

Page 40: Structured Query  Language(SQL)

GROUP BY The GROUP BY statement is used in

conjunction with the aggregate functions to group the result-set by one or more columns.

SQL GROUP BY SyntaxSELECT column_name,

aggregate_function(column_name)FROM table_nameWHERE column_name operator valueGROUP BY column_name;

Page 41: Structured Query  Language(SQL)

Demo Databases

OrderID CustomerID EmployeeID OrderDate ShipperID

10248 90 5 1996-07-04 3

10249 81 6 1996-07-05 1

10250 34 4 1996-07-08 2

10251 35 3 1996-07-09 1

ShipperID ShipperName Phone

1 Speedy Express (503) 555-9831

2 United Package (503) 555-3199

3 Federal Shipping (503) 555-9931

Orders

Shippers

SELECT Shippers.ShipperName,COUNT(Orders.OrderID) AS NumberOfOrders FROM OrdersLEFT JOIN ShippersON Orders.ShipperID=Shippers.ShipperIDGROUP BY ShipperName;

ShipperName NumberOfOrders

Speedy Express 2

United Package 1

Federal Shipping 1

Result

Page 42: Structured Query  Language(SQL)

SQLite: a SQL engine

SQLite is an in-process library that implements a self-contained, serverless, zero-configuration, transactional SQL database engine. It is the one database, which is zero-configured, that means like other database you do not need to configure it in your system.

SQLite engine is not a standalone process like other databases, you can link it statically or dynamically as per your requirement with your application. The SQLite accesses its storage files directly.

Page 43: Structured Query  Language(SQL)

Why SQLite? SQLite does not require a separate server process or

system to operate.(serverless). SQLite comes with zero-configuration, which means

no setup or administration needed. A complete SQLite database is stored in a single

cross-platform disk file. SQLite is very small and light weight, less than

400KiB fully configured or less than 250KiB with optional features omitted.

Page 44: Structured Query  Language(SQL)

Two interactive tools

Tools interact with SQLite

SQLite Browser

SQLite Manager

How to create tables using these two tools?

Page 45: Structured Query  Language(SQL)

SQLite browser http://sourceforge.net/projects/sqlitebrowser

/ How to create table?

Page 46: Structured Query  Language(SQL)

File-> New Database

Page 47: Structured Query  Language(SQL)
Page 48: Structured Query  Language(SQL)
Page 50: Structured Query  Language(SQL)

Database-> New Database

Page 51: Structured Query  Language(SQL)

Table-> Create Table

Page 52: Structured Query  Language(SQL)

End

Thank you !


Recommended