You are on page 1of 41

SQL Creating

New Table
Creating New Table From
Another Table

2
Creating New Table From
Another Table

• Problem:

• You would like to create a new table


with data copied from another table.

3
Creating New Table From
Another Table

• Example 1:

• Create a new table named


SportingGoods that contain all the
columns and rows in the Part table in
which item Class is SG.
4
The CREATE TABLE USING
SELECT INTO Structure

5
The CREATE TABLE USING
SELECT INTO Structure

Results:

6
Creating New Table From
Another Table

• Example 2:

• Create a new table named


SmallCustomer containing the following
columns from the Customer table:

• CustomerNum, CustomerName,
Balance, CreditLimit and RepNum.
7
Creating New Table From
Another Table

• The columns in the SmallCustomer table


should have the same characteristics as the
corresponding columns in the Customer table.

• After creating a new table, insert into the


SmallCustomer table the CustomerNum,
CustomerName, Balance, CreditLimit and
RepNum for customers with Credit Limits of
$7,500.
8
The CREATE TABLE USING
SELECT INTO Structure

9
The CREATE TABLE USING
SELECT INTO Structure

Results:

10
The SQL UPDATE Command

11
The SQL UPDATE Command

• The SQL UPDATE command is used to


set or change data values in a table.

12
The SQL UPDATE Command

• Syntax:
UPDATE TableName then type
SET (column name and the new
value) then the
WHERE clause (column name and the
value that you want to change)
The SQL UPDATE Command

• Change the name of Customer 842 in


the SmallCustomer table to All Season
Sport.

14
The SQL UPDATE Command

Results:

15
The SQL INSERT Command

16
The SQL INSERT Command

• The SQL INSERT INTO command is


used to add the rows to the tables in the
database.

• You can use the INSERT INTO


command to add additional rows to a
table.

17
The SQL INSERT Command
• Add Customer Number 895 to the
SmallCustomer table. The name is Peter and
Margaret’s, the balance is 0, the credit limit is
$8,000, and the rep number is 20.

18
The SQL INSERT Command

Results:

19
The SQL DELETE Command

20
The SQL DELETE Command

• The SQL DELETE command is used


to delete existing row from a table.

21
The SQL DELETE Command

• In a SmallCustomer table, delete customer


895.

22
The SQL DELETE Command

Results:

23
The SQL ALTER Command

24
The SQL ALTER Command

• We use the ALTER TABLE command


when we add, modify (change), and
delete columns in a table’s definition.

• This command belongs to the SQL’s


DDL (Data Definition Language) group
of commands, because it is used in
changing the definition of a table.
25
Changing a Table’s Definition

• Use the ALTER TABLE command to


change a table structure.

• To add a new column in a table, use the


ADD clause of the ALTER TABLE
command.

26
Changing a Table’s Definition

• You can change the characteristics of an


existing column using the MODIFY clause
of the ALTER TABLE command.

27
Adding a Column in a Table

• Here is the general syntax in adding a


column in a table:

• ALTER TABLE TableName ADD


ColumnName datatype

28
Adding a Column in a Table

Example:
• Premiere Products decides to maintain a
Customer Type for each customer in the
database. These type are R, for Regular
customers, D for Distributors, and S for
Special customers.

• Add this information in a new column named


CustomerType in the SmallCustomer table.
29
Adding a Column in a Table

30
Updating a Column in a Table

• Next is to update the value of


customer type from Null to R.

31
Updating a Column in a Table

Results:

32
Updating a Column in a Table

• Two customers in the


SmallCustomer table have a type
other than R.
• Change the types of customers 842
and 148 to S and D, respectively.

33
Updating a Column in a Table

34
Updating a Column in a Table

Results:

35
Modifying a Column in a Table

Example 1:

• The length of a CustomerName


column in the SmallCustomer table is
too short. Increase its length to 100
characters.

36
Modifying a Column in a Table

37
Modifying a Column in a Table

Example 2:

• Change the data type of the Balance


and CreditLimit columns from
SMALLMONEY to MONEY in the
SmallCustomer table.

38
Modifying a Column in a Table

39
Results

40
Thank you for listening!

That in all things,


God may be glorified!

Prepared By: Engr. Lizel Rose Natividad

You might also like