Creating and Modifying Tables/Views

In this chapter we will be covering some of the most commonly used queries to create and modify tables in SQL.

Examples below are created via PostgreSQL.

1. Creating Tables

1.1. Basic Format (with no input data)

CREATE TABLE /*IF NOT EXISTS*/ table_name (
	column1 DataType TableConstraint DEFAULT default_value,
	column2 DataType,
	column3 DataType);
-- Example:
CREATE TABLE IF NOT EXISTS employees (
	emp_id serial PRIMARY KEY,
	lastname varchar(100),
	firstname varchar(100),
	department varchar(100) NOT NULL,
	start_date timestamp
);

-- ' IF NOT EXISTS ', ' PRIMARY KEY ', ' NOT NULL ' are optional

For all data types, please check PostgreSQL Documentation

1.2. Using Another Table (with input data)

1.3. Using SELECT Statement (with input data)

i. From a Different Table

ii. Manual

Prior to creating a table using a SELECT statement, first create the new table with desired datatypes and constraints, and then transfer the data into newly created table:

Step 1: Create a new table with desired columns, and the datatypes and constraints for those columns.

Step 2: Copy data from the old table to the new one.

2. Data Modifications

2.1. Inserting Rows (Populating Table)

2.2. Updating Rows

2.3. Deleting Rows

3. Table Modifications (Most Commonly Used)

3.1. Adding columns

3.2. Renaming the table

3.3. Dropping tables

Different databases offer different approaches to modify their tables. Therefore, it is advisable to refer to your database documentation before proceeding with any changes. Some of the most commonly used database systems: Postgres, MySQL, SQLite, and Microsoft SQL Server

4. Column Modifications (Most Commonly Used)

4.1. Renaming columns

4.2. Changing columns' datatype

4.3. Dropping columns

Column Modifications Summary

ALTER TABLE <table_name>

  • ADD COLUMN <column_name> DataType DEFAULT '......';

  • RENAME COLUMN <column_name> TO <new_column_name>;

  • ALTER COLUMN <column_name> TYPE <new_column_type>;

  • DROP COLUMN <column_name>;

For complete list of PostgreSQL column modifications, please check the documentation.

If you need to see the datatype for existing tables in Postgres, please check the Displaying Existing Tables' Columns and Their DataTypes in the Data Types section.

5. Creating Views

A view is a stored query that acts as a virtual table. When you query a view, PostgreSQL dynamically executes the underlying query and presents the results as if they were stored in a table. For more information about views please visit What is a View? section under Database Fundamentals.

5.1. Creating View(s)

5.2. Deleting View(s)

5.3. View Real World Example

Scenario:

Suppose we have two tables: orders and customers. we want to create a view that displays each customer’s name and the total amount they have spent on all their orders.

Tables Setup

Creating the View:

Now, let's create a view called customer_total_spending that shows each customer's name and the total amount they've spent on their orders.

Querying the View:

Now you can query the view just like a table:

Result:

The query on the view will return the following:

Benefits of the View:

  • Simplification: The view simplifies querying by pre-joining the customers and orders tables and performing the aggregation.

  • Security: If you want to give a user access to only the summarized data (e.g., total spending) without exposing the full details of each order, you can grant access to the view but not the underlying tables.

Last updated

Was this helpful?