Professional Documents
Culture Documents
Aula T03
Chapter 3: SQL
Adapted from: Database System Concepts - 5th Edition
©Silberschatz, Korth and Sudarshan
Example:
create table branch
(branch_name char(15) not null,
branch_city char(30),
assets integer)
not null
primary key (A1, ..., An )
Ai represents an attribute
Ri represents a relation
P is a predicate.
This query is equivalent to the relational algebra expression.
Find the name, loan number and loan amount of all customers
having a loan at the Perryridge branch.
Find the names of all branches that have greater assets than
some branch located in Brooklyn.
select distinct T.branch_name
from branch as T, branch as S
where T.assets > S.assets and S.branch_city = 'Brooklyn'
Keyword as is optional and may be omitted
borrower as T ≡
borrower T
Find the names of all branches where the average account balance is
more than $1,200.
Find all customers who have a loan at the bank but do not have
an account at the bank
select customer_name
from all_customer
where branch_name = 'Perryridge'
or equivalently
Increase all accounts with balances over $10,000 by 6%, all other
accounts receive 5%.
Write two update statements:
update account
set balance = balance 1.06
where balance > 10000
update account
set balance = balance 1.05
where balance 10000
The order is important
Can be done better using the case statement (next slide)
update account
set balance = case
when balance <= 10000 then balance *1.05
else balance * 1.06
end
Relation loan
Relation borrower
Find all customers who have either an account or a loan (but not both)
at the bank.
select customer_name
from (depositor natural full outer join borrower )
where account_number is null or loan_number is null
Types and domains are similar. Domains can have constraints, such
as not null, specified on them.
not null
primary key
unique
check (P ), where P is a predicate
...
Triggers
Authorization in SQL
70
Sistemas Gestores de Bases de Dados - David Aveiro - UMa 70
Limitations of SQL Authorization
SQL does not support authorization at a tuple level
E.g. we cannot restrict students to see only (the tuples storing) their
own grades
With the growth in Web access to databases, database accesses come
primarily from application servers.
End users don't have database user ids, they are all mapped to the
same database user id
All end-users of an application (such as a web application) may be mapped
to a single database user
The task of authorization in above cases falls on the application program,
with no support from SQL
Benefit: fine grained authorizations, such as to individual tuples, can be
implemented by the application.
Drawback: Authorization must be done in application code, and may
be dispersed all over an application
Checking for absence of authorization loopholes becomes very difficult
since it requires reading large amounts of application code
71
Sistemas Gestores de Bases de Dados - David Aveiro - UMa 71
Triggers
72
Sistemas Gestores de Bases de Dados - David Aveiro - UMa 72
Trigger Example
73
Sistemas Gestores de Bases de Dados - David Aveiro - UMa 73
Trigger Example in SQL:1999
74
Sistemas Gestores de Bases de Dados - David Aveiro - UMa 74
Triggering Events and Actions in SQL
75
Sistemas Gestores de Bases de Dados - David Aveiro - UMa 75
When Not To Use Triggers
76
Sistemas Gestores de Bases de Dados - David Aveiro - UMa 76
Audit Trails
77
Sistemas Gestores de Bases de Dados - David Aveiro - UMa 77
End of Chapter
78
Apresentação do SGBD MySQL
79
Plataforma para o projecto
XAMPP é uma compilação de software livre
Apache Web Server + MySQL + PHP + Perl + …
e ainda: SSL, servidor SMTP, servidor FTP,
phpMyAdmin, etc.
XAMPP é multi-plataforma
Linux, Windows, Mac OS X, Solaris
XAMPP pode ser distribuído livremente
88
Tipos de dados numéricos
Type Bytes Minimum Value Maximum Value
(Signed/Unsigned) (Signed/Unsigned)
TINYINT 1 -128 127
0 255
SMALLINT 2 -32768 32767
0 65535
MEDIUMINT 3 -8388608 8388607
0 16777215
INT 4 -2147483648 2147483647
0 4294967295
BIGINT 8 -9223372036854775808 9223372036854775807
0 18446744073709551615
join_condition:
ON conditional_expr
| USING (column_list)