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.
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 optional1.2. Using Another Table (with input data)
1.3. Using SELECT Statement (with input data)
i. From a Different Table
ii. Manual
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
4. Column Modifications (Most Commonly Used)
4.1. Renaming columns
4.2. Changing columns' datatype
4.3. Dropping columns
5. Creating Views
5.1. Creating View(s)
5.2. Deleting View(s)
5.3. View Real World Example
Scenario:
Benefits of the View:
Last updated