CREATE, DROP, and INSERT Table
In this lesson, we will take a look at three commands regarding relations/tables.
We'll cover the following
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
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)
);
You can verify that your table has been created successfully by using the DESC command:
Get hands-on with 1400+ tech skills courses.