Sql data types

In SQL, data types define the type of data that a column in a database table can hold. Each column in a table is assigned a specific data type, which determines the kind of data it can store, the operations that can be performed on it, and how much space it occupies in the database. SQL (Structured Query Language) supports a wide range of data types to store different kinds of data in database tables. These data types are typically categorized into several groups, each corresponding to a specific kind of data. Here are the main categories and common data types within them:

1. Numeric Data Types

These data types are used to store numbers.

  • INT or INTEGER: Stores integer values (e.g., 1, 100, -50).
  • SMALLINT: Stores smaller integer values (usually in the range of -32,768 to 32,767).
  • BIGINT: Stores large integer values (e.g., up to 9 quintillion).
  • DECIMAL(p, s) or NUMERIC(p, s): Stores fixed-point numbers with precision p and scale s (e.g., DECIMAL(10, 2) for a number with 10 digits and 2 decimal places).
  • FLOAT: Stores approximate numeric values with floating decimal points (e.g., 3.14).
  • REAL: Similar to FLOAT but with lower precision.
  • DOUBLE: Stores larger floating-point numbers (double-precision).

2. Character/String Data Types

Used for storing text and string data.

  • CHAR(n): Stores a fixed-length string of n characters (e.g., CHAR(10) stores 10 characters).
  • VARCHAR(n): Stores variable-length string up to n characters.
  • TEXT: Stores long variable-length strings (no specific length limit in most databases).
  • TINYTEXT: A short version of TEXT (typically up to 255 characters).
  • LONGTEXT: A very large text field for extremely large strings.

3. Date and Time Data Types

Used to store dates, times, and timestamps.

  • DATE: Stores a date (YYYY-MM-DD).
  • TIME: Stores a time (HH:MM:SS).
  • DATETIME: Stores both date and time (YYYY-MM-DD HH:MM:SS).
  • TIMESTAMP: Stores a timestamp, typically representing the number of seconds since a specific point in time (e.g., UNIX epoch).
  • YEAR: Stores a year in the format YYYY.

4. Binary Data Types

Used for storing binary data, such as images, files, etc.

  • BINARY(n): Stores a fixed-length binary string.
  • VARBINARY(n): Stores a variable-length binary string.
  • BLOB: Stands for Binary Large Object, used for storing large amounts of binary data (e.g., images, videos).
  • TINYBLOB: A small version of BLOB (typically up to 255 bytes).
  • LONGBLOB: A large version of BLOB for very large binary data.

5. Boolean Data Type

Used for storing true/false values.

  • BOOLEAN or BOOL: Stores TRUE or FALSE. Some databases use integers (1 or 0) to represent true/false.

6. Other Data Types

Additional data types used for special purposes.

  • ENUM: Stores a predefined list of values (e.g., ‘small’, ‘medium’, ‘large’).
  • SET: Similar to ENUM but can store multiple values from a predefined list.
  • JSON: Stores JSON (JavaScript Object Notation) data, useful for handling structured data in flexible formats.
  • UUID: Stores a Universally Unique Identifier (a 128-bit value used to uniquely identify records).

7. Spatial Data Types

For geographic data storage.

  • POINT: Stores a single point in a coordinate system.
  • LINESTRING: Stores a series of points, representing a line.
  • POLYGON: Stores a polygon, which is a closed shape with multiple points.

Each database system (e.g., MySQL, PostgreSQL, SQL Server, Oracle) may have slight variations in how it handles these types, but the above types are generally common across most systems.

Leave a Reply

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