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 are inspected Relational algebra: procedural approach – Execution methods are unambiguous Relational calculus: declarative (users can tell what they expect in the result set, not the way of calculation) These substantiate the implementation level and the concrete query language (pl. SQL)

7 Bases Queries are executed on relations (e.g. a tables), and the result of the query is also a relation – The schema of the input relation is fixed – The output schema is fixed and determined by the structure of the query language

8 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

9 Relational algebra

10 Goal Creating a system that describes the way of procedure of the relations Procedural approach: how to calculate the result (execution plan)

11 Basic operations

12 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

13 Interpretation of quotient

14 How to get the quotient

15 Example ab ed

16

17 Join

18 General join

19 Natural join visually

20 Practice

21 Practical example Cars (C): plate no, manufacturer, type, color People (P): ID, name, job, address Owns (O): car, people Task: the job of the owners of the blue Audis

22 Steps

23 SQL – 1. condition, projection SELECT * FROM C WHERE color=’blue’ AND type=’Audi’ SELECT plate_no FROM C WHERE color=’blue’ AND type=’Audi’

24 SQL – 2. join with O, projection SELECT * FROM (SELECT plate_no FROM C WHERE color=’blue’ AND type=’Audi’) INNER JOIN O ON (O.car=C.plate_no) SELECT ID FROM (SELECT plate_no FROM C WHERE color=’blue’ AND type=’Audi’) INNER JOIN O ON (O.car=C.plate_no)

25 SQL – 3. join with table P SELECT * FROM (SELECT 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 SQL – 4. projection SELECT job FROM (SELECT 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)

27 Relational calculus

28 Goal

29 Atomic formulas

30 Formulas

31 Example

32 Example no. 2

33 Finally Relational algebra and relational calculus can express the same Declarative part is convenient in the queries (user-friendly) The algebra (way of calculation) is the task of the DB, it is hidden from the user

34 SQL

35 History of SQL and dialects Main advantage: provides a simple and efficient data query Queries are written intuitively, the DBMS does the evaluation – Semantics are important – DBMS can (re)order the operations to optimize, but the solution set has to be the same

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

37 20 April 2009

38 27 January 2010

39 Sun?

40 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

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

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

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

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

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

46 Data types in Oracle Everything is stored in tables char(N) – varchar(N) – varchar2(N) [for special non-ascii characters] blob (vs. clob, ~2-4GB) [binary/character large object] numeric(N[, M]) / number(N[, M]) / int, float date / timestamp

47 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

48 DROP and ALTER tables DROP TABLE Students ALTER TABLE Students ADD firstYear: integer Deletes relation (table) Students. All the schema and all the records are deleted.  Giving new attribute. The new attribute is given to all of the rows with value NULL.

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

50 Giving primary key ALTER TABLE animals ADD ID INTEGER Unsigned PRIMARY KEY AUTO_INCREMENT FIRST; -- FIRST = it will be the first column (at e.g. SELECT *)

51 Thank you for your attention!


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

Similar presentations


Ads by Google