SQL CREATE TABLE Statement

Creating a basic table involves naming the table and defining its columns and each column's data type.
The SQL CREATE TABLE statement is used to create a new table.

The SQL CREATE TABLE Statement

The CREATE TABLE statement is used to create a new table in a database.
Syntax
CREATE TABLE table_name (
    column1 datatype,
    column2 datatype,
    column3 datatype,
   ....
);
The column parameters specify the names of the columns of the table.
The datatype parameter specifies the type of data the column can hold (e.g. varchar, integer, date, etc.).

SQL CREATE TABLE Example

Example 1

The following example creates a table called "users" that contains five columns: id, name, email, country, and password:
  create table users(
        id  int(3) primary key,
        name varchar(20),
        email varchar(20),
        country varchar(20),
        password varchar(20)
  );

Example 2

The following code block is an example, which creates a CUSTOMERS table with an ID as a primary key and NOT NULL are the constraints showing that these fields cannot be NULL while creating records in this table:
CREATE TABLE CUSTOMERS(
   ID   INT              NOT NULL,
   NAME VARCHAR (20)     NOT NULL,
   AGE  INT              NOT NULL,
   ADDRESS  CHAR (25) ,
   SALARY   DECIMAL (18, 2),       
   PRIMARY KEY (ID)
);


Comments