DB_Chapter_7
DB_Chapter_7
1
SQL Introduction
SQL is Structured Query Language, which is a computer
language for storing, manipulating and retrieving data stored in a
relational database.
SQL is the standard language for Relational Database System.
All the Relational Database Management Systems (RDMS) like
MySQL, MS Access, Oracle, Sybase, Informix, Postgres and SQL
Server use SQL as their standard database language.
Many standards out there:
▪ ANSI SQL, SQL92 (a.k.a. SQL2), SQL99 (a.k.a. SQL3), ….
▪ Vendors support various subsets: watch for fun discussions in
class!
2
Why WE CARE SQL?
SQL is widely popular because it offers the following
advantages:
Allows users to access and describe data in the relational
database management systems.
Allows users to define the data in a database and manipulate
that data.
Allows to embed within other languages using SQL modules,
libraries & pre-compilers.
Allows users to create and drop databases and tables.
Allows users to create view, stored procedure, functions in a
database.
Allows users to set permissions on tables, procedures and
views.
3
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
4
SQL Datatypes
SQL data types are used to represent the nature of the data
that can be stored in the database table.
For example, in a particular column of a table, if we want to
store a string type of data then we will have to declare a
string data type of this column.
Data types mainly classified into three categories for every
database.
1. String Data types
2. Numeric Data types
3. Date and time Data types
5
SQL Datatypes(Con..)
Data Types in MySQL, SQL Server and Oracle Databases
A list of data types used in SQL Server database.
SQL Server String Data Type
char(n) It is a fixed width character string data type. Its size can be up to 8000 characters.
varchar(n) It is a variable width character string data type. Its size can be up to 8000
characters.
varchar(max) It is a variable width character string data types. Its size can be up to
1,073,741,824 characters.
text It is a variable width character string data type. Its size can be up to 2GB of text
data.
nchar It is a fixed width Unicode string data type. Its size can be up to 4000 characters.
nvarchar It is a variable width Unicode string data type. Its size can be up to 4000
characters.
ntext It is a variable width Unicode string data type. Its size can be up to 2GB of text
data.
binary(n) It is a fixed width Binary string data type. Its size can be up to 8000 bytes.
varbinary It is a variable width Binary string data type. Its size can be up to 8000 bytes.6
SQL Datatypes(Con..)
SQL Server Numeric Data Types
bit It is an integer that can be 0, 1 or null.
tinyint It allows whole numbers from 0 to 255.
Smallint It allows whole numbers between -32,768 and 32,767.
Int It allows whole numbers between -2,147,483,648 and 2,147,483,647.
bigint It allows whole numbers between -9,223,372,036,854,775,808 and
9,223,372,036,854,775,807.
float(n) It is used to specify floating precision number data from -1.79E+308 to
1.79E+308. The n parameter indicates whether the field should hold the
4 or 8 bytes. Default value of n is 53.
real It is a floating precision number data from -3.40E+38 to 3.40E+38.
money It is used to specify monetary data from -922,337,233,685,477.5808 to
922,337,203,685,477.5807.
7
SQL Datatypes(Con..)
SQL Server Date and Time Data Type
datetime It is used to specify date and time combination. It supports range from
January 1, 1753, to December 31, 9999 with an accuracy of 3.33
milliseconds.
datetime2 It is used to specify date and time combination. It supports range from
January 1, 0001 to December 31, 9999 with an accuracy of 100
nanoseconds
date It is used to store date only. It supports range from January 1, 0001 to
December 31, 9999
time It stores time only to an accuracy of 100 nanoseconds
timestamp It stores a unique number when a new row gets created or modified.
The time stamp value is based upon an internal clock and does not
correspond to real time. Each table may contain only one-time stamp
variable.
8
SQL
Data Definition Language (DDL)
Create/alter/delete tables and their attributes
Following lectures...
Data Manipulation Language (DML)
Query one or more tables – discussed next !
Insert/delete/modify tuples in tables
Data control language(DCL)
Grant, revoke
9
SQL Basic Commands/ Queries
The standard SQL commands to interact with relational
databases are CREATE, SELECT, INSERT, UPDATE, DELETE and
DROP.
These commands can be classified into the following groups
based on their nature −
DDL - Data Definition Language
❖ CREATE:- Creates a new table, a view of a table, or other
object in the database.
ALTER፦ Modifies an existing database object, such as a
table.
DROP፦ Deletes an entire table, a view of a table or other
objects in the database.
TRUNCATE: It is used to delete all the rows from the table and
free the space containing the table. 10
SQL Basic Commands(Con…)
DML - Data Manipulation Language
❖ SELECT:- Retrieves certain records from one or more tables in
the database.
❖ INSERT: Creates new a record in to the database.
❖ UPDATE:- Modifies records.
❖ DELETE:- Deletes records.
11
SQL Constraints
Constraints are the rules enforced on data columns on a table.
These are used to limit the type of data that can go into a table.
Following are some of the most used constraints available in SQL.
NOT NULL Constraint: Ensures that a column cannot have a NULL value.
DEFAULT Constraint: Provides a default value for a column when none is
specified.
UNIQUE Constraint: Ensures that all the values in a column are different.
PRIMARY key: Uniquely identifies each row/record in a database table.
FOREIGN key: Uniquely identifies a row/record in any another database
table.
CHECK Constraint: The CHECK constraint ensures that all values in a
column satisfy certain conditions.
INDEX: Used to create and retrieve data from the database very quickly.
12
Data Integrity
The following categories of data integrity exist with each
RDBMS:
Entity Integrity: There are no duplicate rows in a table.
13
How to create database
The database developers and the users use this statement
in SQL for creating the new database in the database
systems.
It creates the database with the name which has been
specified in the Create Database statement.
Syntax: CREATE DATABASE database_name:
Example : This example creates the Student database.
To create the Student database, you have to type the
following command in Structured Query Language:
CREATE DATABASE Student ;
When this query is executed successfully, then it will
show the following output: Database created
successfully
14
Con….
SQL DROP Database:- The SQL Drop Database statement
deletes the existing database permanently from the database
system.
Syntax of Drop Database Statement in SQL:
DROP DATABASE Database_Name;
In this SQL syntax, we have to specify the name of that database
which we want to delete permanently from the database system.
We can also delete multiple databases easily by using the single
DROP syntax:
DROP DATABASE Database_Name1, [ Database_Name2, ......., Data
base_NameN ] ;
15
Con….
SQL RENAME Database:
In some situations, database users and administrators want to
change the name of the database for some technical reasons.
So, the Rename Database statement in SQL is used to change
the name of the existing database.
Syntax of Rename Database in SQL:-
ALTER DATABASE old_database_name MODIFY NAME = new_data
base_name;
Example :Suppose we want to rename the Student Database.
For this, we have to type the following query in SQL:
ALTER DATABASE Student MODIFY NAME = College ;
16
Con….
SQL SELECT Database
❖ Suppose database users and administrators want to perform
some operations on tables, views, and indexes on the specific
existing database in SQL.
❖ Firstly, they have to select the database on which they want to
run the database queries.
❖ Any database user and administrator can easily select the
particular database from the current database server using
the USE statement in SQL.
❖ Syntax : USE database_name;
17
Con….
Example: Suppose, you want to work with
the Hospital database. For this firstly, you have to check that if
the Hospital database exists on the current database server or
not by using the following query:
❖ SHOW DATABASES;
❖ If the Hospital database is shown in the output, then you have
to execute the following query to select the Hospital
database:
❖ USE Hospital;
18
Working with table structures
❖ Table is a collection of data, organized in terms of rows and
columns. In DBMS term, table is known as relation and row
as tuple.
CREATE TABLE: Create a new table in the database.
TRUNCATE TABLE: Delete all data in a big table fast and efficiently.
19
Tables in SQL
Product
20
Tables Explained
The schema of a table is the table name and its
attributes:
Product(PName, Price, Category, Manfacturer)
A key is an attribute whose values are unique;
we underline a key
Product(PName, Price, Category, Manfacturer)
21
Tables Explained
A tuple = a record
Restriction: all attributes are of atomic type
22
SQL TABLE Variable
The SQL Table variable is used to create, modify, rename,
copy and delete tables.
SQL CREATE TABLE statement is used to create table in a
database.
Table variable was introduced by Microsoft.
Table variables are used to store a set of records. So
declaration syntax generally looks like CREATE TABLE
syntax. create table "tablename"
("column1" "data type",
"column2" "data type",
...
"columnN" "data type");
23
Con…
Example :- Let us take an example to create a STUDENTS table
with ID as primary key and NOT NULL are the constraint
showing that these fields cannot be NULL while creating records
in the table.
SQL> CREATE TABLE STUDENTS (
ID INT NOT NULL,
NAME VARCHAR (20) NOT NULL,
AGE INT NOT NULL,
ADDRESS CHAR (25),
PRIMARY KEY (ID)
);
24
Con…
SQL DROP TABLE : it used to delete a table definition and all
data from a table.
Syntax to drop table form the database
DROP TABLE Table_name;
Example: let us take an example we verify STUDENTS table and then we
would like to delete it from the database .
SQL> DROP TABLE STUDENTS;
25
Con…
SQL DELET TABLE: it used to delete rows from a table . If
you want to remove a specific row from a table you should use
WHERE condition
Syntax: DELETE FROM Table_Name [WHERE Condition];
But if you do not specify the WHERE condition it will remove all the rows from the table.
DELETE FROM table_name;
There are some more terms similar to DELETE statement like as DROP statement and
TRUNCATE statement but they are not exactly same there are some differences
between them.
Difference between DELETE and TRUNCATE statements
There is a slight difference b/w delete and truncate statement.
The DELETE statement only deletes the rows from the
table based on the condition defined by WHERE clause or
delete all the rows from the table when condition is not
specified. 26
Con…
But it does not free the space containing by the table.
The TRUNCATE statement: it is used to delete all the rows from the
table and free the containing space. Let's see an "employee" table.
Emp_id Name Address Salary
1 Abebe kasa 22000
2 Kemal Ahmed 13000
3 Chala Debebe 24000
Execute the following query to truncate the table:
TRUNCATE TABLE employee;
27
Con…
SQL RENAME TABLE
In some situations, database administrators and users want
to change the name of the table in the SQL database
because they want to give a more relevant name to the
table.
Any database user can easily change the name by using the
RENAME TABLE and ALTER TABLE statement in Structured
Query Language.
Syntax : RENAME old_table _name To new_table_name ;
The RENAME TABLE and ALTER TABLE syntax help in
changing the name of the table.
28
Con…
Example: Let's take an example of a table name Employee:
Emp_Id Emp_Name Emp_Salary Emp_City
201 Abebe 25000 Bahir Dar
202 Bontu 45000 Adama
203 kemal 30000 Assosa
204 Decha 29000 Hawasa
Table: Employee
Suppose, you want to change the name of the above table into
the "Coding_Employees".
For this, you have to type the following RENAME statement in SQL:
RENAME Employee To Coding_Employees ;
After this statement, the table "Employee" will be changed into the table
name "Coding_Employees".
29
Con…
Syntax of ALTER TABLE statement in SQL:
ALTER TABLE old_table_name RENAME TO new_table_name;
Example:-
ALTER TABLE Employee RENAME To Coding_Employees ;
30
SQL Query : INSERT
Basic form:
31
SQL Query : INSERT
Basic form:
Insert into Students values(1 ,'Girmaw','Andualem','assosa',30)
Insert into Students values(2 ,'Kemal','Hussen','Assosa',29)
Insert into Students values(3 ,'Fekadu','Eshetu','Assosa',33)
Insert into Students values(4 ,'Balmlay','Gebeyhu','Assosa',27)
Or
Insert into Students values(1
,'Girmaw','Andualem','assosa',30) ,(2
,'Kemal','Hussen','Assosa',29) (3
,'Fekadu','Eshetu','Assosa',33), (4
,'Balmlay','Gebeyhu','Assosa',27)
32
SQL Query : SELECT
Basic form: (plus many more bells and whistles)
SELECT <attributes>
FROM <one or more relations>
WHERE <conditions>
33
Simple SQL Query
Product PName Price Category
Manufactu
rer
Gizmo $19.99 Gadgets GizmoWorks
Powergizmo $29.99 Gadgets GizmoWorks
SingleTouch $149.99 Photography Canon
MultiTouch $203.99 Household Hitachi
SELECT *
FROM Product
WHERE category=‘Gadgets’
34
Simple SQL Query
PName Price Category Manufacturer
Product Gizmo $19.99 Gadgets GizmoWorks
Powergizmo $29.99 Gadgets GizmoWorks
SingleTouch $149.99 Photography Canon
MultiTouch $203.99 Household Hitachi
35
Notation
Input Schema
Output Schema
36
Details
Case insensitive:
Same: SELECT Select select
Same: Product product
Different: ‘Seattle’ ‘seattle’
Constants:
‘abc’ - yes
“abc” - no
37
The LIKE operator
SELECT *
FROM Products
WHERE PName LIKE ‘%gizmo%’
38
Eliminating Duplicates
Category
SELECT DISTINCT category Gadgets
FROM Product Photography
Household
Compare to:
Category
SELECT category FROM
Gadgets
Product Gadgets
Photography
Household
39
Ordering the Results
SELECT pname, price, manufacturer
FROM Product
WHERE category=‘gizmo’ AND price > 50
ORDER BY price, pname
40
POP up question
PName Price Category Manufacturer
Gizmo $19.99 Gadgets GizmoWorks
Powergizmo $29.99 Gadgets GizmoWorks
SingleTouch $149.99 Photography Canon
MultiTouch $203.99 Household Hitachi
GizmoWorks 25 USA
Canon 65 Japan
Hitachi 15 Japan
42
Joins
Product (pname, price, category, manufacturer)
Company (cname, stockPrice, country)
43
Joins
Product Company
PName Price Category Manufacturer
Gizmo $19.99 Gadgets GizmoWorks Cname StockPrice Country
Powergizmo $29.99 Gadgets GizmoWorks GizmoWorks 25 USA
SingleTouch $149.99 Photography Canon Canon 65 Japan
MultiTouch $203.99 Household Hitachi Hitachi 15 Japan
44
More Joins
▪ Product (pname, price, category, manufacturer)
▪ Company (cname, stockPrice, country)
SELECT cname
FROM
WHERE
45
A Subtlety about Joins
▪ Product (pname, price, category, manufacturer)
▪ Company (cname, stockPrice, country)
❑ Find all countries that manufacture some product in the ‘Gadgets’ category.
SELECT Country
FROM Product, Company
WHERE Manufacturer=CName AND Category=‘Gadgets’
USA
Unexpected duplicates USA
46
A Subtlety about Joins
Product Company
Name Price Category Manufacturer Cname StockPrice Country
Gizmo $19.99 Gadgets GizmoWorks GizmoWorks 25 USA
Powergizmo $29.99 Gadgets GizmoWorks Canon 65 Japan
SingleTouch $149.99 Photography Canon Hitachi 15 Japan
MultiTouch $203.99 Household Hitachi
SELECT Country
FROM Product, Company
WHERE Manufacturer=CName AND Category=‘Gadgets’
Country
What is ??
the problem ? ??
What’s the
solution ?
47
Tuple Variables
▪ Person(pname, address, worksfor)
▪ Company(cname, address)
SELECT DISTINCT pname, address Which
FROM Person, Company address ?
WHERE worksfor = cname
Answer = {}
for x1 in R1 do
for x2 in R2 do
…..
for xn in Rn do
if Conditions
then Answer = Answer {(a1,…,ak)}
return Answer
49
An Unintuitive Query
SELECT DISTINCT R.A
FROM R, S, T
WHERE R.A=S.A OR R.A=T.A
50
Subqueries Returning Relations
Company(name, city)
Product(pname, maker)
Purchase(id, product, buyer)
SELECT Company.city
FROM Company
WHERE Company.name IN
(SELECT Product.maker
FROM Purchase , Product
WHERE Product.pname=Purchase.product
AND Purchase .buyer = ‘Aster‘);
51
Subqueries Returning Relations
Is it equivalent to this ?
SELECT Company.city
FROM Company, Product, Purchase
WHERE Company.name= Product.maker
AND Product.pname = Purchase.product
AND Purchase.buyer = ‘Joe Blow’
Beware of duplicates !
52
Removing Duplicates
SELECT DISTINCT Company.city
FROM Company
WHERE Company.name IN
(SELECT Product.maker
FROM Purchase , Product
WHERE Product.pname=Purchase.product
AND Purchase .buyer = ‘Aster‘);
53
Subqueries Returning Relations
You can also use: s > ALL R
s > ANY R
EXISTS R
SELECT name
FROM Product
WHERE price > ALL (SELECT price
FROM Purchase
WHERE maker=‘Gizmo-Works’)
54
Correlated Queries
▪ Movie (title, year, director, length)
Find movies whose title appears more than once.
correlation
Note
▪ scope of variables
▪ this can still be expressed as single SFW
55
Complex Correlated Query
▪Product ( pname, price, category, maker, year)
Find products (and their manufacturers) that are more expensive
than all products made by the same manufacturer before 1972
56
Aggregation
• SQL supports several aggregation operations:
Sum, count, min, Max, avg
SELECT avg(price)
FROM Product
WHERE maker=“Toyota”
SELECT count(*)
FROM Product
WHERE year > 1995
57
Aggregation: Count
COUNT applies to duplicates, unless otherwise stated:
same as Count(*)
We probably want:
58
More Examples
Purchase(product, date, price, quantity)
59
Simple Aggregations
Purchase
Product Date Price Quantity
Bagel 10/21 1 20
Banana 10/3 0.5 10
Banana 10/10 1 10
Bagel 10/25 1.50 20
50 (= 20+30)
60
Grouping and Aggregation
Purchase(product, date, price, quantity)
61
Grouping and Aggregation
1. Compute the FROM and WERE clauses.
62
1&2. FROM-WHERE-GROUPBY
Bagel 10/21 1 20
Banana 10/10 1 10
63
3. SELECT
64
GROUP BY v.s. Nested Quereis
SELECT product, Sum(price*quantity) AS TotalSales
FROM Purchase
WHERE date > ‘10/1/2005’
GROUP BY product
65
Another Example
SELECT product,
sum(price * quantity) AS SumSales
max(quantity) AS MaxQuantity
FROM Purchase
GROUP BY product
What does
it mean ?
66
HAVING Clause
Same query, except that we consider only products that
had at least 100 buyers.
67
General form of Grouping and Aggregation
SELECT S
FROM R1,…,Rn
WHERE C1
GROUP BY a1,…,ak
HAVING C2’
Why ?
S = may contain attributes a1,…,ak and/or any aggregates but NO OTHER
ATTRIBUTES
C1 = is any condition on the attributes in R1,…,Rn
C2 = is any condition on aggregate expressions
68
General form of Grouping and Aggregation
SELECT S
FROM R1,…,Rn
WHERE C1
GROUP BY a1,…,ak
HAVING C2
Evaluation steps:
1. Evaluate FROM-WHERE, apply condition C1
2. Group by the attributes a1,…,ak
3. Apply condition C2 to each group (may have aggregates)
4. Compute aggregates in S and return the result
69
Advanced SQLizing
1. Getting around INTERSECT and EXCEPT
2. Quantifiers
70
INTERSECT and EXCEPT: not in SQL Server
71
2. Quantifiers
▪ Product ( pname, price, company)
▪ Company( cname, city)
Find all companies that make some products with price < 100
Existential: easy ! ☺
72
2. Quantifiers
Find all companies that make only products with price < 100
same as:
Find all companies s.t. all of their products have price < 100
Universal: hard !
73
2. Quantifiers
1. Find the other companies: i.e. s.t. some product 100
SELECT DISTINCT Company.cname
FROM Company
WHERE Company.cname IN (SELECT Product.company
FROM Product
WHERE Produc.price >= 100
2. Find all companies s.t. all their products have price < 100
SELECT DISTINCT Company.cname
FROM Company
WHERE Company.cname NOT IN (SELECT Product.company
FROM Product
WHERE Produc.price >= 100
74
3. Group-by v.s. Nested Query
▪ Author(login,name)
▪ Wrote(login,url)
Find authors who wrote ³ 10 documents:
Attempt 1: with nested queries
75
3. Group-by v.s. Nested Query
Find all authors who wrote at least 10 documents:
Attempt 2: SQL style (with GROUP BY)
76
3. Group-by v.s. Nested Query
▪ Author(login,name)
▪ Wrote(login,url)
▪ Mentions(url,word)
SELECT Author.name
FROM Author, Wrote, Mentions
WHERE Author.login=Wrote.login AND Wrote.url=Mentions.url
GROUP BY Author.name
HAVING count(distinct Mentions.word) > 10000
77
Two Examples
▪ Store(sid, sname)
▪ Product(pid, pname, price, sid)
Find all stores that sell only products with price > 100
same as:
Find all stores s.t. all their products have price > 100)
78
SELECT Store.name
FROM Store, Product
WHERE Store.sid = Product.sid
GROUP BY Store.sid, Store.name Why both ?
HAVING 100 < min(Product.price)
SELECT Store.name
FROM Store
WHERE
100 < ALL (SELECT Product.price
FROM product
WHERE Store.sid = Product.sid)
SELECT Store.name
FROM Store
WHERE Store.sid NOT IN
(SELECT Product.sid
FROM Product
WHERE Product.price <= 100)
79
Two Examples
▪ Store(sid, sname)
▪ Product(pid, pname, price, sid)
80
Two Examples
This is easy but doesn’t do what we want:
SELECT Store.sname, max(Product.price)
FROM Store, Product
WHERE Store.sid = Product.sid
GROUP BY Store.sid, Store.sname
Better:
81
Two Examples
Finally, choose some pid arbitrarily, if there are many with highest price:
82
NULLS in SQL
Whenever we don’t have a value, we can put a NULL
Can mean many things:
▪ Value does not exist
▪ Value exists but is unknown
▪ Value not applicable
▪ Etc.
The schema specifies for each attribute if can be null
(nullable attribute) or not
How does SQL cope with tables that have NULLs ?
83
Null Values
If x= NULL, then 4*(3-x)/7 is still NULL
84
Null Values
C1 AND C2 = min(C1, C2)
C1 OR C2 = max(C1, C2)
NOT C1 = 1 – C1
SELECT *
FROM Person
WHERE (age < 25) AND
(height > 6 OR weight > 190)
85
Null Values
Unexpected behavior:
SELECT *
FROM Person
WHERE age < 25 OR age >= 25
86
Null Values
▪ Can test for NULL explicitly:
x IS NULL
x IS NOT NULL
SELECT *
FROM Person
WHERE age < 25 OR age >= 25 OR age IS NULL
87
Outerjoins
Explicit joins in SQL = “inner joins”:
▪ Product(name, category)
▪ Purchase(prodName, store)
Same as:
SELECT Product.name, Purchase.store
FROM Product, Purchase
WHERE Product.name = Purchase.prodName
89
Product Purchase
Name Category ProdName Store
Name Store
Gizmo Wiz
Camera Ritz
Camera Wiz
OneClick NULL
90
Application
Compute, for each product, the total number of sales in ‘September’
▪ Product(name, category)
▪ Purchase(prodName, month, store)
What’s wrong ?
91
Application
Compute, for each product, the total number of sales in ‘September’
▪ Product(name, category)
▪ Purchase(prodName, month, store)
92
Outer Joins
Left outer join:
93