Search⌘ K

CREATE, DROP, and INSERT Table

Explore how to create tables with defined columns data types and primary keys using the CREATE TABLE statement. Understand how to safely remove tables with DROP TABLE and add new data rows to existing tables using the INSERT INTO command. This lesson prepares you to manage database structures and data effectively.

CREATE TABLE

Creating a basic table involves naming the table and defining its columns and the data type for each column.

The SQL CREATE TABLE statement is used to create a new table.

Syntax

The basic syntax of the CREATE TABLE statement is as follows:

CREATE TABLE table_name(

   column1 datatype,

   column2 datatype,

   column3 datatype,

   .....

   columnN datatype,

   PRIMARY KEY(one or more columns)

);

CREATE TABLE is the keyword telling the database system what you want to do. The unique name or identifier for the table follows the CREATE TABLE statement.

Then, in brackets, comes the list defining each column in the table and what data type it is.

Example

The following code block is an example which creates a CUSTOMERS table with ID as a primary key and NOT NULL is the constraint showing that these fields cannot be NULL ...