Structured Query Language(SQL) XU Yinqing SEEM PHD YEAR 3.

Slides:



Advertisements
Similar presentations
Copyright © by Royal Institute of Information Technology Introduction To Structured Query Language (SQL) 1.
Advertisements

Structure Query Language (SQL) COMSATS INSTITUTE OF INFORMATION TECHNOLOGY, VEHARI.
Introduction to Structured Query Language (SQL)
Introduction to Structured Query Language (SQL)
Introduction to Structured Query Language (SQL)
1ISM - © 2010 Houman Younessi Lecture 3 Convener: Houman Younessi Information Systems Spring 2011.
Introduction to SQL J.-S. Chou Assistant Professor.
Rationale Aspiring Database Developers should be able to efficiently query and maintain databases. This module will help students learn the Structured.
CPS120: Introduction to Computer Science Information Systems: Database Management Nell Dale John Lewis.
ASP.NET Programming with C# and SQL Server First Edition
Relational DBs and SQL Designing Your Web Database (Ch. 8) → Creating and Working with a MySQL Database (Ch. 9, 10) 1.
LOGO 1 Lab_02: Basic SQL. 2 Outline  Database Tables  SQL Statements  Semicolon after SQL Statements?  SQL DML and DDL  SQL SELECT Statement  SQL.
PHP Programming with MySQL Slide 8-1 CHAPTER 8 Working with Databases and MySQL.
 SQL stands for Structured Query Language.  SQL lets you access and manipulate databases.  SQL is an ANSI (American National Standards Institute) standard.
Structured Query Language. SQL is an ANSI (American National Standards Institute) standard computer language for accessing and manipulating database systems.
Database A collection of related data. Database Applications Banking: all transactions Airlines: reservations, schedules Universities: registration, grades.
Sundara Ram Matta Apr 01 st, Sundara Ram Matta Apr 01 st, 2015
Lecture6:Data Manipulation in SQL, Simple SQL queries Prepared by L. Nouf Almujally Ref. Chapter5 Lecture6 1.
15/10/20151 PHP & MySQL 'Slide materials are based on W3Schools PHP tutorial, 'PHP website 'MySQL website.
CPS120: Introduction to Computer Science Lecture 19 Introduction to SQL.
Structure Query Language SQL. Database Terminology Employee ID 3 3 Last name Small First name Tony 5 5 Smith James
CSC 330 E-Commerce Teacher Ahmed Mumtaz Mustehsan Ahmed Mumtaz Mustehsan GM-IT CIIT Islamabad GM-IT CIIT Islamabad CIIT Virtual Campus, CIIT COMSATS Institute.
6 1 Lecture 8: Introduction to Structured Query Language (SQL) J. S. Chou, P.E., Ph.D.
Topic 1: Introduction to SQL. SQL stands for Structured Query Language. SQL is a standard computer language for accessing and manipulating databases SQL.
SQL Unit – 2 Base Knowledge Presented By Mr. R.Aravindhan.
1 DBS201: Introduction to Structure Query Language (SQL) Lecture 1.
SQL. คำสั่ง SQL SQL stands for Structured Query Language is a standard language for accessing and manipulating databases.
CIS 375—Web App Dev II SQL. 2 Introduction SQL (Structured _______ Language) is an ANSI standard language for accessing databases.ANSI SQL can execute.
Database Fundamental & Design by A.Surasit Samaisut Copyrights : All Rights Reserved.
Visual Programing SQL Overview Section 1.
CIS 375—Web App Dev II SQL. 2 Introduction SQL (Structured _______ Language) is an ANSI standard language for accessing databases.ANSI SQL can execute.
SQL Jan 20,2014. DBMS Stores data as records, tables etc. Accepts data and stores that data for later use Uses query languages for searching, sorting,
AL-MAAREFA COLLEGE FOR SCIENCE AND TECHNOLOGY INFO 232: DATABASE SYSTEMS CHAPTER 7 (Part II) INTRODUCTION TO STRUCTURED QUERY LANGUAGE (SQL) Instructor.
Module Review Basic SQL commands: Create Database, Create Table, Insert and Select 2. Connect an SQL Database to PHP 3. Execute SQL Commands in.
CHAPTER 9 SQL อ. ยืนยง กันทะเนตร คณะเทคโนโลยีสารสนเทศและการสื่อสาร มหาวิทยาลัยพะเยา 1.
IS6146 Databases for Management Information Systems Lecture 1: Introduction to IS6146 Rob Gleasure robgleasure.com.
INFANL01-3 ANALYSE 3 WEEK 3 March 2015 Institute voor Communication, Media en Informatietechnology.
Distribution of Marks For Second Semester Internal Sessional Evaluation External Evaluation Assignment /Project QuizzesClass Attendance Mid-Term Test Total.
IS2803 Developing Multimedia Applications for Business (Part 2) Lecture 5: SQL I Rob Gleasure robgleasure.com.
Introduction to Database SEM I, AY Department of Information Technology Salalah College of Technology Chapter No.3 SQL.
7 1 Database Systems: Design, Implementation, & Management, 7 th Edition, Rob & Coronel 7.6 Advanced Select Queries SQL provides useful functions that.
ITS232 Introduction To Database Management Systems Siti Nurbaya Ismail Faculty of Computer Science & Mathematics, Universiti Teknologi MARA (UiTM), Kedah.
LM 5 Introduction to SQL MISM 4135 Instructor: Dr. Lei Li.
ADVANCED SQL.  The SQL ORDER BY Keyword  The ORDER BY keyword is used to sort the result-set by one or more columns.  The ORDER BY keyword sorts the.
MySQL Tutorial. Databases A database is a container that groups together a series of tables within a single structure Each database can contain 1 or more.
Lecturer: Eng. Mohamed Adam Isak PH.D Researcher in CS. M.Sc. and B.Sc. of Information Technology Engineering Senior Lecturer in University of Somalia,
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.
LEC-8 SQL. Indexes The CREATE INDEX statement is used to create indexes in tables. Indexes allow the database application to find data fast; without reading.
SQL Structured Query Language. SQL is an ANSI (American National Standards Institute) standard computer language for accessing and manipulating database.
COM621: Advanced Interactive Web Development Lecture 11 MySQL – Data Manipulation Language.
 MySQL is a database system used on the web  MySQL is a database system that runs on a server  MySQL is ideal for both small and large applications.
Fundamental of Database Systems
SQL SQL Ayshah I. Almugahwi Maryam J. Alkhalifa
Web Systems & Technologies
Module T03d Software Engineering
CHAPTER 7 DATABASE ACCESS THROUGH WEB
SQL Query Getting to the data ……..
Query Methods Where Clauses Start ….
Query Methods Where Clauses Start ….
Introduction to Structured Query Language(SQL)
Structured Query Language – The Basics
MySQL Joins MySQL joins are used to combine rows from two or more tables. Different SQL JOINs INNER JOIN: Returns all rows when there is at least one match.
OpenLink Virtuoso - SQL & RDF
Introduction To Structured Query Language (SQL)
SQL Queries Chapter No 3.
Structured Query Language – The Fundamentals
Introduction To Structured Query Language (SQL)
Structured Query Language
SQL Tutorial w3schools.
Presentation transcript:

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.

Database Tables CustomerIDCustomer Name ContactNa me AddressCityPostalCodeCountry 1Alfreds Futterkiste Maria Anders Obere Str. 57Berlin12209Germany 2Ana Trujillo Empareda dos y helados Ana TrujilloAvda. de la Constitución 2222 México D.F Mexico 3Antonio Moreno Taquería Antonio Moreno Mataderos 2312 México D.F Mexico 4Around the Horn Thomas Hardy 120 Hanover Sq. LondonWA1 1DPUK 5Berglunds snabbköp Christina Berglund Berguvsväge n 8 LuleåS Sweden

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_name FROM table_name;  and  SELECT * FROM table_name;

Database Tables CustomerIDCustomer Name ContactNa me AddressCityPostalCodeCountry 1Alfreds Futterkiste Maria Anders Obere Str. 57Berlin12209Germany 2Ana Trujillo Empareda dos y helados Ana TrujilloAvda. de la Constitución 2222 México D.F Mexico 3Antonio Moreno Taquería Antonio Moreno Mataderos 2312 México D.F Mexico 4Around the Horn Thomas Hardy 120 Hanover Sq. LondonWA1 1DPUK 5Berglunds snabbköp Christina Berglund Berguvsväge n 8 LuleåS Sweden

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_name FROM table_name;  EXAMPLE:  SELECT DISTINCT City FROM Customers;

Database Tables CustomerIDCustomer Name ContactNa me AddressCityPostalCodeCountry 1Alfreds Futterkiste Maria Anders Obere Str. 57Berlin12209Germany 2Ana Trujillo Empareda dos y helados Ana TrujilloAvda. de la Constitución 2222 México D.F Mexico 3Antonio Moreno Taquería Antonio Moreno Mataderos 2312 México D.F Mexico 4Around the Horn Thomas Hardy 120 Hanover Sq. LondonWA1 1DPUK 5Berglunds snabbköp Christina Berglund Berguvsväge n 8 LuleåS Sweden

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_name FROM table_name WHERE column_name operator value;

Database Tables CustomerIDCustomer Name ContactNa me AddressCityPostalCodeCountry 1Alfreds Futterkiste Maria Anders Obere Str. 57Berlin12209Germany 2Ana Trujillo Empareda dos y helados Ana TrujilloAvda. de la Constitución 2222 México D.F Mexico 3Antonio Moreno Taquería Antonio Moreno Mataderos 2312 México D.F Mexico 4Around the Horn Thomas Hardy 120 Hanover Sq. LondonWA1 1DPUK 5Berglunds snabbköp Christina Berglund Berguvsväge n 8 LuleåS Sweden

WHERE  The following SQL statement selects all the customers from the country "Mexico", in the "Customers" table:  SELECT * FROM Customers WHERE Country='Mexico';

Operators in The WHERE Clause OperatorDescription =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 BETWEENBetween an inclusive range LIKESearch for a pattern INTo specify multiple possible values for a column

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

Database Tables CustomerIDCustomer Name ContactNa me AddressCityPostalCodeCountry 1Alfreds Futterkiste Maria Anders Obere Str. 57Berlin12209Germany 2Ana Trujillo Empareda dos y helados Ana TrujilloAvda. de la Constitución 2222 México D.F Mexico 3Antonio Moreno Taquería Antonio Moreno Mataderos 2312 México D.F Mexico 4Around the Horn Thomas Hardy 120 Hanover Sq. LondonWA1 1DPUK 5Berglunds snabbköp Christina Berglund Berguvsväge n 8 LuleåS Sweden SELECT * FROM Customers WHERE Country='Germany' AND City='Berlin';

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_name ORDER BY column_name,column_name ASC|DESC;

ORDER BY  ORDER BY Example  SELECT * FROM Customers ORDER BY Country;  ORDER BY DESC Example  SELECT * FROM Customers ORDER BY Country DESC;

Database Tables CustomerIDCustomer Name ContactNa me AddressCityPostalCodeCountry 1Alfreds Futterkiste Maria Anders Obere Str. 57Berlin12209Germany 2Ana Trujillo Empareda dos y helados Ana TrujilloAvda. de la Constitución 2222 México D.F Mexico 3Antonio Moreno Taquería Antonio Moreno Mataderos 2312 México D.F Mexico 4Around the Horn Thomas Hardy 120 Hanover Sq. LondonWA1 1DPUK 5Berglunds snabbköp Christina Berglund Berguvsväge n 8 LuleåS Sweden

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,...);

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

Database Tables CustomerIDCustomer Name ContactNa me AddressCityPostalCodeCountry 1Alfreds Futterkiste Maria Anders Obere Str. 57Berlin12209Germany 2Ana Trujillo Empareda dos y helados Ana TrujilloAvda. de la Constitución 2222 México D.F Mexico 3Antonio Moreno Taquería Antonio Moreno Mataderos 2312 México D.F Mexico 4Around the Horn Thomas Hardy 120 Hanover Sq. LondonWA1 1DPUK 5Berglunds snabbköp Christina Berglund Berguvsväge n 8 LuleåS Sweden INSERT INTO Customers (CustomerName, ContactName, Address, City, PostalCode, Country) VALUES ('Cardinal','Tom B. Erichsen','Skagen 21','Stavanger','4006','Norway');

UPDATE  The UPDATE statement is used to update existing records in a table.  SQL UPDATE Syntax  UPDATE table_name SET 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';

Database Tables CustomerIDCustomer Name ContactNa me AddressCityPostalCodeCountry 1Alfreds Futterkiste Maria Anders Obere Str. 57Berlin12209Germany 2Ana Trujillo Empareda dos y helados Ana TrujilloAvda. de la Constitución 2222 México D.F Mexico 3Antonio Moreno Taquería Antonio Moreno Mataderos 2312 México D.F Mexico 4Around the Horn Thomas Hardy 120 Hanover Sq. LondonWA1 1DPUK 5Berglunds snabbköp Christina Berglund Berguvsväge n 8 LuleåS Sweden UPDATE Customers SET ContactName='Alfred Schmidt', City='Hamburg' WHERE CustomerName='Alfreds Futterkiste';

DELETE  The DELETE statement is used to delete rows in a table.  SQL DELETE Syntax  DELETE FROM table_name WHERE 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';

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!

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 table1 LEFT JOIN table2 ON table1.column_name=table2.column_name;  or:  SELECT column_name(s) FROM table1 LEFT OUTER JOIN table2 ON table1.column_name=table2.column_name;

Demo Database CustomerIDCustomerNameContactNameAddressCityPostalCodeCountry 1Alfreds FutterkisteMaria AndersObere Str. 57Berlin12209Germany 2Ana Trujillo Emparedados y helados Ana TrujilloAvda. de la Constitución 2222 México D.F.05021Mexico 3Antonio Moreno TaqueríaAntonio Moreno Mataderos 2312 México D.F.05023Mexico Customers OrderIDCustomerIDEmployeeIDOrderDateShipperID Orders SELECT Customers.CustomerName, Orders.OrderID FROM Customers LEFT JOIN Orders ON Customers.CustomerID=Orders.CustomerID ORDER BY Customers.CustomerName;

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 ProductIDProductNam e SupplierIDCategoryIDUnitPrice 1Chais1110 boxes x 20 bags 18 2Chang oz bottles 19 3Aniseed Syrup ml bottles 10 4Chef Anton's Cajun Seasoning oz jars Chef Anton's Gumbo Mix 2236 boxes25 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 ProductIDProductNam e SupplierIDCategoryIDUnitPrice 1Chais1110 boxes x 20 bags 18 2Chang oz bottles 19 3Aniseed Syrup ml bottles 10 4Chef Anton's Cajun Seasoning oz jars Chef Anton's Gumbo Mix 2236 boxes25 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_name WHERE column_name operator value GROUP BY column_name;

Demo Databases OrderIDCustomerIDEmployeeIDOrderDateShipperID ShipperIDShipperNamePhone 1Speedy Express(503) United Package(503) Federal Shipping(503) Orders Shippers SELECT Shippers.ShipperName,COUNT(Orders.OrderID) AS NumberOfOrders FROM Orders LEFT JOIN Shippers ON Orders.ShipperID=Shippers.ShipperID GROUP BY ShipperName; ShipperNameNumberOfOrders Speedy Express2 United Package1 Federal Shipping1 Result

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   How to create table?

File-> New Database

SQLite Manager  US/firefox/addon/sqlite-manager/ US/firefox/addon/sqlite-manager/  How to create table?

Database-> New Database

Table-> Create Table

End  Thank you !