8/9/2019 SQL Lesson1
1/15
SQLSQL
8/9/2019 SQL Lesson1
2/15
What is SQL?What is SQL?
y SQL stands for Structured Query
Language
y SQL lets you access and manipulate
databases
y SQL is an ANSI (American National
Standards Institute) standard
8/9/2019 SQL Lesson1
3/15
What Can SQL do?What Can SQL do?
y SQL can execute queries against a databasey SQL can retrieve data from a database
y SQL can insert records in a databasey SQL can update records in a databasey SQL can delete records from a databasey SQL can create new databasesy SQL can create new tables in a databasey SQL can create stored procedures in a
databasey SQL can create views in a database
y SQL can set permissions on tables,procedures, and views
8/9/2019 SQL Lesson1
4/15
SQL is a StandardSQL is a Standard
yAlthough SQL is an ANSI (AmericanNational Standards Institute) standard,there are many different versions of theSQL language.
y However, to be compliant with the ANSIstandard, they all support at least themajor commands (such as SELECT,UPDATE, DELETE, INSERT, WHERE) in
a similar manner.y Note: Most of the SQL database
programs also have their own proprietaryextensions in addition to the SQLstandard!
8/9/2019 SQL Lesson1
5/15
Using SQL in Your Web SiteUsing SQL in Your Web Site
y To build a web site that shows some
data from a database, you will need
the following:
yAn RDBMS database program (i.e.
MS Access, SQL Server, MySQL)
yA server-side scripting language, like
PHP or ASPy SQL
y HTML / CSS
8/9/2019 SQL Lesson1
6/15
RDBMSRDBMS
y RDBMS stands for Relational DatabaseManagement System.
y RDBMS is the basis for SQL, and for all
modern database systems like MS SQLServer, IBM DB2, Oracle, MySQL, andMicrosoft Access.
y The data in RDBMS is stored in
database objects called tables.yA table is a collections of related data
entries and it consists of columns androws.
8/9/2019 SQL Lesson1
7/15
Database TablesDatabase Tables
The table above contains three records (one for each person) and
five columns (P_Id, LastName, FirstName, Address, and City).
8/9/2019 SQL Lesson1
8/15
SQLStatementsSQLStatements
yMost of the actions you need to
perform on a database are done with
SQL statements.
y The following SQL statement will
select all the records in the "Persons"
table:
y SELECT * FROM Personsy In this tutorial we will teach you all
about the different SQL statements.
8/9/2019 SQL Lesson1
9/15
SQLDML and DDLSQLDML and DDLy SQL can be divided into two parts: The Data Manipulation Language
(DML) and the Data Definition Language (DDL).
y The query and update commands form the DML part of SQL:
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
y The DDL part of SQL permits database tables to be created ordeleted. It also define indexes (keys), specify links between tables,and impose constraints between tables. The most important DDLstatements in SQL are:
CREATEDATABASE - creates a new database
ALTERDATABASE - modifies a database
CREATE TABLE - creates a new table ALTER TABLE - modifies a table
DROP TABLE - deletes a table
CREATEINDEX - creates an index (search key)
DROPINDEX - deletes an index
8/9/2019 SQL Lesson1
10/15
The SQLSELECT StatementThe SQLSELECT Statement
y The SELECT statement is used to
select data from a database.
y The result is stored in a result table,
called the result-set.
y SQLSELECT Syntax
SELECT column_name(s)
FROM table_name and SELECT * FROM table_name
8/9/2019 SQL Lesson1
11/15
The SQLSELECT DISTINCTThe SQLSELECT DISTINCT
StatementStatementy In a table, some of the columns may
contain duplicate values. This is not aproblem, however, sometimes you will
want to list only the different (distinct)values in a table.
y The DISTINCT keyword can be usedto return only distinct (different)
values.y SQLSELECT DISTINCT Syntax
SELECT DISTINCT column_name(s)FROM table_name
8/9/2019 SQL Lesson1
12/15
The WHERE ClauseThe WHERE Clause
y The WHERE clause is used to extract
only those records that fulfill a
specified criterion.
y SQL WHERESyntax
SELECT column_name(s)
FROM table_name
WHERE column_name operator value
8/9/2019 SQL Lesson1
13/15
WHERE Clause ExampleWHERE Clause Example
8/9/2019 SQL Lesson1
14/15
Quotes Around Text FieldsQuotes Around Text Fieldsy SQL uses single quotes around text values (most database
systems will also accept double quotes).
y Although, numeric values should not be enclosed in quotes.
y For text values:
This is correct:
SELECT * FROM Persons WHERE FirstName='Tove'
This is wrong:
SELECT * FROM Persons WHERE FirstName=Tove For numericvalues:
This is correct:
SELECT * FROM Persons WHERE Year=1965
This is wrong:
SELECT * FROM Persons WHERE Year='1965'
8/9/2019 SQL Lesson1
15/15
OperatorsAllowed in theOperatorsAllowed in the
WHERE ClauseWHERE Clause