0% found this document useful (0 votes)
26 views15 pages

Data types

The document provides an overview of various data types used in MySQL Server, including their definitions and examples of usage. It covers types such as BigInt, Binary, Bit, Char, Date, and more, detailing their characteristics and how they can be implemented in SQL queries. Each data type is illustrated with a CREATE TABLE statement and an INSERT example to demonstrate practical application.

Uploaded by

virshaarif59
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
26 views15 pages

Data types

The document provides an overview of various data types used in MySQL Server, including their definitions and examples of usage. It covers types such as BigInt, Binary, Bit, Char, Date, and more, detailing their characteristics and how they can be implemented in SQL queries. Each data type is illustrated with a CREATE TABLE statement and an INSERT example to demonstrate practical application.

Uploaded by

virshaarif59
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 15

Data types Used in MySQL Server

 BigInt

The bigint data type is intended for use when integer values might exceed the range that is
supported by the int data type. Like x = 9007199254740995n.

For Example:

CREATE TABLE Employees (


EmployeeID BIGINT PRIMARY KEY,
Name NVARCHAR(100),
Salary BIGINT
);

INSERT INTO Employees (EmployeeID, Name, Salary)


VALUES (10000000001, 'John Doe', 120000000);

 Binary(50)

Since the binary data types are unstructured types, they can store many different types of
information. It can store IP addresses, MAC addresses, or device identification numbers from
RFID tags.

For Example:

CREATE TABLE Files (


FileID INT PRIMARY KEY,
FileName NVARCHAR(100),
FileData BINARY(50)
);

INSERT INTO Files (FileID, FileName, FileData)


VALUES (1, 'example.txt', 0x48656C6C6F20576F726C64);

 Bit
The bit data type can be used to store Boolean values. The string values TRUE and FALSE
can be converted to bit values: TRUE is converted to 1 , and FALSE is converted to 0 .

For Example:

CREATE TABLE Users (


UserID INT PRIMARY KEY,
UserName NVARCHAR(100),
IsActive BIT -- Stores either 0 or 1
);

INSERT INTO Users (UserID, UserName, IsActive)


VALUES (1, 'John Doe', 1);

 Char(10)

CHAR(10) is a fixed-length character data type that stores exactly 10 characters. When you
define a column as CHAR(10), the database reserves space for 10 characters, regardless of how
many characters are actually stored. If you insert a string shorter than 10 characters, the
database will pad it with spaces to ensure it occupies 10 characters.

For Example:

CREATE TABLE Products (


ProductID INT PRIMARY KEY,
ProductCode CHAR(10),
ProductName VARCHAR(50)
);

INSERT INTO Products (ProductID, ProductCode, ProductName)


VALUES (1, 'AB12345', 'Sample Product');

 Date

The DATE type is used for values with a date part but no time part. MySQL retrieves and
displays DATE values in YYYY-MM-DD.

For Example:
CREATE TABLE Employees (
EmployeeID INT PRIMARY KEY,
FirstName VARCHAR(50),
LastName VARCHAR(50),
BirthDate DATE -- Stores only the date part
);

INSERT INTO Employees (EmployeeID, FirstName, LastName, BirthDate)


VALUES (1, 'John', 'Doe', '1990-05-15');

 DateTime

The DATETIME data type in SQL is used to store date and time information. It includes both
the date (year, month, day) and the time (hours, minutes, seconds).

For Example:

CREATE TABLE Events (


EventID INT PRIMARY KEY,
EventName NVARCHAR(100),
EventDate DATETIME
);

INSERT INTO Events (EventID, EventName, EventDate)


VALUES (1, 'Conference', '2024-09-17 10:30:00');

 DateTime2(7)
DATETIME2(7) is a data type in SQL Server that stores date and time values with higher
precision and a wider date range compared to the standard DATETIME.

The (7) represents the fractional seconds precision, meaning it can store up to 7 decimal places
for the seconds component, which provides precision down to 100 nanoseconds.

For Example:

CREATE TABLE Events (

EventID INT PRIMARY KEY,

EventName NVARCHAR(100),
EventTimestamp DATETIME2(7)

);

INSERT INTO Orders (OrderID, OrderDate)

VALUES (1, '2024-09-17 14:30:15.1234567');

 Datetimeoffset(7)

Returns a datetime2(7) value containing the date and time of the computer on which the
instance of SQL server is running.

For Example:

CREATE TABLE Appointments (


AppointmentID INT PRIMARY KEY,
Description NVARCHAR(100),
AppointmentTime DATETIMEOFFSET(7)
);

INSERT INTO Appointments (AppointmentID, Description, AppointmentTime)


VALUES (1, 'Meeting with Client', '2024-09-17 14:30:15.1234567 -05:00');

 Decimal(18,0)

DECIMAL(18,0) is a fixed-point data type in databases that stores exact numeric values
without any fractional (decimal) places.
Precision (18): This means the value can have up to 18 digits in total.
Scale (0): The scale of 0 means there are no digits after the decimal point. In other
words, it's used to store whole numbers (integers) with high precision, but without
any fractional or decimal part.

For Example:

CREATE TABLE Accounts (


AccountID INT PRIMARY KEY,
AccountNumber DECIMAL(18,0), -- Stores whole numbers up to 18 digits Balance
DECIMAL(18,0) -- Stores account balances as whole numbers
);

INSERT INTO Transactions (TransactionID, Amount)


VALUES (1, 150000000000000000);

 Float

The float data types are used to store positive and negative numbers with a decimal point.
It can store values like , 35.3, -2.34, or 3597.34987.

For Example:

CREATE TABLE Measurements (


MeasurementID INT PRIMARY KEY,
Temperature FLOAT, -- Stores approximate values for temperature
Pressure FLOAT -- Stores approximate values for pressure
);

INSERT INTO Measurements (MeasurementID, Temperature, Pressure)


VALUES (1, 23.4567, 1013.25);

 Geography

The geography spatial data type, geography, is implemented as a . NET common language
runtime (CLR) data type in SQL Server. This type represents data in a round-earth coordinate
system. The SQL Server geography data type stores ellipsoidal (round-earth) data, such as GPS
latitude and longitude coordinates.

For Example:

CREATE TABLE Locations (


LocationID INT PRIMARY KEY,
Name NVARCHAR(100),
Coordinates GEOGRAPHY -- Stores geographic coordinates
);

INSERT INTO Locations (ID, Name, Location)


VALUES (1, 'Seattle', GEOGRAPHY::Point(47.6097, -122.3331, 4326));
 Geometry

The GEOMETRY data type in databases (such as SQL Server, MySQL, and PostgreSQL with
PostGIS extension) is used to store planar (flat) spatial data. This type is typically used for
representing spatial objects like points, lines, polygons, and more in a two-dimensional (2D)
coordinate system. Unlike the GEOGRAPHY type, which accounts for the Earth's curvature,
GEOMETRY assumes a flat, Cartesian coordinate system.

For Example:

CREATE TABLE Shapes (


ShapeID INT PRIMARY KEY,
Name NVARCHAR(100),
Shape GEOMETRY -- Stores geometric shapes
);

INSERT INTO Shapes (ShapeID, Name, Shape)


VALUES (1, 'Triangle', GEOMETRY::STGeomFromText('POLYGON((0 0, 10 0, 5 10, 0 0))',
0));

 Hierrchyid

The HIERARCHYID data type in databases, specifically in Microsoft SQL Server, is used to
store hierarchical data in an efficient manner. It is designed to represent data with a tree-like
structure, such as organizational charts, file systems, or product categories, where each node
can have a parent-child relationship.

For Example:

CREATE TABLE Employees (


EmployeeID INT PRIMARY KEY,
Name NVARCHAR(100),
OrgNode HIERARCHYID -- Stores hierarchical position in the organization
);

INSERT INTO OrganizationalStructure (EmployeeID, EmployeeName, Position)


VALUES (1, 'CEO', HIERARCHYID::GetRoot());
INSERT INTO OrganizationalStructure (EmployeeID, EmployeeName, Position)
VALUES (2, 'Manager', HIERARCHYID::GetRoot().GetDescendant(NULL, NULL));
 Image

The IMAGE data type in databases (like older versions of SQL Server) was historically used to
store binary large objects (BLOBs), specifically for storing large binary data such as
images, videos, or other multimedia content. However, in modern versions of SQL Server,
the IMAGE data type has been deprecated in favor of the more flexible VARBINARY(MAX)
type.

For Example:

CREATE TABLE Documents (


DocumentID INT PRIMARY KEY,
DocumentName NVARCHAR(255),
DocumentData VARBINARY(MAX) -- Stores binary data such as images
);

 Int

The INT data type in a database is used to store integer values, which are whole numbers
without any decimal places.it can store values like , 35 ,-10.

For Example:

CREATE TABLE Multimedia (


MediaID int PRIMARY KEY,
MediaContent IMAGE
);

 Money

The MONEY data type stores currency amounts.

For Example:

CREATE TABLE Products (


ProductID INT PRIMARY KEY,
ProductName NVARCHAR(100),
Price MONEY, -- Stores the price of the product in monetary terms
Discount MONEY -- Stores any discount amount applied to the product
);

 Nchar(10)

The NCHAR(10) data type in databases is used to store fixed-length Unicode character
strings. The 10 specifies that each value stored in this column will have a length of exactly 10
characters, and if the data is shorter, it will be padded with spaces to reach the specified length.

For Example:

CREATE TABLE Employees (


EmployeeID INT PRIMARY KEY,
FirstName NCHAR(10), -- Fixed length of 10 characters for the first name
LastName NCHAR(10) -- Fixed length of 10 characters for the last name
);

INSERT INTO Products (ProductID, ProductCode)


VALUES (1, 'ABCD1234 ');
INSERT INTO Products (ProductID, ProductCode)
VALUES (2, 'XYZ9876 ');

 Ntext

The NTEXT data type in databases is used to store large amounts of Unicode text. It is a deprecated
data type in some database systems but was historically used for storing long text data with Unicode
support.

For Example:

CREATE TABLE Articles (


ArticleID INT PRIMARY KEY,
Title NVARCHAR(255),
Content NTEXT -- Large Unicode text data for storing article content );

INSERT INTO Articles (ArticleID, Title, Content)


VALUES (1, N'SQL Server Data Types', );
 Unicode Support
NTEXT supports Unicode characters, which means it can store text in multiple languages and
character sets, making it suitable for international applications

For Example:

CREATE TABLE Articles (


ArticleID INT PRIMARY KEY,
Title NVARCHAR(255),
Content NTEXT -- Large Unicode text data for storing article content
);

 Nvarchar(50)

The NVARCHAR(50) data type in databases is used to store variable-length Unicode


character strings with a maximum length of 50 characters.

NVARCHAR(MAX) is a data type in SQL Server used to store variable-length Unicode data.
The "MAX" keyword allows the field to store up to 2^31-1 bytes of data (which is about 2 GB).
This is useful for storing large amounts of text that might exceed the usual 4,000-character limit
of a regular NVARCHAR field.

For Example:

CREATE TABLE Customers (


CustomerID INT PRIMARY KEY,
FirstName NVARCHAR(50), -- First name of the customer (up to 50 characters)
LastName NVARCHAR(50), -- Last name of the customer (up to 50 characters)
Email NVARCHAR(50) -- Email address of the customer (up to 50 characters)
);

 Smalldatetime

The SMALLDATETIME data type in SQL Server is used to store date and time values with a
smaller range and precision compared to the DATETIME type. Specifically:

 Range: From January 1, 1900, to June 6, 2079.


 Precision: Stores dates and times to the nearest minute (not seconds or milliseconds).

For Example:
 Smallint

The SMALLINT data type in SQL is used to store small integer values. It is a 2-byte (16-bit) signed
integer, meaning it can store numbers within a limited range compared to larger integer types like INT
or BIGINT.

For Example:

CREATE TABLE Employees (


EmployeeID SMALLINT PRIMARY KEY,
Age SMALLINT );

INSERT INTO Employees (EmployeeID, Age)


VALUES (100, 30);

 Smallmoney

The SMALLMONEY data type in databases is used to store small monetary values. It has a
fixed precision and scale, and is typically used in financial applications where exact decimal
values are required but the range of values is relatively small.

For Example:

CREATE TABLE Products (


ProductID INT PRIMARY KEY,
ProductName NVARCHAR(50),
Price SMALLMONEY -- Price stored as SMALLMONEY with up to four decimal places );

 Sql-variant
The SQL_VARIANT data type in databases is used to store values of different data types in a
single column. It allows for flexible storage of various data types within a single column,
accommodating different types of data without requiring a separate column for each type.

For Example:

CREATE TABLE FlexibleData (


DataID int PRIMARY KEY,
DataValue SQL_VARIANT );
INSERT INTO FlexibleData (DataID, DataValue)
VALUES (1, 12345);
INSERT INTO FlexibleData (DataID, DataValue)
VALUES (2, 'Text Data');

 Text

The TEXT data type stores any kind of text data. It can contain both single-byte and multibyte
characters that the locale supports.

For Example:

CREATE TABLE Articles (


ArticleID INT PRIMARY KEY,
Title NVARCHAR(255),
Content TEXT -- Large text content
);

INSERT INTO Articles (ArticleID, Title, Content)


VALUES (1, N'Introduction to SQL',
'This article explains the basics of SQL, including queries, joins, and functions...');

 Time(7)

Time(7) is a data type in SQL Server that stores time values with 7 decimal places of precision.
It has a range of 00:00:00.0000000 through 23:59:59.9999999.

For Example:

CREATE TABLE EventLogs (


EventID INT PRIMARY KEY,
EventName NVARCHAR(255),
EventTime TIME(7) -- Time of the event with high precision
);

 TinyInt

The TINYINT data type is used to store unsigned integers requiring 1 byte of storage.
For Example:

CREATE TABLE Employees (


EmployeeID INT PRIMARY KEY,
Name NVARCHAR(100),
Age TINYINT -- Age stored as a small integer value
);

INSERT INTO Employees (EmployeeID, Name, Age)


VALUES (1, N'John Doe', 30),
(2, N'Jane Smith', 25);

 Timestamp

The TIMESTAMP data type is used to store a unique binary number that is automatically
generated each time a row is inserted or updated. It is often used to track changes to rows and
for concurrency control

For Example:

CREATE TABLE Employees (


EmployeeID INT PRIMARY KEY,
Name NVARCHAR(100),
LastModified ROWVERSION -- Automatically generated unique value for concurrency
control
);

 Uniqueidentifer

The Uniqueidentifier is a data type in Microsoft SQL Server , which is used to store Globally
Unique Identifier (GUIDs) . A GUID (globally unique identifier) is a 16-bit text string that
represents an identification (ID).

Commonly used for primary keys in tables where the uniqueness needs to be maintained
independently of the database system. It’s also useful in distributed systems and for data
integration scenarios.
For Example:

CREATE TABLE Users (


UserID UNIQUEIDENTIFIER PRIMARY KEY DEFAULT NEWID(),
UserName VARCHAR(100)
);

INSERT INTO Users (UserName)


VALUES ('Alice');
INSERT INTO Users (UserName)
VALUES ('Bob');

 Varbinary(50)

The VARBINARY(50) data type in SQL Server is used to store variable-length binary data
with a maximum length of 50 bytes. It can be used to store any kind of binary data, such as
images, files, or other binary objects, up to the specified.

For Example:

CREATE TABLE Files (


FileID INT PRIMARY KEY,
FileName NVARCHAR(255),
FileData VARBINARY(50) -- Column for storing binary data up to 50 bytes
);

 Varbinary(MAX)

The VARBINARY(MAX) data type in SQL Server is used to store variable-length binary data
with a maximum size of 2^31-1 bytes (about 2 GB). This type is useful for storing large
amounts of binary data, such as images, files, or other large binary objects.

For Example:
CREATE TABLE Files (

FileID INT PRIMARY KEY,

FileName NVARCHAR(255), -- The name of the file FileContent

VARBINARY(MAX) -- The binary content of the file (up to 2 GB)


);

INSERT INTO Files (FileID, FileName, FileContent)


VALUES (1, N'ImageFile.png',
CONVERT(VARBINARY(MAX), 0x89504E470D0A1A0A)
);

 Varchar(50)

The VARCHAR(50) data type in databases is used to store variable-length character strings.
The 50 specifies the maximum number of characters that can be stored in the column.
VARCHAR can store strings of varying lengths up to the specified maximum.

For Example:

CREATE TABLE Comments (


CommentID int PRIMARY KEY,
CommentText VARCHAR(50)
);

INSERT INTO Comments (CommentID, CommentText)


VALUES (1, 'This is a comment.');
INSERT INTO Comments (CommentID, CommentText)
VALUES (2, 'Another comment.');

 Varchar(MAX)

The VARCHAR(MAX) data type in databases is used to store variable-length character


strings with a maximum length of up to 2^31-1 characters (which is approximately 2 GB of
data). It is designed for situations where the length of the text data is not known in advance or
can be very large, exceeding the limits of standard VARCHAR types.

For Example:
CREATE TABLE LongComments (
CommentID int PRIMARY KEY,
CommentText VARCHAR(MAX)
);

INSERT INTO LongComments (CommentID, CommentText)


VALUES (1, REPLICATE('A', 1000000)
);

 xml

A XML database is a database that can be used to store large amounts of data or information in
the XML . They can handle data which is of any size or format. XML is a markup language that
uses “tags” or specially formatted text labels, to identify the function of varied data elements
within a document.

For Example:

CREATE TABLE XmlData (


XmlID int PRIMARY KEY,
XmlContent XML
);

INSERT INTO XmlData (XmlID, XmlContent)


VALUES (1, 'Value');
INSERT INTO XmlData (XmlID, XmlContent)
VALUES (2, '');

You might also like