SQL Join and Architecture
Uploaded image
give me all answers
Here are answers to the questions provided in the uploaded image:
Group B (Short Answer Questions)
Answer any three of the following:
1. Briefly explain different JOIN operations with examples.
INNER JOIN: Combines rows from two tables where there is a match in both tables.
Example:
sql
SELECT employees.name, departments.dept_name
FROM employees
INNER JOIN departments ON employees.dept_id = departments.dept_id;
LEFT JOIN: Returns all rows from the left table and matched rows from the right table.
Example:
sql
SELECT employees.name, departments.dept_name
FROM employees
LEFT JOIN departments ON employees.dept_id = departments.dept_id;
RIGHT JOIN: Returns all rows from the right table and matched rows from the left table.
FULL OUTER JOIN: Combines results of both LEFT and RIGHT JOIN.
2. Explain the three-tier architecture. What is the difference between two-tier and three-tier
architecture?
Three-tier architecture:
1. Presentation Layer: User interface.
2. Application Layer: Business logic.
3. Database Layer: Data storage.
Difference:
In a two-tier architecture, the application directly interacts with the database.
In a three-tier architecture, there is an intermediate layer (application server) between
the user and database.
Printed using Save ChatGPT as PDF, powered by PDFCrowd HTML to PDF API. 1/9
3. Define Optimistic Concurrency Control with an example.
It assumes conflicts are rare and checks for conflicts before committing a transaction.
Example: A user reads data, makes changes, and checks if data was altered before
committing.
4. Define entity. Explain strong and weak entities.
Entity: A real-world object represented in a database.
Strong entity: Has a primary key (e.g., Employee).
Weak entity: Depends on a strong entity and has no primary key of its own (e.g., Dependent).
5. What is primary indexing and secondary indexing?
Primary indexing: Index on the primary key, ensures uniqueness.
Secondary indexing: Index on non-primary key attributes for faster querying.
Group C (Long Answer Questions)
Answer any three of the following:
7. How does a B-tree differ from a B+ tree? Construct a B+ tree.
B-tree: Contains keys and data in internal and leaf nodes.
B+ tree: Keys in internal nodes, data only in leaf nodes (linked for sequential access).
Example Construction: [Key values: 3, 10, 12, 14, 29, 38, 45, 55, 60, 68].
8. Relational database query expressions:
Given:
scss
Employee(ename, street, city)
Works(ename, company_name, salary)
Company(company_name, city)
Manages(ename, manager_name)
a. Find names of employees working for "First Bank Corporation":
sql
SELECT ename
FROM Works
WHERE company_name = 'First Bank Corporation';
b. Find names of employees living in the same city and street as their managers:
sql
SELECT E1.ename
FROM Employee E1, Employee E2, Manages M
WHERE E1.street = E2.street AND E1.city = E2.city AND E1.ename = M.manager_name;
c. Employees earning more than the average salary:
Printed using Save ChatGPT as PDF, powered by PDFCrowd HTML to PDF API. 2/9
sql
SELECT ename, salary
FROM Works
WHERE salary > (SELECT AVG(salary) FROM Works);
d. Employees whose names contain a "T":
sql
SELECT ename
FROM Employee
WHERE ename LIKE '%T%';
9. Concept of sub-query and differences among DROP, TRUNCATE, and DELETE:
Sub-query: A query within another query.
DROP: Removes the table entirely.
TRUNCATE: Deletes all rows but keeps the structure.
DELETE: Deletes specific rows.
10. Difference between DBMS and RDBMS:
DBMS: No relationships (e.g., file systems).
RDBMS: Supports relational models.
Entity Integrity and Referential Integrity:
Entity Integrity: Primary key ensures uniqueness.
Referential Integrity: Foreign keys maintain consistency.
Why ER model is used:
Represents data and relationships graphically.
Let me know if you'd like a more detailed answer to any specific question!
Uploaded image
give me all answers
Here are the answers to the questions from the uploaded image:
Group B (Short Answer Questions)
Answer any three of the following:
Printed using Save ChatGPT as PDF, powered by PDFCrowd HTML to PDF API. 3/9
1. What is a View? How do you create an Insertable and Updatable view?
View: A virtual table derived from one or more base tables. It does not store data itself.
Insertable and Updatable View:
Use the `CREATE VIEW` statement with `WITH CHECK OPTION` to ensure only valid data is
inserted or updated.
Example:
sql
CREATE VIEW view_name AS
SELECT column1, column2
FROM table_name
WHERE condition
WITH CHECK OPTION;
2. What is a Database? Advantages of DBMS over File Oriented System?
Database: A collection of organized data that can be accessed and managed easily.
Advantages of DBMS:
1. Reduces data redundancy.
2. Ensures data integrity.
3. Centralized control of data.
4. Supports concurrent access.
3. Explain various types of Data Models in DB systems.
Hierarchical Model: Data organized in a tree-like structure.
Network Model: Uses graph structure to represent relationships.
Relational Model: Data in tables (rows and columns).
Entity-Relationship Model (ER): Graphical representation of entities and their relationships.
4. What is the Degree of Relationships?
Refers to the number of entities participating in a relationship.
Unary: Single entity type.
Binary: Two entities.
Ternary: Three entities.
5. What is 2NF? Give Example.
Second Normal Form (2NF): A table is in 2NF if:
1. It is in 1NF.
2. All non-key attributes are fully functionally dependent on the primary key.
Example:
plaintext
Table: Student(Course_ID, Student_ID, Course_Name)
Problem: Course_Name depends only on Course_ID, not the entire key (Course_ID, Stu
dent_ID).
Solution: Split into two tables:
Course(Course_ID, Course_Name)
Enrollment(Course_ID, Student_ID)
Group C (Long Answer Questions)
Printed using Save ChatGPT as PDF, powered by PDFCrowd HTML to PDF API. 4/9
Answer any three of the following:
6. Functional Dependencies and Closures:
(a) Trivial Functional Dependencies:
A functional dependency is trivial if the dependent attribute is a subset of the determinant.
Example: `A → A`, `AB → A`.
(b) Armstrong's Axioms:
1. Reflexivity: If B ⊆ A, then A → B.
2. Augmentation: If A → B, then AC → BC.
3. Transitivity: If A → B and B → C, then A → C.
(c) Closure of FDs:
Given FDs for R(A, B, C, D, E):
mathematica
A → BC
C → E
B → D
E → A
Closure of {A}: {A, B, C, D, E}.
Candidate Key: `{A}`.
7. Normalization:
Why is normalization necessary?
Reduces redundancy.
Improves data integrity.
Avoids update anomalies.
BCNF vs 3NF:
BCNF: Every determinant is a candidate key.
3NF: Allows non-prime attributes to depend on other non-prime attributes if transitive
dependency exists.
Normalize Relation to 3NF:
Follow decomposition based on FDs to eliminate partial and transitive dependencies.
8. Trigger, Stored Procedure, Database Cursor:
Trigger: Executes automatically in response to events like `INSERT`, `UPDATE`, or `DELETE`.
Stored Procedure: Predefined SQL statements that can be executed repeatedly.
Cursor: Pointer to retrieve rows from a result set one at a time.
Example:
sql
DECLARE cursor_name CURSOR FOR SELECT * FROM table_name;
Additional Questions:
Printed using Save ChatGPT as PDF, powered by PDFCrowd HTML to PDF API. 5/9
9. SQL Built-in Functions:
Aggregate Functions: `COUNT`, `SUM`, `AVG`, `MIN`, `MAX`.
Constraints: Ensure data integrity (`NOT NULL`, `UNIQUE`, `FOREIGN KEY`).
Cascading Updates and Deletes: Automatic updates or deletions in dependent tables when
changes occur in parent tables.
10. ERD for Online Movie Ticket Booking System:
Entities: Customer, Movie, Theater, Booking, Payment.
Relationships:
Customer books a Ticket.
Movie shown in Theater.
Payment made for Booking.
11. DDL, DML, DCL:
DDL (Data Definition Language): `CREATE`, `ALTER`, `DROP`.
DML (Data Manipulation Language): `SELECT`, `INSERT`, `UPDATE`, `DELETE`.
DCL (Data Control Language): `GRANT`, `REVOKE`.
Let me know if you want detailed ER diagrams or SQL examples!
Name and briefly describe the five SQL built-in functions?
Define and discuss data constraints?
What is a cascading update and delete?
Five SQL Built-in Functions
SQL provides many built-in functions for data manipulation and processing. Here are five key types:
1. Aggregate Functions: Perform operations on a set of values and return a single result.
`COUNT()`: Counts the number of rows.
sql
SELECT COUNT(*) FROM employees;
`SUM()`: Calculates the total of a numeric column.
sql
SELECT SUM(salary) FROM employees;
`AVG()`: Calculates the average of a numeric column.
Printed using Save ChatGPT as PDF, powered by PDFCrowd HTML to PDF API. 6/9
sql
SELECT AVG(salary) FROM employees;
`MIN()`: Returns the smallest value in a column.
sql
SELECT MIN(salary) FROM employees;
`MAX()`: Returns the largest value in a column.
sql
SELECT MAX(salary) FROM employees;
2. String Functions: Operate on string data.
`UPPER()`: Converts a string to uppercase.
sql
SELECT UPPER(name) FROM employees;
`LOWER()`: Converts a string to lowercase.
`CONCAT()`: Concatenates two or more strings.
3. Date and Time Functions: Manipulate date and time data.
`NOW()`: Returns the current date and time.
sql
SELECT NOW();
`DATE()`:
Extracts the date part of a date-time value.
4. Mathematical Functions: Perform arithmetic operations.
`ROUND()`: Rounds a number to the specified number of decimal places.
sql
SELECT ROUND(123.456, 2);
`ABS()`:Returns the absolute value of a number.
5. Conversion Functions: Convert data from one type to another.
`CAST()`: Converts data to a specified type.
Printed using Save ChatGPT as PDF, powered by PDFCrowd HTML to PDF API. 7/9
sql
SELECT CAST(salary AS CHAR) FROM employees;
`CONVERT()`: Converts data and formats it.
Data Constraints
Data constraints ensure data integrity and accuracy in SQL tables. Common types include:
1. NOT NULL: Ensures a column cannot have a NULL value.
sql
CREATE TABLE employees (
id INT NOT NULL,
name VARCHAR(50) NOT NULL
);
2. UNIQUE: Ensures all values in a column are unique.
sql
CREATE TABLE employees (
email VARCHAR(100) UNIQUE
);
3. PRIMARY KEY: Combines `NOT NULL` and `UNIQUE`, uniquely identifying a row.
sql
CREATE TABLE employees (
id INT PRIMARY KEY
);
4. FOREIGN KEY: Ensures a column's value matches the primary key in another table.
sql
CREATE TABLE orders (
order_id INT,
employee_id INT,
FOREIGN KEY (employee_id) REFERENCES employees(id)
);
5. CHECK: Ensures values meet a specific condition.
Printed using Save ChatGPT as PDF, powered by PDFCrowd HTML to PDF API. 8/9
sql
CREATE TABLE employees (
salary INT CHECK (salary > 0)
);
Cascading Updates and Deletes
1. Cascading Update: Automatically updates foreign key values in child tables when the
corresponding primary key value in the parent table changes.
Example:
sql
CREATE TABLE employees (
id INT PRIMARY KEY
);
CREATE TABLE orders (
order_id INT,
employee_id INT,
FOREIGN KEY (employee_id) REFERENCES employees(id)
ON UPDATE CASCADE
);
If `id` in the `employees` table is updated, the `employee_id` in the `orders` table is
automatically updated.
2. Cascading Delete: Automatically deletes rows in child tables when the corresponding row in the
parent table is deleted.
Example:
sql
CREATE TABLE employees (
id INT PRIMARY KEY
);
CREATE TABLE orders (
order_id INT,
employee_id INT,
FOREIGN KEY (employee_id) REFERENCES employees(id)
ON DELETE CASCADE
);
If an employee is deleted from the `employees` table, all their orders in the `orders` table are
also deleted.
Let me know if you'd like additional examples or clarification!
Printed using Save ChatGPT as PDF, powered by PDFCrowd HTML to PDF API. 9/9