SQL Data Types: How to Define the Kind of Data that a Column Can Store

In SQL, each column (in a table) has a data type. This restricts the type of data that can be stored in that column.

Example

CREATE TABLE Employees (
  id INTEGER PRIMARY KEY,
  name TEXT,
  age INTEGER,
  salary REAL
);

Run Code

In the above example, we created a table named Employees with four columns: id, name, age, and salary. The id and age columns use the INTEGER data type, the name column uses TEXT, and the salary column uses REAL.


SQL Data Types Syntax

The syntax for SQL Data Types is:

CREATE TABLE table_name (
   column1_name datatype1,
   column2_name datatype2,
   column3_name datatype3,
   ...
);

Here,

  • column1_name, column2_name, column3_name, ... are the names of the columns
  • datatype1, datatype2, datatype3, ... are the data types such as INTEGER, TEXT, etc. to be stored in the respective columns

Note: The supported data types can vary across different database systems, which means that not all systems will support the same types of data.


SQL Server Data Types

The data types supported by SQL Server are,

Numeric Data Types

Data TypeDescription
BITcan store single bit (0 or 1) or NULL
TINYINTcan store numbers from 0 to 255
SMALLINTcan store numbers from -32,768 to 32,767
INTcan store numbers between -2,147,483,648 and 2,147,483,647
BIGINTcan store numbers between -9,223,372,036,854,775,808 and 9,223,372,036,854,775,807

String Data Types

Data TypeDescription
CHAR(x)can store characters of fixed length (max 8000 characters)
VARCHAR(x)can store characters up to given length (max 8000 characters)
TEXTcan store characters up to 2 GB size
IMAGEcan store binary string up to 2 GB size

Date and Time Data Types

Data TypeDescription
DATETIMEcan store date from January 1, 1753 to December 31, 9999 with time
DATETIME2can store date from January 1, 0001 to December 31, 9999 with time
DATEcan only store date from January 1, 0001 to December 31, 9999
TIMEcan store only time

MySQL Data Types

The data types supported by MySQL are,

Numeric Data Types

Data TypeDescription
BIT(x)can store x-bit values. x can range from 1 to 64
TINYINTcan store numbers from -128 to 127
SMALLINTcan store numbers from -32768 to 32767
MEDIUMINTcan store numbers from -8,388,608 to 8,388,607
INTcan store numbers from -2,147,483,648 to 2,147,483,647
BIGINTcan store numbers from -9,223,372,036,854,775,808 to 9,223,372,036,854,775,807
DECIMAL(x, y)can store decimal number of total x digits (max up to 65) of which y digits (max up to 30) are allocated after the decimal point

String Data Types

Data TypeDescription
CHAR(x)can store characters of fixed length (max 8000 characters)
VARCHAR(x)can store characters up to given length (max 8000 characters)
BINARY(x)can store binary strings of fixed length
VARBINARY(x)can store binary strings up to given length
TINYTEXTcan store up to 255 characters
TEXT(x)can store characters up to the given limit (max 65,535 bytes)
MEDIUMTEXTcan store characters up to 16,777,215 characters
LONGTEXTcan store characters up to 4,294,967,295 characters
BLOB(x)can store binary large object up to 65,535 bytes
MEDIUMBLOBcan store binary large object up to 16,777,215 bytes
LONGBLOBcan store binary large object up to 4,294,967,295 bytes

Date and Time Data Types

Data TypeDescription
DATEcan store date in format of YYYY-MM-DD ranging from 1000-01-01 to 9999-12-31
DATETIMEcan store date and time in format of YYYY-MM-DD hh:mm:ss
TIME
 
can only store time in format of hh:mm:ss ranging from -838:59:59 to 838:59:59
YEARcan store year in 4 digits format ranging from 1901 to 2155
TIMESTAMPcan store timestamp from the current time zone to UTC

PostgreSQL Data Types

The data types supported by PostgreSQL are,

Numeric Data Types

Data TypeDescription
SMALLINTcan store numbers between -32,768 to 32,767
INTEGERcan store numbers between -2,147,483,648 and 2,147,483,647
BIGINTcan store numbers between -9,223,372,036,854,775,808 and 9,223,372,036,854,775,807
DECIMALcan store numbers up to 131072 digits before the decimal point; up to 16383 digits after the decimal point
SMALLSERIALcan store small auto incrementing number from 1 to 32767
SERIALcan store auto incrementing number from 1 to 2147483647
BIGSERIALcan store big auto incrementing number from 1 to 9223372036854775807

String Data Types

Data TypeDescription
CHAR(x)can store characters of fixed length
VARCHAR(x)can store characters up to given length
TEXTcan store characters without limitation

Date and Time Data Types

Data TypeDescription
TIMESTAMPcan store date and time
DATEcan store only date
TIMEcan store only time

Oracle Data Types

The data types supported by Oracle are,

Numeric Data Types

Data TypeDescription
NUMBERcan store numbers

String Data Types

Data TypeDescription
CHAR(x)can store characters of fixed length up to 2000 bytes or characters
VARCHAR(x)can store characters up to given length (max is 4000 bytes or characters)
LONGcan store characters up to 2 GB

Date and Time Data Types

Data TypeDescription
TIMESTAMPcan store date and time
DATEcan only store date from January 1, 4712 BC to December 31, 9999 AD
TIMEcan only store time

#sql  #database 

SQL Data Types: How to Define the Kind of Data that a Column Can Store
1.05 GEEK