DMBS Topic
DMBS Topic
pdf) for
you. Here is the text from the notes:
Page 1
● Database Language:
○ Two types of Database Language:
■ (i) DDL (Data Definition Language)
■ (ii) DML (Data Manipulation Language)
○
●
● DDL: defines logical Schema.
● # creating a database
● SQL
SHOW DATABASES;
●
●
● # creating table/relation in database
● SQL
Page 2
● # create table with foreign key constraint
● SQL
Page 3
● ALTER Statements - modifies the structure of an existing table.
● # add new column to an existing table
● SQL
ALTER TABLE Students ADD email VARCHAR(100); -- (VARCHAR size assumed)
●
●
● # Adding a Primary key to an existing table
● SQL
ALTER TABLE Students DROP PRIMARY KEY; -- (Syntax might vary slightly based on DBMS)
-- OR (if constraint name known)
ALTER TABLE Students DROP CONSTRAINT PK_Student;
●
●
● # Adding a Foreign key
● SQL
ALTER TABLE Students MODIFY age SMALLINT; -- (Syntax might be MODIFY COLUMN
depending on DBMS)
●
●
● # Rename a column
● SQL
Page 5
● TRUNCATE
○ Deletes all data inside a table but keeps its structure intact.
●
● # remove all records from table but keep its structure
● SQL
RENAME TABLE Students TO Student_Records; -- (Syntax might vary, e.g., ALTER TABLE ...
RENAME TO ...)
●
●
● COMMENT
○ Adds comments to database objects <!-- end list -->
● SQL
Page 6
● # Show all the tables in a database
● SQL
DESC tbl_branch;
●
●
● # Showing all the values from a table
● SQL
DESCRIBE Students;
-- OR
SHOW CREATE TABLE Students;
●
●
● # Create a table from an existing table structure (with data)
● SQL
Page 7
● Constraints: Enforce rules on table columns to maintain data integrity.
● Constraints are: NOT NULL, UNIQUE, PRIMARY KEY, FOREIGN KEY, CHECK,
DEFAULT.
● # NOT NULL
● SQL
Page 8
5. Create a composite primary key <!-- end list -->
● SQL
CREATE TABLE orderdetails (
order_id INT,
product_id INT,
quantity INT,
PRIMARY KEY (order_id, product_id)
);
●
●
5. Change data type of a column <!-- end list -->
● SQL
DROP TABLE Employees CASCADE CONSTRAINTS; -- (Syntax might vary, CASCADE ensures
dependent objects are also dropped/handled)
●
●
5. Add a new Column to an existing table <!-- end list -->
● SQL
Page 10
● DML (Data Manipulation Language)
● Four primary DML commands: INSERT, UPDATE, DELETE, SELECT
● INSERT Statement - Add new records to a table
● SQL
Page 11
● Bank Schema
○ tbl-branch (<u>branchID</u>, br_name, br_city, br_assets)
○ tbl-account (<u>accountNo</u>, branchID*, ac_amount, ac_type)
○ tbl-customer (<u>customerID</u>, cs-name, cs-street, cs-city)
○ tbl-loan (<u>loanNo</u>, branchID*, ln_amount)
○ tbl-depositor (<u>customerID*</u>, <u>accountNo*</u>) --
(Assuming PK and FKs based on context)
○ tbl-borrower (<u>customerID*</u>, <u>loanNo*</u>) -- (Assuming
PK and FKs based on context)
●
● Q.1: Find the name of all branches with their city and street
● SQL
Page 12
● Q.3: Find the details of all branches order by branch name/ branch city/ assets
● SQL
SELECT DISTINCT br_name, br_assets -- (Distinct might not be needed if br_name is unique)
FROM tbl_branch
WHERE br_city = 'Sylhet'
ORDER BY br_assets; [cite: 22, 23]
●
●
Page 13
● Q.6: Find those branches which have assets value more than 9 crore & less than
50 crore.
● SQL
SELECT br_name
FROM tbl_branch
WHERE br_assets > 90000000 AND br_assets < 500000000 -- (Assuming 9 crore = 90M, 50 crore
= 500M)
ORDER BY br_assets; [cite: 24, 25]
●
●
● Q.7: Find the asset values of the branch which are located at cities where 'Bazar'
is part of the city name. (And assets between 9 and 50 crore - seems added
here)
● SQL
-- Method 1: Subquery
SELECT AVG(ac_amount)
FROM tbl_account
WHERE branchID = (SELECT branchID FROM tbl_branch WHERE br_name = 'Zindabazar');
Page 14
● Q.8 (cont. Join method):
● SQL
SELECT AVG(a.ac_amount)
FROM tbl_account AS a
JOIN tbl_branch AS b ON a.branchID = b.branchID
WHERE b.br_name = 'Zindabazar';
●
●
● Q.9: Find the number of depositors at each branch.
● SQL
Page 15
● Q.10: Find the total number of accounts for each branch.
● SQL
SELECT c.*, b.br_name AS Branch -- Selecting all customer details and branch name
FROM tbl_customer AS c
JOIN tbl_depositor AS d ON c.customerID = d.customerID
JOIN tbl_account AS a ON d.accountNo = a.accountNo
JOIN tbl_branch AS b ON a.branchID = b.branchID
WHERE b.br_name = 'Zindabazar'
ORDER BY c.customerID; -- (Or customer name)
●
●
● Creating a View (vw_totalBranch)
● SQL
Page 17
● Relational Algebra
● Relational algebra is a formal system used to manipulate relations (tables) in a
database.
● Procedural -> Relational Algebra
● Non-procedural -> SQL
● Basic Operators:
○ Projection (π)
○ Selection (σ)
○ Union (∪)
○ Difference (-)
○ Cross Product (×)
○ Rename (ρ)
●
● Derived Operators:
○ Join (⋈)
○ Intersect (∩) (XcapY=X−(X−Y))
○ Division (/)
●
● Order of Operations: Selection > projection > set > Join
Page 18
● Projection (π): operation used to select specific attributes/columns from a table /
filtering attributes.
● Basic Syntax: pi_column_name,... (relation)
● Query: Retrieve the rollno from table (student)
○ pi_Rollno (student)
●
● Selection (σ): Select rows/tuples from a relation that satisfy a given condition.
● Basic Syntax: sigma_condition (relation)
● Works on rows.
Page 19
● Cross product (×): combines every row from one table with every row from
another table.
● Syntax: Relation1 × Relation2
● Result rows = rows1 * rows2; Result columns = cols1 + cols2
● (Join condition): At least one column common.
● Set difference (-): used to subtract one table from another.
● Gives you the rows that are in the first relation but not in the second.
● Syntax: Relation1 - Relation2
● Conditions: No of columns must be same; Domain of columns must be same.
Page 20
● Example query (Difference): Name of person who is a student but not employee
○ (pi_name (Student) - pi_name (Employee))
○ Clue: (not, didn't)
●
● Union (∪): used to combine the rows (tuples) of two relations and gives a result
that contains all rows from both, without duplicates.
● Syntax: Relation1 ∪ Relation2
● Conditions: No of Columns must be same; Attributes must have same data types
(Same Structure).
Page 21
● Division (/): helps find entries related to all values in another set. Use when "all",
"every", "each".
● A(x,y)/B(y) results in values x such that for every y value in relation B, there
should be a tuple $\ in relation A.
● Query: Retrieve Sid of students who enrolled in every/all courses.
○ E(sid, cid) / pi_cid(Course)
●
● Join (⋈): Used to combine rows from two or more tables based on a related
column between them.
● Types: Inner Join, Outer Join, Self Join.
● Inner Join is like Cross product + Select statement (Condition).
● Format in RA: R1bowtie_conditionR2 (Theta join) or R1bowtieR2 (Natural join -
joins on common attribute names).
Page 22
● Intersection (∩): returns only the common tuples (rows) that exist in both input
relations. (Keywords: Both, common, share, present).
● Ex: Find customers who ordered both "Laptop" and "Tablet":
○ Aleftarrowsigma_product=′Laptop′ (Orders)
○ Bleftarrowsigma_product=′Tablet′ (Orders)
○ Result: pi_customerID(A)cappi_customerID(B)
●
● Rename (ρ): operation used to give new names to relations or their attributes
(columns).
● Rename Relation: rho_NewRelationName (OldRelationName)
● Rename Attributes: rho_NewAttrName1,NewAttrName2,... (RelationName)
Page 23
● Query Questions (Relational Algebra Examples)
● Q1: Find those who are involved with this University. (Student, Faculty, Staff)
○ Involved_people = π_name(Student) ∪ π_name(Faculty) ∪ π_name(Staff)
●
● Q2: Find those who are student as well as faculty.
○ Student_and_Faculty = π_name(Student) ∩ π_name(Faculty)
●
● Q3: Find those faculties who are graduated from our university.
○ (Assumes an Alumni table or similar) π_name(Faculty) ∩ π_name(Alumni)
●
● Q4: Find those students who joined as Faculty members.
○ Students_As_Faculty = π_name(Student) ∩ π_name(Faculty)
●
● Q5: Find those faculties who are not from this University.
○ E = π_name(σ_{university_name != 'Metropolitan'} (Faculty))
●
Page 24
● Q6: Find all female students:
○ Female_Students = π_name(σ_{gender='Female'} (Student))
●
● Q7: Find all Cities from where students are coming to our university.
○ E = π_city(σ_{university_name='Metropolitan'} (Student))
●
● Q8: Find those Cities from faculty members are joining to our university.
○ E = π_city(σ_{university_name='Metropolitan'} (Faculty))
●
● Q9: Find all Students who carry A+ blood group.
○ E = π_name(σ_{blood_group='A+'} (Student))
●
● Q10: find the male students who carry AB+ blood group.
○ E = π_name(σ_{gender='Male' AND blood_group='AB+'} (Student))
●
Page 25
● (Schema context: Customer, Depositor, Account, Branch)
● Q.11: Find the name of those customer who have deposited into Zindabazar
branch.
○ E1 = σ_{Branch_name='Zindabazar'} (Account)
○ E2 = Customer ⋈ Depositor ⋈ E1
○ Result = π_{customer_name} (E2)
●
● Q.12: Find those customer who didn't deposited money yet.
○ Result = π_{name}(Customer) - π_{name}(π_{customerID}(Depositor) ⋈ Customer)
●
● Q.13: Find the Student who's roll number is 22 and age is 21.
○ E = σ_{roll_number=22 AND age=21} (Student)
●
● Q.14: Find the student name who is Student but not an employee.
○ Result = π_{name}(Student) - π_{name}(Employee)
●
Page 26
● Find all students and employees.
○ E = π_{name}(Student) ∪ π_{name}(Employee)
●
● Find all customers of the bank who have an account but not a loan.
○ (Schema Context: Customer, Depositor, Account, Borrower, Loan, Branch)
○ E1 = π_{customerID} (Depositor) -- (Customers with accounts, assuming
Depositor links Customer and Account)
○ E2 = π_{customerID} (Borrower) -- (Customers with loans)
○ Result_IDs = E1 - E2
○ Final_Result = π_{customerName} (Result_IDs ⋈ Customer)
●
Page 27
● Schema Reiteration (Batch-1&2)
○ tbl-account (<u>accountNo</u>, Br_name, ac_balance, ac_type) --
(Br_name likely should be branchID FK)
○ tbl-customer (<u>customerName</u>, cs-street, cs-city) --
(customerID likely PK)
○ tbl-branch (<u>br_name</u>, br_city, br_assets) -- (branchID likely
PK)
○ tbl-depositor (<u>CustomerName*</u>, <u>accountNo*</u>)
○ tbl-loan (<u>loanNo</u>, br_name, ln_amount) -- (br_name likely
should be branchID FK)
○ tbl-employee (<u>employeeName</u>, br_name, salary) -- (br_name
likely should be branchID FK)
●
● Simple ER Diagram sketch showing linkages between Account, Branch,
Employee, Depositor, Loan, Customer, Borrower.