/

Creating a Table in SQL

Creating a Table in SQL

In a SQL database, a table is a fundamental component of a database system. This article will guide you on how to create a table using the CREATE TABLE command in SQL.

When creating a table, you need to specify the column names and their respective data types. SQL provides various data types, but the most commonly used ones include:

  • CHAR: fixed-length character string
  • TEXT: variable-length character string
  • VARCHAR: variable-length character string with a maximum length specified
  • DATE: date value
  • TIME: time value
  • DATETIME: combination of date and time
  • TIMESTAMP: date and time, typically used for recording modification times

There are also numeric data types available in SQL, such as:

  • TINYINT: 1-byte integer value
  • INT: 4-byte integer value
  • BIGINT: 8-byte integer value
  • SMALLINT: 2-byte integer value
  • DECIMAL: fixed-point decimal value
  • FLOAT: floating-point value

These numeric data types differ in their range and storage requirements. For instance, a TINYINT can store values from 0 to 255, while an INT can store values from -2^31 to +2^31.

To illustrate the creation of a table, let’s consider an example of creating a table called people with two columns: age (an integer) and name (a variable-length string).

1
2
3
4
CREATE TABLE people (
age INT,
name CHAR(20)
);

In the above example, we use the CREATE TABLE command to create the people table with the specified columns. The age column is of type INT, and the name column is of type CHAR with a maximum length of 20 characters.

By following this syntax, you can create tables in SQL databases effectively.

tags: [“SQL”, “table creation”, “data types”]