Creating Tables
Now that we know how to read data from tables, we will look at how to create new tables. There are two ways to do this: by creating blank tables or by using SELECT
queries.
Creating Blank Tables
To create a new blank table, we use the CREATE TABLE
statement. This statement takes the following structure:
CREATE TABLE {table_name} ( {column_name_1} {data_type_1} {column_constraint_1}, {column_name_2} {data_type_2} {column_constraint_2}, {column_name_3} {data_type_3} {column_constraint_3}, … {column_name_last} {data_type_last} {column_constraint_last}, );
Here, {table_name}
is the name of the table, {column_name}
is the name of the column, {data_type}
is the data type of the column, and {column_constraint}
is one or more optional keywords giving special properties to the column. Before we discuss how to use the CREATE TABLE
query, we will first discuss column constraints.
Column Constraints
Column constraints are keywords that give special properties to a column. Some major column constraints are as follows:
NOT NULL
: This constraint guarantees that no value in a column can beNULL
.UNIQUE
: This constraint guarantees that every single row for a column has a unique value and that no value is repeated.PRIMARY KEY
: This is a special constraint that is unique for each row and helps you to find the row quicker. Only one column in a table can be a primary key.
Suppose we want to create a table called state_populations
with columns for the initials and populations of states. The query would look as follows:
CREATE TABLE state_populations ( Â Â state VARCHAR(2) PRIMARY KEY, Â Â population NUMERIC );
This query produces the following results:
Query returned successfully in 122 msec.
Note
Sometimes, you may run a CREATE TABLE
query and get the error relation {table_name} already exists
. This simply means that a table with the same name already exists. You will either have to delete the table with the same name or change the name of your table.
We will now discuss the second way to create a table, which is by using a SQL query. But, first, let's perform an exercise to create a table in SQL.
Exercise 1.07: Creating a Table in SQL
In this exercise, we will create a table using the CREATE TABLE
statement. The marketing team at ZoomZoom would like to create a table called countries
to analyze the data of different countries. It should have four columns: an integer key column, a unique name column, a founding year column, and a capital column.
Perform the following steps to complete the exercise:
- Open your favorite SQL client and connect to the
sqlda
database. - Execute the following query to drop the
countries
table if it already exists in the database:DROP TABLE IF EXISTS countries;
- Run the following query to create the
countries
table:CREATE TABLE countries ( Â Â key INT PRIMARY KEY, Â Â name text UNIQUE, Â Â founding_year INT, Â Â capital text );
You should get a blank table as follows:
Note
To access the source code for this specific section, please refer to https://packt.live/3cWFoSE.
In this exercise, we learned how to create a table using different column constraints and the CREATE TABLE
statement. In the next section, we will create tables using the SELECT
query.
Creating Tables with SELECT
We know how to create a table. However, say you wanted to create a table using data from an existing table. This can be done by using a modification of the CREATE TABLE
statement:
CREATE TABLE {table_name} AS ( {select_query} );
Here, {select_query}
is any SELECT
query that can be run in your database. For instance, say you wanted to create a table based on the products
table that only had products from the year 2014. Let's call this table products_2014
. You could then write the following query:
CREATE TABLE products_2014 AS ( SELECT Â Â * FROM Â Â products WHERE Â Â year=2014 );
This can be done with any query, and the table will inherit all the properties of the output query.