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

Similar documents
Introduction to SQL on GRAHAM ED ARMSTRONG SHARCNET AUGUST 2018

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

Basis Data Terapan. Yoannita

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

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

Data and Tables. Bok, Jong Soon

MySQL Creating a Database Lecture 3

Chapter 8 How to work with data types

SQL: Data Definition Language

Programming and Database Fundamentals for Data Scientists

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

SQL Functionality SQL. Creating Relation Schemas. Creating Relation Schemas

Data Definition Language with mysql. By Kautsar

Basic SQL. Basic SQL. Basic SQL

Data Definition and Data Manipulation. Lecture 5: SQL s Data Definition Language CS1106/CS5021/CS6503 Introduction to Relational Databases

Lecture 5: SQL s Data Definition Language

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

Acknowledgments About the Authors

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)

MySQL: an application

Overview of MySQL Structure and Syntax [2]

*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.

Chapter 3 Introduction to relational databases and MySQL

More MySQL ELEVEN Walkthrough examples Walkthrough 1: Bulk loading SESSION

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

Selections. Lecture 4 Sections Robb T. Koether. Hampden-Sydney College. Wed, Jan 22, 2014

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

Simple Quesries in SQL & Table Creation and Data Manipulation

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

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

1 INTRODUCTION TO EASIK 2 TABLE OF CONTENTS

user specifies what is wanted, not how to find it

Lab # 4. Data Definition Language (DDL)

Advanced SQL. Nov 21, CS445 Pacific University 1

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

CSCU9Q5 Introduction to MySQL. Data Definition & Manipulation (Over ~two Lectures)

SQL: Data De ni on. B0B36DBS, BD6B36DBS: Database Systems. h p:// Lecture 3

Functional Logic Programming with Databases

CS 253. January 14, 2017

The Top 20 Design Tips

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

Full file at

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

SQL OVERVIEW. CS121: Relational Databases Fall 2017 Lecture 4

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

HKTA TANG HIN MEMORIAL SECONDARY SCHOOL SECONDARY 3 COMPUTER LITERACY. Name: ( ) Class: Date: Databases and Microsoft Access

CIS 363 MySQL. Chapter 10 SQL Expressions Chapter 11 Updating Data

Create Basic Databases and Integrate with a Website Lesson 1

What is MySQL? [Document provides the fundamental operations of PHP-MySQL connectivity]

Senturus Analytics Connector. User Guide Cognos to Power BI Senturus, Inc. Page 1

COSC 304 Introduction to Database Systems SQL DDL. Dr. Ramon Lawrence University of British Columbia Okanagan

Full file at

SQL Structured Query Language Introduction

Database Programming with PL/SQL

DB Programming. Database Systems

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

Documentation Accessibility. Access to Oracle Support. Supported Browsers

Deepak Bhinde PGT Comp. Sc.

Lecture 1. Monday, August 25, 2014

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

Senturus Analytics Connector. User Guide Cognos to Tableau Senturus, Inc. Page 1

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

SQL: Concepts. Todd Bacastow IST 210: Organization of Data 2/17/ IST 210

ECE 122. Engineering Problem Solving with Java

ECE 122. Engineering Problem Solving with Java

Chapter 4. Basic SQL. SQL Data Definition and Data Types. Basic SQL. SQL language SQL. Terminology: CREATE statement

Introducing Hashing. Chapter 21. Copyright 2012 by Pearson Education, Inc. All rights reserved

Database Systems CSE 303. Lecture 02

INFORMATION TECHNOLOGY NOTES

Database Systems CSE 303. Lecture 02

SQL DATA DEFINITION LANGUAGE

SQL DATA DEFINITION LANGUAGE

SQL DATA DEFINITION LANGUAGE

COMP 430 Intro. to Database Systems

Chapter 4. Basic SQL. Copyright 2011 Pearson Education, Inc. Publishing as Pearson Addison-Wesley

After completing this unit, you should be able to: Define the terms

Ingres 9.3. QUEL Reference Guide ING-93-QUR-01

BASIC SQL CHAPTER 4 (6/E) CHAPTER 8 (5/E)

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

Principles of Data Management

SQL Introduction. CS 377: Database Systems

Review: Exam 1. Your First C++ Program. Declaration Statements. Tells the compiler. Examples of declaration statements

MTAT Introduction to Databases

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

Basic data types. Building blocks of computation

EGCI 321: Database Systems. Dr. Tanasanee Phienthrakul

Draft. Students Table. FName LName StudentID College Year. Justin Ennen Science Senior. Dan Bass Management Junior

DS Introduction to SQL Part 1 Single-Table Queries. By Michael Hahsler based on slides for CS145 Introduction to Databases (Stanford)

1) Introduction to SQL

GridDB Advanced Edition SQL reference

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

Data File Header Structure for the dbase Version 7 Table File

Lecture 2 Tao Wang 1

Exercise: Using Numbers

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

IBM DB2 UDB V7.1 Family Fundamentals.

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

Constraints. Primary Key Foreign Key General table constraints Domain constraints Assertions Triggers. John Edgar 2

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

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

Transcription:

- Database P&A Data Types in MySQL MySQL Data Types Data types define the way data in a field can be manipulated For example, you can multiply two numbers but not two strings We have seen data types mentioned in CREATE TABLE statements Now we look at why it is useful to choose the correct type http://dev.mysql.com/doc/refman/5.0/en/data-types.html 1 2 Common Data Types Consequences of Data Types Numeric Storage size Character String Date and Time Range of values specified Operations performed on the data 3 4 (c) University of Stirling 2016 1

- Database P&A Numeric Types Integer types of various sizes (pick the right one to minimise storage space) Tinyint = 1 byte (-128 to 127 or 0 to 256 UNSIGNED) Smallint = 2 bytes Mediumint = 3 bytes Int = 4 bytes Bigint = 8 bytes Maximum number of digits can be specified: eg INT(size) Floating Point Float = 4 bytes Double = 8 bytes Fixed Point Numeric or Decimal(size, d) d = number of digits to right of decimal point Bool is implemented as Tinyint Boolean: True = 1 and False = 0 String Types CHAR(length) Stores strings with length characters smaller strings are right padded with blanks Up to a maximum of 255 characters Blanks removed on retrieval Indexed searching is faster (only if no fields are variable length). VARCHAR(length) Stores variable size (up to length ) strings with no padding ie only actual characters stored Trailing blanks may or may not be removed (version dependent) Stores prefix (1 or 2 bytes) that contains the length of the string Uses less storage if strings really are variable length If they are always the same length, actually uses more storage (Length can be considered way of imposing a weak security check on data insertion) 5 6 String Types (2) TEXT Types CHAR(length) Fred Bloggs Mary Smith John Fortescue Joe Bloe VARCHAR(length) Fred Bloggs Mary Smith John Fortescue Joe Bloe TEXT types hold variable length character strings TINYTEXT (up to 255 characters) TEXT (up to 65,535 characters) MEDIUMTEXT (up to 16,777,215 characters) LONGTEXT (up to 4,294,967,295 characters) TEXT has no trailing space removal on INSERT or SELECT (unlike CHAR or VARCHAR) TEXT has padding added in comparisons to fit the compared object (like CHAR and VARCHAR) 7 8 (c) University of Stirling 2016 2

- Database P&A BLOB Types BLOB is a Binary Large OBject BLOB types hold variable length binary strings: Tinyblob Blob Mediumbl ob LongBlob Useful for encrypted or compressed data BLOB data is ordered by the binary values TEXT (and other character types) is ordered by the collation of the character set _ci (case insensitive), _cs (case sensitive), or _bin (binary) Case insensitive: A < b and a < B Enumerated Types You can create enumerated types CREATE TABLE sizes ( name ENUM('small', 'medium', 'large')) Enumerated types have an index that starts at 1 Index zero is an error value (zero) Entering a value that was not defined, e.g. huge Enumerated values are sorted by their index which is the order in which they were defined so: small < medium < large 9 10 Set Types The SET type can have zero or more values from a list of permitted values Each value should appear at most once CREATE TABLE mytable (col SET('a', 'b', 'c', 'd')); Insert values like this INSERT INTO mytable (col) VALUES ('a,d'); 11 Set Types (2) Retrieval automatically removes duplicates: CREATE TABLE myset (col SET('a', 'b', 'c', 'd')); If you insert the values 'a,d', 'd,a', 'a,d,a': INSERT INTO myset (col) VALUES ('a,d'), ('d,a'), ('a,d,a'); Then all these values appear as 'a,d' when retrieved: SELECT col FROM myset; col 12 (c) University of Stirling 2016 3

- Database P&A Date and Time Types DATETIME stores a date and time: YYYY-MM-DD HH:MM:SS DATE stores just a date: YYYY-MM-DD You can enter values in a reasonable number of formats: yyyy-mm-dd as a string yyyy/mm/dd as a string yyyymmdd as a string or number yymmdd as a string or number TIME as HH:MM:SS YEAR as YY or YYYY Date and Time Functions There are a great many functions in MySQL for dealing with dates and times Part extraction: Hour(), Month() etc. Current time: Now() Counting Dayofmonth(), Dayofyear() Adding Addtime() You can also use standard comparisons: WHERE date1 > date2 WHERE date BETWEEN date1 AND date2 13 14 Comparing Types Data Type Integrity MySQL is very tolerant of mixing of types Different types can be compared: SEL EC T * W HE RE in tfi el d = floatfield SEL EC T * W HE RE in tfi el d = stringfield Are allowed (but be careful when sorting e.g numbers sorted alphabetically!) You can use LIKE for numbers: SELECT * WHERE intfield LIKE 12% Finds all rows where intfield starts with 12 You can specify things about the qualities of data that is to be stored We have seen that decimal allows you to be precise about decimal places You can specify whether or not a field can contain NULL (ie whether it has to have a value) Enumerating types or using sets helps you control what values go into a field Dates are automatically checked for correct format Variable length fields have a maximum size that cannot be exceeded 15 16 (c) University of Stirling 2016 4

- Database P&A End of Lecture The next SQL lecture will look at using Java to connect to and query a database 17 (c) University of Stirling 2016 5