Chapter 8 How to work with data types

Similar documents
Lab # 2 Hands-On. DDL Basic SQL Statements Institute of Computer Science, University of Tartu, Estonia

TINYINT[(M)] [UNSIGNED] [ZEROFILL] A very small integer. The signed range is -128 to 127. The unsigned range is 0 to 255.

Introduction to SQL on GRAHAM ED ARMSTRONG SHARCNET AUGUST 2018

Chapter 3 Introduction to relational databases and MySQL

The M in LAMP: MySQL CSCI 470: Web Science Keith Vertanen Copyright 2014

Chapter 11 How to create databases, tables, and indexes

Data Types in MySQL CSCU9Q5. MySQL. Data Types. Consequences of Data Types. Common Data Types. Storage size Character String Date and Time.

Chapter 3 How to retrieve data from a single table

CSCI-UA: Database Design & Web Implementation. Professor Evan Sandhaus

Chapter 7 How to code subqueries

CSCI-UA: Database Design & Web Implementation. Professor Evan Sandhaus

Basis Data Terapan. Yoannita

Database Management Systems

Data and Tables. Bok, Jong Soon

HNDIT 1105 Database Management Systems

Acknowledgments About the Authors

Tool/Web URL Features phpmyadmin. More on phpmyadmin under User Intefaces. MySQL Query Browser

Basic SQL. Basic SQL. Basic SQL

SQL. Often times, in order for us to build the most functional website we can, we depend on a database to store information.

Information Systems Engineering. SQL Structured Query Language DDL Data Definition (sub)language

Overview of MySQL Structure and Syntax [2]

Conceptual Design. The Entity-Relationship (ER) Model

SQL Workshop. Introduction Queries. Doug Shook

MySQL: an application

The Top 20 Design Tips

MySQL Creating a Database Lecture 3

Lesson 05: How to Insert, Update, and Delete Data. By S. Sabraz Nawaz Senior Lecturer in MIT FMC, SEUSL

Lecture 9: Database Design. Wednesday, February 18, 2015

Oracle SQL Developer. Supplementary Information for MySQL Migrations Release 2.1 E

CIS 363 MySQL. Chapter 4 MySQL Connectors Chapter 5 Data Types Chapter 6 Identifiers

Data Definition Language with mysql. By Kautsar

Basic SQL. Dr Fawaz Alarfaj. ACKNOWLEDGEMENT Slides are adopted from: Elmasri & Navathe, Fundamentals of Database Systems MySQL Documentation

Programming and Database Fundamentals for Data Scientists

Chapter 1 An introduction to relational databases and SQL

Simple Quesries in SQL & Table Creation and Data Manipulation

Create Basic Databases and Integrate with a Website Lesson 1

General References on SQL (structured query language) SQL tutorial.

SQL OVERVIEW. CS121: Relational Databases Fall 2017 Lecture 4

Using IBM-Informix datatypes with IDS 10 and web application server Keshava Murthy, IBM Informix Development

SQL Data Definition and Data Manipulation Languages (DDL and DML)

Data types String data types Numeric data types Date, time, and timestamp data types XML data type Large object data types ROWID data type

HOW TO CREATE AND MAINTAIN DATABASES AND TABLES. By S. Sabraz Nawaz Senior Lecturer in MIT FMC, SEUSL

SQL Workshop. Database Design. Doug Shook

Chapter 4 How to retrieve data from two tables

Careerarm.com. 1. What is MySQL? MySQL is an open source DBMS which is built, supported and distributed by MySQL AB (now acquired by Oracle)

Explaining the MySQL EXPLAIN

*this is a guide that was created from lecture videos and is used to help you gain an understanding of what is mysql used for.

Homework Assignment. 1. Stored Procedures (SPs)

Deepak Bhinde PGT Comp. Sc.

2.9 Table Creation. CREATE TABLE TableName ( AttrName AttrType, AttrName AttrType,... )

SQL: Data Definition Language

1 INTRODUCTION TO EASIK 2 TABLE OF CONTENTS

MTAT Introduction to Databases

Lecture 3: Continuing SQL. Monday, February 2, 2015

SQL Data Definition Language: Create and Change the Database Ray Lockwood

Advanced SQL. Nov 21, CS445 Pacific University 1

ALTER TABLE Improvements in MARIADB Server. Marko Mäkelä Lead Developer InnoDB MariaDB Corporation

Department of Computer Science University of Cyprus. EPL342 Databases. Lab 1. Introduction to SQL Server Panayiotis Andreou

sqoop Automatic database import Aaron Kimball Cloudera Inc. June 18, 2009

SQL Workshop. Joins. Doug Shook

SQL Functionality SQL. Creating Relation Schemas. Creating Relation Schemas

Lab 4: Tables and Constraints

Introduction to relational databases and MySQL

Database Design and Implementation

More MySQL ELEVEN Walkthrough examples Walkthrough 1: Bulk loading SESSION

IBM DB2 UDB V7.1 Family Fundamentals.

EE 495M - Lecture 3. Overview

New Features Bulletin Replication Server Options 15.6

Lecture 1. Monday, August 25, 2014

Exact Numeric Data Types

SAS/ACCESS Supplement for MySQL. SAS/ACCESS for Relational Databases

This lab will introduce you to MySQL. Begin by logging into the class web server via SSH Secure Shell Client

Getting started with Java

GridDB Advanced Edition SQL reference

TUSC Client Chronicle

Data Storage and Query Answering. Data Storage and Disk Structure (4)

618 Index. BIT data type, 108, 109 BIT_LENGTH, 595f BIT VARYING data type, 108 BLOB data type, 108 Boolean data type, 109

How to Migrate Microsoft SQL Server Connections from the OLE DB to the ODBC Provider Type

Oracle SQL. murach s. and PL/SQL TRAINING & REFERENCE. (Chapter 2)

SQL Fundamentals. Chapter 3. Class 03: SQL Fundamentals 1

MySQL 101. Designing effective schema for InnoDB. Yves Trudeau April 2015

COMP 430 Intro. to Database Systems

The type of all data used in a C (or C++) program must be specified

Code Centric: T-SQL Programming with Stored Procedures and Triggers

Department of Computer Science University of Cyprus. EPL342 Databases. Lab 2

Standard Query Language: Current standard Logical Schema Design: Schema Definition with SQL (DDL)

Database Applications and Web-Enabled Databases. University of California, Berkeley School of Information IS 257: Database Management

Documentation Accessibility. Access to Oracle Support. Supported Browsers

Princeton University. Computer Science 217: Introduction to Programming Systems. Data Types in C

Functional Logic Programming with Databases

Java Notes. 10th ICSE. Saravanan Ganesh

MySQL Workshop. Scott D. Anderson

Informatica Cloud Spring Data Integration Hub Connector Guide

SQL Workshop. Subqueries. Doug Shook

Creating Tables, Defining Constraints. Rose-Hulman Institute of Technology Curt Clifton

Programming in C++ 4. The lexical basis of C++

Information Systems for Engineers Fall Data Definition with SQL

Programming in C++ 6. Floating point data types

COMP2611: Computer Organization. Data Representation

SQL Introduction. CS 377: Database Systems

Transcription:

Chapter 8 How to work with data types Murach's MySQL, C8 2015, Mike Murach & Associates, Inc. Slide 1

Objectives Applied Code queries that convert data from one data type to another. Knowledge Describe the data that can be stored in any of the character, numeric, date/time, and large object data types. Describe ENUM and SET data types. Murach's MySQL, C8 2015, Mike Murach & Associates, Inc. Slide 2

MySQL data type categories Character Numeric Date and time Large Object (LOB) Spatial Murach's MySQL, C8 2015, Mike Murach & Associates, Inc. Slide 3

The character types Type Bytes CHAR(M) Mx3 VARCHAR(M) L+1 How the character types work Data type Original value Value stored Bytes used CHAR(2) 'CA' 'CA' 6 CHAR(10) 'CA' 'CA ' 30 VARCHAR(10) 'CA' 'CA' 3 VARCHAR(20) 'California' 'California' 11 VARCHAR(20) 'New York' 'New York' 9 VARCHAR(20) "Murach's MySQL" "Murach's MySQL" 15 Murach's MySQL, C8 2015, Mike Murach & Associates, Inc. Slide 4

Terms to know Latin1 character set UTF-8 character set Unicode standard Murach's MySQL, C8 2015, Mike Murach & Associates, Inc. Slide 5

The integer types Type Bytes BIGINT 8 INT 4 MEDIUMINT 3 SMALLINT 2 TINYINT 1 Murach's MySQL, C8 2015, Mike Murach & Associates, Inc. Slide 6

How the UNSIGNED and ZEROFILL attributes work Data type Original value Value stored Value displayed INT 99 99 99 INT -99-99 -99 INT UNSIGNED 99 99 99 INT UNSIGNED -99 ERROR ERROR INT ZEROFILL 99 99 0000000099 INT(4) ZEROFILL 99 99 0099 Murach's MySQL, C8 2015, Mike Murach & Associates, Inc. Slide 7

The fixed-point type Type DECIMAL(M, D) Bytes Vary The floating-point types Type Bytes DOUBLE 8 FLOAT 4 How the fixed-point and floating-point types work Data type Original value Value stored Bytes used DECIMAL(9,2) 1.2 1.20 5 DECIMAL(9,2) 1234567.89 1234567.89 5 DECIMAL(9,2) -1234567.89-1234567.89 5 DECIMAL(18,9) 1234567.89 1234567.890000000 8 DOUBLE 1234567.89 1234567.89 8 FLOAT 1234567.89 1234570 4 Murach's MySQL, C8 2015, Mike Murach & Associates, Inc. Slide 8

Terms to know Real number Precision Scale Exact numeric type Floating-point number Approximate numeric type Murach's MySQL, C8 2015, Mike Murach & Associates, Inc. Slide 9

The date and time types Type Bytes DATE 3 TIME 3 DATETIME 8 TIMESTAMP 4 YEAR[(2 4)] 1 Murach's MySQL, C8 2015, Mike Murach & Associates, Inc. Slide 10

How MySQL interprets date/time literals Literal value '2014-08-15' 2014-08-15 '2014-8-15' 2014-08-15 '14-8-15' 2014-08-15 '20140815' 2014-08-15 20140815 2014-08-15 '2014.08.15' 2014-08-15 '14/8/15' 2014-08-15 '8/15/14' ERROR '2014-02-31' ERROR Value stored in DATE column Murach's MySQL, C8 2015, Mike Murach & Associates, Inc. Slide 11

How MySQL interprets date/time literals (continued) Literal value '7:32' 07:32:00 '19:32:11' 19:32:11 '193211' 19:32:11 193211 19:32:11 '19:61:11' ERROR Literal value Value stored in TIME column Value stored in DATETIME or TIMESTAMP column '2014-08-15 19:32:11' 2014-08-15 19:32:11 '2014-08-15' 2014-08-15 00:00:00 Murach's MySQL, C8 2015, Mike Murach & Associates, Inc. Slide 12

The ENUM and SET types Type Bytes ENUM 1-2 SET 1-8 How values are stored in ENUM columns Value 'Yes' 'No' 'Maybe' 'Possibly' Stored in column ENUM ('Yes', 'No', 'Maybe') 'Yes' 'No' 'Maybe' '' Murach's MySQL, C8 2015, Mike Murach & Associates, Inc. Slide 13

How values are stored in SET columns Value 'Pepperoni' 'Mushrooms' 'Pepperoni, Bacon' 'Olives, Pepperoni' Stored in column SET ('Pepperoni', 'Mushrooms', 'Olives') 'Pepperoni' 'Mushrooms' 'Pepperoni' 'Pepperoni, Olives' Murach's MySQL, C8 2015, Mike Murach & Associates, Inc. Slide 14

The large object types Type Bytes LONGBLOB L+4 MEDIUMBLOB L+3 BLOB L+2 TINYBLOB L+1 LARGETEXT L+4 MEDIUMTEXT L+3 TEXT L+2 TINYTEXT L+1 Terms to know BLOB (binary large object) types CLOB (character large object) types Murach's MySQL, C8 2015, Mike Murach & Associates, Inc. Slide 15

Implicitly convert data a number to a string SELECT invoice_total, CONCAT('$', invoice_total) FROM invoices Implicitly convert a string to a number SELECT invoice_number, 989319/invoice_number FROM invoices Implicitly convert a date to a number SELECT invoice_date, invoice_date + 1 FROM invoices Murach's MySQL, C8 2015, Mike Murach & Associates, Inc. Slide 16

The syntax of the CAST function CAST(expression AS cast_type) The syntax of the CONVERT function CONVERT(expression, cast_type) Cast types you can use in these functions CHAR[(N)] DATE DATETIME TIME SIGNED [INTEGER] UNSIGNED [INTEGER] DECIMAL[(M[,D])] Murach's MySQL, C8 2015, Mike Murach & Associates, Inc. Slide 17

A statement that uses the CAST function SELECT invoice_id, invoice_date, invoice_total, CAST(invoice_date AS CHAR(10)) AS char_date, CAST(invoice_total AS SIGNED) AS integer_total FROM invoices A statement that uses the CONVERT function SELECT invoice_id, invoice_date, invoice_total, CONVERT(invoice_date, CHAR(10)) AS char_date, CONVERT(invoice_total, SIGNED) AS integer_total FROM invoices Murach's MySQL, C8 2015, Mike Murach & Associates, Inc. Slide 18

The FORMAT and CHAR functions FORMAT(number,decimal) CHAR(value1[,value2]...) FORMAT function examples Function Result FORMAT(1234567.8901,2) 1,234,567.89 FORMAT(1234.56,4) 1,234.5600 FORMAT(1234.56,0) 1,235 Murach's MySQL, C8 2015, Mike Murach & Associates, Inc. Slide 19

CHAR function examples for common control characters Function CHAR(9) CHAR(10) CHAR(13) Control character Tab Line feed Carriage return A statement that uses the CHAR function SELECT CONCAT(vendor_name, CHAR(13,10), vendor_address1, CHAR(13,10), vendor_city, ', ', vendor_state, ' ', vendor_zip_code) FROM vendors WHERE vendor_id = 1; US Postal Service Attn: Supt. Window Services Madison, WI 53707 Murach's MySQL, C8 2015, Mike Murach & Associates, Inc. Slide 20