540 likes | 788 Views
Structured Query Language(SQL). XU Yinqing SEEM PHD YEAR 3. Outline. SQL Statements Basic statements SQL functions SQLite: a SQL engine SQLite browser SQLite Manager: add-on for Firefox. What is SQL?. SQL stands for Structured Query Language
E N D
Structured Query Language(SQL) XU Yinqing SEEM PHD YEAR 3
Outline • SQL Statements • Basic statements • SQL functions • SQLite: a SQL engine • SQLite browser • SQLite Manager: add-on for Firefox
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
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 • …
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.
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 ASP • To use SQL to get the data you want • To use HTML / CSS
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.
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.
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
SELECT • SQL SELECT Syntax • SELECT column_name,column_nameFROM table_name; • and • SELECT * FROM table_name;
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;
SELECT DISTINCT • The DISTINCT keyword can be used to return only distinct (different) values. • SQL SELECT DISTINCT Syntax • SELECT DISTINCT column_name,column_nameFROM table_name; • EXAMPLE: • SELECT DISTINCT City FROM Customers;
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;
WHERE • The following SQL statement selects all the customers from the country "Mexico", in the "Customers" table: • SELECT * FROM CustomersWHERE Country='Mexico';
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.
AND & OR • AND Operator Example • SELECT * FROM CustomersWHERE Country='Germany'AND City='Berlin'; • OR Operator Example • SELECT * FROM CustomersWHERE City='Berlin'OR City='München'; • Combining AND & OR • SELECT * FROM CustomersWHERE Country='Germany'AND (City='Berlin' OR City='München');
SELECT * FROM CustomersWHERE Country='Germany'AND City='Berlin'; Database Tables
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_nameFROM table_nameORDER BY column_name,column_nameASC|DESC;
ORDER BY • ORDER BY Example • SELECT * FROM CustomersORDER BY Country; • ORDER BY DESC Example • SELECT * FROM CustomersORDER BY Country DESC;
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_nameVALUES (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,...);
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');
INSERT INTO Customers (CustomerName, ContactName, Address, City, PostalCode, Country)VALUES ('Cardinal','Tom B. Erichsen','Skagen 21','Stavanger','4006','Norway'); Database Tables
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 "AlfredsFutterkiste" with a new contact person and city. • We use the following SQL statement: • UPDATE CustomersSET ContactName='Alfred Schmidt', City='Hamburg'WHERE CustomerName='AlfredsFutterkiste';
UPDATE CustomersSET ContactName='Alfred Schmidt', City='Hamburg'WHERE CustomerName='AlfredsFutterkiste'; Database Tables
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 "AlfredsFutterkiste" from the "Customers" table. • We use the following SQL statement: • DELETE FROM CustomersWHERE CustomerName='AlfredsFutterkiste' AND ContactName='Maria Anders';
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;orDELETE * FROM table_name; • Be very careful when deleting records. You cannot undo this statement!
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;
SELECT Customers.CustomerName, Orders.OrderIDFROM CustomersLEFT JOIN OrdersON Customers.CustomerID=Orders.CustomerIDORDER BY Customers.CustomerName; Demo Database Customers Orders
SQL FUNCTION • MAX • MIN • GROUP BY
MAX • The MAX() function returns the largest value of the selected column. • SQL MAX() Syntax • SELECT MAX(column_name) FROM table_name;
Demo database SELECT MAX(Price) AS HighestPrice FROM Products;
MIN • The MIN() function returns the smallest value of the selected column. • SQL MIN() Syntax • SELECT MIN(column_name) FROM table_name;
Demo database SELECT MIN(Price) AS SmallestOrderPrice FROM Products;
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 Syntax • SELECT column_name, aggregate_function(column_name)FROM table_nameWHERE column_name operator valueGROUP BY column_name;
SELECT Shippers.ShipperName,COUNT(Orders.OrderID) AS NumberOfOrders FROM OrdersLEFT JOIN ShippersON Orders.ShipperID=Shippers.ShipperIDGROUP BY ShipperName; Demo Databases Orders Shippers
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.
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. • …
Two interactive tools • Tools interact with SQLite • SQLite Browser • SQLite Manager • How to create tables using these two tools?
SQLite browser • http://sourceforge.net/projects/sqlitebrowser/ • How to create table?
SQLite Manager • https://addons.mozilla.org/en-US/firefox/addon/sqlite-manager/ • How to create table?