0% found this document useful (0 votes)
24 views13 pages

Unit-9 - CREATE A Table From Another Table

Uploaded by

zeelsoni
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
24 views13 pages

Unit-9 - CREATE A Table From Another Table

Uploaded by

zeelsoni
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 13

CREATE a table from another

table
CREATE a table from another table
• You can also 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).
 The basic syntax is:
 CREATE TABLE new_table
AS (SELECT * FROM old_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.
Copying selected columns from
another table
 The basic syntax is:
 CREATE TABLE new_table
AS (SELECT column_1, column2, ... column_n FROM
old_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 companies table.
 Again, if there were records in the companies table,
then the new suppliers table would also contain the
records selected by the SELECT statement.
Copying selected columns from
multiple tables
 The basic syntax is:
 CREATE TABLE new_table
AS (SELECT column_1, column2, ... column_n
FROM old_table_1, old_table_2, ... old_table_n);
 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.
SQLlitestudio
• Syntax
• The syntax for the CREATE TABLE AS statement in SQLite is:

CREATE TABLE new_table AS SELECT


expressions FROM existing_tables [WHERE
conditions];

• Example:

CREATE TABLE active_employees AS SELECT *


FROM employees WHERE hire_date IS NOT NULL;
INSERT Statement
 The INSERT statement allows you to insert a
single record or multiple records into a table.
 The syntax for the INSERT statement is:
 INSERT INTO table
(column-1, column-2, ... column-n)
VALUES
(value-1, value-2, ... value-n);
More complex example

 You can also perform more complicated inserts


using sub-selects.
 For example:
 INSERT INTO suppliers (supplier_id,supplier_name,
SELECT account_no, name FROM customers
WHERE city = 'Newark’);
 By placing a "select" in the insert statement,
you can perform multiples inserts quickly
UPDATE Statement
 The UPDATE statement allows you to update a
single record or multiple records in a table.
 The syntax for the UPDATE statement is:
UPDATE table SET column = expression
WHERE predicates;
UPDATE Statement
 For example:
 UPDATE suppliers
SET name = 'HP'
WHERE name = 'IBM';
 This statement would update all supplier
names in the suppliers table from IBM to HP.
Updating multiple columns example
 UPDATE statement.
 UPDATE suppliers
SET name = 'Apple', product = 'iPhone'
WHERE name = 'Rim';
 When you wish to update multiple columns, you
can do this by separating the column/value pairs
with commas.
 This statement would update the supplier name
to "Apple" and product to "iPhone" where the
name of the supplier is "Rim".
DELETE Statement
 The DELETE statement allows you to delete a
single record or multiple records from a
table.
 The syntax for the DELETE statement is:
 DELETE FROM table WHERE predicates;
 For example
 DELETE FROM suppliers
WHERE supplier_name = 'IBM';
 This would delete all records from the
suppliers table where the supplier_name is
IBM.
SELECT Statement
 The SELECT statement allows you to retrieve
records from one or more tables in your
database.
 The syntax is :
SELECT <attribute list>
FROM <table list>
WHERE <condition>;

You might also like