SQL SQL Tutorial SQL Database



SQL Data Types

Structured Query Language (SQL) is a programming language used to manage and manipulate relational databases. SQL data types are used to define the type of data that can be stored in a database table column. The data type of a column determines the kind of data that can be stored in that column. SQL data types are classified into several categories, including numeric, character, date/time, and Boolean data types.

Numeric Data Types

Numeric data types are used to store numeric values. The most common numeric data types in SQL are:

  • INTEGER - used to store whole numbers
  • FLOAT - used to store floating-point numbers
  • DECIMAL - used to store decimal numbers

Here are some examples of how to define numeric data types in SQL:

<pre>
CREATE TABLE employees (
  id INTEGER,
  salary DECIMAL(10,2),
  age FLOAT
);
</pre>

Character Data Types

Character data types are used to store character strings. The most common character data types in SQL are:

  • CHAR - used to store fixed-length character strings
  • VARCHAR - used to store variable-length character strings
  • TEXT - used to store large character strings

Here are some examples of how to define character data types in SQL:

<pre>
CREATE TABLE customers (
  id INTEGER,
  name VARCHAR(50),
  address TEXT
);
</pre>

Date/Time Data Types

Date/time data types are used to store date and time values. The most common date/time data types in SQL are:

  • DATE - used to store date values
  • TIME - used to store time values
  • TIMESTAMP - used to store date and time values

Here are some examples of how to define date/time data types in SQL:

<pre>
CREATE TABLE orders (
  id INTEGER,
  order_date DATE,
  order_time TIME,
  order_timestamp TIMESTAMP
);
</pre>

Boolean Data Types

Boolean data types are used to store true/false values. The most common Boolean data type in SQL is:

  • BOOLEAN - used to store true/false values

Here is an example of how to define a Boolean data type in SQL:

<pre>
CREATE TABLE products (
  id INTEGER,
  in_stock BOOLEAN
);
</pre>

Conclusion

SQL data types are an important aspect of managing and manipulating relational databases. By understanding the different types of data that can be stored in a database table column, you can ensure that your database is structured correctly and that your data is stored in the most efficient way possible.

References

Activity