0% found this document useful (0 votes)
3 views

Unit 2

The document covers key concepts in database management systems, focusing on Relational Algebra and Relational Calculus, which are foundational for data retrieval and manipulation. It explains SQL command types including DDL, DML, DCL, TCL, and DQL, along with examples and differences between queries and subqueries. Additionally, it discusses aggregate functions used to summarize data, providing syntax and examples for each function.

Uploaded by

officialtaeliens
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
3 views

Unit 2

The document covers key concepts in database management systems, focusing on Relational Algebra and Relational Calculus, which are foundational for data retrieval and manipulation. It explains SQL command types including DDL, DML, DCL, TCL, and DQL, along with examples and differences between queries and subqueries. Additionally, it discusses aggregate functions used to summarize data, providing syntax and examples for each function.

Uploaded by

officialtaeliens
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 24

Unit 2

Question 1. Relational Algebra & Relational Calculus

Relational Algebra aur Relational Calculus DBMS ke core topics hain jo relational databases me data retrieval aur manipulation ke
theoretical foundation ko define karte hain:

Relational Algebra

Kya Hai?

Relational Algebra ek procedural query language hai jo database me data ko retrieve aur manipulate karne ke liye operations provide
karta hai. Iska matlab hai ki hume step-by-step batana padta hai ki data kaise aur kahan se retrieve karna hai.

Basic Operations in Relational Algebra

1. Selection (σ)

o Specific rows (tuples) ko filter karne ke liye use hota hai.

o Syntax:
σ<condition>(Table)

o Example:
Students table me Course = 'B.Tech' wale students dikhana:

Code:

σCourse='B.Tech'(Students)

2. Projection (π)

o Specific columns (attributes) ko retrieve karne ke liye use hota hai.

o Syntax:
π<columns>(Table)

o Example:
Name aur Course wale columns dikhana:

Code:

πName, Course(Students)

3. Union (∪)

o Do tables ke similar rows ko combine karta hai.

Table1 ∪ Table2
o Syntax:

o Example:
Students1 aur Students2 ka combined list.

4. Set Difference (−)


o Ek table ke rows jo dusre table me nahi hain, wo dikhata hai.

o Syntax:
Table1 − Table2

o Example:
Students jo Course1 me hain but Course2 me nahi.

5. Cartesian Product (×)

o Do tables ka cross-product karta hai, yani har row ko dusri table ki har row ke saath combine karta hai.

o Syntax:
Table1 × Table2

o Example:
Students aur Courses ka cartesian product.

6. Rename (ρ)

o Table ya column ka naam badalne ke liye use hota hai.

o Syntax:
ρ<new_name>(Table)

o Example:
Students table ko Enrolled rename karna:

Code:

ρEnrolled(Students)

7. Join

o Do tables ke related rows ko combine karta hai.

o Types:

 Theta Join (θ): Condition ke basis par join.

 Natural Join (⋈): Common attributes ke basis par join.

Relational Calculus

Kya Hai?

Relational Calculus ek non-procedural query language hai jo sirf batata hai ki data kya chahiye, kaise retrieve karna hai yeh specify
nahi karta.

Types of Relational Calculus

1. Tuple Relational Calculus (TRC)

o Conditions ke basis par tuples retrieve karte hain.

o Syntax:
{t | Condition(t)}
Yaha t ek tuple hai aur Condition specify karta hai ki kaunsa tuple select karna hai.

o Example:
Students table se Course = 'B.Tech' wale students:
Code:

{t | t ∈ Students ∧ t.Course = 'B.Tech'}

2. Domain Relational Calculus (DRC)

o Specific attributes ke values retrieve karte hain.

o Syntax:
{<attributes> | Condition}

o Example:
Name aur Course retrieve karna jaha Course = 'B.Tech':

Code:

{<Name, Course> | ∃t ∈ Students (t.Course = 'B.Tech')}

Difference Between Relational Algebra and Relational Calculus

Aspect Relational Algebra Relational Calculus

Type Procedural (Step-by-step batata hai) Non-Procedural (Sirf kya chahiye batata hai)

Focus Kaise data retrieve karna hai (How) Kya data chahiye (What)

Example Syntax πName(σCourse='B.Tech'(Students)) `{t

Ease of Use Algorithmic aur execution focus Logical aur condition focus

Short Example Comparison

Relational Algebra

Students table se Course = 'B.Tech' wale students ke Name retrieve karna:

Code:

πName(σCourse='B.Tech'(Students))

Relational Calculus

Students table me se Course = 'B.Tech' wale Name:

Code:

{t.Name | t ∈ Students ∧ t.Course = 'B.Tech'}

Short Summary

1. Relational Algebra: Step-by-step procedural approach.

2. Relational Calculus: Non-procedural, condition-based approach.

3. Exam ke liye: Definitions, basic operations, aur examples zaroor practice karein.

Conclusion
Relational Algebra aur Relational Calculus relational databases ke theoretical foundation hain. Dono approaches ka use database
queries likhne aur samajhne me hota hai.

Question 2. Types of SQL Commands

SQL Commands ke Types ek important topic hai jo database se interact karne ke liye use hoti hai. SQL commands ko 5 categories me
divide kiya jata hai: DDL, DML, DCL, TCL, aur DQL. Chaliye ise simple hinglish me samajhte hain:

1. DDL (Data Definition Language)

 Kya hai?
DDL commands database ki structure define aur modify karne ke liye use hoti hain. Iska kaam schema create karna aur
usme changes karna hota hai.

 Common Commands:

o CREATE: Naya database ya table banata hai.

Code:

CREATE TABLE Students (

Roll_No INT PRIMARY KEY,

Name VARCHAR(50),

Course VARCHAR(50)

);

o ALTER: Existing table me structure change karta hai (columns add/remove).

Code:

ALTER TABLE Students ADD Email VARCHAR(100);

o DROP: Table ya database ko permanently delete karta hai.

Code:

DROP TABLE Students;

o TRUNCATE: Table ke sare records delete karta hai, lekin structure safe rakhta hai.

Code:

TRUNCATE TABLE Students;

2. DML (Data Manipulation Language)

 Kya hai?
DML commands data ko insert, update, delete, aur retrieve karne ke liye use hoti hain. Ye data manipulation ke liye kaam
karti hain.

 Common Commands:

o INSERT: Naye records table me add karta hai.


Code:

INSERT INTO Students (Roll_No, Name, Course)

VALUES (101, 'Aman', 'B.Tech');

o UPDATE: Existing records ko modify karta hai.

Code:

UPDATE Students

SET Course = 'MBA'

WHERE Roll_No = 101;

o DELETE: Table se specific records delete karta hai.

Code:

DELETE FROM Students

WHERE Roll_No = 101;

3. DCL (Data Control Language)

 Kya hai?
DCL commands database me permissions aur access control handle karti hain.

 Common Commands:

o GRANT: Kisi user ko database access dena.

Code:

GRANT SELECT, INSERT ON Students TO User1;

o REVOKE: User se access rights wapas lena.

Code:

REVOKE INSERT ON Students FROM User1;

4. TCL (Transaction Control Language)

 Kya hai?
TCL commands transactions ko manage aur control karne ke liye use hoti hain.

 Common Commands:

o COMMIT: Changes ko permanently save karta hai.

Code:

COMMIT;

o ROLLBACK: Changes ko undo karta hai (wapis le jata hai).

Code:

ROLLBACK;
o SAVEPOINT: Transaction ke andar ek point set karta hai jaha se rollback possible hai.

Code:

SAVEPOINT sp1;

5. DQL (Data Query Language)

 Kya hai?
DQL commands database se data retrieve karne ke liye use hoti hain. Is category me sirf ek command aati hai:

o SELECT: Data ko retrieve karna.

Code:

SELECT * FROM Students;

 Specific columns:

Code:

SELECT Name, Course FROM Students;

 Condition ke sath:

Code:

SELECT * FROM Students WHERE Course = 'B.Tech';

Short Example:

Suppose humare paas ek Students table hai:

Code:

Roll_No | Name | Course

101 | Aman | B.Tech

102 | Priya | MBA

 DDL Example:

Code:

ALTER TABLE Students ADD Email VARCHAR(100);

 DML Example:

Code:

UPDATE Students SET Course = 'MCA' WHERE Roll_No = 102;

 DCL Example:

Code:

GRANT SELECT ON Students TO User2;

 TCL Example:

Code:
SAVEPOINT sp1;

ROLLBACK TO sp1;

 DQL Example:

Code: SELECT Name FROM Students WHERE Course = 'B.Tech';

Difference Table

Category Purpose Example Command

DDL Structure define/change karna CREATE, ALTER, DROP

DML Data insert/update/delete INSERT, UPDATE, DELETE

DCL Access control manage karna GRANT, REVOKE

TCL Transactions ko control karna COMMIT, ROLLBACK, SAVEPOINT

DQL Data retrieve karna SELECT

Conclusion

SQL commands database ke har aspect ko handle karne ke liye categorized hain. Exam ke liye examples aur syntax practice karein,
aur commands ka practical use samajhne ki koshish karein.

Question 3. Queries & SubQueries

Queries aur Subqueries DBMS ke essential concepts hain jo database se information retrieve karte hain:

Query Kya Hai?

 Query ek SQL statement hai jo database se specific data retrieve karne ke liye likhi jati hai.

 Queries ka kaam database se questions puchhna aur unka jawab lana hota hai.

Basic Query Example:

Code:

SELECT * FROM Students WHERE Course = 'B.Tech';

 Yaha Students table se un students ki details fetch ki gayi hain jo B.Tech course me enrolled hain.

Subquery Kya Hai?

 Subquery ek query hai jo dusri query ke andar likhi jati hai.

 Iska kaam inner query ka result outer query me use karna hota hai.

 Subqueries ko nested queries bhi kehte hain.


Example of Subquery:

Code:

SELECT Name FROM Students WHERE Roll_No IN

(SELECT Roll_No FROM Results WHERE Marks > 80);

 Inner query (SELECT Roll_No FROM Results...) un students ke roll numbers la rahi hai jinke marks 80 se zyada hain.

 Outer query un roll numbers ke names fetch kar rahi hai.

Types of Subqueries

1. Single Row Subquery:

o Ek row (single value) return karta hai.

o Example:

Code:

SELECT Name FROM Students

WHERE Roll_No =

(SELECT Roll_No FROM Toppers WHERE Rank = 1);

 Yaha inner query Toppers table se rank 1 wale student ka roll number de rahi hai.

2. Multiple Row Subquery:

o Multiple rows (values) return karta hai.

o Example:

Code:

SELECT Name FROM Students

WHERE Roll_No IN

(SELECT Roll_No FROM Results WHERE Marks > 80);

 Inner query multiple roll numbers return karegi jinke marks 80 se zyada hain.

3. Correlated Subquery:

o Subquery outer query ke har row ke liye alag execute hoti hai.

o Example:

Code:

SELECT Name FROM Students s

WHERE EXISTS

(SELECT * FROM Results r WHERE s.Roll_No = r.Roll_No AND r.Marks > 80);
 Yaha har student ke liye check hoga ki uska roll number Results table me marks > 80 ke saath exist karta
hai ya nahi.

Differences Between Query and Subquery

Aspect Query Subquery

Definition Independent SQL statement Dusri query ke andar likhi jati hai

Execution Directly execute hoti hai Inner query pe depend karta hai

Result Final output deta hai Result outer query ke liye input hota hai

Example SELECT * FROM Students; SELECT Name FROM Students WHERE Roll_No IN (SELECT Roll_No FROM Results);

Short Examples

Query Example:

Table: Students

Code:

Roll_No | Name | Course

101 | Aman | B.Tech

102 | Priya | MBA

103 | Rahul | B.Tech

Query:

Code:

SELECT Name FROM Students WHERE Course = 'B.Tech';

Output:

Aman, Rahul

Subquery Example:

Table: Results

Code:

Roll_No | Marks

101 | 85

102 | 70

103 | 90

Subquery:

Code:

SELECT Name FROM Students WHERE Roll_No IN


(SELECT Roll_No FROM Results WHERE Marks > 80);

Output:

Aman, Rahul

Key SQL Keywords for Subqueries

 IN: Specific values ko match karne ke liye.

 EXISTS: Check karta hai ki subquery ka result exist karta hai ya nahi.

 ANY/ALL: Comparison ke liye.

o Example:

code

SELECT Name FROM Students

WHERE Marks > ANY

(SELECT Marks FROM Results WHERE Course = 'B.Tech');

Nested Subquery Example

Multiple levels of subqueries bhi likhe ja sakte hain:

Code:

SELECT Name FROM Students

WHERE Roll_No IN

(SELECT Roll_No FROM Results

WHERE Marks >

(SELECT AVG(Marks) FROM Results));

 Yaha inner-most query Results table ka average marks nikalti hai.

 Middle query un roll numbers ko fetch karti hai jinke marks average se zyada hain.

 Outer query un roll numbers ke names fetch karti hai.

Conclusion

1. Query: Direct SQL command to fetch data.

2. Subquery: Query within another query for dynamic data retrieval.

3. Exam ke liye: Queries aur subqueries ke examples practice karein.

4. Practical implementation samajhne ke liye SELECT, IN, EXISTS, aur JOIN ka use seekhein.
Question 4. Aggregate Functions

Aggregate Functions DBMS me wo functions hain jo database table ke rows ke group par kaam karte hain aur ek single summary
value return karte hain, jaise ki sum, average, maximum, etc:

Aggregate Functions Kya Hain?

Aggregate functions ka kaam data ko summarize karna hota hai. Ye ek ya multiple rows ke data ko process karke ek single value
return karte hain.

Example Table: Students

Roll_No Name Marks

101 Aman 85

102 Priya 90

103 Rahul 75

104 Anjali 95

Common Aggregate Functions

1. COUNT()

 Kya karta hai?


Total rows ya specific values ki count batata hai.

 Syntax:

Code:

SELECT COUNT(*) FROM Table_Name;

 Example:

Code:

SELECT COUNT(*) FROM Students;

Output:

2. SUM()

 Kya karta hai?


Numeric column ka total (sum) nikalta hai.

 Syntax:

Code:

SELECT SUM(Column_Name) FROM Table_Name;


 Example:

Code:

SELECT SUM(Marks) FROM Students;

Output:

345

3. AVG()

 Kya karta hai?


Numeric column ka average calculate karta hai.

 Syntax:

Code:

SELECT AVG(Column_Name) FROM Table_Name;

 Example:

Code:

SELECT AVG(Marks) FROM Students;

Output:

86.25

4. MAX()

 Kya karta hai?


Numeric ya alphabetic column me se maximum value return karta hai.

 Syntax:

Code:

SELECT MAX(Column_Name) FROM Table_Name;

 Example:

Code:

SELECT MAX(Marks) FROM Students;

Output:

95

5. MIN()

 Kya karta hai?


Numeric ya alphabetic column me se minimum value return karta hai.

 Syntax:

Code:

SELECT MIN(Column_Name) FROM Table_Name;


 Example:

Code:

SELECT MIN(Marks) FROM Students;

Output:

75

GROUP BY Clause ke Saath Aggregate Functions

 Aggregate functions ko GROUP BY ke saath use karte hain jab hume data ko group karke summary chahiye hoti hai.

Example Table: Students

Roll_No Name Course Marks

101 Aman B.Tech 85

102 Priya MBA 90

103 Rahul B.Tech 75

104 Anjali MBA 95

Query:

Code:

SELECT Course, AVG(Marks) AS Average_Marks

FROM Students

GROUP BY Course;

Output:

Course Average_Marks

B.Tech 80.0

MBA 92.5

HAVING Clause ke Saath Aggregate Functions

 HAVING clause ko GROUP BY ke saath filter lagane ke liye use karte hain.

Query:

Code:

SELECT Course, AVG(Marks) AS Average_Marks

FROM Students

GROUP BY Course

HAVING AVG(Marks) > 85;


Output:

Course Average_Marks

MBA 92.5

Aggregate Functions Ke Features

1. Numeric Columns: SUM() aur AVG() sirf numeric columns pe kaam karte hain.

2. NULL Values:

o COUNT(): NULL values ko consider nahi karta.

o SUM(), AVG(), MAX(), MIN(): NULL values ko skip karte hain.

Short Summary

Function Purpose Example Output (Marks Column)

COUNT() Total rows count COUNT(*) 4

SUM() Total sum of values SUM(Marks) 345

AVG() Average of values AVG(Marks) 86.25

MAX() Maximum value MAX(Marks) 95

MIN() Minimum value MIN(Marks) 75

Exam Ke Liye Tips

1. Syntax aur Examples Practice Karein.

2. GROUP BY aur HAVING ka use samajhein.

3. Aggregate functions ko real-world scenarios se connect karke samajhne ki koshish karein.

Conclusion:
Aggregate functions jaise COUNT, SUM, AVG, MAX, aur MIN data ko summarize karne aur useful information nikalne ke liye use hote
hain. Inhe SQL queries ke saath efficiently use karna database handling ke liye zaroori hai.
Question 5: SQL Joins- 3PYQ

SQL Joins ek important topic hai jo multiple tables ke data ko combine karne ke liye use hota hai.

SQL Joins Kya Hain?

 SQL Joins ka kaam tables ke rows ko relational column(s) ke basis pe combine karna hota hai.

 Jab ek table ka data dusre table ke data se linked hota hai, tab Joins ka use hota hai.

Types of Joins:

1. INNER JOIN

2. LEFT JOIN (OUTER JOIN)

3. RIGHT JOIN (OUTER JOIN)

4. FULL JOIN (OUTER JOIN)

5. CROSS JOIN

6. SELF JOIN

1. INNER JOIN

 Kya karta hai?


Dono tables ke common rows return karta hai (jaha match hota hai).

 Syntax:

Code:

SELECT columns

FROM table1

INNER JOIN table2

ON table1.common_column = table2.common_column;

 Example:
Tables:

Students

Roll_No Name Course

101 Aman B.Tech

102 Priya MBA


Results

Roll_No Marks

101 85

103 90

Query:

Code:

SELECT Students.Name, Results.Marks

FROM Students

INNER JOIN Results

ON Students.Roll_No = Results.Roll_No;

Output:

Name Marks

Aman 85

2. LEFT JOIN (LEFT OUTER JOIN)

 Kya karta hai?


Left table ke sare rows return karta hai, chahe right table me match ho ya na ho.
Right table ka data match na hone par NULL hota hai.

 Syntax:

Code:

SELECT columns

FROM table1

LEFT JOIN table2

ON table1.common_column = table2.common_column;

 Example:
Query:

Code:

SELECT Students.Name, Results.Marks

FROM Students

LEFT JOIN Results

ON Students.Roll_No = Results.Roll_No;

Output:
Name Marks

Aman 85

Priya NULL

3. RIGHT JOIN (RIGHT OUTER JOIN)

 Kya karta hai?


Right table ke sare rows return karta hai, chahe left table me match ho ya na ho.
Left table ka data match na hone par NULL hota hai.

 Syntax:

Code:

SELECT columns

FROM table1

RIGHT JOIN table2

ON table1.common_column = table2.common_column;

 Example:
Query:

Code:

SELECT Students.Name, Results.Marks

FROM Students

RIGHT JOIN Results

ON Students.Roll_No = Results.Roll_No;

Output:

Name Marks

Aman 85

NULL 90

4. FULL JOIN (FULL OUTER JOIN)

 Kya karta hai?


Dono tables ke sare rows return karta hai, chahe match ho ya na ho.
Match na hone par NULL values hoti hain.

 Syntax:

Code:
SELECT columns

FROM table1

FULL JOIN table2

ON table1.common_column = table2.common_column;

 Example:
Query:

Code:

SELECT Students.Name, Results.Marks

FROM Students

FULL JOIN Results

ON Students.Roll_No = Results.Roll_No;

Output:

Name Marks

Aman 85

Priya NULL

NULL 90

5. CROSS JOIN

 Kya karta hai?


Dono tables ka Cartesian product return karta hai (har row ko har row ke sath combine karta hai).

 Syntax:

Code:

SELECT columns

FROM table1

CROSS JOIN table2;

 Example:
Query:

Code:

SELECT Students.Name, Results.Marks

FROM Students

CROSS JOIN Results;

Output:
Name Marks

Aman 85

Aman 90

Priya 85

Priya 90

6. SELF JOIN

 Kya karta hai?


Ek hi table ko apne upar join karta hai.

 Syntax:

Code:

SELECT a.columns, b.columns

FROM table1 a, table1 b

WHERE condition;

 Example:

Code:

SELECT a.Name, b.Name

FROM Students a, Students b

WHERE a.Roll_No < b.Roll_No;

PYQs Based on Joins

Question 1:

Explain INNER JOIN with an example (AKTU 2021)


Answer:
INNER JOIN sirf common rows return karta hai jaha tables match hote hain. Example:

Code:

SELECT Students.Name, Results.Marks

FROM Students

INNER JOIN Results

ON Students.Roll_No = Results.Roll_No;

Question 2:
Differentiate between LEFT JOIN and RIGHT JOIN with examples (AKTU 2020)
Answer:

 LEFT JOIN: Left table ke sare rows aur right table ke matching rows.

 RIGHT JOIN: Right table ke sare rows aur left table ke matching rows.

LEFT JOIN Example:

Code:

SELECT Students.Name, Results.Marks

FROM Students

LEFT JOIN Results

ON Students.Roll_No = Results.Roll_No;

RIGHT JOIN Example:

Code:

SELECT Students.Name, Results.Marks

FROM Students

RIGHT JOIN Results

ON Students.Roll_No = Results.Roll_No;

Question 3:

Write a query to display all students along with their marks, if available (AKTU 2019)
Answer:

Code:

SELECT Students.Name, Results.Marks

FROM Students

LEFT JOIN Results

ON Students.Roll_No = Results.Roll_No;

Conclusion

1. Joins Types: INNER, LEFT, RIGHT, FULL, CROSS, SELF.

2. Focus on Syntax aur Examples: PYQs me mostly syntax aur query-based questions aate hain.

3. Practical Use: Real-world problems ko samajhne ke liye practice zaroor karein.

Question 6. Triggers - 2PYQ


Ye database ke operations ko automate karne me help karte hain.

Triggers Kya Hain?

 Trigger ek special stored procedure hota hai jo automatically execute hota hai jab kisi table me koi specific event (INSERT,
UPDATE, DELETE) hoti hai.

 Triggers ka use data integrity maintain karne, audit logs banane, aur automated tasks execute karne ke liye hota hai.

Key Points about Triggers:

1. Triggers hamesha kisi table se linked hote hain.

2. Ye automatically fire hote hain (manual execution nahi hoti).

3. Triggers 3 events par kaam karte hain:

o INSERT

o UPDATE

o DELETE

Trigger Syntax in SQL

Code:

CREATE TRIGGER trigger_name

AFTER | BEFORE event

ON table_name

FOR EACH ROW

BEGIN

-- SQL statements

END;

 AFTER: Event hone ke baad trigger execute hoga.

 BEFORE: Event hone se pehle trigger execute hoga.

 event: INSERT, UPDATE, DELETE.

Example Triggers

1. Trigger to Track Insert Operations

 Requirement: Jab bhi ek new row insert ho, ek log table me entry banani hai.

Tables:
1. Students (Main Table)

Roll_No Name Course Marks

2. Logs (Log Table)

Log_ID Operation Timestamp

Trigger Code:

Code:

CREATE TRIGGER after_insert_students

AFTER INSERT

ON Students

FOR EACH ROW

BEGIN

INSERT INTO Logs (Operation, Timestamp)

VALUES ('INSERT', NOW());

END;

Explanation:

 Jab bhi Students table me new row add hoti hai, Logs table me ek INSERT entry ban jati hai.

2. Trigger to Prevent Negative Marks

 Requirement: Students table me koi bhi negative marks insert hone na de.

Trigger Code:

Code:

CREATE TRIGGER before_insert_students

BEFORE INSERT

ON Students

FOR EACH ROW

BEGIN

IF NEW.Marks < 0 THEN

SIGNAL SQLSTATE '45000'

SET MESSAGE_TEXT = 'Marks cannot be negative!';

END IF;

END;

Explanation: Ye trigger ensure karta hai ki Students table me negative marks insert na ho.

PYQs Based on Triggers


Question 1:

Explain triggers with an example (AKTU 2021)

Answer:
Triggers ek special procedure hain jo automatically fire hote hain jab kisi table par INSERT, UPDATE, ya DELETE operation hota hai.
Example:

Code:

CREATE TRIGGER after_insert_students

AFTER INSERT

ON Students

FOR EACH ROW

BEGIN

INSERT INTO Logs (Operation, Timestamp)

VALUES ('INSERT', NOW());

END;

Question 2:

Write a trigger to ensure no student gets marks less than 0 in a Students table (AKTU 2020)

Answer:

Code:

CREATE TRIGGER before_insert_students

BEFORE INSERT

ON Students

FOR EACH ROW

BEGIN

IF NEW.Marks < 0 THEN

SIGNAL SQLSTATE '45000'

SET MESSAGE_TEXT = 'Marks cannot be negative!';

END IF;

END;

Trigger Use-Cases

1. Data Validation: Negative marks prevent karna.

2. Audit Logs: Changes ko track karna.

3. Automatic Updates: Kisi event ke baad dusre table me data update karna.
Exam Preparation Tips:

1. Syntax Practice Karein: Triggers ka proper syntax yaad karein.

2. Examples Samajhein: PYQs ke through triggers ke real-world examples ko relate karein.

3. Events Par Focus Karein: INSERT, UPDATE, DELETE par triggers ka kaam kaise hota hai ye samjhein.

Conclusion:
Triggers ek powerful feature hai jo database operations ko automate aur secure banate hain. PYQs me mostly examples ya syntax
related questions aate hain, to unhe achhe se prepare karein.

You might also like