Download SQL

Survey
yes no Was this document useful for you?
   Thank you for your participation!

* Your assessment is very important for improving the workof artificial intelligence, which forms the content of this project

Document related concepts

Database wikipedia , lookup

Microsoft Jet Database Engine wikipedia , lookup

Open Database Connectivity wikipedia , lookup

Microsoft SQL Server wikipedia , lookup

Entity–attribute–value model wikipedia , lookup

Clusterpoint wikipedia , lookup

Ingres (database) wikipedia , lookup

SQL wikipedia , lookup

Extensible Storage Engine wikipedia , lookup

Object-relational impedance mismatch wikipedia , lookup

Join (SQL) wikipedia , lookup

PL/SQL wikipedia , lookup

Database model wikipedia , lookup

Relational model wikipedia , lookup

Transcript
SQL (Structured Query Language)



CS 157A
By
JIA HUANG
Definition

SQL-
is a database sublanguage for
querying and modifying
relational databases
History

SQL was developed by IBM research
in the mid 70’s and standardized by
ANSI in 1986.
Language Structure




SQL is a keyword based language.
Each statement begins with a unique
keyword.
SQL statements consist of clauses
which begin with a keyword.
SQL syntax is not case sensitive.
There are 3 basic categories of
SQL Statements:



SQL-Data Statements
SQL-Transaction Statements
SQL-Schema Statements
SQL-Data Statements
SELECT
-- query tables and views in the

INSERT
-- add rows to tables

UPDATE-- modify columns in table rows

DELETE

database
-- remove rows from tables
Syntax
SELECT column_name (using * for all
column)
FROM table_name
SELECT column_name
FROM table_name
WHERE condition
Example
“Customer" table
LastName
FirstName
Address
City
Hanson
Olla
5354 9th ST
San Jose
Stevenson
Peterson
Trove
Kari
57 Broadway Las Vegas
43 Storage
Reno
SELECT LastName,FirstName FROM
Customer
LastName
FirstName
Hanson
Olla
Stevenson
Peterson
Trove
Kari
Example
“Customer" table
LastName
FirstName
Address
City
Hanson
Olla
5354 9th ST
San Jose
Stevenson
Peterson
Trove
Kari
57 Broadway Las Vegas
43 Storage
Reno
SELECT Address FROM Customer WHERE
LASTNAME=“Hanson”
Address
5354 9th ST
With the WHERE clause, the following
operators can be used:
Operator
Description
=
Equal
<>
Not equal
>
Greater than
<
Less than
>=
Greater than or equal
<=
Less than or equal
BETWEEN
Between an inclusive
range
LIKE
Search for a pattern
Syntax
INSERT INTO table_name
VALUES (value1, value2,....)
You can also specify the columns for which you
want to insert data:
INSERT INTO table_name (column1,
column2,...)
VALUES (value1, value2,....)
Example
“Customer" table
LastName
FirstName
Address
City
Hanson
Olla
5354 9th ST
San Jose
Stevenson
Trove
57 Broadway Las Vegas
INSERT INTO Customer
VALUES (“Peterson”, ”Kari”, “43 Story”, ”Reno”)
LastName
FirstName
Address
City
Hanson
Olla
5354 9th ST
San Jose
Stevenson
Trove
57 Broadway Las Vegas
Peterson
Kari
43 Storage
Reno
Example
“Customer" table
LastName
FirstName
Address
City
Hanson
Olla
5354 9th ST
San Jose
Stevenson
Trove
57 Broadway Las Vegas
INSERT INTO Customer (LastName, City)
VALUES (“Peterson”, ”Reno”)
LastName
FirstName
Address
City
Hanson
Olla
5354 9th ST
San Jose
Stevenson
Trove
57 Broadway Las Vegas
Peterson
Reno
Syntax

UPDATE table_name
SET column_name = new_value
WHERE column_name = some_value
Example
“Customer" table
LastName
FirstName
Address
City
Hanson
Olla
5354 9th ST
San Jose
Stevenson
Trove
57 Broadway Las Vegas
UPDATE Customer
SET Address = “21 Century”
WHERE LastName =“Stevenson”
LastName
FirstName
Address
City
Hanson
Olla
5354 9th ST
San Jose
Stevenson
Trove
21 Century
Las Vegas
Syntax

DELETE FROM table_name
WHERE column_name = some_value
You can delete all rows by:
DELETE FROM table_name
DELETE * FROM table_name
Example
“Customer" table
LastName
FirstName
Address
City
Hanson
Olla
5354 9th ST
San Jose
Stevenson
Trove
57 Broadway Las Vegas
DELETE FROM Customer
WHERE LastName =“Stevenson”
LastName
FirstName
Address
City
Hanson
Olla
5354 9th ST
San Jose
Example
“Customer" table
LastName
FirstName
Address
City
Hanson
Olla
5354 9th ST
San Jose
Stevenson
Trove
57 Broadway Las Vegas
DELETE FROM Customer
OR
DELETE * FROM Customer
LastName
FirstName
Address
City
SQL-Schema Statements




CREATE TABLE -- create tables
DROP TABLE -- drop tables
GRANT -- grant privileges on tables
and views to other users
REVOKE-- revoke privileges on tables
and views from other users
Syntax

To create a database:
CREATE DATABASE database_name
To create a table in a database:
CREATE TABLE table_name (
column_name1 data_type,
column_name2 data_type, ....... )
Example

CREATE TABLE Customer ( LastName
varchar, FirstName varchar, Address varchar,
Age int )
Result:
“Customer" table
LastName
FirstName
Address
Age
The table below contains the most common data
types in SQL:
Data Type
Description
Integer (size)
int (size)
smallint (size)
tinyint (size)
Hold integers only. The maximum number of digits are
specified in parenthesis.
Decimal (size,d)
numeric (size,d)
Hold 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.
Date (yyyymmdd) Holds a date
Syntax

To delete a table (the table structure,
attributes, and indexes will also be
deleted):
DROP TABLE table_name

To delete a database:
DROP DATABASE database_name

Some other commands:
ORDER BY
AND & OR
JOIN
UNION
ALTER
GROUP BY
SELECT INTO
…
References

http://www.w3schools.com/sql/defau
lt.asp

http://www.firstsql.com/tutor.htm

http://www.tc.umn.edu/~hause011/