SQL Tutorial
SQL is a standard language for accessing databases.
Our SQL tutorial will teach you how to use SQL to access and manipulate
data in: MySQL, SQL Server, Access, Oracle, Sybase, DB2, and other
database systems.
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 retrieve data from 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 can create stored procedures in a database
+ SQL can create views in a database
+ SQL can set permissions on tables, procedures, and views
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.MySQL Data Types
In MySQL there are three main types : text, number, and Date/Time types.
Text types:
Data type
CHAR(size)
VARCHAR(size)
TINYTEXT.
TEXT
BLOB
(MEDIUMTEXT
MEDIUMBLOB
LONGTEXT
LONGBLOB
Description
Holds a fixed length string (can contain letters, numbers, and special characters). The
fixed size is specified in parenthesis. Can store up to 255 characters
Holds a variable length string (can contain letters, numbers, and special characters).
‘The maximum size is specified in parenthesis. Can store up to 255 characters. Note: If
you put a greater value than 255 it will be converted toa TEXT type
Holds a string witha maximum length of 255 characters
Holds a string witha maximum length of 65,535 characters
For BLOBs (Binary Large OBjects). Holds up to 65,535 bytes of data
Holds string witha maximum length of 16,777,215 characters
For BLOBs (Binary Large OBjects). Holds up to 16,777,215 bytes of data
Holds string witha maximum length of 4,294,967,295 characters
For BLOBs (Binary Large OBjects). Holds up to 4,294,967,295 bytes of dataENUM(x,y,z,ete.)
set
Number types:
Data type
TINYINT(size)
SMALLINT(size)
MEDIUMINT(size)
INT(size)
BIGINT(size)
FLOAT(size,d)
Let youentera list of possible values. You can list up to 65535 valuesinan ENUM list. If
avalueisinserted thats not in the list, a blank value will be inserted.
Note: The values are sorted in the order you enter them.
You enter the possible values in this format: ENUM('X',Y','Z)
Similarto ENUM except that SET may contain up to 64 listitemsandcan store more
than one choice
Description
-128 to 127 normal.0 to 255 UNSIGNED*, The maximum number of digits may be
specified in parenthesis
-32768 to 32767 normal.0 to 65535 UNSIGNED*. The maximum number of digits
may be specified in parenthesis
-8388608 to 8388607 normal.0 to 16777215 UNSIGNED*, The maximumnumber of
digits may be specifiedin parenthesis
-2147483648 to 2147483647 normal.0 to 4294967295 UNSIGNED*. The maximum
number of digits may be specifiedin parenthesis
-9223372036854775808 to 9223372036854775807 normal. Oto
18446744073709551615 UNSIGNED*. The maximum numberof digits may be
specified in parenthesis
‘A small number with a floating decimal point. The maximum number of digits may
be specified inthe size parameter. The maximum number of digits to the rightof the
decimal point is specified in the d parameterDOUBLE(size,d)
DECIMAL(size,d)
A large number with a floating decimal point. The maximum number of digits may be
specifiedin the size parameter. The maximum number of digits to the rightof the
decimal pointis specifiedin the d parameter
A DOUBLE stored as a string, allowing fora fixed decimal point. The maximum
number of digits may be specified in the size parameter. The maximum number of
digitstothe right of the decimal pointis specified nthe d parameter
*The integer types have an extra option called UNSIGNED. Normally, the
integer goes from an negative to positive value. Adding the UNSIGNED
attribute will move that range up so it starts at zero instead of a negative
number.
Date types:
Data type
DATE()
DATETIME()
TIMESTAMP(),
TIME()
Description
A date. Format: YYYY-MM-DD
Note: The supported range is from '1000-01-01' to '9999-12-
at
*A date and time combination. Format: YYYY-MM-DD
HH:MI:SS
Note: The supported range is from '1000-01-01 00:00:00' to
"9999-12-31 23:59:59"
*A timestamp. TIMESTAMP values are stored as the number of
‘seconds since the Unix epoch ('1970-01-01 00:00:00' UTC).
Format: YYYY-MM-DD HH:MI:SS
Note: The supported range is from 1970-01-01 00:00:01' UTC
to '2038-01-09 03:14:07" UTC
A time. Format: HH:MI:SS
Note: The supported range is from '-838:59:59" to '838:59:59°YEAR() A year in two-digit or four-digit format.
Note: Values allowed in four-digit format: 1901 to 2155.
Values allowed in two-digit format: 70 to 69, representing
years from 1970 to 2069
*Even if DATETIME and TIMESTAMP return the same format, they work very
differently. In an INSERT or UPDATE query, the TIMESTAMP automatically set
itself to the current date and time. TIMESTAMP also accepts various formats,
like YYYYMMDDHHMISS, YYMMODHHMISS, YYYYMMDD, or YYMMDD.CustomerID
A database most often contains one or more tables. Each table is identified
by a name (e.g. "Customers" or "Orders"). Tables contain records (rows)
with data.
In this tutorial we will use the well-known Northwind sample database
(included in MS Access and MS SQL Server).
Below is a selection from the "Customers" table:
CustomerName
Alfreds Futterkiste
Ana Trujillo Emparedados
yhelados
Antonio Moreno Taqueria
Around the Horn
Berglunds snabbkép
‘ContactName
Maria Anders
Ana Trujillo
Antonio
Moreno
Thomas Hardy
Christina
Berglund
Address
Obere Str. 57
Avda. dela
Constitucion 2222
Mataderos 2312
120 HanoverSq.
Berguvsvigen8
City
Berlin
México
México
DF.
London
Ltuled
PostalCode
12209
05021
05023
WA1 10P
'$-958 22
The table above contains five records (one for each customer) and seven
columns (CustomerID, CustomerName, ContactName, Address, City,
PostalCode, and Country).
SQL Statements
Most of the actions you need to perform ona database are done with SQL
statements.The following SQL statement selects all the records in the "Customers" table:
Example
SELECT * FROM Customers;
In this tutorial we will teach you all about the different SQL statements.
Keep in Mind That...
+ SQL keywords are NOT case sensitive: select is the same as SELECT
In this tutorial we will write all SQL keywords in upper-case.
Semicolon after SQL Statements?
Some database systems require a semicolon at the end of each SQL
statement.
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.
In this tutorial, we will use semicolon at the end of each SQL statement.
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
The SQL SELECT Statement
The SELECT statement is used to select data from a database.
The result is stored in a result table, called the result-set.
SQL SELECT Syntax
SELECT column_name,col.umn_name
FROM tabLe_name;
and
SELECT * FROM table_name;
SELECT Column Example
The following SQL statement selects the "CustomerName" and "City" columns
from the "Customers" table:
Example
SELECT CustomerName,City FROM Customers;
SELECT * Example
The following SQL statement selects all the columns from the "Customers"
table:
Example
SELECT * FROM Customers;The SQL SELECT DISTINCT Statement
In a table, a column may contain many duplicate values; and sometimes you
only want to list the different (distinct) values.
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;
SELECT DISTINCT Example
The following SQL statement selects only the distinct values from the "City"
columns from the "Customers" table:
Example
SELECT DISTINCT City FROM Customers;
The WHERE clause is used to filter records.
The SQL WHERE Clause
The WHERE clause is used to extract only those records that fulfill a specified
criterion.
SQL WHERE Syntax
SELECT column_name, col umn_name
FROM table_name
WHERE column_name operator value;