Module 4 _DBMS
Module 4 _DBMS
SYSTEMS
SEMESTER IV
1
22ISE42 - DATABASE MANAGEMENT SYSTEMS
MODULE - IV
2
22ISE42 - DATABASE MANAGEMENT SYSTEMS
MODULE – IV
SQL - Introduction, data definition in SQL, table, and key and foreign key revisit, update
behaviors. Querying in SQL – basic select-from-where block and its semantics, nested
queries - correlated and uncorrelated, notion of aggregation, aggregation functions group
by and having clauses, embedded SQL.
SQL – INTRODUCTION:
The name SQL is presently expanded as Structured Query Language. Originally, SQL
was called SEQUEL (Structured English QUEry Language) and was designed and
implemented at IBM Research.
SQL is a comprehensive database language:
It has statements for data definitions, queries, and updates. Hence, it is both a DDL and
a DML.
3
22ISE42 - DATABASE MANAGEMENT SYSTEMS
tuples) can be specified within the CREATE TABLE statement while the attributes are
declared, or they can be added later using the ALTER TABLE command.
Figure 6.1 shows sample data definition statements in SQL for the COMPANY
relational database schema shown in Figure 3.7.
Base Table and Virtual relations: The relations declared through CREATE TABLE statements
are called base tables (or base relations); this means that the table and its rows are actually
created and stored as a file by the DBMS. Base relations are distinguished from virtual
relations, created through the CREATE VIEW statement, which may or may not correspond
to an actual physical file.
Syntax:
CREATE VIEW [Male students] AS
SELECT StudentName, StudentContact
FROM Student
WHERE Gendre = 'MALE';
4
22ISE42 - DATABASE MANAGEMENT SYSTEMS
Example:
CREATE TABLE Persons (
PersonID int,
LastName varchar(255),
FirstName varchar(255),
Address varchar(255),
City char(55)
);
5
22ISE42 - DATABASE MANAGEMENT SYSTEMS
OUTPUT:
Persons
PersonID LastName FirstName Address City
SQL Constraints: SQL constraints are used to specify rules for the data in a table.
Constraints can be column level or table level. Column level constraints apply to a column,
and table level constraints apply to the whole table.
SQL NOT NULL Constraint: By default, a column can hold NULL values.
Example for NOT NULL:
CREATE TABLE Persons (
ID int NOT NULL,
LastName varchar(255) NOT NULL,
FirstName varchar(255) NOT NULL,
Age int
);
6
22ISE42 - DATABASE MANAGEMENT SYSTEMS
Persons Table
PersonID LastName FirstName Age
1 Hansen Ola 30
2 Svendson Tove 23
3 Pettersen Kari 20
Orders Table
OrderID OrderNumber PersonID
1 77895 3
2 44678 3
3 22456 2
4 24562 1
Notice that the "PersonID" column in the "Orders" table points to
the "PersonID" column in the "Persons" table.
The "PersonID" column in the "Persons" table is the PRIMARY KEY in
the "Persons" table. The "PersonID" column in the "Orders" table is
a FOREIGN KEY in the "Orders" table.
The FOREIGN KEY constraint prevents invalid data from being
inserted into the foreign key column, because it has to be one of
the values contained in the parent table.
The following SQL creates a FOREIGN KEY on the "PersonID" column when
the "Orders" table is created in MySQL:
CREATE TABLE Orders (
OrderID int NOT NULL,
OrderNumber int NOT NULL,
PersonID int,
PRIMARY KEY (OrderID),
FOREIGN KEY (PersonID) REFERENCES Persons(PersonID)
);
UPDATE BEHAVIORS: The SQL UPDATE statement is used to modify the data that is
already in the database. The condition in the WHERE clause decides that which row is
to be updated.
Syntax
1. UPDATE table_name
2. SET column1 = value1, column2 = value2, ...
3. WHERE condition;
7
22ISE42 - DATABASE MANAGEMENT SYSTEMS
Sample Table
EMPLOYEE
Updating single record: Update the column EMP_NAME and set the value to 'Emma'
in the row where SALARY is 500000.
Syntax:
1. UPDATE table_name
2. SET column_name = value
3. WHERE condition;
Query:
1. UPDATE EMPLOYEE
2. SET EMP_NAME = 'Emma'
3. WHERE SALARY = 500000;
Output: After executing this query, the EMPLOYEE table will look like:
8
22ISE42 - DATABASE MANAGEMENT SYSTEMS
Updating multiple records: If you want to update multiple columns, you should
separate each field assigned with a comma. In the EMPLOYEE table, update the column
EMP_NAME to 'Kevin' and CITY to 'Boston' where EMP_ID is 5.
Syntax
1. UPDATE table_name
2. SET column_name = value1, column_name2 = value2
3. WHERE condition;
Query
1. UPDATE EMPLOYEE
2. SET EMP_NAME = 'Kevin', City = 'Boston'
3. WHERE EMP_ID = 5;
Output
Without use of WHERE clause: If you want to update all row from a table, then you
don't need to use the WHERE clause. In the EMPLOYEE table, update the column
EMP_NAME as 'Harry'.
Syntax
1. UPDATE table_name
2. SET column_name = value1;
Query
1. UPDATE EMPLOYEE
2. SET EMP_NAME = 'Harry';
9
22ISE42 - DATABASE MANAGEMENT SYSTEMS
Output
WHERE Syntax:
SELECT column1, column2, ...
FROM table_name
WHERE condition;
Note: The WHERE clause is not only used in SELECT statements, it is also
used in UPDATE, DELETE, etc.!
10
22ISE42 - DATABASE MANAGEMENT SYSTEMS
WHERE Clause Example: The following SQL statement selects all the
customers from the country "Mexico", in the "Customers" table:
Example
SELECT * FROM Customers
WHERE Country='Mexico';
= Equal
<> Not equal. Note: In some versions of SQL this operator may be written as !=
BETWEEN Syntax:
SELECT column_name(s)
FROM table_name
WHERE column_name BETWEEN value1 AND value2;
Demo Database: Below is a selection from the "Products" table:
1 Chais 1 1 10 boxes x 20 18
bags
2 Chang 1 1 24 - 12 oz 19
bottles
11
22ISE42 - DATABASE MANAGEMENT SYSTEMS
BETWEEN Example: The following SQL statement selects all products with a price
between 10 and 20:
Example
SELECT * FROM Products
WHERE Price BETWEEN 10 AND 20;
NOT BETWEEN Example: To display the products outside the range of the previous
example, use NOT BETWEEN:
Example
SELECT * FROM Products
WHERE Price NOT BETWEEN 10 AND 20;
SQL IN Example: The following SQL statement selects all products whose CategoryID is
not 1,2, and 3:
Example
SELECT * FROM Products
WHERE CategoryID NOT IN (1,2,3);
SQL LIKE Examples: The following SQL statement selects all products with a
ProductName starting with "a":
Example
SELECT * FROM Products
WHERE ProductName LIKE 'a%';
Example:
SELECT * FROM Customers WHERE
CustomerID IN (SELECT CustomerID FROM Orders);
UNCORRELATED subquery:
12
22ISE42 - DATABASE MANAGEMENT SYSTEMS
When this subquery is executed only once and the result of this subquery is used to
extract the data in the main query, then this type of subquery is known as UNCORRELATED
subquery.
Orders Table:
OrderID OrderNumber CustomerID
1 77895 3
2 44678 3
3 22456 2
4 24562 1
For finding the details of customers who have ordered, the query will be
SELECT * FROM Customers WHERE
CustomerID IN (SELECT CustomerID FROM Orders);
CORRELATED subquery: In Correlated Query, Outer query executes first and for
every Outer query row Inner query is executed. Hence, Inner query uses values
from Outer query. i.e when a subquery refers to the main query for each execution, then the
subquery is known as CORRELATED subquery.
Example –
For finding the details of customers who have ordered.
13
22ISE42 - DATABASE MANAGEMENT SYSTEMS
COUNT() Syntax:
SELECT COUNT(column_name)
FROM table_name
AVG() Syntax:
SELECT AVG(column_name)
FROM table_name
SUM() Syntax:
SELECT SUM(column_name)
FROM table_name
1 Chais 1 1 10 boxes x 20 18
bags
2 Chang 1 1 24 - 12 oz 19
bottles
14
22ISE42 - DATABASE MANAGEMENT SYSTEMS
The MIN() function returns the smallest value of the selected column.
Example for MIN():
SELECT MIN (Price) AS SmallestPrice
FROM Products;
Result:
SmallestPrice
10
The MAX() function returns the largest value of the selected column.
Example for MAX():
SELECT MAX (Price) AS LargestPrice
FROM Products;
Result:
LargestPrice
22
The SQL GROUP BY Statement: The GROUP BY statement groups rows that have the
same values into summary rows, like "find the number of customers in each country".
15
22ISE42 - DATABASE MANAGEMENT SYSTEMS
The following SQL statement lists the number of customers in each country:
SELECT COUNT (CustomerID), Country
FROM Customers
GROUP BY Country;
Result:
COUNT(CustomerID) Country
1 Germany
2 Mexico
1 Sweden
1 UK
The SQL HAVING Clause: The HAVING clause was added to SQL because the WHERE
keyword could not be used with aggregate functions.
Result:
COUNT(CustomerID) Country
2 Mexico
Embedded SQL:
So far we described the SQL statements for data definition, manipulation, queries,
updates and constraints etc., Now we discuss some of the methods that have been
developed for accessing databases from programs such as Java, C/C++/C#, PHP,
Python etc..
When database statements are included in a program, the general-purpose
programming language is called the host language, whereas the database language—
SQL—is called the data sublanguage.
The rules for embedding SQL statements into a general-purpose programming
language, generally known as embedded SQL. Example making airline reservations or
online purchases.
16
22ISE42 - DATABASE MANAGEMENT SYSTEMS
The database statements are embedded into the host programming language, but
they are identified by a special prefix. For example, the prefix for embedded SQL is the
string EXEC SQL, which precedes all SQL commands in a host language program.
A precompiler or preproccessor scans the source program code to identify
database statements and extract them for processing by the DBMS. They are replaced
in the program by function calls to the DBMS-generated code. This technique is
generally referred to as embedded SQL.
/* End program */
All statements that begin EXEC SQL are embedded SQL database statements. Some
additional explanation as follows:
17
22ISE42 - DATABASE MANAGEMENT SYSTEMS
We are not limited to SELECT statements. We can embed any statement available in
standard SQL. For example we can create a table as follows:
EXEC SQL CREATE TABLE employees
(name c20,
emp_number int)
This creates a table called employees that has two fields, name a character field with
20 characters and emp_number an integer field.
*********************
18