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 stringTEXT
: variable-length character stringVARCHAR
: variable-length character string with a maximum length specifiedDATE
: date valueTIME
: time valueDATETIME
: combination of date and timeTIMESTAMP
: date and time, typically used for recording modification times
There are also numeric data types available in SQL, such as:
TINYINT
: 1-byte integer valueINT
: 4-byte integer valueBIGINT
: 8-byte integer valueSMALLINT
: 2-byte integer valueDECIMAL
: fixed-point decimal valueFLOAT
: 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).
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.