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

Week 3

The document provides an overview of SQL, detailing its components such as Data Definition Language (DDL), Data Manipulation Language (DML), and transaction control. It explains various SQL operations including queries, joins, and integrity constraints, as well as advanced features like views, triggers, and user-defined types. Additionally, it covers SQL's capabilities for data aggregation, authorization, and the use of embedded SQL within programming languages.

Uploaded by

laptop18122022
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)
1 views

Week 3

The document provides an overview of SQL, detailing its components such as Data Definition Language (DDL), Data Manipulation Language (DML), and transaction control. It explains various SQL operations including queries, joins, and integrity constraints, as well as advanced features like views, triggers, and user-defined types. Additionally, it covers SQL's capabilities for data aggregation, authorization, and the use of embedded SQL within programming languages.

Uploaded by

laptop18122022
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/ 37

Introduction to SQL

Overview of the SQL Query Language

The SQL language has several parts:

• Data-definition language(DDL).TheSQL DDL provides commands for defining relation schemas,


deleting relations, and modifying relation schemas.

• Data-manipulation language (DML). The SQL DML provides the ability to query information from the
database and to insert tuples into, delete tuples from, and modify tuples in the database.

• Integrity. The SQL DDL includes commands for specifying integrity constraints that the data stored in
the database must satisfy. Updates that violate integrity constraints are disallowed.

• View definition. The SQL DDL includes commands for defining views.

• Transaction control. SQL includes commands for specifying the beginning


and ending of transactions.

• Embedded SQL and dynamic SQL. Embedded and dynamic SQL define how SQL statements can be
embedded within general-purpose programming languages, such as C, C++, and Java.

• Authorization.TheSQLDDLincludescommandsforspecifyingaccessrights to relations and views.

SQL Data Definition


The set of relations in a database must be specified to the system by means of a data-definition language (DDL).

The SQL DDL allows specification of not only a set of relations, but also information about each relation, including:
d -

Q



..

Queries on a Single Relation

Let us consider a simple query using our university


example, “Find the names of all instructors.” Instructor
names are found in the instructor relation, so we put
that relation in the from clause. The instructor’s name
appears in the name attribute, so we put that in the
select clause.

-
Now consider another query, “Find the department names of all instructors,” which can be written as:

SQL allows us to use the keyword all to specify explicitly that duplicates are
not removed:

· T
The Natural Join

Consider the query “For all instructors in the


university who have taught some course, find their
names and the course ID of all courses they taught”,
which we wrote earlier as:

This query can be written more concisely using the natural-join


operation in SQL as: The natural join of the instructor relation with the teaches relation.
9

Both of the above queries generate the same result.

For example, suppose we wish to answer the query “List the names of instructors along with the the titles of courses that they teach.”
The query can be written in SQL as follows:
The Rename Operation

Consider again the query that we used earlier:

The names of the attributes in the result are derived from the names of the attributes in the relations in the from clause.

SQL provides a way of renaming the attributes of a result relation. It uses the as clause, taking the form:
String Operations

Attribute Specification in Select Clause


-important
Basic Aggregation

Aggregation with Grouping


As an illustration, consider the query “Find the average salary in each department.”
We write this query as follows:
As another example of aggregation on groups of tuples, consider the query “Find the number of instructors in each
department who teach a course in the Spring 2010 semester.”
SQL allows testing tuples for membership in a relation. The in connective tests for set membership, where the set is a
collection of values produced by a select clause. The not in connective tests for the absence of set membership.

Set Comparison

As an example of the ability of a nested subquery to compare sets, consider the query “Find the names of all instructors
whose salary is greater than at least one instructor in the Biology department.”

Same

L
Let us find the names of all instructors that have a salary value greater than that of each instructor in the
Biology department. The construct > all corresponds to the phrase “greater than all.” Using this construct, we
write the query as follows:

.
The With Clause
Complex query for
with statement ,
-
·
An outer join in database management is a powerful technique used to combine data from two or more
tables. Unlike an inner join, which only returns rows when there’s a match in both tables, an outer join
retrieves both matched and unmatched rows from the tables involved in the query.

Types of Outer Joins:

SQL Full Outer Join: In a full outer join, all rows from both tables are included. If there are
any unmatched rows, they appear with NULL values for the missing data.

SQL Left Outer Join: A left outer join returns all rows from the left table and matching rows
from the right table. If there’s no match in the right table, the result includes NULL values
for the right table’s columns.

SQL Right Outer Join: Similar to the left outer join, but it includes all rows from the right
table and matching rows from the left table.

A full outer join would include all rows from both tables, even if there’s no match. For example,
it would show both employees and their respective departments, filling in NULL values where
necessary.

A left outer join would include all employees and their departments (if available), but also
show NULL values for employees without a department.

A right outer join would include all departments and their associated employees (if available),
with NULL values for departments without employees.
Joined Relations

Equi Join

An Equi Join is a type of inner join used in SQL to combine data from two or more tables based on the equality
of specific columns. Here are the key points about Equi Joins

In an Equi Join, the comparison is made using the equals sign (=) as the comparison
operator.
It involves matching rows where specific columns have equal values.

If we perform an Equi Join on the AGENT_CODE column, the result would include data where the agent
codes match between the two tables.
Views

Views Defined Using Other Views


Transactions

Integrity Constraints
Constraints on a Single Relation

Referential Integrity
Cascading Actions in Referential Integrity

Integrity Constraint Violation During Transactions

SQL Data Types and Schemas

Built-in Data Types in SQL


Index Creation

User-Defined Types

Create Domains

Large-Object Types
Authorization

Privileges in SQL

Revoking Authorization in SQL


Roles
Functions and Procedures

SQL Functions

Structure
,

Returne
d table
SQL Procedures

While and repeat


sample :
– – Registers a student after ensuring classroom capacity is not exceeded
– – Returns 0 on success, and -1 if capacity is exceeded.
Case Statement
Triggers

Types of Triggers:

ingest
of temporarale
BEFORE
on
kind
-

AFTER
Row Level and Statement Level Triggers

Triggering Events and Actions in SQL

Trigger to Maintain credits earned value


How to use triggers?

How not to use triggers?

Relational Algebra
Six Basic operators :-
SelectrowD
-

select-
Division operator :

Example >
-

You might also like