DataTypes

  1. Character Data Types
    • CHAR
    • VARCHAR
    • TEXT
  2. Numeric Data Types
    • INT
    • DECIMAL
    • FLOAT
  3. Date and Time Data Types
    • DATE
    • TIME
    • TIMESTAMP
  4. Boolean Data Type
    • BOOLEAN
  5. Binary Data Types
    • BINARY
    • VARBINARY
    • BLOB
  6. Other Data Types
    • ENUM
    • JSON
    • GEOMETRY

1. Character Data Types
CHAR
The CHAR data type is used to store fixed-length character strings. For

CREATE TABLE Employees (
EmployeeID INT,
FirstName CHAR(20),
LastName CHAR(20)
);

VARCHAR
VARCHAR is used for variable-length character strings. It’s more space-efficient than CHAR. Example:

CREATE TABLE Products (
ProductID INT,
ProductName VARCHAR(255)
);

TEXT
The TEXT data type is used for large text data. It’s suitable for storing long descriptions or documents:

CREATE TABLE Articles (
ArticleID INT,
Content TEXT
);

2. Numeric Data Types

INT
INT is for storing whole numbers. It’s commonly used for IDs, counts, and other integer values:
CREATE TABLE Orders (
OrderID INT,
Quantity INT
);

DECIMAL
DECIMAL is used for fixed-point numeric values with a specific precision and scale:

CREATE TABLE Prices (
ProductID INT,
Price DECIMAL(10, 2)
);

FLOAT
FLOAT is used for approximate numeric values with a variable number of decimal places:

CREATE TABLE SensorData (
SensorID INT,
Value FLOAT
);

3. Date and Time Data Types
DATE
DATE is used to store dates without time information:

CREATE TABLE Events (
EventID INT,
EventDate DATE
);

TIME
TIME is used to store time values without date information:

CREATE TABLE Appointments (
AppointmentID INT,
StartTime TIME
);

TIMESTAMP
TIMESTAMP is used to store date and time together:

CREATE TABLE LogEntries (
LogID INT,
Timestamp TIMESTAMP
);

4. Boolean Data Type
BOOLEAN
BOOLEAN represents a true or false value:

sql
Copy code
CREATE TABLE Tasks (
TaskID INT,
IsComplete BOOLEAN
);
5. Binary Data Types
BINARY
BINARY is used for fixed-length binary data, such as encryption keys:

CREATE TABLE Users (
UserID INT,
PublicKey BINARY(64)
);

VARBINARY
VARBINARY is for variable-length binary data:

CREATE TABLE Files (
FileID INT,
Content VARBINARY(MAX)
);

BLOB
BLOB is used to store large binary objects like images or documents:

CREATE TABLE Images (
ImageID INT,
ImageData BLOB
);

6. Other Data Types
ENUM
ENUM is used for creating a list of predefined values:

sql
Copy code
CREATE TABLE SurveyAnswers (
QuestionID INT,
Answer ENUM(‘Yes’, ‘No’, ‘Maybe’)
);
JSON
JSON data type is used for storing JSON-formatted data:

sql
Copy code
CREATE TABLE Products (
ProductID INT,
ProductDetails JSON
);
GEOMETRY
GEOMETRY is used for storing spatial data, like geographical information:

sql
Copy code
CREATE TABLE Locations (
LocationID INT,
Coordinates GEOMETRY
);
Conclusion:
Understanding SQL data types and their appropriate usage is essential for efficient database design and querying. This blog post has provided you with an overview of the most commonly used SQL data types, along with practical examples to help you grasp their implementation. Whether you’re a beginner or an experienced SQL developer, this knowledge will be invaluable in your journey to mastering SQL.

Leave a Comment

Your email address will not be published. Required fields are marked *

Scroll to Top