SQL Create Table
The SQL CREATE TABLE Statement
In a database, a new table is created using the CREATE TABLE statement.
Syntax
CREATE TABLE table_name (
column1 datatype,
column2 datatype,
column3 datatype,
....
);
The table’s column names are specified by the column parameters.
The kind of data that a column can contain is indicated by the datatype argument (varchar, integer, date, etc.).
Advice: Visit our comprehensive Data kinds Reference for a summary of all the data kinds that are accessible.
SQL CREATE TABLE Example
A table named “Persons” with the following five columns is created using the following example: PersonID, LastName, FirstName, Address, and City.
Example
----- EXAMPLE MUKAVU -----
An integer will be stored in the PersonID column, which is of type int.
The maximum length for the varchar columns containing the LastName, FirstName, Address, and City is 255 characters. These columns are meant to hold characters.
This is how the “Persons” table will seem after it is empty:
----- CHART MUKAVU -----
Advice: The SQL INSERT INTO statement can now be used to insert data into the vacant “Persons” table.
Create Table Using Another Table
generate TABLE can also be used to generate a clone of an existing table.
The column definitions are the same for the new table. You can choose to pick all columns or just some of them.
The values from the old table will be transferred to the new table if you create it from an existing one.
Syntax
CREATE TABLE new_table_name AS
SELECT column1, column2,...
FROM existing_table_name
WHERE ....;
A replica of the “Customers” table named “TestTable” is created by running the following SQL query:
Example
CREATE TABLE TestTable AS
SELECT customername, contactname
FROM customers;