Data Types in MySQL
• Data types define the way data in a field can be manipulated
Master in Information Technology • For example, you can multiply two numbers but not two strings
ICT - 209
Database Applications • We have seen data types mentioned in CREATE TABLE statements
• Now we look at why it is useful to choose the correct type
MySQL 3
http://dev.mysql.com/doc/refman/5.0/en/data-types.html
Data Types
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
Numeric Types String Types
• Integer types of various sizes (pick the right one to minimise storage • CHAR(length)
space) – Stores strings with length characters – smaller strings are right padded
– Tinyint = 1 byte – Indexed searching is faster (only is no fields are variable length).
– Smallint = 2 bytes • VARCHAR(length)
– Mediumint = 3 bytes – Stores variable length (up to length) strings with no padding
– Int = 4 bytes – Stores a prefix that denotes the length of the string
– Bigint = 8 bytes – Uses less storage if strings really are variable length.
• Floating Point – If they are always the same length, actually uses more storage
– Float = 4 bytes
– Double = 8 bytes
• Fixed Point
– Numeric or Decimal(precision, scale)
• Bool is implemented as Tinyint
5 6
Enumerated Types SET Types
• You can create enumerated types • The SET type can have zero or more values from a list of permitted
values
• CREATE TABLE sizes ( name ENUM('small', 'medium', 'large'))
• Each value should appear at most once
• Enumerated types have an index that starts at 1
• CREATE TABLE mytable (col SET('a', 'b', 'c', 'd'));
• Index zero is error (a value that was not defined, e.g. ‘huge’)
• Insert values like this
• Enumerated values are sorted by their index – which is the order in
which they were defined so small < medium < large • INSERT INTO myset (col) VALUES ('a,d') ;
7 8
BLOB and TEXT Types Date and Time Types
• BLOB types hold variable length binary strings: • DATETIME stores a date and time: YYYY-MM-DD HH:MM:SS
– Tinyblob • DATE stores just a data: YYYY-MM-DD
– Blob
• You can enter values in a reasonable number of formats:
– Mediumblob
– yyyy-mm-dd as a string
– LongBlob
– Yyyy/mm/dd as a string
• TEXT types hold variable length text strings – yyyymmdd as a string or number
– yymmdd as a string or number
• BLOB data is ordered by the binary values
• TIME as HH:MM:SS
• TEXT data is ordered by the collation of the character set • YEAR as YY or YYYY
– A<b
– a<B
9 10
Comparing Types Date and Time Functions
• MySQL is very tolerant of mixing of types • There are a great many functions in MySQL for dealing with dates and
times
• Different types can be compared:
– SELECT * WHERE intfield = floatfield • Part extraction:
– Hour(), Month() etc.
– SELECT * WHERE intfield = stringfield
• Current time:
– Now()
– Are allowed
• Counting
– Dayofmonth(), Dayofyear()
• You can use LIKE for numbers:
• Adding
– SELECT * WHERE intfield LIKE 12% – Addtime()
• You can also use standard comparisons:
– Finds all rows where intfield starts with 12 – WHERE date1 > date2
– WHERE date BETWEEN date1 AND date2
11 12
Data Type Integrity
• 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
• 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
13