SQL Tutorial
SQL Clauses / Operators
SQL-Injection
SQL Functions
SQL Queries
PL/SQL
MySQL
SQL Server
Misc
In SQL, datatypes specify the type of data that a column can store. Every column in a table is required to have a datatype associated with it. Datatypes can vary between different relational database management systems (RDBMS), but there are many commonalities.
Here's an overview of some commonly used SQL datatypes:
INTEGER: Stores whole numbers. Variants include SMALLINT
, BIGINT
, and sometimes TINYINT
.
DECIMAL(p, s) or NUMERIC(p, s): Fixed precision and scale numbers. p
is the total number of digits, and s
is the number of digits after the decimal.
FLOAT or REAL: Floating-point numbers.
DOUBLE PRECISION: Double-precision floating-point numbers.
DATE: Stores a date (year, month, day).
TIME: Stores a time (hours, minutes, seconds).
TIMESTAMP: Stores date and time together.
CHAR(n): Fixed-length character string, where n
is the string length.
VARCHAR(n) or CHARACTER VARYING(n): Variable-length character string with a maximum length of n
.
TEXT: For storing long text. Not all databases have this type; in some RDBMSs, VARCHAR
can be used without specifying a length.
NCHAR(n) or NATIONAL CHAR(n): Fixed-length Unicode character string.
NVARCHAR(n) or NATIONAL CHARACTER VARYING(n): Variable-length Unicode character string.
BINARY(n): Fixed-length binary data.
VARBINARY(n): Variable-length binary data.
BLOB: For storing long binary data, like images or files.
BOOLEAN: Stores TRUE
or FALSE
values.
ENUM: A list of predefined strings. Used in MySQL.
INT[]
for an integer array.JSON: Stores JSON-formatted data. Supported by databases like PostgreSQL and MySQL.
JSONB: Binary JSON data type. Specifically in PostgreSQL.
Remember, the exact support and specification of these datatypes can vary by RDBMS. It's important to refer to the official documentation of the specific database system you're using when defining tables and selecting datatypes.
Numeric Datatypes in SQL:
CREATE TABLE example_table ( id INTEGER, price DECIMAL(10, 2), quantity INT );
String Datatypes in SQL:
CREATE TABLE example_table ( name VARCHAR(50), description TEXT );
Date and Time Datatypes in SQL:
CREATE TABLE example_table ( order_date DATE, delivery_time TIMESTAMP );
Boolean Datatypes in SQL:
CREATE TABLE example_table ( is_active BOOLEAN );
Binary Datatypes in SQL:
CREATE TABLE example_table ( image_data BLOB );
Special-Purpose Datatypes in SQL:
CREATE TABLE example_table ( user_id UUID, json_data JSON, xml_content XML );
User-Defined Datatypes in SQL:
CREATE TYPE email_address AS VARCHAR(100); CREATE TABLE example_table ( user_email email_address );
Altering Datatypes in SQL:
ALTER TABLE example_table ALTER COLUMN quantity SET DATA TYPE SMALLINT;
Datatype Conversion in SQL:
SELECT CAST(column_name AS INT) FROM example_table;
Handling NULL Values with SQL Datatypes:
CREATE TABLE example_table ( name VARCHAR(50) NULL, description TEXT );
NULL
where appropriate to represent missing or unknown information.