Lesson 3 Data Types
Lesson 3 Data Types
Learning Objectives:
At the end of the lesson, the learners should be able to:
1. Define data type in MySQL
2. Identify the different data types in MySQL: Numeric, Date and Time and
String
3. Define and identify NULL and NOT NULL values
MANIPULATING DATABASE TABLE
In MySQL, the structure for holding the blocks of information ( records ) is called
table.
The records hold the smallest piece of information is called datatype.
The hierarchy of a MySQL database looks as follows:
Database < Table < Record < Datatype
In order to create a table the user has to specify the following parameters:
• name of the table ( which has to be unique in the scope of a particular
database )
• a column name and its data type
• the primary key of the table .
MySQL DATA TYPES
The definition of a table column includes the specification of the column
name and the specification of the data type that this column holds.
In MySQL, there are three main data types: numeric, date and time, and string. It is an
attribute that specifies the type of data of any object.
You should use only the type and size of field you really need to use. For example, do
not define a field 10 characters wide, if you know you are only going to use 2
characters.
1. Numeric data type
INT() A normalsized integer that can be signed or unsigned. If signed, the
allowable range is from 2147483648 to 2147483647. If unsigned, the allowable
range is from 0 to 4294967295. You can specify a width of up to 11 digits.
FLOAT (p,m) A floatingpoint number that cannot be unsigned. You can
define the display length or precission (P) and the number of decimals or
mantissa (M). This is not required and will default to 10,2, where 2 is the number
of decimals and 10 is the total number of digits (including decimals). Decimal
precision can go to 24 places for a FLOAT.
2. Date and Time data type
DATE() A date in YYYYMMDD format, between 10000101 and 99991231.
For example, December 30 th 1973 would be stored as 19731230.
DATETIME() A date and time combination in YYYYMMDD HH:MM:SS format,
between 10000101 00:00:00 and 99991231 23:59:59. For example, 3:30 in the
afternoon on December 30 th 1973 would be stored as 19731230 15:30:00.
TIME() Stores the time in a HH:MM:SS format.
YEAR() Stores a year in a 2digit or a 4digit format. If the length is specified
as 2 (for example YEAR(2)), YEAR can be between 1970 to 2069 (70 to 69). If the
length is specified as 4, then YEAR can be 1901 to 2155. The default length is 4.
3. String data type
CHAR(N) Fixed width character string. Maximum 8,000 characters
VARCHAR(N) Variable with character string. Maximum 8,000 characters
DEFINING NULL VALUES
A field with a NULL value is a field with no value. It is the term used to represent
a missing value. A NULL value in a table is a value in a field that appears to be blank.
It is very important to understand that a NULL value is different from a zero value
or a field that contains spaces. A field with a NULL value is one that has been left
blank during record creation.
NOT NULL signifies that column should always accept an explicit value of the
given data type.
ACTIVITY #3
Based on the previous activity, describe your fields using the different data
types you have learned today.
Example:
STUDENT ID NO INT(11)
NAME CHAR(90)
BIRTHDAY DATE
ADDRESS VARCHAR(90)
GRADE LEVEL INT(2)
SECTION VARCHAR(10)
REFERENCES
1. https://coronet.iicm.tugraz.at/mysql/structure.html
2. https://www.w3schools.com/sql/sql_datatypes.asp
3. https://www.tutorialspoint.com/sql/sqlnullvalues.htm