How to Create Table in SQL

How to Create Table in SQL

The CREATE TABLE statement is used to create a new table in a database. The column specifies the name of the column of the table. The datatype parameter specifies the type of data the column can hold.

CREATE TABLE table_name (
column1 datatype,
column2 datatype,
column3 datatype,

Data Types in MySQL

Creating Table with Constraints

Constraints can be specified when the table is created with the CREATE TABLE statement, or after the table is created with the ALTER TABLE statement. Constraints are used to specify rules for the data in a table.

CREATE TABLE table_name (
column1 datatype constraint,
column2 datatype constraint,
column3 datatype constraint,

Constraints are used to limit the type of data that can go into a table. This ensures the accuracy and reliability of the data in the table. If there is any violation between the constraint and the data action, the action is aborted.

The following constraints are used in SQL:

  1. NOT NULL - Ensures that a column cannot have a NULL value
  2. UNIQUE - Ensures that all values in a column are different
  3. PRIMARY KEY - A combination of a NOT NULL and UNIQUE. Uniquely identifies each row in a table
  4. FOREIGN KEY - Uniquely identifies a row or record in another table
  5. CHECK - Ensures that all values in a column satisfies a specific condition
  6. DEFAULT - Sets a default value for a column when no value is specified
  7. INDEX - Used to create and retrieve data from the database very quickly
Star InactiveStar InactiveStar InactiveStar InactiveStar Inactive