sql
  1. sql-create-table

SQL Tables CREATE TABLE Page

Syntax

The syntax for creating a table in SQL is as follows:

CREATE TABLE table_name (
   column1 datatype,
   column2 datatype,
   column3 datatype,
   .....
   columnN datatype,
   PRIMARY KEY (one or more columns)
);

Example

Consider the following example to create a table named Students with columns:

  • StudentID (Primary Key)
  • FirstName
  • LastName
  • Age
  • Gender
CREATE TABLE Students (
   StudentID int NOT NULL,
   FirstName varchar(255),
   LastName varchar(255),
   Age int,
   Gender varchar(10),
   PRIMARY KEY (StudentID)
);

Output

On executing the above SQL query, a new table named "Students" will be created with the specified columns.

Explanation

The CREATE TABLE statement is used to create a new table in a database. The table_name parameter specifies the name of the table that you want to create.

The column1, column2, column3,..., columnN parameters specify the names of the columns in the table. The datatype specifies the type of data that can be stored in the columns.

The PRIMARY KEY parameter specifies the column or combination of columns that uniquely identifies each row in the table.

Use

The CREATE TABLE statement is used to create a new table in a database. It is used to define the structure of a table and the data types of the columns that are associated with the table.

Important Points

  • The CREATE TABLE statement can be used to create a new table in an existing database.
  • The PRIMARY KEY constraint specifies that a column or combination of columns uniquely identifies each row in a table.
  • The NOT NULL constraint specifies that a column must contain a value and cannot be null.
  • The UNIQUE constraint specifies that the values in the column must be unique across all rows in the table.

Summary

In summary, the CREATE TABLE statement is used to create a new table in a database. It specifies the name of the table, the columns in the table, and the data types of the columns. It also includes constraints such as PRIMARY KEY and NOT NULL to enforce data integrity.

Published on: