View
222
Download
3
Category
Preview:
Citation preview
Reminder
• Data independence: logical and physical• Concurrent processing
– Transaction– Deadlock– Rollback– Logging
• ER Diagrams
• Decision in design– Entity or attribute?– Entity or relationship?– Binary or ternary relationship?
Relational Query Languages
• Aim• Relational model supports simple and efficient
QLs• Query language programming language
– Non Turing complete (Turing machine)
Formal relational query languages
• 2 formalisms– Relational algebra– Relational calculus
• They substantiate the implementation level and the concrete query language (e.g. SQL)
Example
• Sailors• Boats• Reserves
R
S
B bid bname color
101 Santa Maria Black
102 Pinta Yellow
103 Nina White
sid sname rating age
22 dustin 7 45.0
31 lubber 8 55.0
58 rusty 10 35.0
sid bid day
22 101 25/09/13
58 103 11/09/13
Basic operations
• Union: • Difference: • Descartes multiplication: • Projection: • Selection:
– F: ith and jth element , where
Consequent operations
• Intersection: • Quotient:
– Just if S R
a1 b1
a1 b2
a1 b3
a1 b4
a2 b1
a2 b3
a3 b2
a3 b3
a3 b4
a4 b1
a4 b2
a4 b3
b1
b2
b3
a1
a4
S R
Interpretation of quotient
• E.g. who are the sailors who reserved all the boats?
• b: boat• s: sailor
• : s so that -for all b- there is an {s,b} record in A
sno pnos1 p1s1 p2s1 p3s1 p4s2 p1s2 p2s3 p2s4 p2s4 p4
pno p2
pnop2p4
pnop1p2p4
snos1s2s3s4
snos1s4
snos1
𝐴
𝐵1𝐵2
𝐵3
𝐴÷𝐵1 𝐴÷𝐵2 𝐴÷𝐵3
Join
• General join: , that fulfills T• Natural join (inner join): Same values in the same
attributes, • Outer join:
– There can be NULL in the result– R left join S: every line of R comes up at least once (if no
match than NULL for S)– R right join S: every line of S comes up at least once (if no
match than NULL for R)– R full join S: every line of S and R comes up at least once (if
no match than NULL for the other)
Practical example
• Cars (C): plate no, manufacturer, type, color• People (P): ID, name, job, address• Owns (O): car, people_id
• Task: query the job of the owners of the blue Audis
Steps
• Find the blue Audis
• Drop all the attributes except the plate no
• Connect to attribute car of relation O
• Drop all the attributes except people_id (projection)
• Connect with table P: • Filter to attribute job: ∏
job
( ∏people ID
( ∏plate no
(𝜎 color = blue t ype = Audi(𝐶))⋈plate no =car (𝑂 ))⋈𝑃 )
SQL – 1. blue Audis, projection
• SELECT * FROM C WHEREcolor=’blue’ AND type=’Audi’
• SELECT plate_no FROM C WHEREcolor=’blue’ AND type=’Audi’
SQL – 2. join with O, projection
• SELECT * FROM (SELECT plate_no FROM C WHERE
color=’blue’ AND type=’Audi’) inner_table1 INNER JOIN O ON (O.car=inner_table1.plate_no)• SELECT people_ID as ID FROM (SELECT plate_no FROM C WHERE
color=’blue’ AND type=’Audi’) inner_table1 INNER JOIN O ON (O.car=inner_table1.plate_no)
SQL – 3. join with table P
• SELECT * FROM (SELECT people_ID as ID FROM (SELECT plate_no FROM C WHERE
color=’blue’ AND type=’Audi’) INNER JOIN O ON (O.car=C.plate_no) inner_table2 INNER JOIN P USING (ID)
SQL – 4. projection
• SELECT job FROM (SELECT people_ID as ID FROM (SELECT plate_no FROM C WHERE
color=’blue’ AND type=’Audi’) INNER JOIN O ON (O.car=C.plate_no)) INNER JOIN P USING (ID)
Goal
• Formalism for the rows of the relation• Declarative• Expression:
– Rows t that satisfy function – E.g. Select … WHERE …
Atomic formulas
1. – row relation 2. – is an operator (e.g. <,>,=,…) that holds
between row ’s th element and row ’s th element
3. – hold between row ’s th element and constant
Formulas
is a formula if1. it is an atomic formula2. an expression made of formulas and logical operators 3. an expression made of formulas and logical operators (
– – there exist , for what holds– – for all , holds
4. expressions can be parenthesized to override precedence ()
5. there is no other formula
Finally
• Relational algebra and relational calculus can express the same
• Declarative part is user-friendly• The algebra (way of calculation) is the task of
the DB
History of SQL and dialects
• Main advantage: simple and efficient query• DBMS does the evaluation
– Semantics– DBMS can (re)order the operations to optimize– Solution set has to be the same
Development
• SQL-86• SQL-89 (smaller change)• SQL-92 (bigger change)• SQL:1999 (significantly extended)• SQL:2003 (slighly extended)• SQL:2008 (splitted into parts, extended)• SQL:2011 (newest, most of the DBMSs cannot
use)
20 April 2009
• Oracle buys Sun Microsystems• PostgreSQL (Sun), MySQL ( Sun), Firebird,
Interbase, MSACCESS• Operating systems: Oracle: Linux, Sun: Unix
(Solaris), Microsoft: Windows Server, IBM: Unix (AIX)
• Environment: Sun: Java, Microsoft: .NET, Others with external libraries
• Hardware: Sun: Sparc + Server/Workstation/ Storage, IBM: Datacenter, Server
27 January 2010• Oracle Corporation (NASDAQ:ORCL) announced today that
it had completed its acquisition of Sun Microsystems, Inc.”• „This time, the difficulty was persuading European antitrust
regulators who were concerned about the fate under Oracle of the open-source MySQL database software business that was part of Sun.”
• „With the addition of servers, storage, Sparc processors, the Solaris operating system, Java, and the MySQL database to Oracle's portfolio of database, middleware, and business applications, we plan to engineer and deliver open and integrated systems -- from applications to disk -- where all the pieces fit and work together out of the box.”
• www.sun.com www.oracle.com/sun/
Sun?
• OpenSolaris (OS): KO ( OpenIndiana)• MySql (DB): „still alive” ( MariaDB)• OpenOffice: KO ( LibreOffice, ApacheOO)• Licence replacements, support contracts
replaced ( obligatory HW support)
Oracle is unavoidable We’re gonna talk about Oracle dialect
Queries
Structured Query Language (SQL) – 4 parts• Data Definition Language (DDL): CREATE and
ALTER tables, views, and indexes• Data Manipulation Language (DML): INSERT,
UPDATE, and DELETE records• Data Control Language (DCL): GRANT and
REVOKE permissions, COMMIT and ROLLBACK transactions
• Query Language: SELECT
DDL – Data Definition Language
• Tables– CREATE TABLE– ALTER TABLE– DROP TABLE
• Views– CREATE VIEW– DROP VIEW
• Indexes– CREATE [UNIQUE] INDEX– DROP INDEX
Syntaxis
• CREATE TABLE name (attribute description,…keyforeign key(s),constraints (table, column),indexes)
• CREATE TABLE name(attribute description,…PRIMARY KEY,UNIQUE – unique/secondary key (column)FOREIGN KEY – relationship with other tableCHECK – constraints for columns (attributes),CREATE INDEX …)
Primary key
• Has to be– Unique– Cannot be NULL– In many cases an unsigned integer
• AUTO_INCREMENT– Increased automatically when inserted
Creating relations (tables)
• The domain of theattributes is given
• DBMS validates thedomain constraintson every insertion
CREATE TABLE Students(sid: CHAR(20), name: CHAR(20), login: CHAR(10), age: INTEGER, gpa: REAL)
CREATE TABLE Enrolled(sid: CHAR(20), cid: CHAR(20), grade: CHAR(2))
Data types in Oracle
• Everything is stored in tables• char(N) – varchar(N) – varchar2(N)• blob (vs. clob, ~2-4GB)• numeric(N[, M]) / number(N[, M]) / int, float• date / timestamp
Data types in MySQL
• char(N) – varchar(N)• blob vs. text: tiny~, ~, medium~, long~, max. 4GB)• [signed/ unsigned] tinyint (sbyte), smallint
(integer), int, bigint (long), float, double • date / time / datetime / timestamp /year
– DATE: YYYY-MM-DD– DATETIME: YYYY-MM-DD HH:MM:SS– TIMESTAMP: YYYY-MM-DD HH:MM:SS– TIME: HH:MM:SS– YEAR: YYYY
Example
• CREATE TABLE animals (name VARCHAR2(25),species VARCHAR2(20),gender CHAR(1),born DATE,died DATE);
Thank you for your attention!
• www.uni-obuda.hu/users/varkonyi.teri• OR• elearning.uni-obuda.hu• varkonyi.teri@nik.uni-obuda.hu
Recommended