Okay, I will transcribe the content from the PDF file (CamScanner 04-18-2025 19.36.
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
CREATE DATABASE db_bank_tasnia;
●
●
● # showing all database
● SQL
SHOW DATABASES;
●
●
● # creating table/relation in database
● SQL
CREATE TABLE tbl_branch (
branch_ID INT,
br_name VARCHAR(255),
br_city VARCHAR(255),
br_assets DECIMAL -- (Assuming based on context, type not fully clear)
);
●
●
Page 2
● # create table with foreign key constraint
● SQL
CREATE TABLE Enrollment (
enrollment_id INT PRIMARY KEY,
Student_id INT,
course_id INT,
FOREIGN KEY (Student_id) REFERENCES students (Student_id),
FOREIGN KEY (course_id) REFERENCES Courses (course_id) -- (Assuming course_id
references a Courses table)
);
●
●
● # students Table
● SQL
CREATE TABLE students (
students_id INT PRIMARY KEY,
student_name VARCHAR(200) NOT NULL,
student_age INT
);
●
●
● # Course Table
● SQL
CREATE TABLE Courses (
course_id INT PRIMARY KEY,
course_name VARCHAR(200) NOT NULL
);
●
●
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 ADD CONSTRAINT PK_Student PRIMARY KEY (students_id); --
(Assuming students_id is the column)
●
●
● # Removing primary key
● 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 Enrollment
ADD FOREIGN KEY (Student_id) REFERENCES Students(students_id)
ON DELETE CASCADE
ON UPDATE CASCADE;
●
●
● # Removing a foreign key
● SQL
ALTER TABLE Enrollment
DROP CONSTRAINT <ForeignKeyName>; -- (Replace <ForeignKeyName> with the actual
constraint name)
-- Note seems to incorrectly reference PK_Student here. It should be the FK constraint name.
●
●
Page 4
● # Modify data types of a column
● SQL
ALTER TABLE Students MODIFY age SMALLINT; -- (Syntax might be MODIFY COLUMN
depending on DBMS)
●
●
● # Rename a column
● SQL
ALTER TABLE Students RENAME COLUMN age TO student_age;
●
●
● # Remove a column from a table
● SQL
ALTER TABLE Students DROP COLUMN email;
●
●
● # Delete a table permanently
● SQL
DROP TABLE Students;
●
●
● # Delete a database permanently
● SQL
DROP DATABASE University_DB; -- (Assuming DB name)
●
●
Page 5
● TRUNCATE
○ Deletes all data inside a table but keeps its structure intact.
●
● # remove all records from table but keep its structure
● SQL
TRUNCATE TABLE Students;
●
●
● RENAME
○ Rename a database object
●
● # rename table
● 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
COMMENT ON TABLE Student_Records IS 'This table stores student information';
●
●
● DESCRIBE
● SQL
-- (Shows table structure)
DESCRIBE Student_Records;
●
●
Page 6
● # Show all the tables in a database
● SQL
SHOW TABLES; -- (Or query information_schema depending on DBMS)
●
●
● # Show attributes (columns) in a table
● SQL
DESC tbl_branch;
●
●
● # Showing all the values from a table
● SQL
SELECT * FROM tbl_branch;
●
●
● # Check structure of a table - includes column names, data types, constraints
● SQL
DESCRIBE Students;
-- OR
SHOW CREATE TABLE Students;
●
●
● # Create a table from an existing table structure (with data)
● SQL
CREATE TABLE student_copy AS SELECT * FROM Students;
●
●
● # Copy only structure of a table without data
● SQL
CREATE TABLE student_structure AS SELECT * FROM Students WHERE 1=0;
-- OR (Often preferred)
CREATE TABLE student_structure LIKE Students;
●
●
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
ALTER TABLE Students
MODIFY student_name VARCHAR(50) NOT NULL;
●
●
● # UNIQUE
● SQL
ALTER TABLE Students
ADD CONSTRAINT unique_email UNIQUE (email);
●
●
● # CHECK
● SQL
ALTER TABLE Students
ADD CONSTRAINT check_age CHECK (age >= 18); -- (Assuming constraint)
●
●
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
ALTER TABLE Employees
MODIFY COLUMN Salary DECIMAL(15, 2);
●
●
5. Drop a Table and its associated Constraints <!-- 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
ALTER TABLE Employees
ADD COLUMN hire_date DATE;
●
●
5. Add a default value to a column <!-- end list -->
● SQL
ALTER TABLE Employees
ALTER COLUMN Status SET DEFAULT 'Active';
●
●
Page 9
7. Create a table with constraints and default values <!-- end list -->
● SQL
CREATE TABLE Students (
Student_id INT PRIMARY KEY,
name VARCHAR(100) NOT NULL,
age INT CHECK (age >= 18),
email VARCHAR(100) UNIQUE,
enrollment_date DATE DEFAULT CURRENT_DATE
);
●
●
7. Create a table with Auto increment column <!-- end list -->
● SQL
CREATE TABLE users (
user_id INT AUTO_INCREMENT PRIMARY KEY,
username VARCHAR(50) NOT NULL
);
●
●
Page 10
● DML (Data Manipulation Language)
● Four primary DML commands: INSERT, UPDATE, DELETE, SELECT
● INSERT Statement - Add new records to a table
● SQL
INSERT INTO Students (student_id, student_name, age)
VALUES (1, 'Alice', 20),
(2, 'Bob', 20),
(3, 'Tom', 30); -- (Assuming 'Tom' was intended name)
●
●
● UPDATE Statement - modify existing records
● SQL
UPDATE Students
SET age = 21
WHERE student_id = 1;
●
●
● DELETE Statement - Remove records from a table
● SQL
DELETE FROM students
WHERE student_id = 1;
●
●
● SELECT Statement - Retrieve data from a table
● SQL
SELECT * FROM Student;
●
●
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
SELECT br_name, br_city, br_street FROM tbl_branch; -- (Assuming br_street exists)
●
●
● Q.2: Find the name of all branches along with their assets
● SQL
SELECT br_name, br_assets FROM tbl_branch;
●
●
Page 12
● Q.3: Find the details of all branches order by branch name/ branch city/ assets
● SQL
SELECT * FROM tbl_branch ORDER BY br_city, br_name, br_assets DESC;
●
●
● Q.4: Find those cities where all branches are located.
● SQL
SELECT DISTINCT br_city FROM tbl_branch; [cite: 21]
●
●
● Q.5: Find those branches along with their assets located in Sylhet city sorted by
asset value.
● 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
SELECT * -- (Or specific columns like br_assets)
FROM tbl_branch
WHERE br_city LIKE '%Bazar%' -- (Assuming city name in br_city)
AND br_assets > 90000000 AND br_assets < 500000000;
●
●
● Q.8: Find the average account balance at the Zindabazar branch.
● SQL
-- Method 1: Subquery
SELECT AVG(ac_amount)
FROM tbl_account
WHERE branchID = (SELECT branchID FROM tbl_branch WHERE br_name = 'Zindabazar');
-- Method 2: Join (Shown on next page in notes)
●
●
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
SELECT b.br_name, COUNT(DISTINCT d.customerID) AS total_depositors -- Assuming count
distinct customers
FROM tbl_branch AS b
JOIN tbl_account AS a ON b.branchID = a.branchID
JOIN tbl_depositor AS d ON a.accountNo = d.accountNo
GROUP BY b.br_name
ORDER BY b.br_name;
●
●
Page 15
● Q.10: Find the total number of accounts for each branch.
● SQL
SELECT b.br_name, COUNT(a.accountNo) AS totalAccounts
FROM tbl_account AS a
JOIN tbl_branch AS b ON a.branchID = b.branchID
GROUP BY b.br_name
ORDER BY b.br_name;
●
●
● Q.11: Find the total number of accounts for each branch which has more than
200 accounts.
● SQL
SELECT b.br_name, COUNT(a.accountNo) AS totalAccounts
FROM tbl_account AS a
JOIN tbl_branch AS b ON a.branchID = b.branchID
GROUP BY b.br_name
HAVING COUNT(a.accountNo) > 200 -- (Uses HAVING for filtering after aggregation)
ORDER BY totalAccounts;
●
●
Page 16
● Q.12: Find customer details of those customers who deposited money on
Zindabazar 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
CREATE VIEW vw_totalBranch AS (
SELECT b.br_name AS branchName, COUNT(a.accountNo) AS totalAccount
FROM tbl_account AS a
JOIN tbl_branch AS b ON a.branchID = b.branchID
GROUP BY b.br_name
-- HAVING totalAccount > 200 -- (This seems to be from Q11, maybe included in view?)
-- ORDER BY totalAccount -- (ORDER BY usually not in VIEW definition unless TOP/LIMIT)
);
●
●
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.