Skip to main content

Column

In SQL, columns are used to categorize the data in a table. A column serves as a structure that stores a specific type of data (ints, str, bool, etc.) in a table. Each column in a table is designed with a type, which configures the data that it can hold. Using the right column types and size can help to maintain data integrity and optimize performance.

Common SQL Column Types

  1. CHAR(n) - It is a fixed-length character string that holds n characters. The size can be 1 to 255. For example,

    CREATE TABLE Employee(ID CHAR(25));
  2. VARCHAR(n) - A variable-length character string up to n characters where n can be from 1 to 255. For example,

    CREATE TABLE Employee(ID VARCHAR(100));
  3. INT - This type is used for integers. For example,

    CREATE TABLE Customers(Age INT);
  4. DECIMAL(p,s) - This is a decimal type used for precision and scale. p represents the total number of digits and s for numbers after the decimal. For example,

    CREATE TABLE Products(Price DECIMAL(5,2));
  5. DATE - This type is used for date format 'YYYY-MM-DD'. For example,

    CREATE TABLE Orders(OrderedDate DATE);
  6. BOOL - It stores Boolean data types. It can only take True or False values. For example,

    CREATE TABLE Users(IsActive BOOL);

In SQL, the column type helps in interpreting what kind of data to store in which column, whether it's number, text, date, or logical data. Remember, a table contains multiple columns and each column should have its unique name.

When creating a table, you should specify the column names, types, and maximum length of the type [if required].