DDBS Lab File
DDBS Lab File
(CDCSC18)
QUESTIONS
1. Retrieve student names and the courses they are enrolled in:
SELECT s.student_name,
(SELECT c.course_name
FROM Course c
WHERE c.course_id = e.course_id) AS course_name
FROM Student s
JOIN Enrollment e ON s.student_id = e.student_id;
2. Retrieve all students, along with their enrolled courses (if any)
3. Retrieve all courses, along with the students enrolled in each course (if any):
SELECT c.course_name,
(SELECT s.student_name
FROM Student s
WHERE s.student_id = e.student_id) AS student_name
FROM Course c
LEFT JOIN Enrollment e ON c.course_id = e.course_id;
4. Retrieve all students and courses, showing matches where available and NULL values
otherwise:
UNION
SELECT *
FROM Employee
WHERE EAge BETWEEN 19 AND 32;
6. Display employees whose name starts with "S" and does not contain "I"
SELECT *
FROM Employee
WHERE Ename LIKE 'S%' AND Ename NOT LIKE '%I%';
7. Display unique cities of employees
SELECT DISTINCT ECity
FROM Employee;
FROM EMP
SELECT
CASE
END AS Site,
PROJECTS.PNO,
PROJECTS.BUDGET
FROM PROJECTS;
DDBS LAB ASSIGNMENT-4
Q)Consider the Relations given in the lab assignment:3, Implement the COM_MIN and
PHORIZONTAL Algorithms (as discussed in the classroom) to obtain the horizontal fragmentations for
any given relation and predicates. The inputs and outputs must be corresponding to these
algorithms given in the book pdf. You can refer to the different predicates given in the book.
Minimal predicates-
1.Remove redundancy (if DUR > 36, we don’t need DUR BETWEEN 12 AND 36).
2. Combine independent predicates.
CREATE TABLE EMP_F1 AS SELECT * FROM EMP WHERE TITLE = ‘Elect. Eng.’;
CREATE TABLE EMP_F2 AS SELECT * FROM EMP WHERE TITLE = ‘Syst. Anal.’;
CREATE TABLE EMP_F3 AS SELECT * FROM EMP WHERE TITLE = ‘Mech. Eng.’;
CREATE TABLE EMP_F4 AS SELECT * FROM EMP WHERE TITLE = ‘Programmer’;
CREATE TABLE ASG_F1 AS SELECT * FROM ASG WHERE RESP = ‘Manager’ AND DUR > 36;
CREATE TABLE ASG_F2 AS SELECT * FROM ASG WHERE RESP = ‘Analyst’ AND DUR BETWEEN 12 AND
36;
CREATE TABLE ASG_F3 AS SELECT * FROM ASG WHERE RESP = ‘Engineer’ AND DUR <= 12;
1. SELECT S.*
FROM STUDENT S
JOIN CLASS C ON S.USN = C.USN
JOIN SEMSEC SS ON C.SSID = SS.SSID
WHERE SS.Sem = 4 AND SS.Sec = 'C';
________________________________________________________
2. SELECT SS.Sem, SS.Sec, S.Gender, COUNT(*) AS Total_Students
FROM STUDENT S
JOIN CLASS C ON S.USN = C.USN
JOIN SEMSEC SS ON C.SSID = SS.SSID
GROUP BY SS.Sem, SS.Sec, S.Gender;
________________________________________________________
3. CREATE VIEW Test1_Marks AS
SELECT I.USN, I.Subcode, S.Title, I.Test1
FROM IAMARKS I
JOIN SUBJECT S ON I.Subcode = S.Subcode
WHERE I.USN = '1BI15CS101';
________________________________________________________
4. UPDATE IAMARKS
SET FinalCA = (Test1 + Test2 + Test3 - LEAST(Test1, Test2, Test3)) / 2;
________________________________________________________
5. SELECT S.USN, S.SName, SS.Sem, SS.Sec, I.FinalCA,
CASE
WHEN I.FinalCA BETWEEN 17 AND 20 THEN 'Outstanding'
WHEN I.FinalCA BETWEEN 12 AND 16 THEN 'Average'
WHEN I.FinalCA < 12 THEN 'Weak'
END AS CAT
FROM STUDENT S
JOIN CLASS C ON S.USN = C.USN
JOIN SEMSEC SS ON C.SSID = SS.SSID
JOIN IAMARKS I ON S.USN = I.USN
WHERE SS.Sem = 8 AND SS.Sec IN ('A', 'B', 'C')
GROUP BY S.USN, I.FinalCA;
if __name__ == "__main__":
num_attributes = int(input("Enter the number of attributes: "))
affinity_matrix = np.array(affinity_matrix)
optimized_sequence =
optimize_order_bond_energy(affinity_matrix.shape[0], affinity_matrix)
SELECT e.Grade
FROM Student s, Enrollment e
WHERE s.regNo = e.regNo
AND s.sName = 'Ali'
AND e.cCode = 'CS101';
SELECT e.Grade
FROM Student s, Enrollment e
WHERE s.regNo = e.regNo
AND s.sName = 'Irshad'
AND e.semester = 7;
SELECT s.sName
FROM Student s
WHERE s.regNo NOT IN (
SELECT e.regNo FROM Enrollment e
);
DDBS LAB ASSIGNMENT-10
Conflict Serializability and Conflict Equivalence Checker
1. Determine if each history is conflict serializable by building a precedence graph and detecting
cycles.
2. Compare pairs of histories to check if they are conflict equivalent based on conflicting
operation pairs.
def parse_input():
num_histories = int(input("Enter number of histories (e.g. 3): "))
histories = []
for h in range(num_histories):
print(f"Enter operations for history {h + 1} (e.g., R1x W2x):
")
ops = input().split()
history = [(op[0], int(op[1]), op[2]) for op in ops]
histories.append(history)
return histories
def build_precedence_graph(schedule):
graph = defaultdict(set)
for i in range(len(schedule)):
op1 = schedule[i]
for j in range(i + 1, len(schedule)):
op2 = schedule[j]
if op1[2] == op2[2] and op1[1] != op2[1]:
if (op1[0] == 'W' or op2[0] == 'W'):
graph[op1[1]].add(op2[1])
return graph
def has_cycle(graph):
visited = set()
rec_stack = set()
def dfs(node):
visited.add(node)
rec_stack.add(node)
for neighbor in graph[node]:
if neighbor not in visited:
if dfs(neighbor):
return True
elif neighbor in rec_stack:
return True
rec_stack.remove(node)
return False
graph = build_precedence_graph(hist)
print("Precedence Graph:")
for node in graph:
print(f"T{node} -> {[f'T{n}' for n in graph[node]]}")
if has_cycle(graph):
print("Conflict Serializable: NO (Cycle detected)")
else:
print("Conflict Serializable: YES (No cycles in precedence
graph)")
if __name__ == '__main__':
histories = parse_input()
analyze_histories(histories)
OUTPUT:
Enter number of histories (e.g. 3): 5
Enter operations for history 1 (e.g., R1x W2x):
R1x W2x R1y W1x
Enter operations for history 2 (e.g., R1x W2x):
R1x R2x W1x W2y
Enter operations for history 3 (e.g., R1x W2x):
W1x R2x W2x
Enter operations for history 4 (e.g., R1x W2x):
R1y R2y W1z W2z
Enter operations for history 5 (e.g., R1x W2x):
R1x W1x R2y W2y
Analyzing History 1:
Precedence Graph:
T1 -> ['T2']
T2 -> ['T1']
Conflict Serializable: NO (Cycle detected)
Analyzing History 2:
Precedence Graph:
T2 -> ['T1']
Conflict Serializable: YES (No cycles in precedence graph)
Analyzing History 3:
Precedence Graph:
T1 -> ['T2']
Conflict Serializable: YES (No cycles in precedence graph)
Analyzing History 4:
Precedence Graph:
T1 -> ['T2']
Conflict Serializable: YES (No cycles in precedence graph)
Analyzing History 5:
Precedence Graph:
Conflict Serializable: YES (No cycles in precedence graph)