# MySQL Data Types – slides presentation

This presentation describes MySQL data types covering Numeric,DATETIME, DATE, TIMESTAMP, String Types and MySQL Extensions for Spatial Data.

## Transcript

**Introduction**

► MySQL supports a number of SQL standard data types in various categories. Following Data types are are based on MySQL community server 5.6

★ Numeric

★ DATETIME

★ DATE

★ TIMESTAMP

★ String

**Numeric Types**

**Integer Types**

Type | Length in Bytes | Minimum Value (Signed) |
Maximum Value (Signed) |
Minimum Value (Unsigned) |
Maximum Value (Unsigned) |

TINYINT | 1 | -128 | 127 | 0 | 255 |

SMALLINT | 2 | -32768 | 32767 | 0 | 65535 |

MEDIUMINT | 3 | -8388608 | 8388607 to | 0 | 16777215 |

INT | 4 | -2147483648 | 2147483647 | 0 | 4294967295 |

BIGINT | 8 | -9223372036854775808 | 92233720368 54775807 | 0 | 184467440737 09551615 |

**Floating-Point Types**

Type | Length in Bytes |
Minimum Value (Signed) |
Maximum Value (Signed) |
Minimum Value (Unsigned) |
Maximum Value (Unsigned) |

FLOAT | 4 | -3.402823466E+38 | -1.175494351E-38 | 1.175494351E-38 | 3.402823466E+38 |

DOUBLE | 8 | -1.7976931348623 157E+ 308 |
-2.22507385850720 14E- 308 |
0, and 2.22507385850720 14E- 308 |
1.797693134862315 7E+ 308 |

**Fixed-Point Types**

► In standard SQL the syntax DECIMAL(5,2) (where 5 is the precision and 2 is the scale.) be able to store any value with five digits and two decimals. Therefore the value range will be from -999.99 to 999.99. The syntax DECIMAL(M) is equivalent to DECIMAL(M,0). Similarly, the syntax DECIMAL is equivalent to DECIMAL(M,0). MySQL supports both of these variant forms of DECIMAL syntax. The default value of M is 10. If the scale is 0, DECIMAL values contain no decimal point or fractional part.

The maximum number of digits for DECIMAL is 65, but the actual range for a given DECIMAL column can be constrained by the precision or scale for a given column.

**Bit Value Types**

► The BIT data type is used to store bit-field values. A type of BIT(N) enables storage of N-bit values. N can range from 1 to 64.

► To specify bit values, b'value' notation can be used. ‘value’ is a binary value written using zeros and ones. For example, b'111' and b'10000000' represent 7 and 128, respectively

**Date and Time Types**

**DATETIME, DATE, and TIMESTAMP**

Types | Description | Display Format |
Range |

DATETIME | Use when you need values containing both date and time information. | YYYY-MM-DD HH:MM:SS | '1000-01-01 00:00: 00' to '9999-12-31 23:59:59'. |

DATE | Use when you need only date information. | YYYY-MM-DD | '1000-01-01' to '9999-12-31'. |

TIMESTAMP | Values are converted from the current timezone to UTC while storing, and converted back from UTC to the current time zone when retrieved. | YYYY-MMDD HH:MM:SS | '1970-01-01 00:00: 01' UTC to '2038-01- 19 03:14:07' UTC |

**Time Type**

► MySQL fetches and displays TIME values in 'HH:MM:SS' format or 'HHH:MM:SS' format The range of. TIME values from '-838:59:59' to '838:59:59'. The hours part may be rather large because not only the TIME type can be used to represent the time of day, i.e. less than 24 hours, but also the passed time or a time of interval between two events.

► The TIME values in MySQL can be recognized in different formats, some of which can include a trailing fractional seconds part in up to 6 digits microseconds precision. The range for TIME values is '-838:59:59.000000' to '838:59:59.000000'.

**Year Type**

► The YEAR type is a 1-byte type used to represent year values. It can be declared as YEAR (2) or YEAR(4) to specify a display width of two or four characters. If no width is given the default is four characters

String length | Range |

4-digit string | '1901' to '2155'. |

4-digit number | 1901 to 2155. |

1- or 2-digit string | '0' to '99'. Values in the ranges '0' to '69' and '70' to '99' are converted to YEAR values in the ranges 2000 to 2069 and 1970 to 1999. |

1- or 2-digit number | 1 to 99. Values in the ranges 1 to 69 and 70 to 99 are converted to YEAR values in the ranges 2001 to 2069 and 1970 to 1999. |

**String Types**

**CHAR and VARCHAR Types**

►The CHAR and VARCHAR types are similar, but differ in the way they are stored and retrieved. They also differ in maximum length and in whether trailing spaces are retained.

Types | Description | Display Format | Range in characters |

CHAR | Contains non-binary strings. Length is fixed as you declare while creating a table. When stored, they are rightpadded with spaces to the specified length. | Trailing spaces are removed. | The length can be any value from 0 to 255. |

VARCHAR | Contains non-binary strings. Columns are variable-length strings. | As stored. | A value from 0 to 255 before MySQL 5.0.3, and 0 to 65,535 in 5.0.3 and later versions. |

**BINARY and VARBINARY Types**

► The BINARY and VARBINARY types are similar to CHAR and VARCHAR, except that they contain binary strings rather than nonbinary strings.

Types | Description | Range in bytes |

BINARY | Contains binary strings. | 0 to 255 |

VARBINARY | Contains binary strings. | A value from 0 to 255 before MySQL 5.0.3, and 0 to 65,535 in 5.0.3 and later versions. |

**BLOB and TEXT Types**

Types | Description | Categories | Range |

BLOB | Large binary object that containing a variable amount of data. Values are treated as binary strings.You don't need to specify length while creating a column. | TINYBLOB | Maximum length of 255 characters. |

MEDIUMBLOB | Maximum length of 16777215 characters. | ||

LONGBLOB | Maximum length of 4294967295 characters | ||

TEXT | Values are treated as character strings having a character set. | TINYBLOB | Maximum length of 255 characters. |

MEDIUMBLOB | Maximum length of 16777215 characters. |

**Miscellaneous Type**

► ENUM Types

A string object whose value is chosen from a list of values given at the time of table creation. For example -

CREATE TABLE length ( length ENUM('small', 'medium', 'large') );

► Set Types

A string object having zero or more comma separated values (maximum 64).
Values are chosen from a list of values given at the time of table creation.

**Extensions for Spatial Data**

**MySQL Spatial Data**

►The Open Geospatial Consortium publishes the OpenGIS® Implementation Standard for Geographic information - Simple feature access - Part 2: SQL option, a document that proposes several conceptual ways for extending an SQL RDBMS to support spatial data. This specification is available from the OGC Web site at http://www.opengeospatial.org/standards/sfs.

Following the Open Geospatial Consortium specification, MySQL implements spatial extensions as a subset of the SQL with Geometry Types environment. This term refers to an SQL environment that has been extended with a set of geometry types. A geometry-valued SQL column is implemented as a column that has a geometry type.

**MySQL Spatial Data Types**

★ Geometry

★ Point

★ LineString

★ Polygon

★ MultiPoint

★ MultiLineString

★ MultiPolygon

★ GeometryCollection

► Geometry

Geometry is a word that denotes a geographic feature. Originally the word geometry meant measurement of the earth. Another meaning comes from cartography, referring to the geometric features that cartographers use to map the world.

Example : Use the CREATE TABLE statement to create a table with a spatial column -

CREATE TABLE geotest (code int(5),descrip varchar(50), g GEOMETRY);
MySQL Spatial Data Types

► Point

A Point is a geometry which represents a single location in coordinate space.

Usage of Point : On a city map, a Point object could represent a rail station.

Point Properties :

➔ X-coordinate value

➔ Y-coordinate value

➔ Point is defined as a zero-dimensional geometry

➔ The boundary of a Point is the empty set

► LineString Type

A LineString is a Curve with linear interpolation between points.

Usage of LineString : LineString objects could represent a river within a country map.

LineString Properties :

➔ A LineString has coordinates of segments, defined by each consecutive pair of points

➔ A LineString is a Line if it consists of exactly two points

➔ A LineString is a LinearRing if it is both closed and simple

**MySQL Spatial Data Types**

► Polygon Type

A Polygon is a planar Surface representing a multi sided geometry. It is defined by a single
exterior boundary and zero or more interior boundaries, where each interior boundary
defines a hole in the Polygon.

Usage of Polygon: LineString objects could represent a river within a country map.
Polygon Properties :

➔ The boundary of a Polygon consists of a set of LinearRing objects that make up its
exterior and interior boundaries

➔ A Polygon has no rings that cross. The rings in the boundary of a Polygon may
intersect at a Point, but only as a tangent

➔ A Polygon has no lines, spikes, or punctures

➔ A Polygon has an interior that is a connected point set

➔ A Polygon may have holes. The exterior of a Polygon with holes is not connected

► MultiPoint

A MultiPoint is a geometry collection composed of Point elements. The points are not connected or ordered in any way. Usage of LineString : On a world map, a MultiPoint could represent a chain of small islands.

MultiPoint Properties :

➔ A MultiPoint is a zero-dimensional geometry.

➔ A MultiPoint is simple if no two of its Point values are equal (have identical coordinate values).

➔ The boundary of a MultiPoint is the empty set.

► MultiLineString

A MultiLineString is a MultiCurve geometry collection composed of LineString elements.

Usage of LineString : On a region map, a MultiLineString could represent a river system or a highway system.

► MultiPolygon

MultiPolygon is a MultiSurface object composed of Polygon elements.

Usage of LineString : A MultiPolygon could represent a system of lakes On a region map

MultiPolygon Properties :

➔ A MultiPolygon is a two-dimensional geometry

➔ A MultiPolygon boundary is a set of closed curves (LineString values) corresponding to the boundaries of its Polygon elements

➔ Each Curve in the boundary of the MultiPolygon is in the boundary of exactly one Polygon element.

➔ Every Curve in the boundary of an Polygon element is in the boundary of the MultiPolygon

► GeometryCollection

A GeometryCollection is a geometry that is a collection of one or more geometries of any class.

All the elements in a GeometryCollection must be in the same Spatial Reference System. There are no other constraints on the elements of a GeometryCollection, although the subclasses of GeometryCollection described in the following sections may restrict membership. Restrictions may be based on :

➔ Element type (for example, a MultiPoint may contain only Point elements)

➔ Dimension

➔ Constraints on the degree of spatial overlap between elements

**New Content published on w3resource :**- Python Numpy exercises
- Python GeoPy Package exercises
- Python Pandas exercises
- Python nltk exercises
- Python BeautifulSoup exercises
- Form Template
- Composer - PHP Package Manager
- PHPUnit - PHP Testing
- Laravel - PHP Framework
- Angular - JavaScript Framework
- React - JavaScript Library
- Vue - JavaScript Framework
- Jest - JavaScript Testing Framework