EDUCBA

EDUCBA

MENUMENU
  • Free Tutorials
  • Free Courses
  • Certification Courses
  • 600+ Courses All in One Bundle
  • Login
Home Data Science Data Science Tutorials SQL Tutorial SQL Data Types
Secondary Sidebar
SQL Tutorial
  • Basic
    • What is SQL
    • Careers in SQL
    • Careers in SQL Server
    • IS SQL Microsoft?
    • SQL Management Tools
    • What is SQL Developer
    • Uses of SQL
    • How to Install SQL Server
    • What is SQL Server
    • SQL Quick References
    • SQL Like Wildcard
    • SQL Like with Multiple Values
    • SQL Examples
    • SQL Server Versions
    • SQL DROP DB
    • SQL Case Insensitive
    • SQL Expressions
    • Database in SQL
    • SQL Data Types
    • SQL Keywords
    • Composite Key in SQL
    • SQL WAITFOR
    • SQL Constraints
    • Transactions in SQL
    • First Normal Form
    • SQL Server Data Types
    • SQL Administration
    • SQL Variables
    • SQL Enum
    • SQL GROUP BY WHERE
    • SQL ROW
    • SQL EXECUTE
    • SQL EXCLUDE
    • SQL Performance Tuning
    • SQL UUID
    • Begin SQL
    • SQL Update Join
    • SQL Between Dates
    • Cheat sheet SQL
  • Operators
    • SQL Operators
    • SQL Arithmetic Operators
    • SQL Logical Operators
    • SQL String Operators
    • Ternary Operator in SQL
  • Commands
    • SQL Commands
    • sqlplus set commands
    • SQL Alter Command
    • SQL Commands Update
    • SQL DML Commands
    • SQL DDL Commands
    • FETCH in SQL
  • Clause
    • SQL Clauses
    • SQL IN Operator
    • SQL SELECT DISTINCT Multiple Columns
    • SQL Null Values
    • SQL LIKE
    • SQL LIKE Query
    • SQL LIKE Operator
    • SQL LIKE Clause
    • SQL NOT Operator
    • SQL Minus
    • SQL WHERE Clause
    • SQL with Clause
    • SQL HAVING Clause
    • SQL HAVING Clause
    • SQL GROUP BY DAY
    • ORDER BY Clause in SQL
    • SQL ORDER BY CASE
    • SQL ORDER BY DESC
    • SQL ORDER BY DATE
    • SQL ORDER BY Alphabetical
    • SQL ORDER BY Ascending
    • SQL Order by Count
    • SQL GROUP BY Month
    • SQL GROUP BY Multiple Columns
    • SQL GROUPING SETS
  • Queries
    • SQL Insert Query
    • SQL SELECT Query
    • SQL SELECT RANDOM
    • SQL Except Select
    • SQL Subquery
    • SQL SELECT DISTINCT
    • SQL WITH AS Statement
  • Keys
    • SQL Keys
    • SQL Foreign Key
    • Primary Key in SQL
    • Foreign Key in SQL
    • Unique Key in SQL
    • SQL UNIQUE Constraint
    • SQL Primary Key
    • Alternate Key in SQL
    • SQL Super Key
  • Functions
    • SQL Date Function
    • SQL Server Functions
    • SQL String Functions
    • SQL Compare String
    • Timestamp to Date in SQL
    • SQL REGEX
    • SQL Window Functions
    • SQL Syntax
    • SQL CONCAT
    • SQL ALTER TABLE
    • SQL MOD()
    • SQL Timestamp
    • SQL Min and Max
    • SQL TO_DATE()
    • SQL DATEADD()
    • SQL DATEDIFF()
    • SQL HOUR()
    • SQLite? functions
    • ANY in SQL
    • LIKE Query in SQL
    • SQL NOT NULL
    • SQL NOT IN
    • SQL MAX()
    • SQL MIN()
    • SQL SUM()
    • SQL COUNT
    • SQL identity
    • SQL DELETE Trigger
    • SQL Declare Variable
    • SQL Text Search
    • SQL COUNT DISTINCT
    • SQL TEXT
    • SQL Limit Order By
    • BETWEEN in SQL
    • LTRIM() in SQL
    • TOP in SQL
    • SQL Select Top
    • Merge SQL
    • SQL TRUNCATE()
    • SQL UNION
    • SQL ALL
    • SQL INTERSECT
    • SQL Alias
    • SQL Server Substring
    • CUBE in SQL
    • SQL RANK()
    • SQL CTE
    • SQL LAG()
    • SQL MID
    • SQL avg()
    • SQL WEEK
    • SQL DELETE
    • SQL DATEPART()
    • SQL DECODE()
    • SQL DENSE_RANK()
    • SQL NTILE()
    • SQL NULLIF()
    • SQL Stuff
    • SQL Ceiling
    • SQL EXISTS
    • SQL LEAD()
    • SQL COALESCE
    • SQL BLOB
    • SQL ROW_NUMBER
    • SQL Server Replace
    • SQL Ranking Function
    • SQL Server Permission
  • T-SQL
    • T-SQL pivot
    • T-SQL Formatter
    • T-SQL TRY CATCH
    • T-SQL CTE
    • T-SQL CASE
    • T-SQL DATEPART
    • T-SQL Date Format
    • T-SQL ROUND
    • T-SQL Loop
    • T-SQL IIF
    • T-SQL Union
    • T-SQL CREATE TABLE
    • T-SQL INSERT
    • T-SQL Stuff
    • T-SQL ISNULL
    • T-SQL ADD Column
    • T-SQL DATEDIFF
  • Joins
    • Join Query in SQL
    • Types of Joins in SQL
    • Types of Joins in SQL Server
    • SQL Inner Join
    • SQL Join Two Tables
    • SQL Delete Join
    • SQL Left Join
    • LEFT OUTER JOIN in SQL
    • SQL Right Join
    • SQL Cross Join
    • SQL Outer Join
    • SQL Full Join
    • SQL Self Join
    • Natural Join SQL
    • SQL Multiple Join
  • Advanced
    • MDF File in SQL Server
    • SQL Aliases
    • SQL Hosting
    • SQL Auto Increment
    • SQL Injection
    • SQL Wildcards
    • SQL Check
    • SQL Indexes
    • Select Distinct
    • SQL BETWEEN
    • SQLPlus spool
    • SQL Create Table
    • SQL Schema
    • Comparison Operators in SQL
    • SQL_plus
    • SQL Formatter
    • SQL LEFT INNER JOIN
    • SQL Plus Command
    • SQLPlus not found
    • SQL Injection Attack
    • Aggregate Functions in SQL
    • SQL REVOKE
    • SQL Select Distinct Count
    • IF ELSE Statement in SQL
    • SQL CASE Statement
    • SQL While Loop
    • SQL BIGINT
    • SQL Crosstab
    • SQL Wildcard Character
    • SQL INSTR()
    • SQL now
    • SQL synonyms
    • SQLite?export to csv
    • What is Procedure in SQL
    • Stored Procedure in SQL?
    • SQL Server Constraints
    • SQL DELETE ROW
    • Column in SQL
    • Table in SQL
    • SQL Virtual Table
    • SQL Merge Two Tables
    • SQL Table Partitioning
    • SQL Temporary Table
    • SQL Clone Table
    • SQL Rename Table
    • SQL LOCK TABLE
    • SQL Clear Table
    • SQL DESCRIBE TABLE
    • SQL Mapping
    • Cursors in SQL
    • AND in SQL
    • Wildcard in SQL
    • SQL FETCH NEXT
    • SQL Views
    • SQL Delete View
    • Triggers in SQL
    • SQL UPDATE Trigger
    • SQL AFTER UPDATE Trigger
    • SQL Update Statement
    • SQL DROP TRIGGER
    • SQL DROP Table
    • Types of SQL Views
    • SQL Port
    • SQL Clustered Index
    • SQL COMMIT
    • Distinct Keyword in SQL
    • PARTITION BY in SQL
    • SQL Set Operators
    • SQL UNION ALL
    • Metadata in SQL
    • SQL Bulk Insert
    • Array in SQL
    • SQL REGEXP
    • JSON in SQL
    • SQL For loop
    • EXPLAIN in SQL
    • ROLLUP in SQL
    • Escape Character SQL
    • SQL Cluster
    • SQL Backup
    • SQL Pattern Matching
    • SQL Users
    • ISNULL SQL Server
    • SQL pivot
    • SQL Import CSV
    • SQL if then else
    • SQL ignore-case
    • SQL Matches
    • SQL Search String
    • SQL Column Alias
    • SQL extensions
    • SQL Substring Function
    • Charindex SQL
  • SqlAlchemy
    • What is SQLAlchemy
    • SqlAlchemy ORM
    • SQLAlchemy count
    • SQLAlchemy update object
    • SQLAlchemy pip
    • SQLAlchemy Connection
    • SQLAlchemy Metadata
    • SQLAlchemy Raw SQL
    • SQLAlchemy Filter in List
    • SQLAlchemy Alias
    • SQLAlchemy unique
    • SQLAlchemy JSONB
    • SQLAlchemy Async
    • SQLAlchemy Types
    • SQLAlchemy Many to Many
    • SQLAlchemy Example
    • SQLAlchemy Model
    • SQLAlchemy Data Types
    • SQLAlchemy Filter
    • SQLAlchemy SQLite
    • SQLAlchemy DateTime
    • SQLAlchemy create_engine
    • SQLAlchemy Delete
    • SQLAlchemy Migrations
  • NoSQL
    • NoSQL Databases List
    • NoSQL Data Modeling
    • Types of NoSQL Databases
    • NoSQL Injection
    • NoSQL vs SQL Databases
    • NoSQL Use Cases
    • NoSQL Key Value
  • Interview Questions
    • SQL Interview Questions
    • Advance SQL Interview Questions
    • SQL Joins Interview Questions
    • SQL Server Interview Questions
    • SQL Current Month

Related Courses

JDBC Training Course

PHP course

Windows 10 Training

SQL Course Training

PL/SQL Certification Courses

Oracle Certification Courses

SQL Data Types

By Priya PedamkarPriya Pedamkar

SQL Data Types

Introduction to SQL Data Types

Data types in SQL, just like any other programming language, define what type of data can the column or the variable hold. The type of data can be anything from a character, through a series of characters, through numbers, to date and time. A data type is a very crucial concept of tightly coupled programming. It helps resolve most of the type compatibility errors during the program compilation itself. We would be broadly categorizing SQL Data types and then exploring various types and examples under those categories.

Not all database management systems support all the data types. For example, Oracle does not support DateTime. These specifics are given along with the description of the data type in the article. It is advised to be mindful of this while designing the database. Similarly, there are many other data types that are specific to any particular database management system. For example, MS SQL has a money data type. Such types are not generic and are beyond the scope of this article.

Types of SQL Data

Types of SQL Data

1. Binary

Binary data type holds a sequence of binary byte strings. Unlike the usual character strings, the binary strings are used to store unconventional data such as images.

Start Your Free Software Development Course

Web development, programming languages, Software testing & others

  • Binary(size): Stores binary byte strings of column length specified by the size parameter. The size of the column is fixed. If the size is defined as 10 bytes and data stored is 5 bytes, the remaining 5 bytes are still occupied in the memory by the column. These remaining bytes are null padded to the right by most SQL parsers. The maximum size possible is 8000 bytes.
  • Varbinary(size): Stores binary byte strings of column length specified by the size parameter. The size of the column is not fixed. If the size is defined as 10 bytes and data stored is 5 bytes, the column occupies only 5 bytes in the memory. The maximum size possible is 8000 bytes.
  • Varbinary(max): Specifying the keyword max in the size parameter increases the maximum possible size to 2GB and allocates variable size to columns based on the data stored. MySQL does not support this data type.

Example:

Code:

create table BinaryDataTypes (col_binary BINARY(5), col_varbinary VARBINARY(5));
insert into BinaryDataTypes values (101, 121);
select * from BinaryDataTypes;

Output:

sql data types

Code:

insert into BinaryDataTypes values (12345, 123456);
insert into BinaryDataTypes values (123456, 123456);

Output:

binary data types

The error occurs because we had defined the size of columns as 5 bytes only.

All in One Software Development Bundle(600+ Courses, 50+ projects)
Python TutorialC SharpJavaJavaScript
C Plus PlusSoftware TestingSQLKali Linux
Price
View Courses
600+ Online Courses | 50+ projects | 3000+ Hours | Verifiable Certificates | Lifetime Access
4.6 (86,883 ratings)

2. Character/String

These data types store either a single character or a series of characters forming a string. These include characters, numbers, and special characters. The nature of the character(s) is, however, limited to non-Unicode characters only.

Unicode Character/String

  • Char(size): Stores fixed-length character strings of the length specified by the size parameter. The sizing of the column works in a similar fashion to the BINARY data type. The default size is 1 character. The maximum is 255 in most database management systems.
  • Varchar(size): Stores variable-length character strings. However, the length specified by the size parameter defines the maximum length of the column. The maximum possible length is 65535 characters.
  • Varchar(max): Specifying the keyword max in the size parameter increases the maximum possible size to 2GB and allocates variable size to columns based on the data stored. MySQL does not support this data type.

Text: MySQL supports text data type instead of varchar(max). The text also increases the maximum possible size to 2GB.

Example:

Code:

create table CharacterDataTypes (col_char CHAR(300));

Output:

Character/String

Code:

create table CharacterDataTypes (col_char CHAR(20), col_varchar VARCHAR(20), col_text TEXT);
insert into CharacterDataTypes values ('Hello World', 'How you doing?', 'This is an incredibly longer text to demonstrate the data type text in MySQL. The data type text makes it possible to store 2GB of data in a column. That\'s equal to 2*1024*1024*1024 = over 2.14 billion characters. Whoa!');

3. Unicode Character/String

These data types store either a single or a string of Unicode characters.

Unicode Character

  • Nchar(size): This is similar to char data type and supports Unicode characters as well. The maximum limit is, however, reduced to 4000 bytes.
  • Nvarchar(size): This is similar to the nvarchar data type and supports Unicode characters as well. The maximum limit is, however, reduced to 4000 bytes.
  • Nvarchar(max): Specifying the keyword max in the size parameter increases the maximum possible size to 1GB and allocates variable size to columns based on the data stored. MySQL does not support this data type.
  • Ntext: MySQL supports the ntext data type instead of nvarchar(max). Ntext also increases the maximum possible size to 1GB.

Example:

Code:

create table UnicodeCharacterDataTypes (col_unicode_char NCHAR(200));
insert into UnicodeCharacterDataTypes values ('局层局层');
select * from UnicodeCharacterDataTypes;

Output:

Unicode Character/String

4. Numeric

Numeric

Numeric data types support integers and real/fractional/decimal numbers.

  • Bit/Bool/Boolean: These datatypes store only two values – 0 and 1. 0 denotes false while 1 denotes true. Most databases support a bit. Some advanced databases do support bool and boolean as well.
  • INT: Stores positive and negative integers up to 4 Bytes in size. This means the value must be in the range [-2147483648, 2147483647]. Specifying the keyword unsigned restricts the column to store only positive values in the range [0, 4294967295].
  • TINYINT: Stores positive and negative integers up to 1 Byte in size. This means the value must be in the range [-128, 127]. Specifying the keyword unsigned restricts the column to store only positive values in the range [0, 255].
  • Smallint: Stores positive and negative integers up to 2 Bytes in size. This means the value must be in the range [-32768, 32767]. Specifying the keyword unsigned restricts the column to store only positive values in the range [0, 65535].
  • Bigint: Stores positive and negative integers up to 8 Bytes in size. This means the value must be in the range [-263, 263-1]. Specifying the keyword unsigned restricts the column to store only positive values in the range [0, 264-1].
  • Decimal (size, D): Stores a fixed-point number. The parameter size specifies the total number of digits, whereas d specifies the number of digits following the decimal point. Default values for size and d are 10 and 0, respectively.
  • Float (size, D) / Double (size, D): Stores a floating-point number. Float is used for smaller numbers up to 4 Bytes in size. Double stores larger numbers. However, double is not supported by all databases.
  • Out-of-syllabus knowledge: The difference between fixed-point and floating-point numbers is that the decimal point is fixed in one and floating in another. Fixed-point means that the number of digits is fixed after and before the decimal point. Floating-point means that the number of digits before and after the decimal point can vary relative to the significance of the digits in the number.

Example:

Code:

create table NumericDataTypes (col_tinyint_u TINYINT UNSIGNED, col_smallint SMALLINT, col_int INT, col_bigint BIGINT UNSIGNED, col_decimal DECIMAL(5,2), col_float FLOAT);
insert into NumericDataTypes values (255, -32768, 1234567890, (POWER(2, 63)-1), 987.65, 987.65);

Output:

Numeric

5. Date & Time

  • Date: Stores the data in the format YYYY-MM-DD, limited from 1000-01-01 to 9999-12-31.
  • Time: Stores the time in the format hh:mm: ss.
  • DateTime: Stores both the date and time.
  • TimeStamp: The timestamp is used to mark a timestamp whenever a row is entered or updated in the table. This is usually done through keywords DEFAULT CURRENT_TIMESTAMP and ON UPDATE CURRENT_TIMESTAMP.

Example:

Code:

create table DateTimeDataTypes (col_date DATE, col_time TIME, col_datetime DATETIME, col_ts TIMESTAMP DEFAULT CURRENT_TIMESTAMP);
insert into DateTimeDataTypes(col_date, col_time, col_datetime) values (CURDATE(), '08:50:41', NOW());

Output:

Date & Time

6. Miscellaneous-SQL Data Types

Miscellaneous-SQL Data Types

  • CLOB: CLOB stands for Character Large Objects. They can hold character data for up to 2GB. Not supported by MySQL, though. MySQL uses Text to achieve the same.
  • BLOB: BLOB stands for Binary Large Objects. They are used for storing binary objects of size up to 2GB. Usually, images are converted into binary objects and stored in BLOB columns.
  • XML: Used for storing XML data.
  • JSON: Used for storing JSON data. Not supported by MySQL.

Conclusion

The SQL data types help a lot in strategically optimizing the database space. In a digital world, where data is ever-growing at an exponential rate, a developer must wisely choose the type for every bit of data. This helps in the reduction of costs arising due to massive data storage.

Recommended Articles

This is a guide to SQL Data Types. Here we discuss the introduction, types of SQL data like character/string, unicode character/string, numeric, etc in detail. You can also go through our other suggested articles to learn more –

  1. PostgreSQL Data Types
  2. SQL WHERE Clause
  3. MySQL REINDEX
  4. SQL CTE
Popular Course in this category
SQL Training Program (7 Courses, 8+ Projects)
  7 Online Courses |  8 Hands-on Projects |  73+ Hours |  Verifiable Certificate of Completion
4.5
Price

View Course

Related Courses

JDBC Training (6 Courses, 7+ Projects)4.9
PHP Training (5 Courses, 3 Project)4.8
Windows 10 Training (4 Courses, 4+ Projects)4.7
PL SQL Training (4 Courses, 2+ Projects)4.7
Oracle Training (14 Courses, 8+ Projects)4.7
0 Shares
Share
Tweet
Share
Primary Sidebar
Footer
About Us
  • Blog
  • Who is EDUCBA?
  • Sign Up
  • Live Classes
  • Corporate Training
  • Certificate from Top Institutions
  • Contact Us
  • Verifiable Certificate
  • Reviews
  • Terms and Conditions
  • Privacy Policy
  •  
Apps
  • iPhone & iPad
  • Android
Resources
  • Free Courses
  • Java Tutorials
  • Python Tutorials
  • All Tutorials
Certification Courses
  • All Courses
  • Software Development Course - All in One Bundle
  • Become a Python Developer
  • Java Course
  • Become a Selenium Automation Tester
  • Become an IoT Developer
  • ASP.NET Course
  • VB.NET Course
  • PHP Course

ISO 10004:2018 & ISO 9001:2015 Certified

© 2022 - EDUCBA. ALL RIGHTS RESERVED. THE CERTIFICATION NAMES ARE THE TRADEMARKS OF THEIR RESPECTIVE OWNERS.

EDUCBA
Free Software Development Course

C# Programming, Conditional Constructs, Loops, Arrays, OOPS Concept

*Please provide your correct email id. Login details for this Free course will be emailed to you

By signing up, you agree to our Terms of Use and Privacy Policy.

EDUCBA Login

Forgot Password?

By signing up, you agree to our Terms of Use and Privacy Policy.

EDUCBA
Free Software Development Course

Web development, programming languages, Software testing & others

*Please provide your correct email id. Login details for this Free course will be emailed to you

By signing up, you agree to our Terms of Use and Privacy Policy.

EDUCBA

*Please provide your correct email id. Login details for this Free course will be emailed to you

By signing up, you agree to our Terms of Use and Privacy Policy.

Let’s Get Started

By signing up, you agree to our Terms of Use and Privacy Policy.

This website or its third-party tools use cookies, which are necessary to its functioning and required to achieve the purposes illustrated in the cookie policy. By closing this banner, scrolling this page, clicking a link or continuing to browse otherwise, you agree to our Privacy Policy

Loading . . .
Quiz
Question:

Answer:

Quiz Result
Total QuestionsCorrect AnswersWrong AnswersPercentage

Explore 1000+ varieties of Mock tests View more