Chapter 5 Types. 5-2 Topics in this Chapter Values vs. Variables Types vs. Representations Type Definition Operators Type Generators SQL Facilities.

Slides:



Advertisements
Similar presentations
Copyright © 2011 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Chapter 5 More SQL: Complex Queries, Triggers, Views, and Schema Modification.
Advertisements

The Relational Model and Relational Algebra Nothing is so practical as a good theory Kurt Lewin, 1945.
Relational Database Design UNIT II 1. 2 Advantages of Using Database Systems Centralized control of a firm’s data Redundancy can be reduced (avoid keeping.
D ATABASE S YSTEMS I R ELATIONAL A LGEBRA. 22 R ELATIONAL Q UERY L ANGUAGES Query languages (QL): Allow manipulation and retrieval of data from a database.
Chapter 7:: Data Types Programming Language Pragmatics
Concepts of Database Management Seventh Edition
1 Relational Algebra & Calculus. 2 Relational Query Languages  Query languages: Allow manipulation and retrieval of data from a database.  Relational.
Compiler Construction
Copyright © 2011 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Chapter 5 More SQL: Complex Queries, Triggers, Views, and Schema Modification.
Introduction to Structured Query Language (SQL)
Operator Overloading in C++ Systems Programming. Systems Programming: Operator Overloading 22   Fundamentals of Operator Overloading   Restrictions.
1 Chapter 4 Language Fundamentals. 2 Identifiers Program parts such as packages, classes, and class members have names, which are formally known as identifiers.
Chapter 3 An Introduction to Relational Databases.
1 Type Type system for a programming language = –set of types AND – rules that specify how a typed program is allowed to behave Why? –to generate better.
Database Systems More SQL Database Design -- More SQL1.
Introduction to Structured Query Language (SQL)
Copyright © 2011 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Chapter 11 Object and Object- Relational Databases.
Microsoft Access 2010 Chapter 7 Using SQL.
Chapter 6 Relations. Copyright © 2004 Pearson Addison-Wesley. All rights reserved.6-2 Topics in this Chapter Tuples Relation Types Relation Values Relation.
DATABASES AND SQL. Introduction Relation: Relation means table(data is arranged in rows and columns) Domain : A domain is a pool of values appearing in.
The Relational Database Model
Chapter 6 Relations. Topics in this Chapter Tuples Relation Types Relation Values Relation Variables SQL Facilities.
Concepts of Database Management, Fifth Edition
Chapter 3 An Introduction to Relational Databases.
Introduction to Databases Chapter 7: Data Access and Manipulation.
1-1 University of Hail College of Computer Science and Engineering Department of computer Science and Software Engineering Course: ICS313: Fundamentals.
CSE314 Database Systems More SQL: Complex Queries, Triggers, Views, and Schema Modification Doç. Dr. Mehmet Göktürk src: Elmasri & Navanthe 6E Pearson.
Chapter 5 Types. Topics in this Chapter Values vs. Variables Types vs. Representations Type Definition Operators Type Generators SQL Facilities.
CHAPTER:14 Simple Queries in SQL Prepared By Prepared By : VINAY ALEXANDER ( विनय अलेक्सजेंड़र ) PGT(CS),KV JHAGRAKHAND.
The Data in a Relation To consider atomic data in relations; To consider data types in a relation; To consider missing data & NULLs in relations. Objectives.
Using Special Operators (LIKE and IN)
NULLs & Outer Joins Objectives of the Lecture : To consider the use of NULLs in SQL. To consider Outer Join Operations, and their implementation in SQL.
Information Technologies and Microsoft SQL Server Day 2 by Alper Özpınar
6 1 Lecture 8: Introduction to Structured Query Language (SQL) J. S. Chou, P.E., Ph.D.
1 Relational Algebra and Calculas Chapter 4, Part A.
Database Systems Design, Implementation, and Management Coronel | Morris 11e ©2015 Cengage Learning. All Rights Reserved. May not be scanned, copied or.
Chapter 8: SQL. Data Definition Modification of the Database Basic Query Structure Aggregate Functions.
Arithmetic Expressions
IS 230Lecture 6Slide 1 Lecture 7 Advanced SQL Introduction to Database Systems IS 230 This is the instructor’s notes and student has to read the textbook.
Chapter 5 Types. Copyright © 2004 Pearson Addison-Wesley. All rights reserved.5-2 Topics in this Chapter Values vs. Variables Types vs. Representations.
Queries SELECT [DISTINCT] FROM ( { }| ),... [WHERE ] [GROUP BY [HAVING ]] [ORDER BY [ ],...]
Topics Related to Attribute Values Objectives of the Lecture : To consider sorting relations by attribute values. To consider Triggers and their use for.
Concepts of Database Management Seventh Edition Chapter 3 The Relational Model 2: SQL.
Chapter 5 : Integrity And Security  Domain Constraints  Referential Integrity  Security  Triggers  Authorization  Authorization in SQL  Views 
Copyright © 2011 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Chapter 6 The Relational Algebra and Relational Calculus.
March 31, ICE 1341 – Programming Languages (Lecture #11) In-Young Ko Programming Languages (ICE 1341) Lecture #11 Programming Languages (ICE 1341)
How to execute Program structure Variables name, keywords, binding, scope, lifetime Data types – type system – primitives, strings, arrays, hashes – pointers/references.
Relations Prof. Yin-Fu Huang CSIE, NYUST Chapter 6.
Types Prof. Yin-Fu Huang CSIE, NYUST Chapter 5. Advanced Database SystemYin-Fu Huang Relation, tuple, cardinality, attribute, degree, domain, primary.
1 Theory, Practice & Methodology of Relational Database Design and Programming Copyright © Ellis Cohen Relational State Assertions These slides.
© 2002 by Prentice Hall 1 Structured Query Language David M. Kroenke Database Concepts 1e Chapter 3 3.
A Guide to SQL, Eighth Edition Chapter Four Single-Table Queries.
Integrity Prof. Yin-Fu Huang CSIE, NYUST Chapter 9.
SQL: Structured Query Language It enables to create and operate on relational databases, which are sets of related information stored in tables. It is.
Chapter 3 An Introduction to Relational Databases.
Concepts of Database Management, Fifth Edition Chapter 3: The Relational Model 2: SQL.
More SQL: Complex Queries, Triggers, Views, and Schema Modification
More SQL: Complex Queries,
Chapter 7: Expressions and Assignment Statements
Type Checking, and Scopes
PL/SQL LANGUAGE MULITPLE CHOICE QUESTION SET-1
Chapter 7: Expressions and Assignment Statements
Lecture 2 The Relational Model
College of Computer Science and Engineering
Chapter 4 Summary Query.
More SQL: Complex Queries, Triggers, Views, and Schema Modification
Chapter 8 Advanced SQL.
Chapter 7 Expressions and Assignment Statements.
Database Design: Relational Model
PRESENTED BY ADNAN M. UZAIR NOMAN
Presentation transcript:

Chapter 5 Types

5-2 Topics in this Chapter Values vs. Variables Types vs. Representations Type Definition Operators Type Generators SQL Facilities

5-3 Types A type is a set of values F/k/a (formerly known as) a domain Types can be system-defined or user-defined All types have associated operators Formally, this means that the operator can take the given type as a parameter For example integers can be passed to an addition operator but not a sub-string operator

5-4 Values vs. Variables - Values A value is an individual constant It has no location in time or space A value is represented by encoding, which generates its appearance, which is spatial and temporal Appearances can occur in different times and spaces: they are manifold A value cannot be updated, for then it would be some other value: a value is immutable

5-5 Values vs. Variables - Variables A variable is a holder for an appearance of a value It has location in time or space A variable can be updated, that is, it can hold another value A variable maintains its identity during the update: it is still the same variable

5-6 Values vs. Variables Values can be simple or complex –Simple: integer, char –Complex: an XML document, a relation A value per se can have multiple appearances An appearance can have multiple encodings When we refer to a “value,” we often mean “an appearance of an encoding of a value”

5-7 Values and Variables are Typed Every value has its immutable type Every variable has its immutable type, so that all its values will be of that type Every attribute of every relvar has its immutable type Operators have a type when operating, but this can be polymorphic in different contexts –e.g. = can operate on integers or characters, but not both at the same time

5-8 Types and their Representations A type per se is idealized, conceptual, a model A representation (appearance) of the type is its implementation Sometimes a type is called an ADT – Abstract Data Type, but this is inherently redundant This logical and physical distinction is an aspect of data independence

5-9 Types and their Representations - Scalar vs. Non-Scalar A scalar type is atomic and encapsulated –Integer, char, bool A non-scalar type is complex and user-visible –Name, address, employee, radiology image Values, variables, attributes, operators, parameters, expressions: all can be scalar or not, mutatis mutandis

5-10 Types and their Representations - Possible Representations Let T be a scalar type The physical representation is hidden from the user Values of type T must have at least one possible representation, which is not hidden from the user The possible representation of a scalar type may have components, and if so, then at least one set of these must be visible to the user

5-11 Types and their Representations - Possible Representations Each type has at least one POSSREP visible to the user in its declaration Each POSSREP includes two operators –Selector to specify a value for each representation –Ex.: QTY (100), QTY(N1 – N2) –THE_ to access each representation –Ex.: THE_QTY (Q), THE_QTY (Q1 – Q2), QTY cannot equal 100, because quantity is not an integer, if it has been declared as a type QTY can equal QTY(100)

5-12 Type Definition TYPE WEIGHT POSSREP { D DECIMAL (5,1) CONSTRAINT D > 0.0 AND D < }; TYPE WEIGHT POSSREP LBS { L DECIMAL (5,1) CONSTRAINT L > 0.0 AND L < }; POSSREP GMS { G DECIMAL (7,1) CONSTRAINT G > 0.0 AND G < AND MOD (G, 45.4) = 0.0 };

5-13 Operators OPERATOR ABS (Z RATIONAL) RETURNS RATIONAL; RETURN (CASE WHEN Z > 0.0 THEN +Z WHEN Z < 0.0 THEN –Z END CASE); END OPERATOR;

5-14 Operators OPERATOR REFLECT (P POINT) UPDATES P; BEGIN; THE_X (P) := - THE_X (P) ; THE_Y (P) := - THE_Y (P) ; RETURN; END; END OPERATOR; DROP OPERATOR REFLECT;

5-15 Type Conversions QTY(100) converts an integer to a quantity THE_QTY (Q1) converts a quantity to an integer P# = ‘P2’ violates the rule that both sides of an assignment must be of the same type Compiler uses the P# selector implicitly to convert ‘P2’ from Char to P# a/k/a “Coercion” Coercion is not permitted

5-16 Type Conversions Coercion is not permitted Explicit casting is permitted CAST_AS_CHAR (530.00) This is called strong typing: i.e., every value has a type, and the compiler checks to verify that operands are of the correct type for an operation –Can’t add weight to quantity, but can multiply them

5-17 Type and Domain All types are known to the system The types in a database are a closed set Assignments and comparisons, ditto In a database system, a domain is a type, and thereby is an object class Hence we can speak about relations and objects simultaneously

5-18 Type Generators a/k/a parameterized types, or templates ARRAY is a classic invocation of a type generator ARRAY can take in all sorts of types, and can return all sorts of other types VAR SALES ARRAY INTEGER [12]; ARRAY operators such as assignment, equality, THE_ work equally well with any valid type, i.e., a type known to the system

5-19 SQL Facilities Built-in operators such as CHAR, NUMERIC, BLOB, BOOL… Mostly strong typing, but SQL will coerce FLOAT to NUMERIC, for example Supports two kinds of user-defined types: distinct types and structured types SQL does not support POSSREP – only one representation per type SQL does not support CONSTRAINTs

5-20 SQL Facilities – Distinct Types CREATE TYPE WEIGHT AS DECIMAL (5,1) FINAL; For distinct types, SQL supports Selector and THE_ POSSREP not supported, so WEIGHT is always a DECIMAL Distinct types are strong, so you cannot use a comparison operator between the type and its underlying representation

5-21 SQL Facilities – Structured Types CREATE TYPE POINT AS (X FLOAT,Y FLOAT) NOT FINAL; Uses operators in place of Select and THE_ Observe and mutate methods Structured types can be ALTERed or DROPped Tuples and relations are structured types

5-22 SQL Facilities –Type Generators SQL includes three type generators: REF, ROW, ARRAY REFERENCE generates a reference ROW generates a set of fields ARRAY generates an array

5-23 SQL Commands GROUP BY and HAVING By including a GROUP BY clause functions such as SUM and COUNT are applied to groups of items sharing values. When you specify GROUP BY region the result is that you get only one row for each different value of region. All the other columns must be "aggregated" by one of SUM, COUNT... The HAVING clause allows use to filter the groups which are displayed. The WHERE clause filters rows before the aggregation, the HAVING clause filters after the aggregation. If a ORDER BY clause is included we can refer to columns by their position.

5-24 Orderby Command Select * from emp order by ename; SELECT ename, salary FROM emp WHERE salary > 1000 ORDER BY salary DESC Display Sum in Query. SELECT SUM(population), SUM(gdp) FROM bbc WHERE region = 'Europe‘; Distinct Command SELECT DISTINCT region FROM bbc

5-25 Set Operators Set operators combine the results of two component queries into a single result. Queries containing set operators are called compound queries. They are fully described, including restrictions on these operators, in Operators OperatorReturns UNION - All rows selected by either query UNION ALL - All rows selected by either query, including all duplicates INTERSECT - All distinct rows selected by both queries MINUS - All distinct rows selected by the first query but not the second

5-26 Max, Min Select max(sal) from emp; Select min(sal) from emp;