0% found this document useful (0 votes)
8 views

Data Types

Uploaded by

moazmizan666
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
8 views

Data Types

Uploaded by

moazmizan666
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 5

In MySQL, data types define the kind of data that can be stored in a table's columns.

Data types
can be categorized broadly into numeric, date and time, and string (or character) types.

Here's an overview of the main data types in MySQL with examples.

1. Numeric Data Types

These are used for storing numbers, including integers and decimals.

a. INT

 Used for storing whole numbers (positive or negative) without decimals.


 Example:

CREATE TABLE Employees (


EmployeeID INT,
Salary INT
);

Here, EmployeeID and Salary are integers.

b. DECIMAL (M, D)

 Stores exact numeric values with a fixed number of decimal places.


 M specifies the maximum number of digits, and D specifies the number of decimal places.
 Example:

CREATE TABLE Products (


ProductID INT,
Price DECIMAL(10, 2)
);

This will store prices with up to 10 digits, 2 of which are decimal places.

c. FLOAT

 Stores approximate numeric values with floating-point precision.


 Example:

CREATE TABLE Measurements (


MeasurementID INT,
Value FLOAT
);

This can store fractional values like 12.34 or 45.67.


d. BOOLEAN

 Stores TRUE or FALSE. MySQL treats it as an alias for TINYINT(1) where 0 is FALSE and
any non-zero value is TRUE.
 Example:

CREATE TABLE Users (


UserID INT,
IsActive BOOLEAN
);

Here, IsActive will store whether a user is active (TRUE) or not (FALSE).

2. Date and Time Data Types

These store date and time values.

a. DATE

 Stores date values in the format YYYY-MM-DD.


 Example:

CREATE TABLE Orders (


OrderID INT,
OrderDate DATE
);

This will store dates like 2024-10-01.

b. DATETIME

 Stores both date and time in the format YYYY-MM-DD HH:MM:SS.


 Example:

CREATE TABLE Events (


EventID INT,
StartTime DATETIME
);

This will store values like 2024-10-01 14:30:00.

c. TIMESTAMP

 Similar to DATETIME, but also automatically stores the current timestamp when a record is
inserted or updated.
 Example:
CREATE TABLE AuditLog (
LogID INT,
ActionTime TIMESTAMP DEFAULT CURRENT_TIMESTAMP
);

This will automatically store the time when a log entry is created.

d. TIME

 Stores time values in the format HH:MM:SS.


 Example:

CREATE TABLE ShiftSchedule (


ShiftID INT,
StartTime TIME
);

This will store time values like 08:30:00.

3. String (or Character) Data Types

These store text and binary data.

a. VARCHAR (M)

 Stores variable-length strings up to M characters.


 Example:

CREATE TABLE Customers (


CustomerID INT,
Name VARCHAR(50)
);

This will store customer names with up to 50 characters.

b. CHAR (M)

 Stores fixed-length strings. If the stored value is shorter than the specified length, it is
padded with spaces.
 Example:

CREATE TABLE Employees (


EmployeeID INT,
Gender CHAR(1)
);

This will store a single character (like M or F for gender).


c. TEXT

 Stores large text data. Useful for storing paragraphs or long descriptions.
 Example:

CREATE TABLE Articles (


ArticleID INT,
Content TEXT
);

This can store long blocks of text like article content.

d. BLOB (Binary Large Object)

 Stores binary data, such as images, audio, or video files.


 Example:

CREATE TABLE Images (


ImageID INT,
ImageData BLOB
);

This will store binary data, such as an image in the form of a binary stream.

4. Other Data Types

a. ENUM

 Stores one value from a predefined list of allowed values.


 Example:

CREATE TABLE Orders (


OrderID INT,
Status ENUM('Pending', 'Shipped', 'Delivered', 'Cancelled')
);

This limits Status to one of the specified values.

b. SET

 Stores one or more values from a predefined set of values.


 Example:

CREATE TABLE Preferences (


UserID INT,
FavoriteColors SET('Red', 'Green', 'Blue', 'Yellow')
);
This allows a user to select multiple favorite colors.

Summary of Key MySQL Data Types

Category Data Type Example


Numeric INT Salary INT
DECIMAL(M, D) Price DECIMAL(10, 2)
FLOAT Value FLOAT
BOOLEAN IsActive BOOLEAN
Date & Time DATE OrderDate DATE
DATETIME StartTime DATETIME
TIMESTAMP ActionTime TIMESTAMP
TIME StartTime TIME
String VARCHAR(M) Name VARCHAR(50)
CHAR(M) Gender CHAR(1)
TEXT Content TEXT
BLOB ImageData BLOB
Other ENUM Status ENUM('Pending',...)
SET FavoriteColors SET(...)

You might also like