Structured Query Language: Rita Sinha Iucaa
Structured Query Language: Rita Sinha Iucaa
Rita Sinha
IUCAA
June 2007 1
SQL
Rita Sinha
IUCAA
June 2007 2
Outline
SQL Ingredients
Syntax, Key words
DATABASES…
Example queries
Link to web tutorials on SQL
MySQL - demo
3
October 2007
Database
4
October 2007
Structure of the database
5
October 2007
Terminology
6
October 2007
Data Types
Integer(size), int(size), smallint(size), tinyint(size)
Holds integers only. The maximum number of digits specified in
parenthesis.
decimal(size,d), numeric(size,d)
Holds numbers with fractions. The maximum number of digits are
specified in "size". The maximum number of digits to the right of the
decimal is specified in "d".
CHAR(size)
Holds a fixed length string (can contain letters, numbers, and special
characters). The fixed size is specified in parenthesis.
varchar(size)
Holds a variable length string (can contain letters, numbers, and
special characters). The maximum size is specified in parenthesis.
DATEdate(yyyymmdd) Holds a date
TIME Holds time
7
October 2007
What is SQL ?
An ANSI standard computer language
Allows you to access a database
Execute queries against a database
Retrieve data from a database
Insert new records in a database
Delete records from a database
Update records in a database
Querying Language for database programs like
MySQL, MS SQL Server, ADQL, MS Access, DB2,
Informix, Oracle, Sybase, etc.
Easy
Note: Some database systems require a semicolon at the end of the SQL statement
8
October 2007
Ingredients
9
October 2007
Data Manipulation Language (DML) :
10
October 2007
Basic Syntax
Select - Attributes, column name
From - Table name
Where - Restrictions, constraints
[Note: To query data on an existing database which you are not allowed to change]
12
October 2007
Select …
DISTINCT
Outputs only one entry/row of data for every group of rows/entries
that is identical.
ORDERS
OrderNumber Time Name Items Amount
400 1300 Mr. R. Sharma TomatoSoup 45
401 1310 Mr. S. Pal Fried rice 80
402 1315 Mr. R. Sharma Pasta 75
14
October 2007
Where …
Operator Description
= Equal
<> or != Not equal
> Greater than
< Less than
>= Greater than or equal
<= Less than or equal
BETWEEN Between an inclusive range
LIKE Search for a pattern
15
October 2007
Where …
AND and OR join two or more conditions in a WHERE clause.
The AND operator displays a row if ALL conditions listed are true.
The OR operator displays a row if ANY of the conditions listed are true.
SELECT *
FROM Persons
WHERE Surname=‘Sharma’
17
October 2007
Where …
LIKE condition- used to specify a search for a pattern in a column.
A "%" sign can be used to define wildcards (missing letters in the pattern)
both before and after the pattern.
For example:
The following SQL statement will return persons with first names that start
with an 'R':
SELECT * FROM Persons
WHERE FirstName LIKE ‘R%’
The following SQL statement will return persons with first names that end
with an 'a':
SELECT * FROM Persons
WHERE FirstName LIKE ‘%a’
18
October 2007
Duplication of attributes ?
When two tables have a common attribute or
column name then we write both the table name
and the column name, separated by a period. By
this we qualify the names.
Making Life Easier…
19
October 2007
Duplication of attributes ?
When two tables have a common attribute or column name
then we write both the table name and the column name,
separated by a period. By this we qualify the names.
Types of Functions
The basic types of functions are:
Aggregate Functions: operate against a collection of values, but
return a single value.
AVG(column), COUNT(column), MAX(column),
MIN(column), STDEV(column), SUM(column)
Scalar functions: operate against a single value, and return a single
value based on the input value.
25
October 2007
Summary Select … from ….. where
Select ….
AS (alias for column)
DISTINCT
COUNT
From …
AS (alias for table)
Where …
AND/OR
BETWEEN
JOIN
GROUP BY
ORDER BY
26
October 2007
Links
http://www.w3schools.com/sql/sql_quickref.asp
http://cas.sdss.org/astro/en/help/howto/search
27
October 2007
MySQL
Runs on all platforms: Unix/Linux, Windows
Runs as a daemon under Unix/Linux and service under
windows
28
October 2007