Grade 12 Ict Dbms Using SQL
Grade 12 Ict Dbms Using SQL
Grade 12 Ict Dbms Using SQL
Session 1
What is a DBMS?
• Collection of interrelated data – manual or
computerized or online
• Set of programs to access the data
• DBMS provides an environment that is both
convenient and efficient to use.
2
Applications Areas of DBMS?
5
Relational Model
Attributes
6
A Logically Related Database
7
Database Users
Users are differentiated by the way they expect to interact with the
system
• Application programmers – interact with system through DML
calls
• Sophisticated users – form requests in a database query
language
• Specialized users – write specialized database applications that
do not fit into the traditional data processing framework
• Naïve users – invoke one of the permanent application
programs that have been written previously
E.g. people accessing database over the web, bank tellers, clerical staff
8
Database Administrator
• Coordinates all the activities of the database
system
• Has a good understanding of the enterprise’s
information resources and needs.
• Database administrator’s responsibilities include:
Schema definition
Storage structure and access method definition
Schema and physical organization modification
Granting user authority to access the database
Specifying integrity constraints
Acting as liaison with users
Monitoring performance and responding to changes in
9
requirements
Transaction Management
• A transaction is a collection of operations that
performs a single logical function in a database
application
• Transaction-management component ensures that the
database remains in a consistent (correct) state
despite system failures (e.g., power failures and
operating system crashes) and transaction failures.
• Concurrency-control manager controls the interaction
among the concurrent transactions, to ensure the
consistency of the database.
10
Storage Management
• Storage manager is a program module that
provides the interface between the low-level
data stored in the database and the
application programs and queries submitted to
the system.
11
Overall
System
Structure
12
Application Architectures
14
The Language of DBMS
SQL
Structured Query Language
Standard language for querying and manipulating
data. Very widely used.
1. Data Definition Language (DDL)
– Create/alter/delete tables and their attributes
2. Data Manipulation Language (DML)
– Insert/delete/modify tuples in tables
SQL
• SQL: widely used non-procedural language
– E.g. find the name of the customer with customer-id 192-83-7465
select customer.customer-name
from customer
where customer.customer-id = ‘192-83-7465’
– E.g. find the balances of all accounts held by the customer with
customer-id 192-83-7465
select account.balance
from depositor, account
where depositor.customer-id = ‘192-83-7465’ and
depositor.account-number = account.account-
number
• Application programs generally access databases through one of
– Language extensions to allow embedded SQL
– Application program interface (e.g. ODBC/JDBC) which allow SQL
16
queries to be sent to a database
Table name Attribute names
Tables in RDBMS
Product
Tuples or rows 17
Steps to Define the Schema
Step 1: Define table name and its attributes
Product(PName, Price, Category, Manufacturer)
Product
PName Price Category Manufacturer
Pname : Varchar,
Price: Float,
Category: Varchar
Manfacturer: Varchar
21
Step 3: Specifying Constraints.
Product(PName, Price, Category, Manfacturer)
22
Specifying Attribute and Domain Constraints
• NOT NULL
NULL is not permitted for a particular attribute
• Default value
DEFAULT <value>
• CHECK clause
Dnumber > 0 AND Dnumber < 21;
• UNIQUE clause
Specifies attributes that have unique values 23
Specifying Key Constraints
Manufacturer Varchar
25
LET’S CODE
TOGETHER!!
26
Creating a Database
Step 1. Create a Database Company
CREATE
CREATEDATABASE
DATABASE <DATABSE
<DATABSENAME>;
NAME>;
use company;
Step 2. SHOW TABLES
show tables;
27
Creating a Table
Step 1. Create a TABLE
CREATE
CREATETABLE
TABLE <TABLE
<TABLENAME>
NAME> ((
<ATTRIBUTE
<ATTRIBUTELIST>
LIST> <DATA
<DATATYPE>
TYPE> <CONSTRAINT>,
<CONSTRAINT>,
<ATTR2>
<ATTR2> <DATA
<DATATYPE>,<CONSTRAINT>);
TYPE>,<CONSTRAINT>);
Attribute Data Type Constraints
Pname Varchar Primary Key
Price Float Not Null
Category Varchar Gadget,
Photography,
Household
Manufacturer Varchar
28
Creating a Table
create table product(Pname varchar(20) primary key,
price float NOT NULL,category varchar(20)
CHECK(category
in("Gadget","Photography","Household")),
manufacturer varchar(20));
Attribute Data Type Constraints
Pname Varchar Primary Key
Price Float Not Null
Category Varchar Gadget,
Photography,
Household
Manufacturer Varchar
VIPS: Oct - Dec 2019 29
Show Existing Tables
Show
Showtables;
tables;
Desc product;
30
Insert records in Table
INSERT
INSERT INTO
INTO R(A1,….,
R(A1,….,An)
An) VALUES
VALUES (v1,….,
(v1,…., vn)
vn)
SELECT
SELECT **
FROM
FROM product;
product;
PName Price Category Manufacturer
“selection”
Gizmo 19.99 Gadgets GizmoWorks
32
Select Query using WHERE
PName Price Category Manufacturer
Gizmo 19.99 Gadgets GizmoWorks
Powergizmo 29.99 Gadgets GizmoWorks
Product
SingleTouch 149.99 Photography Canon
MultiTouch 203.99 Household Hitachi
SELECT
SELECT Pname,
Pname,Price
Price
FROM
FROM Product
Product PName Price
Gizmo 19.99
Powergizmo 29.99
SingleTouch 149.99
“projection”
MultiTouch 203.99
33
Select Query using WHERE
SELECT
SELECT **
FROM
FROM Product
Product
WHERE
WHERE category=‘Gadgets’;
category=‘Gadgets’;
34
Select Query using WHERE
SELECT
SELECT PName,
PName,Price,
Price,Manufacturer
Manufacturer
FROM
FROM Product
Product
WHERE
WHERE Price
Price>>100;
100;
PName Price Manufacturer
“selection” and SingleTouch 149.99 Canon
where 35
Select Query using WHERE
SELECT
SELECT PName,
PName,Price,
Price,Manufacturer
Manufacturer
FROM
FROM Product
Product
WHERE
WHERE Price
Price>>100
100and
and manufacturer
manufacturer=“Canon”;
=“Canon”;
SELECT
SELECT PName,
PName,Price,
Price,Manufacturer
Manufacturer
FROM
FROM Product
Product
WHERE
WHERE manufacturer
manufacturer=“Hitachi”
=“Hitachi”or
or
manufacturer
manufacturer==“Canon”;
“Canon”;
37
Select Query using WHERE
PName Price Category Manufacturer
Product
Gizmo 19.99 Gadgets GizmoWorks
Powergizmo 29.99 Gadgets GizmoWorks
SingleTouch 149.99 Photography Canon
MultiTouch 203.99 Household Hitachi
SELECT
SELECT PName,
PName,Price,
Price,Manufacturer
Manufacturer
FROM
FROM Product
Product
WHERE
WHERE manufacturer
manufacturer
IN(“Hitachi”,“Canon”);
IN(“Hitachi”,“Canon”);
38
Note That
• Case insensitive:
– Same: SELECT Select select
– Same: Product product
– Different: ‘Seattle’ ‘seattle’
• Constants:
– ‘abc’ - yes
– “abc” - no
39
The LIKE operator
SELECT
SELECT **
FROM
FROM Products
Products
WHERE
WHERE PName
PName LIKE
LIKE <pattern>
<pattern>
40
Like Operator with %
Product name that starts with P
SELECT
SELECT **
FROM
FROM Product
Product
WHERE
WHERE Pname
Pnamelike
like‘p%’;
‘p%’;
SELECT
SELECT **
FROM
FROM Product
Product
WHERE
WHERE Pname
Pnamelike
like‘%Touch’;
‘%Touch’;
PName Price Category Manufacturer
SingleTouch 149.99 Photography Canon
MultiTouch 203.99 Household Hitachi
42
Like Operator with %
Product name that contains e anywhere in the name
SELECT
SELECT **
FROM
FROM Product
Product
WHERE
WHERE Pname
Pnamelike
like‘%e%’;
‘%e%’;
SELECT
SELECT **
FROM
FROM Product
Product
WHERE
WHERE Pname
Pnamelike
like‘_o%’;
‘_o%’;
SELECT
SELECT **
FROM
FROM Product
Product
WHERE
WHERE Pname
Pnamelike
like‘%c_’;
‘%c_’;
PName Price Category Manufacturer
SingleTouch 149.99 Photography Canon
MultiTouch 203.99 Household Hitachi
45
Eliminating Duplicates
Category
SELECT
SELECT DISTINCT
DISTINCT category
category Gadgets
FROM
FROM Product;
Product; Photography
Household
Compare to:
Category
Gadgets
SELECT
SELECT category
category Gadgets
FROM
FROM Product;
Product; Photography
Household
46
Aggregate Functions
Sum
Max
Min
Avg
Count
47
Aggregate Functions – SUM
Sum of Price of all Products
Product PName Price Category Manufacturer
Gizmo 19.99 Gadgets GizmoWorks
Powergizmo 29.99 Gadgets GizmoWorks
SingleTouch 149.99 Photography Canon
MultiTouch 203.99 Household Hitachi
SELECT
SELECT sum(price)
sum(price)
FROM
FROM Product;
Product;
403.96
48
Aggregate Functions – MAX
Max of Price of all Products
Product PName Price Category Manufacturer
Gizmo 19.99 Gadgets GizmoWorks
Powergizmo 29.99 Gadgets GizmoWorks
SingleTouch 149.99 Photography Canon
MultiTouch 203.99 Household Hitachi
SELECT
SELECT max(price)
max(price)
FROM
FROM Product;
Product;
203.96
49
Aggregate Functions – MIN
Min of Price of all Products
Product PName Price Category Manufacturer
Gizmo 19.99 Gadgets GizmoWorks
Powergizmo 29.99 Gadgets GizmoWorks
SingleTouch 149.99 Photography Canon
MultiTouch 203.99 Household Hitachi
SELECT
SELECT min(price)
min(price)
FROM
FROM Product;
Product;
19.99
50
Aggregate Functions – AVG
Avg of Price of all Products
Product PName Price Category Manufacturer
Gizmo 19.99 Gadgets GizmoWorks
Powergizmo 29.99 Gadgets GizmoWorks
SingleTouch 149.99 Photography Canon
MultiTouch 203.99 Household Hitachi
SELECT
SELECT avg(price)
avg(price)
FROM
FROM Product;
Product;
100.99
51
Aggregate Functions – COUNT
Total number of Products
Product PName Price Category Manufacturer
Gizmo 19.99 Gadgets GizmoWorks
Powergizmo 29.99 Gadgets GizmoWorks
SingleTouch 149.99 Photography Canon
MultiTouch 203.99 Household Hitachi
SELECT
SELECT count(price)
count(price)
FROM
FROM Product;
Product;
4
SELECT
SELECT count(*)
count(*)
FROM
FROM Product;
Product;
52
More Examples
Query Sql
53
PName Price Category Manufacturer
WRITE Gizmo 19.99 Gadgets GizmoWorks
THE Powergizmo
SingleTouch
29.99
149.99
Gadgets
Photography
GizmoWorks
Canon
QUERY MultiTouch 203.99 Household Hitachi
54
Ordering the Results
SELECT
SELECT pname,
pname, price,
price, manufacturer
manufacturer
FROM
FROM Product
Product
WHERE
WHERE manufacturer=‘GizmoWorks’
manufacturer=‘GizmoWorks’AND ANDprice
price>> 50
50
ORDER
ORDERBYBY price,
price, pname;
pname;
• Ties are broken by the second attribute on the ORDER
BY list, etc.
SELECT
SELECT pname,
pname, price,
price, manufacturer
manufacturer
FROM
FROM Product
Product
ORDER
ORDERBYBY price
priceDESC;
DESC;
55
PName Price Category Manufacturer
FIND Gizmo 19.99 Gadgets GizmoWorks
SELECT
SELECT DISTINCT
DISTINCTcategory
?
category
FROM
FROM Product
Product
ORDER
ORDERBYBYcategory
category
SELECT
SELECT Category
?
Category
FROM
FROM Product
Product
ORDER
ORDERBYBY PName
PName
SELECT
SELECT DISTINCT
DISTINCTcategory
?
category
FROM
FROM Product
Product
ORDER
ORDERBYBYPName
PName 56
Practice Exercise
57
Create a new table in your current database
‘COMPANY’ with the following schema
Stock_Price Float
Country Varchar
58
Create a new table named ‘COMPDTLS’ in your
current database with the following schema
Country Varchar
59
Insert the following Records in COMPDTLS
60
Write SQL Queries for:
1. List the details of all companies
2. List the registration date of all companies
3. Show the details of all companies of Japan
4. List the company name whose stock price is 65
5. List the companies of Japan or India
6. Show the maximum stock price.
7. Show the average stock price.
8. Show the distinct countries
9. Show the total no of countries
10. Show the company name whose country name ends with ‘a’.
61