0% found this document useful (0 votes)
40 views42 pages

CT042-3-1-IDB-Week 10

This document provides an overview of SQL topics including aggregate functions, altering data types, grouping data, and joining databases. It covers SQL commands like ALTER, GROUP BY, ORDER BY, and JOIN. Aggregate functions, sorting with ORDER BY, joining multiple tables, and different types of joins like inner, left, right, and full joins are discussed. The document is made up of 42 slides that provide explanations, examples, and review questions about advanced SQL concepts.

Uploaded by

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

CT042-3-1-IDB-Week 10

This document provides an overview of SQL topics including aggregate functions, altering data types, grouping data, and joining databases. It covers SQL commands like ALTER, GROUP BY, ORDER BY, and JOIN. Aggregate functions, sorting with ORDER BY, joining multiple tables, and different types of joins like inner, left, right, and full joins are discussed. The document is made up of 42 slides that provide explanations, examples, and review questions about advanced SQL concepts.

Uploaded by

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

Introduction to Databases

CT042-3-1-IDB (version1)

Week 10: SQL (Part 2)


Topic & Structure of The Lesson

• Aggregate functions
• Altering data type
• Grouping data
• Joining database

CT042-3-1-IDB SQL Slide 2 of 42


Learning Outcomes

• At the end of this topic, You should be able


to
– Write SQL to manipulate database and joining
database

CT042-3-1-IDB SQL Slide 3 of 42


Key Terms You Must Be Able To
Use
• If you have mastered this topic, you should be able to use the following
terms correctly in your assignments and exams:
– Alter
– Modify
– Drop
– Group by
– order by
– JOIN

CT042-3-1-IDB SQL Slide 4 of 42


Advanced Data Definition
Commands
• All changes in table structure are made by
using ALTER command
– Followed by keyword that produces specific
change
– Following three options are available:
• ADD
• MODIFY
• DROP

CT042-3-1-IDB SQL Slide 5 of 42


Changing a Column’s Data Type

• ALTER can be used to change data type


• Some RDBMSs (such as Oracle) do not
permit changes to data types unless
column to be changed is empty

CT042-3-1-IDB SQL Slide 6 of 42


Changing a Column’s Data
Characteristics
• Use ALTER to change data characteristics
• If column to be changed already contains
data, changes in column’s characteristics
are permitted if those changes do not alter
the data type

CT042-3-1-IDB SQL Slide 7 of 42


Adding a Column

• Use ALTER to add column


– Do not include the NOT NULL clause for new
column

CT042-3-1-IDB SQL Slide 8 of 42


Dropping a Column

• Use ALTER to drop column


– Some RDBMSs impose restrictions on the
deletion of an attribute

CT042-3-1-IDB SQL Slide 9 of 42


Advanced Data Updates

CT042-3-1-IDB SQL Slide 10 of 42


Copying Parts of Tables

• SQL permits copying contents of selected


table columns so that the data need not be
reentered manually into newly created
table(s)
• First create the PART table structure
• Next add rows to new PART table using
PRODUCT table rows

CT042-3-1-IDB SQL Slide 11 of 42


Copying Parts of Tables (continued)

CT042-3-1-IDB SQL Slide 12 of 42


Adding Primary and Foreign Key
Designations
• When table is copied, integrity rules do not
copy, so primary and foreign keys need to
be manually defined on new table
• User ALTER TABLE command
– Syntax:
• ALTER TABLE tablename ADD
PRIMARY KEY(fieldname);
• For foreign key, use FOREIGN KEY in place of
PRIMARY KEY

CT042-3-1-IDB SQL Slide 13 of 42


Deleting a Table from the
Database
• DROP
– Deletes table from database
– Syntax:
• DROP TABLE tablename;

CT042-3-1-IDB SQL Slide 14 of 42


Advanced Select Queries

• SQL provides useful functions that can:


– Count
– Find minimum and maximum values
– Calculate averages
• SQL allows user to limit queries to only
those entries having no duplicates or
entries whose duplicates may be grouped

CT042-3-1-IDB SQL Slide 15 of 42


Ordering a Listing

CT042-3-1-IDB SQL Slide 16 of 42


Ordering a Listing (continued)

CT042-3-1-IDB SQL Slide 17 of 42


Ordering a Listing (continued)

CT042-3-1-IDB SQL Slide 18 of 42


Listing Unique Values

CT042-3-1-IDB SQL Slide 19 of 42


Aggregate Functions

CT042-3-1-IDB SQL Slide 20 of 42


Aggregate Functions (continued)

CT042-3-1-IDB SQL Slide 21 of 42


Aggregate Functions (continued)

CT042-3-1-IDB SQL Slide 22 of 42


Aggregate Functions (continued)

CT042-3-1-IDB SQL Slide 23 of 42


Aggregate Functions (continued)

CT042-3-1-IDB SQL Slide 24 of 42


Grouping Data

CT042-3-1-IDB SQL Slide 25 of 42


Grouping Data (continued)

CT042-3-1-IDB SQL Slide 26 of 42


Grouping Data (continued)

CT042-3-1-IDB SQL Slide 27 of 42


Virtual Tables: Creating a View

• View is virtual table based on SELECT


query
– Can contain columns, computed columns,
aliases, and aggregate functions from one or
more tables
• Base tables are tables on which view is
based
• Create view by using CREATE VIEW
command

CT042-3-1-IDB SQL Slide 28 of 42


Virtual Tables: Creating a View
(continued)

CT042-3-1-IDB SQL Slide 29 of 42


Joining Database Tables

• Ability to combine (join) tables on common


attributes is most important distinction
between relational database and other
databases
• Join is performed when data are retrieved
from more than one table at a time
• Join is generally composed of an equality
comparison between foreign key and
primary key of related tables

CT042-3-1-IDB SQL Slide 30 of 42


Joining Database Tables (continued)

CT042-3-1-IDB SQL Slide 31 of 42


Joining Database Tables (continued)

CT042-3-1-IDB SQL Slide 32 of 42


Joining Database Tables (continued)

CT042-3-1-IDB SQL Slide 33 of 42


Joining Tables with an Alias

• Alias can be used to identify source table


• Any legal table name can be used as alias
• Add alias after table name in FROM
clause
– FROM tablename alias

CT042-3-1-IDB SQL Slide 34 of 42


Recursive Joins

CT042-3-1-IDB SQL Slide 35 of 42


Recursive Joins (continued)

CT042-3-1-IDB SQL Slide 36 of 42


Outer Joins

CT042-3-1-IDB SQL Slide 37 of 42


Outer Joins (continued)

CT042-3-1-IDB SQL Slide 38 of 42


Quick Review Question

• State the difference between types of


JOIN below
– Inner join
– Left join
– Right join
– Full join

CT042-3-1-IDB SQL Slide 39 of 42


Summary of Main Teaching Points

• Aggregate functions
– Special functions that perform arithmetic
computations over a set of rows
• ORDER BY clause
– Used to sort output of SELECT statement
– Can sort by one or more columns and use either an
ascending or descending order
• Join output of multiple tables with SELECT statement
• Natural join uses join condition to match only rows with
equal values in specified columns
• Right outer join and left outer join used to select rows
that have no matching values in other related table
CT042-3-1-IDB SQL Slide 40 of 42
Question and Answer Session

Q&A

CT042-3-1-IDB SQL Slide 41 of 42


What we will cover next

• Degrees of data abstraction


• Extended Entity Relationship Model

CT042-3-1-IDB SQL Slide 42 of 42

You might also like