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

Slides:



Advertisements
Similar presentations
Composition CMSC 202. Code Reuse Effective software development relies on reusing existing code. Code reuse must be more than just copying code and changing.
Advertisements

Chapter 2: Using Objects Part 1. To learn about variables To understand the concepts of classes and objects To be able to call methods To learn about.
Chapter 7:: Data Types Programming Language Pragmatics
Chapter 5 Types. 5-2 Topics in this Chapter Values vs. Variables Types vs. Representations Type Definition Operators Type Generators SQL Facilities.
Type Checking.
Compiler Construction
Road Map Introduction to object oriented programming. Classes
Operator Overloading in C++ Systems Programming. Systems Programming: Operator Overloading 22   Fundamentals of Operator Overloading   Restrictions.
ISBN Chapter 11 Abstract Data Types and Encapsulation Concepts.
ISBN Chapter 7 Expressions and Assignment Statements.
Introduction to Computers and Programming Lecture 4: Mathematical Operators New York University.
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.
Encapsulation by Subprograms and Type Definitions
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.
Lecture 9 Concepts of Programming Languages
Abstract Data Types and Encapsulation Concepts
RIZWAN REHMAN, CCS, DU. Advantages of ORDBMSs  The main advantages of extending the relational data model come from reuse and sharing.  Reuse comes.
1 CSC241: Object Oriented Programming Lecture No 07.
Chapter 6 Relations. Copyright © 2004 Pearson Addison-Wesley. All rights reserved.6-2 Topics in this Chapter Tuples Relation Types Relation Values Relation.
Introduction To Databases IDIA 618 Fall 2014 Bridget M. Blodgett.
Chapter 6 Relations. Topics in this Chapter Tuples Relation Types Relation Values Relation Variables SQL Facilities.
Introduction to Java Appendix A. Appendix A: Introduction to Java2 Chapter Objectives To understand the essentials of object-oriented programming in Java.
Abstract Types Defined as Classes of Variables Jeffrey Smith, Vincent Fumo, Richard Bruno.
 Value, Variable and Data Type  Type Conversion  Arithmetic Expression Evaluation  Scope of variable.
Chapter 3 An Introduction to Relational Databases.
CISC6795: Spring Object-Oriented Programming: Polymorphism.
1-1 University of Hail College of Computer Science and Engineering Department of computer Science and Software Engineering Course: ICS313: Fundamentals.
ORDBS1 Object Relational Database Systems: 1. Introduction 2. Objects in SQL3 3. Comparison of ODL/OQL and SQL3 Approaches CIS 671.
DEPARTMENT OF COMPUTER SCIENCE & TECHNOLOGY FACULTY OF SCIENCE & TECHNOLOGY UNIVERSITY OF UWA WELLASSA 1 CST 221 OBJECT ORIENTED PROGRAMMING(OOP) ( 2 CREDITS.
CHAPTER:14 Simple Queries in SQL Prepared By Prepared By : VINAY ALEXANDER ( विनय अलेक्सजेंड़र ) PGT(CS),KV JHAGRAKHAND.
Introduction to Java Applications Part II. In this chapter you will learn:  Different data types( Primitive data types).  How to declare variables?
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.
JAVA 0. HAFTA Algorithms FOURTH EDITION Robert Sedgewick and Kevin Wayne Princeton University.
1 COSC3306: Programming Paradigms Lecture 2: Data Types Haibin Zhu, Ph.D. Computer Science Nipissing University (C) 2003.
Introduction to C Programming Chapter 2 : Data Input, Processing and Output.
Object-Oriented Program Development Using Java: A Class-Centered Approach, Enhanced Edition.
Object relational database managmement systems (ORDBMS) Adapted by Edel Sherratt from originals by Nigel Hardy.
Arithmetic Expressions
Topic 1 Object Oriented Programming. 1-2 Objectives To review the concepts and terminology of object-oriented programming To discuss some features of.
Chapter 5 Types. Copyright © 2004 Pearson Addison-Wesley. All rights reserved.5-2 Topics in this Chapter Values vs. Variables Types vs. Representations.
Simple Data Types Built-In and User Defined Chapter 10.
ISBN Chapter 6 Data Types Introduction Primitive Data Types User-Defined Ordinal Types.
CS 338The Relational Model2-1 The Relational Model Lecture Topics Overview of SQL Underlying relational model Relational database structure SQL DDL and.
Java Language Basics By Keywords Keywords of Java are given below – abstract continue for new switch assert *** default goto * package.
CIS 270—Application Development II Chapter 8—Classes and Objects: A Deeper Look.
Chapter 7: Class Inheritance F Superclasses and Subclasses F Keywords: super and this F Overriding methods F The Object Class F Modifiers: protected, final.
 In the java programming language, a keyword is one of 50 reserved words which have a predefined meaning in the language; because of this,
CSI 3125, Preliminaries, page 1 Data Type, Variables.
How to execute Program structure Variables name, keywords, binding, scope, lifetime Data types – type system – primitives, strings, arrays, hashes – pointers/references.
Introduction to Java Applications Part II. In this chapter you will learn:  Different data types( Primitive data types).  How to declare variables?
72 4/11/98 CSE 143 Abstract Data Types [Sections , ]
Dr. M. Al-Mulhem Introduction 1 Chapter 6 Type Systems.
Chapter 7: Expressions and Assignment Statements
Type Checking, and Scopes
Object Oriented Programming
CS 326 Programming Languages, Concepts and Implementation
Chapter 7: Expressions and Assignment Statements
Operator Overloading BCA Sem III K.I.R.A.S.
.NET and .NET Core 5.2 Type Operations Pan Wuming 2016.
Introduction to Abstract Data Types
College of Computer Science and Engineering
CS 363 – Chapter 7 Chapter 7 – type systems Types that are supported
Introduction to Data Structure
Chapter 7 Expressions and Assignment Statements.
Compiler Construction
Java Programming Language
PRESENTED BY ADNAN M. UZAIR NOMAN
Compiler Construction
Presentation transcript:

Chapter 5 Types

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

Types The relational model The relational model as described with Tutorial D SQL 92 SQL 99 MySQL SQL– other

Types A type is a set of values Sometimes called domains in the relational context 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

Enumerated Data Types Syntax: enum enum WeekDay {Mon, Tue, Wed, Thu, Fri}; WeekDay workday; workday = Tue; Enumerated data type variables can only assume values which have been previously declared.

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

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

Values vs. Variables Values can be simple or complex –Simple: integer, char –Complex: a 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”

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

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

Abstract Data Types Definition: A mathematically specified collection of data- storing entities with operations to create, access, change, etc. instances. Also known as ADT. Note: Since the collection is defined mathematically, rather than as an implementation in a computer language, we may reason about effects of the operations, relations to other abstract data types, whether a program implements the data type, etc. National Institute of Standards and Technology

/** * This class represents complex numbers, * and defines methods for performing * arithmetic on complex numbers. **/ public class ComplexNumber { // These are the instance variables. Each // ComplexNumber object holds two double values, // known as x and y. They are private, so they are // not accessible from outside this class. // Instead, they are available // through the real() and imaginary() methods below. private double x, y; /** * This is the constructor. * It initializes the x and y variables **/ public ComplexNumber(double real, double imaginary) { this.x = real; this.y = imaginary; }

ComplexNumber compP = new ComplexNumber(PRealPart, PImaginaryPart); ComplexNumber compQ = new ComplexNumber(QRealPart, QImaginaryPart);

/** * An accessor method. Returns the real part * of the complex number. * Note that there is no setReal() method to set * the real part. This means * that the ComplexNumber class is "immutable". **/ public double real() { return x; } /** * An accessor method. Returns the imaginary part * of the complex number */ public double imaginary() { return y; }

/** * This is a static class method. It takes two complex * numbers, adds them, and returns the result as a third * number. Because it is static, there is no "current * instance" or "this" object. Use it like this: * ComplexNumber c = ComplexNumber.add(a, b); **/ public static ComplexNumber add(ComplexNumber a, ComplexNumber b) { return new ComplexNumber(a.x + b.x, a.y + b.y); } ComplexNumber compT = ComplexNumber.add(compP, compQ);

/** * A static class method to multiply complex * numbers */ public static ComplexNumber multiply(ComplexNumber a, ComplexNumber b) { return new ComplexNumber (a.x*b.x - a.y*b.y, a.x*b.y + a.y*b.x); }

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

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

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)

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 };

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;

Operators To define an operator: OPERATOR REFLECT (P POINT) UPDATES P; BEGIN; THE_X (P) := - THE_X (P) ; THE_Y (P) := - THE_Y (P) ; RETURN; END; END OPERATOR; To remove the operator: DROP OPERATOR REFLECT;

Type Conversions In Java: float x = 2.0; int y = 17; x = y; // coercion x = (float)y; // explicit type conversion // (casting)

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

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

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

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

SQL Facilities Built-in types: Numeric, Decimal, Integer, Smallint, Float Date, Time, Timestamp, Interval Boolean, Bit, Character Binary Large Object (BLOB) Character Large Object (CLOB)

SQL Facilities Binary Large Object (BLOB) not really binary, not really an “object” long string of octets an image, sound file, etc. SQL provides a locator construct to enable handling piecemeal Character Large Object (CLOB)

SQL Facilities 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

SQL Facilities – DISTINCT Types CREATE TYPE WEIGHT AS DECIMAL (5,1) FINAL; For DISTINCT types, SQL supports Selector and THE_ DISTINCT types are strong, so you cannot use a comparison operator between the type and its underlying representation WHERE PART_WEIGHT = WEIGHT(14.0) selector (cast)

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

SQL Facilities –Type Generators SQL includes type generators (called type constructors): ROW generates a set of fields ARRAY generates an array

CREATE TABLE CUST (CUST# CHAR(3), ADDR ROW ( STREET CHAR(50), CITY CHAR(25), STATE CHAR(2), ZIP CHAR(5) ) PRIMARY KEY (CUST#) ); SELECT CX.CUST# FROM CUST AS CX WHERE CX.ADDR.STATE = ‘CA’;

CREATE TABLE ITEM_SALES (ITEM# CHAR(5), SALES INTEGER ARRAY[12], PRIMARY KEY (ITEM#) ); SELECT ITEM# FROM ITEM_SALES WHERE SALES[3] > 10;

type abstract data type (ADT) enumerated data type type coercion type conversion (casting) strong typing type constraint DISTINCT types structured types SQL built-in types

variable vs. value scalar vs. non-scalar system defined type vs. user defined type type vs. representation representation vs. implementation