Monday, May 9, 2016

VIEW


This SQL tutorial explains how to create, update, and drop SQL VIEWS with syntax and examples.

DESCRIPTION

The SQL VIEW is, in essence, a virtual table that does not physically exist. Rather, it is created by a SQL statement that joins one or more tables.

CREATE SQL VIEW

Syntax

The syntax for the SQL CREATE VIEW Statement is:
CREATE VIEW view_name AS
  SELECT columns
  FROM tables
  [WHERE conditions];
view_name
The name of the SQL VIEW that you wish to create.
WHERE conditions
Optional. The conditions that must be met for the records to be included in the VIEW.

Example

Here is an example of how to use the SQL CREATE VIEW:
CREATE VIEW sup_orders AS
  SELECT suppliers.supplier_id, orders.quantity, orders.price
  FROM suppliers
  INNER JOIN orders
  ON suppliers.supplier_id = orders.supplier_id
  WHERE suppliers.supplier_name = 'IBM';
This SQL CREATE VIEW example would create a virtual table based on the result set of the select statement. You can now query the SQL VIEW as follows:
SELECT *
FROM sup_orders;

UPDATE SQL VIEW

You can modify the definition of a SQL VIEW without dropping it by using the SQL CREATE OR REPLACE VIEW Statement.

Syntax

The syntax for the SQL CREATE OR REPLACE VIEW Statement is:
CREATE OR REPLACE VIEW view_name AS
  SELECT columns
  FROM table
  [WHERE conditions];

Example

Here is an example of how you would use the SQL CREATE OR REPLACE VIEW Statement:
CREATE or REPLACE VIEW sup_orders AS
  SELECT suppliers.supplier_id, orders.quantity, orders.price
  FROM suppliers
  INNER JOIN orders
  ON suppliers.supplier_id = orders.supplier_id
  WHERE suppliers.supplier_name = 'Microsoft';
This SQL CREATE OR REPLACE VIEW example would update the definition of the SQL VIEW called sup_orders without dropping it. If the SQL VIEW did not yet exist, the SQL VIEW would merely be created for the first time.

DROP SQL VIEW

Once a SQL VIEW has been created, you can drop it with the SQL DROP VIEW Statement.

Syntax

The syntax for the SQL DROP VIEW Statement is:
DROP VIEW view_name;
view_name
The name of the view that you wish to drop.

Example

Here is an example of how to use the SQL DROP VIEW Statement:
DROP VIEW sup_orders;
This SQL DROP VIEW example would drop/delete the SQL VIEW called sup_orders.

LOCAL TEMPORARY TABLES

This SQL tutorial explains how to create SQL LOCAL TEMPORARY tables with syntax and examples.

DESCRIPTION

SQL LOCAL TEMPORARY TABLES are distinct within modules and embedded SQL programs within SQL sessions.

SYNTAX

The syntax for SQL DECLARE LOCAL TEMPORARY TABLE is:
DECLARE LOCAL TEMPORARY TABLE table_name
( column1 datatype [ NULL | NOT NULL ],
  column2 datatype [ NULL | NOT NULL ],
  ...
);

Parameters or Arguments

table_name
The name of the local temporary table that you wish to create.
column1, column2
The columns that you wish to create in the local temporary table. Each column must have a datatype. The column should either be defined as NULL or NOT NULL and if this value is left blank, the database assumes NULL as the default.

EXAMPLE

Let's look at a SQL DECLARE LOCAL TEMPORARY TABLE example:
DECLARE LOCAL TEMPORARY TABLE suppliers_temp
( supplier_id int NOT NULL,
  supplier_name char(50) NOT NULL,
  contact_name char(50)
);
This example would create a LOCAL TEMPORARY TABLE called suppliers_temp.

GLOBAL TEMPORARY TABLES


This SQL tutorial explains how to create SQL GLOBAL TEMORARY tables with syntax and examples.

DESCRIPTION

SQL GLOBAL TEMPORARY TABLES are tables that are created distinct within SQL sessions.

SYNTAX

The syntax for SQL CREATE GLOBAL TEMPORARY TABLE is:
CREATE GLOBAL TEMPORARY TABLE table_name
( column1 datatype [ NULL | NOT NULL ],
  column2 datatype [ NULL | NOT NULL ],
  ...
);

Parameters or Arguments

table_name
The name of the global temporary table that you wish to create.
column1, column2
The columns that you wish to create in the global temporary table. Each column must have a datatype. The column should either be defined as NULL or NOT NULL and if this value is left blank, the database assumes NULL as the default.

EXAMPLE

Let's look at a SQL CREATE GLOBAL TEMPORARY TABLE example:
CREATE GLOBAL TEMPORARY TABLE suppliers_temp
( supplier_id numeric(10) NOT NULL,
  supplier_name char(50) NOT NULL,
  contact_name char(50)
);

DROP TABLE STATEMENT


This SQL tutorial explains how to use the SQL DROP TABLE statement with syntax and examples.

DESCRIPTION

The SQL DROP TABLE statement allows you to remove or delete a table from the SQL database.

SYNTAX

The syntax for the SQL DROP TABLE statement is:
DROP TABLE table_name;

Parameters or Arguments

table_name
The name of the table to remove from the database.

EXAMPLE

You may have found that you've created a table that you no longer require. You can use the DROP TABLE statement to remove the table from your database.
Let's look at an example that shows how to drop a table using the DROP TABLE statement.
For example:
DROP TABLE suppliers;
This DROP TABLE statement example would drop the table called suppliers. This would both remove the records associated with the suppliers table as well as its table definition.
Once you have dropped the table, you can recreate the suppliers table without getting an error that the table already exists.
Let's look at one more example where we prefix the table name with the database name.
For example:
DROP TABLE totn.contacts;
In this example, e have dropped a table called contacts that is in the totn database.

ALTER TABLE STATEMENT


This SQL tutorial explains how to use the SQL ALTER TABLE statement to add a column, modify a column, drop a column, rename a column or rename a table (with lots of clear, concise examples). We've also added some practice exercises that you can try for yourself.

DESCRIPTION

The SQL ALTER TABLE statement is used to add, modify, or drop/delete columns in a table. The SQL ALTER TABLE statement is also used to rename a table.

ADD COLUMN IN TABLE

Syntax

To add a column in a table, the SQL ALTER TABLE syntax is:
ALTER TABLE table_name
  ADD column_name column-definition;

Example

Let's look at a SQL ALTER TABLE example that adds a column.
For example:
ALTER TABLE supplier
  ADD supplier_name char(50);
This SQL ALTER TABLE example will add a column called supplier_name to the supplier table.

ADD MULTIPLE COLUMNS IN TABLE

Syntax

To add multiple columns to an existing table, the SQL ALTER TABLE syntax is:
ALTER TABLE table_name
  ADD (column_1 column-definition,
       column_2 column-definition,
       ...
       column_n column_definition);

Example

Let's look at SQL ALTER TABLE example that adds more than one column.
For example:
ALTER TABLE supplier
  ADD (supplier_name char(50),
       city char(45));
This SQL ALTER TABLE example will add two columns, supplier_name as a char(50) field and city as a char(45) field to thesupplier table.

MODIFY COLUMN IN TABLE

Syntax

To modify a column in an existing table, the SQL ALTER TABLE syntax is:
For Oracle, MySQL, MariaDB:
ALTER TABLE table_name
  MODIFY column_name column_type;
For SQL Server:
ALTER TABLE table_name
  ALTER COLUMN column_name column_type;
For PostgreSQL:
ALTER TABLE table_name
  ALTER COLUMN column_name TYPE column_definition;

Example

Let's look at an example of how to modify a column called supplier_name using the ALTER TABLE statement. Note that most databases have a slightly different syntax.
For Oracle:
ALTER TABLE supplier
  MODIFY supplier_name char(100) NOT NULL;
For MySQL and MariaDB:
ALTER TABLE supplier
  MODIFY supplier_name VARCHAR(100) NOT NULL;
For SQL Server:
ALTER TABLE supplier
  ALTER COLUMN supplier_name VARCHAR(100) NOT NULL;
For PostgreSQL:
ALTER TABLE supplier
  ALTER COLUMN supplier_name TYPE CHAR(100),
  ALTER COLUMN supplier_name SET NOT NULL;

MODIFY MULTIPLE COLUMNS IN TABLE

Syntax

To modify multiple columns in an existing table, the SQL ALTER TABLE syntax is:
For Oracle:
ALTER TABLE table_name
  MODIFY (column_1 column_type,
          column_2 column_type,
          ...
          column_n column_type);
For MySQL and MariaDB:
ALTER TABLE table_name
  MODIFY column_1 column_definition
    [ FIRST | AFTER column_name ],
  MODIFY column_2 column_definition
    [ FIRST | AFTER column_name ],
  ...
;
For PostgreSQL:
ALTER TABLE table_name
  ALTER COLUMN column_name TYPE column_definition,
  ALTER COLUMN column_name TYPE column_definition,
  ...
;

Example

Let's look at an example that uses the ALTER TABLE statement to modify more than one column. In this example, we will modify two columns called supplier_name and city.
For Oracle:
ALTER TABLE supplier
  MODIFY (supplier_name char(100) NOT NULL,
          city char(75));
For MySQL and MariaDB:
ALTER TABLE supplier
  MODIFY supplier_name VARCHAR(100) NOT NULL,
  MODIFY city VARCHAR(75);
For PostgreSQL:
ALTER TABLE supplier
  ALTER COLUMN supplier_name TYPE CHAR(100),
  ALTER COLUMN supplier_name SET NOT NULL,
  ALTER COLUMN city TYPE CHAR(75);

DROP COLUMN IN TABLE

Syntax

To drop a column in an existing table, the SQL ALTER TABLE syntax is:
ALTER TABLE table_name
  DROP COLUMN column_name;

Example

Let's look at an example that drops (ie: deletes) a column from a table.
For example:
ALTER TABLE supplier
  DROP COLUMN supplier_name;
This SQL ALTER TABLE example will drop the column called supplier_name from the table called supplier.

RENAME COLUMN IN TABLE

Syntax

To rename a column in an existing table, the SQL ALTER TABLE syntax is:
For Oracle and PostgreSQL:
ALTER TABLE table_name
  RENAME COLUMN old_name TO new_name;
For SQL Server (using the stored procedure called sp_rename):
sp_rename 'table_name.old_column', 'new_name', 'COLUMN';
For MySQL and MariaDB:
ALTER TABLE table_name
  CHANGE COLUMN old_name TO new_name;

Example

Let's look at an example that renames a column in the supplier table from supplier_name to sname.
For Oracle (9i Rel2 and up) and PostgreSQL:
ALTER TABLE supplier
  RENAME COLUMN supplier_name TO sname;
For SQL Server (using the stored procedure called sp_rename):
sp_rename 'supplier.supplier_name', 'sname', 'COLUMN';
For MySQL and MariaDB:
ALTER TABLE supplier
  CHANGE COLUMN supplier_name sname VARCHAR(100);
In MySQL and MariaDB, you must specify the data type of the column when you rename it.

RENAME TABLE

Syntax

To rename a table, the SQL ALTER TABLE syntax is:
For Oracle, MySQL, MariaDB, PostgreSQL and SQLite:
ALTER TABLE table_name
  RENAME TO new_table_name;
For SQL Server (using the stored procedure called sp_rename):
sp_rename 'table_name', 'new_table_name';

Example

Let's look at an example that renames a table called supplier to the new name vendor.
For Oracle, MySQL, MariaDB, PostgreSQL and SQLite:
ALTER TABLE supplier
  RENAME TO vendor;
For SQL Server (using the stored procedure called sp_rename):
sp_rename 'supplier', 'vendor';

CREATE TABLE AS STATEMENT


This SQL tutorial explains how to use the SQL CREATE TABLE AS statement with syntax and examples.

DESCRIPTION

You can also use the SQL CREATE TABLE AS statement to create a table from an existing table by copying the existing table's columns.
It is important to note that when creating a table in this way, the new table will be populated with the records from the existing table (based on the SELECT Statement).

CREATE TABLE - BY COPYING ALL COLUMNS FROM ANOTHER TABLE

Syntax

The syntax for the SQL CREATE TABLE AS statement copying all of the columns is:
CREATE TABLE new_table
  AS (SELECT * FROM old_table);

Example

Let's look at an example that shows how to create a table by copying all columns from another table.
For Example:
CREATE TABLE suppliers
AS (SELECT *
    FROM companies
    WHERE id > 1000);
This would create a new table called suppliers that included all columns from the companies table.
If there were records in the companies table, then the new suppliers table would also contain the records selected by the SELECT statement.

CREATE TABLE - BY COPYING SELECTED COLUMNS FROM ANOTHER TABLE

Syntax

The syntax for the CREATE TABLE AS statement copying the selected columns is:
CREATE TABLE new_table
  AS (SELECT column_1, column2, ... column_n
      FROM old_table);

Example

Let's look at an example that shows how to create a table by copying selected columns from another table.
For Example:
CREATE TABLE suppliers
  AS (SELECT id, address, city, state, zip
      FROM companies
      WHERE id > 1000);
This would create a new table called suppliers, but the new table would only include the specified columns from the companiestable.
Again, if there were records in the companies table, then the new suppliers table would also contain the records selected by the SELECT statement.

CREATE TABLE - BY COPYING SELECTED COLUMNS FROM MULTIPLE TABLES

Syntax

The syntax for the CREATE TABLE AS statement copying columns from multiple tables is:
CREATE TABLE new_table
  AS (SELECT column_1, column2, ... column_n
      FROM old_table_1, old_table_2, ... old_table_n);

Example

Let's look at an example that shows how to create a table by copying selected columns from multiple tables.
For Example:
CREATE TABLE suppliers
  AS (SELECT companies.id, companies.address, categories.cat_type
      FROM companies, categories
      WHERE companies.id = categories.id
      AND companies.id > 1000);
This would create a new table called suppliers based on columns from both the companies and categories tables.

FREQUENTLY ASKED QUESTIONS

Question: How can I create a SQL table from another table without copying any values from the old table?
Answer: To do this, the SQL CREATE TABLE syntax is:
CREATE TABLE new_table
  AS (SELECT *
      FROM old_table WHERE 1=2);
For example:
CREATE TABLE suppliers
  AS (SELECT *
      FROM companies WHERE 1=2);
This would create a new table called suppliers that included all columns from the companies table, but no data from thecompanies table.

CREATE TABLE STATEMENT


This SQL tutorial explains how to use the SQL CREATE TABLE statement with syntax, examples, and practice exercises.

DESCRIPTION

The SQL CREATE TABLE statement allows you to create and define a table.

SYNTAX

The syntax for the SQL CREATE TABLE statement is:
CREATE TABLE table_name
( 
  column1 datatype [ NULL | NOT NULL ],
  column2 datatype [ NULL | NOT NULL ],
  ...
);

Parameters or Arguments

table_name
The name of the table that you wish to create.
column1, column2
The columns that you wish to create in the table. Each column must have a datatype. The column should either be defined as NULL or NOT NULL and if this value is left blank, the database assumes NULL as the default.

EXAMPLE

Let's look at a SQL CREATE TABLE example.
CREATE TABLE suppliers
( supplier_id int NOT NULL,
  supplier_name char(50) NOT NULL,
  contact_name char(50)
);
This SQL CREATE TABLE example creates a table called suppliers which has 3 columns.
  • The first column is called supplier_id which is created as a number datatype (maximum 10 digits in length) and can not contain null values.
  • The second column is called supplier_name which is a char datatype (50 maximum characters in length) and also can not contain null values.
  • The third column is called contact_name which is a char datatype but can contain null values.
Now the only problem with this SQL CREATE TABLE statement is that you have not defined a primary key for the table. We could modify this SQL CREATE TABLE statement and define the supplier_id as the primary key as follows:
CREATE TABLE suppliers
( supplier_id int NOT NULL,
  supplier_name char(50) NOT NULL,
  contact_name char(50),
  CONSTRAINT suppliers_pk PRIMARY KEY (supplier_id)
);

PRACTICE EXERCISE #1:

Create a SQL table called customers that stores customer ID, name, and address information.

Solution for Practice Exercise #1:

The SQL CREATE TABLE statement for the customers table is:
CREATE TABLE customers
( customer_id int NOT NULL,
  customer_name char(50) NOT NULL,
  address char(50),
  city char(50),
  state char(25),
  zip_code char(10)
);

PRACTICE EXERCISE #2:

Create a SQL table called customers that stores customer ID, name, and address information.
But this time, the customer ID should be the primary key for the table.

Solution for Practice Exercise #2:

The SQL CREATE TABLE statement for the customers table is:
CREATE TABLE customers
( customer_id int NOT NULL,
  customer_name char(50) NOT NULL,
  address char(50),
  city char(50),
  state char(25),
  zip_code char(10),
  CONSTRAINT customers_pk PRIMARY KEY (customer_id)
);

PRACTICE EXERCISE #3:

Based on the departments table below, create a SQL table called employees that stores employee number, employee name, department, and salary information. The primary key for the employees table should be the employee number. Create a foreign key on the employees table that references the departments table based on the department_id field.
CREATE TABLE departments
( department_id int NOT NULL,
  department_name char(50) NOT NULL,
  CONSTRAINT departments_pk PRIMARY KEY (department_id)
);

Solution for Practice Exercise #3:

The SQL CREATE TABLE statement for the employees table is:
CREATE TABLE employees
( employee_number int NOT NULL,
  employee_name char(50) NOT NULL,
  department_id int,
  salary int,
  CONSTRAINT employees_pk PRIMARY KEY (employee_number),
  CONSTRAINT fk_departments
    FOREIGN KEY (department_id)
    REFERENCES departments(department_id)
);