Data Types

Character Data

char(20)
varchar(20)

The maximum length for char is 255 bytes, varchar columns is 65,535 bytes.

For Oracle Database, use varchar2 type when defining variable-length character columns.

Character Sets

MySQL can store data using various character sets, both single- and multibyte. To view the supported character sets in your server, you can use the show command.

SHOW CHARACTER SET;

To choose a character set when defining a column:

varchar(20) character set utf8

With MySQL, you may set default character set for your entire database:

create database db_name character set utf8;

Text Data

If you need to store data that might exceed the 64 KB limit for varchar columns, you will need to use one of the text types.

MySQL Text Types

Text TypeMaximum number of bytes
Tinytext255
Text65,535 (~64KB)
Mediumtext16,777,215 (~16MB)
Longtext4,294,967,295 (~4GB)

Notes

  • If data exceeds maximum size, the data will be truncated.
  • Trailing spaces will not be removed.
  • When using text columns for sorting or grouping, only the first 1024 bytes are used (limit may be increased).
  • SQL Server has a single text type for large character data. DB2 and Oracle use a data type called clob (Character Large Object)

Oracle Database:

  • 2000 bytes for char
  • 4000 bytes for varchar2

SQL Server:

  • 8000 bytes for both char and varchar

Numeric Data

MySQL Integer Types

TypeSigned RangeUnsigned Range
Tinyint−128 to 1270 to 255
Smallint−32,768 to 32,7670 to 65,535
Mediumint−8,388,608 to 8,388,6070 to 16,777,215
Int−2,147,483,648 to 2,147,483,6470 to 4,294,967,295
Bigint−9,223,372,036,854,775,808 to 9,223,372,036,854,775,8070 to 18,446,744,073,709,551,615

MySQL Floating-Point Types

TypeNumeric Range
−3.402823466E+38 to −1.175494351E-38 and 1.175494351E-38 to 3.402823466E+38
−1.7976931348623157E+308 to −2.2250738585072014E-308 and 2.2250738585072014E-308 to 1.7976931348623157E+308

you can specify a precision (the total number of allowable digits both to the left and to the right of the decimal point) and a scale (the number of allowable digits to the right of the decimal point), but they are not required.

float(4,2) will store a total of four digits, two to the left of the decimal and two to the right of the decimal. Such a column would handle the numbers 27.44 and 8.19 just fine, but the number 17.8675 would be roun- ded to 17.87, and attempting to store the number 178.375 in your float(4,2) column would generate an error.

Temporal Data

MySQL Temporal Types

TypeDefault FormatAllowable Values
DateYYYY-MM-DD1000-01-01 to 9999-12-31
DatetimeYYYY-MM-DD HH:MI:SS1000-01-01 00:00:00 to 9999-12-31 23:59:59
TimestampYYYY-MM-DD HH:MI:SS1970-01-01 00:00:00 to 2037-12-31 23:59:59
YearYYYY1901 to 2155
TimeHHH:MI:SS−838:59:59 to 838:59:59

If you wanted to insert the date March 23, 2005 into a date column using the default format YYYY-MM-DD, you would use the string ‘2005-03-23’

Date Format Components

ComponentDefinitionRange
YYYYYear, including century1000 to 9999
MMMonth01 (January) to 12 (December)
DDDay01 to 31
HHHour00 to 23
HHHHours (elapsed)−838 to 838
MIMinute00 to 59
SSSecond00 to 59

Date Conversions

The following would fail, since ‘DEC-21-1980’ violates default date format.

UPDATE person
SET birth_date = 'DEC-21-1980'
WHERE person_id = 1;

To convert string to date:

UPDATE person
SET birth_date = str_to_date('DEC-21-1980', '%b-%d-%Y')
WHERE person_id = 1;
PlaceholderDescription
%aThe short weekday name, such as Sun, Mon, …
%bThe short month name, such as Jan, Feb, …
%cThe numeric month (0..12)
%dThe numeric day of the month (00..31)
%fThe number of microseconds (000000..999999)
%HThe hour of the day, in 24-hour format (00..23)
%hThe hour of the day, in 12-hour format (01..12)
%iThe minutes within the hour (00..59)
%jThe day of year (001..366)
%MThe full month name (January..December)
%mThe numeric month
%pAM or PM
%sThe number of seconds (00..59)
%WThe full weekday name (Sunday..Saturday)
%wThe numeric day of the week (0=Sunday..6=Saturday)
%YThe four-digit year

NULL

Scenarios

  • Not Applicable: column not used
  • Unknown Value: optional column
  • Value Undefined

Notes

  • An expression can be null, but it can never equal null
  • Two nulls are never equal to each other
SELECT emp_id, fname, lname, superior_emp_id
FROM employee
WHERE superior_emp_id IS NULL
	AND emp_id IS NOT NULL;