SQL Data Types
In SQL, a data type specifies the type of data that can be stored in a column or variable. The following are some common data types used in SQL:
INTorINTEGER: integer values (e.g., 1, 2, 3, ...)BIGINT: larger integer values (e.g., -9223372036854775808 to 9223372036854775807)SMALLINT: smaller integer values (e.g., -32768 to 32767)DECIMALorNUMERIC: fixed-point decimal numbers (e.g., 123.45)FLOATorREAL: floating-point numbers (e.g., 1.23E-10)DOUBLE PRECISION: double-precision floating-point numbers (e.g., 1.7976931348623157E+308)CHARorCHARACTER: fixed-length character strings (e.g., 'ABC')VARCHARorVARCHAR2: variable-length character strings (e.g., 'ABC', 'ABCD', 'ABCDE', ...)TEXT: large variable-length character strings (e.g., up to 2^31-1 characters)DATE: dates (e.g., '2023-02-28')TIME: times (e.g., '14:30:00')DATETIMEorTIMESTAMP: dates and times (e.g., '2023-02-28 14:30:00')BOOLEANorBOOL: true/false values (e.g.,TRUE,FALSE)
Some databases may also support additional data types beyond these common ones. When creating a table, you specify the data type for each column. For example, the following SQL statement creates a table called employees with four columns, each with a different data type:
CREATE TABLE employees (
id INT,
name VARCHAR(50),
salary DECIMAL(10,2),
hire_date DATE
);
In this example, we are creating a table called employees with four columns: id of type INT, name of type VARCHAR(50), salary of type DECIMAL(10,2), and hire_date of type DATE.
Choosing the appropriate data type for each column is important for data integrity, storage efficiency, and query performance. For example, using a DECIMAL data type for currency values ensures that they are stored with the correct precision, and using an appropriate string data type can improve storage efficiency and query performance.
