Skip links

How to Create a Table in SQL

How to Create a Table in SQL

To create a table in SQL, you use the CREATE TABLE statement followed by the table name and the list of columns and their data types.

Here is the syntax:

scss
CREATE TABLE table_name (
   column1 datatype(size),
   column2 datatype(size),
   column3 datatype(size),
   ...
);

For example, if you want to create a table named students with columns id, name, age, and gender, you can use the following SQL statement:

sql
CREATE TABLE students (
   id INT PRIMARY KEY,
   name VARCHAR(50),
   age INT,
   gender CHAR(1)
);

Here, the id column is defined as an INT data type with a PRIMARY KEY constraint, which means it will be the unique identifier for each record in the table.

The name column is defined as a VARCHAR with a size of 50 characters, age as INT, and gender as CHAR with a size of 1 character.

It’s important to carefully choose the data types for each column, as this will affect how data is stored and retrieved in the table. You can use different data types such as INT, VARCHAR, CHAR, DATE, DECIMAL, etc. based on the type of data you need to store.

Once you have executed the CREATE TABLE statement, a new table with the specified columns and data types will be created in the database. You can then use INSERT statements to add data to the table.