Presentation is loading. Please wait.

Presentation is loading. Please wait.

DBMS 3. course. Reminder Data independence: logical and physical Concurrent processing – Transaction – Deadlock – Rollback – Logging ER Diagrams.

Similar presentations


Presentation on theme: "DBMS 3. course. Reminder Data independence: logical and physical Concurrent processing – Transaction – Deadlock – Rollback – Logging ER Diagrams."— Presentation transcript:

1 DBMS 3. course

2 Reminder Data independence: logical and physical Concurrent processing – Transaction – Deadlock – Rollback – Logging ER Diagrams

3 Decision in design – Entity or attribute? – Entity or relationship? – Binary or ternary relationship?

4 Today Relational algebra Relational calculus SQL DDL

5 Relational Query Languages

6 Formal relational query languages 2 formalisms – Relational algebra – Relational calculus They substantiate the implementation level and the concrete query language (e.g. SQL)

7 Example Sailors Boats Reserves R S B bidbnamecolor 101Santa MariaBlack 102PintaYellow 103NinaWhite sidsnameratingage 22dustin745.0 31lubber855.0 58rusty1035.0 sidbidday 2210125/09/13 5810311/09/13

8 Relational algebra

9 Goal Describes the way of procedure of the relations Execution plan

10 Basic operations

11 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

12 Interpretation of quotient

13 How to get the quotient

14 Example ab ed

15

16 Join

17 General join

18 Natural join visually

19 Practice

20 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

21 Steps

22 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’

23 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)

24 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)

25 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)

26 Relational calculus

27 Goal

28 Atomic formulas

29 Formulas

30 Example

31 Example no. 2

32 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

33 SQL

34 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

35 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)

36 20 April 2009

37 27 January 2010

38 Sun?

39 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

40 DDL – Data Definition Language Tables – CREATE TABLE – ALTER TABLE – DROP TABLE Views – CREATE VIEW – DROP VIEW Indexes – CREATE [UNIQUE] INDEX – DROP INDEX

41 Syntaxis CREATE TABLE name (attribute description, … key foreign key(s), constraints (table, column), indexes)

42 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 …)

43 Primary key Has to be – Unique – Cannot be NULL – In many cases an unsigned integer AUTO_INCREMENT – Increased automatically when inserted

44 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))

45 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

46 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

47 DROP and ALTER tables DROP TABLE Students ALTER TABLE Students ADD firstYear: integer

48 Example CREATE TABLE animals ( name VARCHAR2(25), species VARCHAR2(20), gender CHAR(1), born DATE, died DATE);

49 Giving primary key ALTER TABLE animals ADD ID INTEGER Unsigned PRIMARY KEY AUTO_INCREMENT FIRST;

50 Thank you for your attention! www.uni-obuda.hu/users/varkonyi.teri OR elearning.uni-obuda.hu varkonyi.teri@nik.uni-obuda.hu


Download ppt "DBMS 3. course. Reminder Data independence: logical and physical Concurrent processing – Transaction – Deadlock – Rollback – Logging ER Diagrams."

Similar presentations


Ads by Google