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
-
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));
-
VARCHAR(n) - A variable-length character string up to
n
characters wheren
can be from 1 to 255. For example,CREATE TABLE Employee(ID VARCHAR(100));
-
INT - This type is used for integers. For example,
CREATE TABLE Customers(Age INT);
-
DECIMAL(p,s) - This is a decimal type used for precision and scale.
p
represents the total number of digits ands
for numbers after the decimal. For example,CREATE TABLE Products(Price DECIMAL(5,2));
-
DATE - This type is used for date format 'YYYY-MM-DD'. For example,
CREATE TABLE Orders(OrderedDate DATE);
-
BOOL - It stores Boolean data types. It can only take
True
orFalse
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].