PD-SUM
PD-SUM
Mid–1
• Primary Key: A column or a set of columns that uniquely identifies each row in a
table. It ensures data integrity by preventing duplicate and NULL values.
• Foreign Key: A column in one table that references the Primary Key of another table,
establishing a relationship between the two tables.
Real-world Example
student_name VARCHAR(100),
dob DATE
);
course_name VARCHAR(100)
);
student_id INT,
course_id INT,
);
Example
-- Insert data
-- Update data
-- Delete data
Example
1 John HR NY
2 Alice IT LA
3 Bob HR NY
Here, "HR" is repeated for multiple employees. Normalization splits it into two tables:
Employees Table
1 John 101
2 Alice 102
3 Bob 101
Departments Table
101 HR NY
102 IT LA
Example
emp_name VARCHAR(100),
designation VARCHAR(50),
doj DATE,
salary DECIMAL(10,2),
address VARCHAR(255)
);
b) Retrieve emp_id, designation, salary, address where salary < 100000 or address =
'Khulna'
Here’s how you can create a Relational Database in Microsoft Access named employee
and complete the required tasks.
o doj (Date/Time)
o salary (Currency)
4 Sarah 2018-08-25 HR
4. Add Criteria:
o Salary: >=50000
o Address: "Dhaka"
SQL Equivalent
Primary Key:
A Primary Key is a unique identifier for a record in a table. It ensures that no two rows have
the same value for this column. The primary key cannot contain NULL values and must be
unique.
Example:
Consider a “Students” table:
101 John 22
102 Alice 21
103 Bob 23
Here, Student_ID is the Primary Key because it uniquely identifies each student.
Foreign Key:
A Foreign Key is a column that establishes a relationship between two tables. It references
the Primary Key of another table.
Example:
Consider a Courses table that links students to the courses they enroll in:
Example:
INSERT INTO Students (Student_ID, Name, Age) VALUES (104, 'Emma', 22);
Example:
Example:
DDL is a category of SQL commands that define and modify database structures. It
includes:
Name VARCHAR(50),
Salary DECIMAL(10,2)
);
b) Data Dictionary:
Example:
In an RDBMS, system tables store metadata, such as:
• Table names
Question No. 1
1. Numeric Types:
2. Sequence Types:
3. Set Types:
4. Mapping Type:
5. Boolean Type:
6. Binary Types:
# Numeric Types
x = 10 # int
y = 3.14 # float
z = 2 + 3j # complex
# Sequence Types
# Boolean
status = True
print(type(unique_values), type(person))
print(type(status))
Question No. 2
Data Manipulation Language (DML) is a subset of SQL used to retrieve and manipulate
data.
2. INSERT INTO employees (id, name, salary) VALUES (1, 'Alice', 50000);
Mapping cardinalities define how many instances of one entity can be associated with
instances of another entity.
1. One-to-One (1:1)
2. One-to-Many (1:M)
3. Many-to-One (M:1)
4. Many-to-Many (M:N)
o A student can enroll in many courses, and each course has many students.
Question No. 3
Usage Used for Used when Used for key- Used to store unique
storing a data should value mappings. values efficiently.
sequence of not change.
elements.
A Database Management System (DBMS) is software that allows users to create, manage,
and manipulate databases efficiently. It provides data storage, retrieval, security, integrity,
and backup functionalities.
Why is Relational DBMS (RDBMS) Better than Hierarchical and Network DBMS?
Ease of Use Complex to navigate Complex due to Easy to use with SQL
and update. multiple relationships. for queries.
Scalability Difficult to scale due Better scalability than Highly scalable and
to rigid structure. hierarchical. widely used.
5. Data Integrity & Security – Enforces constraints like primary keys and foreign keys.
Thus, RDBMS is better than hierarchical and network DBMS due to its ease of use, reduced
redundancy, scalability, and efficient query execution
FERDOUS HASSAN MIHAD 17
(b) Identifying Super Key(s), Candidate Key(s), Primary Key(s), Alternate Key(s),
and Composite Key(s)
1. Super Key
A super key is a set of one or more attributes that uniquely identify a record in a table. It
may contain extra attributes that are not necessary for uniqueness.
Examples:
Key Point: Every candidate key is a super key, but not every super key is a candidate
key.
2. Candidate Key
Examples:
• {Name, Phone} (If a name alone isn’t unique, but a combination of name and phone
number is, then this is a candidate key)
Key Point: A table can have multiple candidate keys, but only one is chosen as the
primary key.
A primary key is the candidate key that is selected to uniquely identify records in a table. It
ensures no duplicate or NULL values.
Example:
• {EID} (Chosen as the primary key because it's always unique and not NULL)
4. Alternate Key
An alternate key is any candidate key that is not chosen as the primary key.
Examples:
• {NID} (If EID is chosen as the primary key, NID remains an alternate key)
• {Name, Phone} (If unique, but not the primary key, it is an alternate key)
Key Point: Alternate keys are still unique but are not used as the main identifier.
5. Unique Key
A unique key is similar to a primary key but allows NULL values (unlike primary keys, which
must always be filled).
Examples:
• {NID} (If NID is unique but not mandatory, it can be a unique key)
• {Name, Phone} (If this combination must be unique but is not the primary key)
Key Point: A table can have multiple unique keys, but NULL values are allowed.
6. Composite Key
A composite key is a key that consists of two or more attributes together to uniquely identify
a record.
Examples:
• {Name, Phone} (If Name alone is not unique but combined with Phone, it becomes
unique)
Key Point: A composite key is used when no single attribute can uniquely identify a
row.
7. Foreign Key
A foreign key is an attribute in one table that establishes a link between two tables by
referencing a primary key in another table. It ensures referential integrity.
Example:
Consider two tables:
Employees Table
1 Alex 0123456789
2 Bob 0987654321
Salaries Table
101 1 50,000
102 2 40,000
Here, EID in the Salaries Table is a foreign key referring to EID in the Employees Table.
Key Point: A foreign key creates a relationship between tables and maintains data
integrity.
Super Key A set of attributes that uniquely {EID}, {EID, Name}, {EID, Phone},
identify a record. {Name, Phone}
Alternate Any candidate key that is not the {NID}, {Name, Phone}
Key primary key.
Unique Key A key that ensures uniqueness but {NID}, {Name, Phone}
allows NULL values.
Foreign Key A key that links two tables by EID in Salaries Table referencing
referencing a primary key. EID in Employees Table
Given Table
Super Key A set of attributes that {ID}, {Email}, Any column or set of columns
uniquely identify a row. {ID, Name}, {ID, that uniquely identifies a row.
Salary}
Candidate A minimal super key {ID}, {Email} ID and Email are unique for
Key (no redundant each row, and no unnecessary
attributes). columns are included.
Alternate A candidate key that is {Email} Since Email is also unique but
Key not chosen as the not chosen as the primary key,
primary key. it is an alternate key.
Composite A key formed by two or {Name, Email} If ID were not available, Name +
Key more attributes. Email together could uniquely
identify records.
Final Answer
1. Cardinality Constraint:
o Defines how many instances of one entity can be associated with instances of
another entity.
o Example: A customer can place multiple orders (1:N), but each order
belongs to one customer.
2. Participation Constraint:
• Entities:
o Order – Product (M:N) → One order can have multiple products, and a
product can be in multiple orders.
Primary Keys:
• Customer: Customer_ID
• Order: Order_ID
• Product: Product_ID
Question No. 6
(a) What is Normalization? Why is 5th Normal Form Better than 4th Normal Form?
USE Company;
First_Name VARCHAR(50),
Last_Name VARCHAR(50),
Salary DECIMAL(10,2)
);
a) What is an Attribute?
o Name (Attribute)
o Age (Attribute)
ER Diagram Example:
c) Mapping Cardinalities
Sdkfjkaksjdfa
Question #04
The JOIN clause in SQL is used to combine rows from two or more tables based on a related
column.
FROM employees
ON employees.department_id = departments.department_id;
2. LEFT JOIN (LEFT OUTER JOIN): Returns all records from the left table and matching
records from the right.
FROM customers
ON customers.customer_id = orders.customer_id;
3. RIGHT JOIN (RIGHT OUTER JOIN): Returns all records from the right table and
matching records from the left.
FROM employees
ON employees.department_id = departments.department_id;
4. FULL JOIN (FULL OUTER JOIN): Returns all records from both tables, with NULL
where there is no match.
ON employees.department_id = departments.department_id;
FROM customers
1. HAVING Clause
2. SQL Subqueries
SELECT name, salary FROM employees WHERE salary > (SELECT AVG(salary) FROM
employees);
3. ORDER BY Clause
4. SQL Aliases
employee_name VARCHAR(100),
employee_location VARCHAR(100)
);
SELECT sales, sales_person, trnx_id, region FROM transactions WHERE sales > 10000;
INSERT INTO employees (employee_id, name, department) VALUES (1, 'Alice', 'HR');
A Relational Database Management System (RDBMS) stores data in tables with rows and
columns. It organizes data using relationships between tables using keys.
1 John 101
2 Alice 102
Department_ID Department_Name
101 HR
102 Finance
Conclusion
• Efficient Data Retrieval: Fetch required data quickly from large datasets.
History of SQL:
SQL (Structured Query Language) was developed in the 1970s by IBM as part of their
System R project. Later, Oracle became the first company to commercialize SQL-based
RDBMS. SQL was standardized by ANSI in 1986 and ISO in 1987.
Features of RDBMS:
Popular RDBMS:
• MySQL
• PostgreSQL
• Oracle Database
• IBM Db2
• MongoDB (Document-based)
A JOIN in SQL is used to retrieve data from multiple tables based on a related column.
Types of JOINs:
2. LEFT JOIN – Returns all records from the left table and matching records from the
right table.
3. RIGHT JOIN – Returns all records from the right table and matching records from the
left table.
4. FULL JOIN – Returns all records when there is a match in either table.
Each table can have only one primary key. A table can have multiple foreign keys.
name VARCHAR(100)
);
customer_id INT,
);
customer_name VARCHAR(100),
customer_location VARCHAR(100)
);
SELECT sales, sales_person, trnx_id, region FROM transactions WHERE sales > 10000;
Key Advantages:
2. Flexibility – Can store different types of data (JSON, key-value pairs, documents,
graphs).
5. Handles Big Data – Ideal for real-time analytics, IoT, and recommendation systems.
• Neo4j (Graph database) – Used for social networks and recommendation engines.
Components of an ERD:
| | |
• 1NF (First Normal Form) – Ensures each column contains atomic (indivisible)
values.
• 2NF (Second Normal Form) – Removes partial dependencies; ensures each column
depends on the primary key.
Example:
Before Normalization (1NF Violation):
---------------------------------------------
After Normalization:
Orders Table:
-----------------------------------
1001 |1 | 2025-04-02
1002 |2 | 2025-04-03
Order_Items Table:
Order_ID | Product_ID
----------------------
1001 | 101
1001 | 102
1002 | 103
1. Prevents Orphan Records – Stops deletion of parent records that have related child
records.
2. Ensures Data Consistency – Foreign key values must match an existing primary key.
name VARCHAR(100)
);
customer_id INT,
order_date DATE,
);
If a customer is deleted, all their associated orders will also be deleted (ON DELETE
CASCADE).
• Non-relational databases are useful for handling big data, unstructured data, and
real-time applications.
This ensures a well-structured, scalable, and reliable database system. Let me know if you
need further clarifications!