DBMS 3. course
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?
Today Relational algebra Relational calculus SQL DDL
Relational Query Languages
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 bidbnamecolor 101Santa MariaBlack 102PintaYellow 103NinaWhite sidsnameratingage 22dustin lubber rusty sidbidday /09/ /09/13
Relational algebra
Goal Describes the way of procedure of the relations Execution plan
Basic operations
Consequent operations a1a1 b1b1 a1a1 b2b2 a1a1 b3b3 a1a1 b4b4 a2a2 b1b1 a2a2 b3b3 a3a3 b2b2 a3a3 b3b3 a3a3 b4b4 a4a4 b1b1 a4a4 b2b2 a4a4 b3b3 b1b1 b2b2 b3b3 a1a1 a4a4
Interpretation of quotient
How to get the quotient
Example ab ed
Join
General join
Natural join visually
Practice
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
SQL – 1. blue Audis, projection SELECT * FROM C WHERE color=’blue’ AND type=’Audi’ SELECT plate_no FROM C WHERE color=’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)
Relational calculus
Goal
Atomic formulas
Formulas
Example
Example no. 2
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
SQL
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
27 January 2010
Sun?
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, … key foreign key(s), constraints (table, column), indexes)
CREATE TABLE name (attribute description, … PRIMARY KEY, UNIQUE – unique/secondary key (column) FOREIGN KEY – relationship with other table CHECK – 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 the attributes is given DBMS validates the domain constraints on 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
DROP and ALTER tables DROP TABLE Students ALTER TABLE Students ADD firstYear: integer
Example CREATE TABLE animals ( name VARCHAR2(25), species VARCHAR2(20), gender CHAR(1), born DATE, died DATE);
Giving primary key ALTER TABLE animals ADD ID INTEGER Unsigned PRIMARY KEY AUTO_INCREMENT FIRST;
Thank you for your attention! OR elearning.uni-obuda.hu