DBMS Concepts and SQL Project 1
DBMS Concepts and SQL Project 1
https://www.youtube.com/watch?v=6Iu45VZGQDk&list=PPSV
2
Syllabus
3
Drawbacks of using file systems to
store data
4
What is a DBMS?
5
6
•Oracle DBMS:
•A proprietary multi-model database management system used in
various enterprise applications.
•MySQL:
•A popular open-source RDBMS known for its performance and
reliability. 7
•PostgreSQL:
•An open-source RDBMS recognized for its extensibility and strong
feature set.
•MongoDB:
•A document-oriented database often used for managing unstructured
or semi-structured data, also finding applications in areas like railway
management.
8
Why DBMS is Crucial in These Sectors
•Security:
•They ensure the security of sensitive data through features like user
access control and encryption.
9
•Efficiency:
•DBMSs allow for efficient data retrieval and manipulation,
supporting real-time operations.
•Data Integrity:
•They maintain the consistency and accuracy of data through
mechanisms like keys and constraints.
•Scalability:
•Modern DBMSs are designed to scale with the growing needs of
these fast-paced industries
10
11
12
13
View of Data
14
Levels of Abstraction
View level: application programs hide details of data types. Views can also
hide information (such as an employee’s salary) for security purposes.
Instances and Schemas
16
Schemas
• Physical schema
• The design of a database at physical level is called physical schema, how the
data stored in blocks of storage is described at this level.
• Logical Schema –
• Design of database at logical level is called logical schema.
• Programmers and database administrators work at this level.
• At this level data can be described as certain types of data.
• Example: The database consists of information about a set of customers and
accounts in a bank and the relationship between them.
Analogous to type information of a variable in a program
• View schema
• Design of database at view level is called view schema. This generally
describes end user interaction with database systems. 17
Schemas
18
Instances
20
Database related
terminology
User
Administrator
1: Transaction
management
2: Storage management
21
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
22
✓ Monitoring performance and responding to changes in
Database Users
26
27
1. Atomicity
Atomicity means a transaction is all-or-nothing either all its
operations succeed, or none are applied. If any part fails, the
entire transaction is rolled back to keep the database consistent.
•Commit: If the transaction is successful, the changes are
permanently applied.
28
29
2. Consistency
Consistency in transactions means that the database must
remain in a valid state before and after a transaction.
•A valid state follows all defined rules, constraints, and
relationships (like primary keys, foreign keys, etc.).
•If a transaction violates any of these rules, it is rolled back to
prevent corrupt or invalid data.
•If a transaction deducts money from one account but doesn't
add it to another (in a transfer), it violates consistency.
3. Isolation
Isolation ensures that transactions run independently without
affecting each other. Changes made by one transaction are not
visible to others until they are committed.
It ensures that the result of concurrent transactions is the
same as if they were run one after another, preventing issues
like:
34
Video (2) related to DBMS
architecture
https://youtu.be/OVVeKjdHitU?si=TNObki6ze5go5PRj
37
DBMS: Allows to Create, Manipulate &
Access the Data
38
SQL
39
SQL
40
SQL The Language of DBMS
Structured Query Language
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
43
Tuples or rows
Steps to Define the Schema
Step 1: Define table name and its attributes
Product(PName, Price, Category, Manufacturer)
Product
PName Price Category Manufacturer
Character-string
• Fixed length: CHAR(n), CHARACTER(n)
• Varying length: VARCHAR(n), CHAR VARYING(n), CHARACTER
VARYING(n)
45
Data Types and Domain of
Attributes
Boolean
• Values of TRUE or FALSE or NULL
DATE
• Ten positions
• Components are YEAR, MONTH, and DAY in the form YYYY-MM-
DD
Timestamp
Includes the DATE and TIME fields
• Plus a minimum of six positions for decimal fractions of seconds
• Optional WITH TIME ZONE qualifier
46
Steps to Define the Schema
Step 2: Define Data Types and Domain of Attributes.
Pname : Varchar,
Price: Float,
Category: Varchar
Manfacturer: Varchar
47
Step 3: Specifying Constraints.
Product(PName, Price, Category, Manfacturer)
• 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 49
Specifying Key Constraints
52
Creating a Database
Step 1. Create a Database Company
CREATE DATABASE <DATABSE NAME>;
use company;
Step 2. SHOW TABLES
show tables;
53
Creating a Table
Step 1. Create a TABLE
CREATE TABLE <TABLE NAME> (
<ATTRIBUTE LIST> <DATA TYPE> <CONSTRAINT>,
<ATTR2> <DATA TYPE>,<CONSTRAINT>);
Desc product;
56
Insert records in Table
INSERT INTO R(A1,…., An) VALUES (v1,…., vn)
SELECT *
FROM product;
PName Price Category Manufacturer
“selection”
Gizmo 19.99 Gadgets GizmoWorks
58
Select Query using WHERE
PName Price Category Manufacturer
Gizmo 19.99 Gadgets GizmoWorks
Product Powergizmo 29.99 Gadgets GizmoWorks
SingleTouch 149.99 Photography Canon
MultiTouch 203.99 Household Hitachi
59
Select Query using WHERE
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 *
FROM Product
WHERE category=‘Gadgets’;
60
Select Query using WHERE
where 61
Select Query using WHERE
Constants:
‘abc’ - yes
“abc” - no
65
The LIKE operator
SELECT *
FROM Products
WHERE PName LIKE <pattern>
66
Like Operator with %
Product name that starts with P
SELECT *
FROM Product
WHERE Pname like ‘p%’;
SELECT *
FROM Product
WHERE Pname like ‘%Touch’;
PName Price Category Manufacturer
SingleTouch 149.99 Photography Canon
MultiTouch 203.99 Household Hitachi
68
Like Operator with %
Product name that contains e anywhere in the name
SELECT *
FROM Product
WHERE Pname like ‘%e%’;
SELECT *
FROM Product
WHERE Pname like ‘_o%’;
SELECT *
FROM Product
WHERE Pname like ‘%c_’;
Compare to:
Category
Gadgets
SELECT category Gadgets
FROM Product; Photography
Household
72
Aggregate Functions
✓ Sum
✓ Max
✓ Min
✓ Avg
✓ Count
73
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 sum(price)
FROM Product;
403.96
74
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 max(price)
FROM Product;
203.96
75
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 min(price)
FROM Product;
19.99
76
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 avg(price)
FROM Product;
100.99
77
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 count(price)
FROM Product;
4
SELECT count(*)
FROM Product;
78
More Examples
Query Sql
79
PName Price Category Manufacturer
WRITE Gizmo 19.99 Gadgets GizmoWorks
THE Powergizmo 29.99 Gadgets GizmoWorks
SingleTouch 149.99 Photography Canon
QUERY MultiTouch 203.99 Household Hitachi
80
Ordering the Results
SELECT pname, price, manufacturer
FROM Product
WHERE manufacturer=‘GizmoWorks’ AND price > 50
ORDER BY price, pname;
83
Create a new table in your current database
‘COMPANY’ with the following schema
Stock_Price Float
Country Varchar
84
Create a new table named ‘COMPDTLS’ in your
current database with the following schema
Country Varchar
85
Insert the following Records in
COMPDTLS
86
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’.
87