0% found this document useful (0 votes)
5 views

Lecture 8. Advanced SQL

The document outlines the learning objectives for a course on Database System Design, Implementation, and Management, focusing on key concepts such as the difference between data and information, database design importance, and SQL commands for creating and manipulating database structures. It also covers procedural SQL, including stored procedures and triggers, to enhance database functionality. The course aims to equip students with practical skills in using SQL for effective database management.

Uploaded by

Trà My Nghiêm
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)
5 views

Lecture 8. Advanced SQL

The document outlines the learning objectives for a course on Database System Design, Implementation, and Management, focusing on key concepts such as the difference between data and information, database design importance, and SQL commands for creating and manipulating database structures. It also covers procedural SQL, including stored procedures and triggers, to enhance database functionality. The course aims to equip students with practical skills in using SQL for effective database management.

Uploaded by

Trà My Nghiêm
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/ 77

Learning Objectives

1. Define the difference between data and information

2. Describe what a database is, the various types of databases, and why they are valuable assets for decision
making

3. Explain the importance of database design

4. Outline how modern databases evolved from file systems

5. Identify flaws in file system data management

6. Outline the main components of the database system

7. Describe the main functions of a database management system (DBMS)

12/19/2024 Database System Design, Implementation And Management 1


National Economics University
Faculty of Data Science & Artificial Intelligence

Database System Design, Implementation And


Management Course – Advanced SQL

 Instructor: Duc Minh Vu (FDA – SLSCM Lab)


 Email: minhvd [at] neu.edu.vn

12/19/2024 Database System Design, Implementation And Management 2


Learning Objectives

8-1 Use SQL to create a table manually

8-2 Use SQL to create a copy of a table using a subquery

8-3 Manipulate the structure of existing tables to add, modify, and remove columns and constraints

8-4 Use SQL to do data manipulation (insert, update, and deleterows of data)

8-5 Use SQL to create database views, including updatable views

8-6 Use procedural SQL to create triggers, stored procedures, and procedural SQL functions

8-7 Create embedded SQL

12/19/2024 Database System Design, Implementation And Management 3


8-1 Data Definition Commands

12/19/2024 Database System Design, Implementation And Management 4


12/19/2024 Database System Design, Implementation And Management 5
8-1b Creating the Database

If you are using an enterprise RDBMS, you must be authenticated by the RDBMS before
you can start creating tables.

Authentication The process through which a DBMS verifies that only registered users can access the

database.

To be authenticated, you must log on to the RDBMS using a user ID and a password created by the database
administrator.

In an enterprise RDBMS, every user ID is associated with a database schema.

12/19/2024 Database System Design, Implementation And Management 6


8-1c The Database Schema

a schema is a logical group of database objects—such as tables and indexes—that are related to each other

the schema belongs to a single user or application

A single database can hold multiple schemas that belong to different users or applications.

Schemas are useful in that they group tables by owner (or function) and enforce a first level of security by

allowing each user to see only the tables that belong to that user

12/19/2024 Database System Design, Implementation And Management 7


8-1d Data Types

12/19/2024 Database System Design, Implementation And Management 8


8-2 Creating Table Structures

12/19/2024 Database System Design, Implementation And Management 9


8-2a CREATE TABLE command

CREATE TABLE A SQL command that creates a table’s structures using the characteristics and attributes given.

12/19/2024 Database System Design, Implementation And Management 10


12/19/2024 Database System Design, Implementation And Management 11
12/19/2024 Database System Design, Implementation And Management 12
12/19/2024 Database System Design, Implementation And Management 13
12/19/2024 Database System Design, Implementation And Management 14
12/19/2024 Database System Design, Implementation And Management 15
12/19/2024 Database System Design, Implementation And Management 16
12/19/2024 Database System Design, Implementation And Management 17
12/19/2024 Database System Design, Implementation And Management 18
12/19/2024 Database System Design, Implementation And Management 19
12/19/2024 Database System Design, Implementation And Management 20
12/19/2024 Database System Design, Implementation And Management 21
12/19/2024 Database System Design, Implementation And Management 22
12/19/2024 Database System Design, Implementation And Management 23
8-2c Creating a Table with a SELECT Statement

SQL provides a way to rapidly create a new table based on selected columns and rows of an existing table

using a subquery.

12/19/2024 Database System Design, Implementation And Management 24


8-2d SQL Indexes

CREATE INDEX A SQL command that creates indexes on the basis of a selected attribute or attributes

indexes can be used to improve the efficiency of searches and to avoid duplicate column values

when you declare a primary key, the DBMS automatically creates a unique index

Using the CREATE INDEX command, SQL indexes can be created on the basis of any selected attribute.

12/19/2024 Database System Design, Implementation And Management 25


8-2d SQL Indexes

DROP INDEX A SQL command used to delete database objects such as tables, views, indexes, and users.

12/19/2024 Database System Design, Implementation And Management 26


8-3 Altering Table Structures

12/19/2024 Database System Design, Implementation And Management 27


8-3 Altering Table Structures

ALTER TABLE The SQL command used to make changes to table structure. When the command is followed by
a keyword (ADD, ALTER, or MODIFY), it adds a column or changes column characteristics.

12/19/2024 Database System Design, Implementation And Management 28


12/19/2024 Database System Design, Implementation And Management 29
12/19/2024 Database System Design, Implementation And Management 30
8-4 Data Manipulation Commands

12/19/2024 Database System Design, Implementation And Management 31


8-4a Adding Table Rows

INSERT INTO tablename VALUES (value1, value2, …, valuen)

12/19/2024 Database System Design, Implementation And Management 32


8-4b Inserting Table Rows with a SELECT Subquery

12/19/2024 Database System Design, Implementation And Management 33


8-4c Saving Table Changes

COMMIT The SQL command that terminates a transaction by permanently saving data changes to a

database.

The COMMIT command permanently saves all changes—such as rows added, attributes modified, and rows
deleted—made to any table in the database

COMMIT [WORK | TRANSACTION]

12/19/2024 Database System Design, Implementation And Management 34


8-4d Updating Table Rows

UPDATE A SQL command that allows attribute values to be changed in one or more rows of a table.

12/19/2024 Database System Design, Implementation And Management 35


8-4e Deleting Table Rows

DELETE A SQL command that allows data rows to be deleted from a table.

12/19/2024 Database System Design, Implementation And Management 36


8-4f Restoring Table Contents

ROLLBACK A SQL command that restores the database table contents to the condition that existed after the

last COMMIT statement.

ROLLBACK;

If you have not yet used the COMMIT command to store the changes permanently in the database, you can

restore the database to its previous condition with the ROLLBACK command.

COMMIT and ROLLBACK work only with data manipulation commands that add, modify, or delete table rows

12/19/2024 Database System Design, Implementation And Management 37


12/19/2024 Database System Design, Implementation And Management 38
8-5 Virtual Tables: Creating a View

12/19/2024 Database System Design, Implementation And Management 39


8-5 Virtual Tables: Creating a View

View A virtual table based on a SELECT query that is saved as an object in the database.

base table The table on which a view is based.

CREATE VIEW A SQL command that creates a logical, “virtual” table. The view can be treated as a real table.

 CREATE VIEW viewname [(column list)] AS SELECT query

12/19/2024 Database System Design, Implementation And Management 40


12/19/2024 Database System Design, Implementation And Management 41
12/19/2024 Database System Design, Implementation And Management 42
8-6 Auto Increment, Identity, and
Sequences

12/19/2024 Database System Design, Implementation And Management 43


8-7 Procedural SQL

12/19/2024 Database System Design, Implementation And Management 44


8-7 Procedural SQL

persistent storage module (PSM) A block of code with standard SQL statements and procedural extensions
that is stored and executed at the DBMS server.

The PSM represents business logic that can be encapsulated, stored, and shared among multiple database
users.

A PSM lets an administrator assign specific access rights to a stored module to ensure that only authorized
users can use it.

Procedural SQL An extension of the SQL programming language, such as PL/SQL or TSQL, that add
procedural programming capabilities, such as variable and logical flow control, to SQL and is designed to run

inside the database.


12/19/2024 Database System Design, Implementation And Management 45
8-7a Stored Procedures

stored procedure (1) A named collection of procedural and SQL statements

They can be used to encapsulate and represent business transactions

Stored procedures substantially reduce network traffic and increase performance.

Stored procedures help reduce code duplication by means of code isolation and code sharing (creating

unique modules that are called by application programs)

12/19/2024 Database System Design, Implementation And Management 46


12/19/2024 Database System Design, Implementation And Management 47
12/19/2024 Database System Design, Implementation And Management 48
12/19/2024 Database System Design, Implementation And Management 49
12/19/2024 Database System Design, Implementation And Management 50
8-7b Working with Variables

12/19/2024 Database System Design, Implementation And Management 51


12/19/2024 Database System Design, Implementation And Management 52
12/19/2024 Database System Design, Implementation And Management 53
12/19/2024 Database System Design, Implementation And Management 54
12/19/2024 Database System Design, Implementation And Management 55
12/19/2024 Database System Design, Implementation And Management 56
12/19/2024 Database System Design, Implementation And Management 57
12/19/2024 Database System Design, Implementation And Management 58
12/19/2024 Database System Design, Implementation And Management 59
8-7g Triggers

Trigger A procedural SQL code that is automatically invoked by the relational database management system
when a data manipulation event occurs.

A trigger is invoked before or after a data row is inserted, updated, or deleted.

A trigger is associated with a database table.

Each database table may have one or more triggers.

A trigger is executed as part of the transaction that triggered it

12/19/2024 Database System Design, Implementation And Management 60


8-7g Triggers (cont.)

For example, if a product’s quantity on hand is updated when the product is sold, the system should
automatically check whether the quantity on hand falls below its minimum allowable quantity.

To demonstrate that process, use the PRODUCT table in Figure 8.22. Note the use of the minimum quantity
(P_MIN) and product reorder flag (P_REORDER) columns.

The P_MIN indicates the minimum quantity on hand allowable before restocking an item.

The P_REORDER column is a numeric field that indicates whether the product needs to be reordered (1 - Yes,
- 5 No).

The initial P_REORDER values are set to 0 (No) to serve as the basis for the initial trigger development.

12/19/2024 Database System Design, Implementation And Management 61


12/19/2024 Database System Design, Implementation And Management 62
12/19/2024 Database System Design, Implementation And Management 63
12/19/2024 Database System Design, Implementation And Management 64
12/19/2024 Database System Design, Implementation And Management 65
12/19/2024 Database System Design, Implementation And Management 66
12/19/2024 Database System Design, Implementation And Management 67
12/19/2024 Database System Design, Implementation And Management 68
12/19/2024 Database System Design, Implementation And Management 69
12/19/2024 Database System Design, Implementation And Management 70
12/19/2024 Database System Design, Implementation And Management 71
12/19/2024 Database System Design, Implementation And Management 72
12/19/2024 Database System Design, Implementation And Management 73
12/19/2024 Database System Design, Implementation And Management 74
12/19/2024 Database System Design, Implementation And Management 75
12/19/2024 Database System Design, Implementation And Management 76
12/19/2024 Database System Design, Implementation And Management 77

You might also like