table_name(
CREATE TABLE IF NOT EXIST
column1 datatype,
column2 datatype,
column3 datatype, .... )
Create Table
New Table
- Syntax:
- The column parameter specify the name of the columns for the new table
- The datatype specifies the type of data the column can hold
- Keep in mind, just running a SQL query doesn’t actually create a table for the data we extract. It just stores it in our local memory.
- To save it, we’ll need to download it as a spreadsheet or save the result into a new table.
- If we now the table doesn’t exist we can omit the IF NOT EXIST part
- Below PersonID will hold an integer and the rest will hold characters with max length of 255 chars.
Persons (
CREATE TABLE
PersonID int, varchar(255),
LastName varchar(255),
FirstName varchar(255),
Address varchar(255) ); City
Here is what we just created:
PersonID | LastName | FirstName | Address | City |
---|---|---|---|---|
Create from Another
- A copy of an existing table can also be created using
CREATE TABLE AS
- The new table gets the same column definitions.
- All columns or specific columns can be selected.
- If you create a new table using an existing table, the new table will be filled with the existing values from the old table.
- Syntax:
CREATE TABLE new_table_name AS
SELECT column1, column2,...
FROM existing_table_name WHERE ....;
- Let’s create a table called NewTable from ExistingTable
CREATE TABLE NewTable AS
SELECT column1, column4, column89 FROM ExistingTable;