External Dbms
External Dbms
queries, constraints, PL/SQL programming, and ER modeling. Below are structured answers to the
questions:
SELECT E_name FROM EMPLOYEE e WHERE EXISTS (SELECT 1 FROM DEPARTMENT d WHERE
d.Dept_no = e.Dept_no AND d.Dept_name='SALES');
a. Insert 5 rows:
INSERT INTO EMPLOYEE (Emp_no, E_name, E_address, E_ph_no, Dept_no, Dept_name, Job_id,
Salary)
3. EMPLOYEE Queries
a. Employees in MECH:
c. Records in SALES:
d. Differences:
b. Maximum age:
c. Minimum age:
Entity: Rectangle
Relationship: Diamond
Attribute: Oval
a. Grouped Salaries:
SELECT Dept_name, SUM(Salary) FROM EMPLOYEE GROUP BY Dept_name;
b. Salaries Ascending:
c. Salaries Descending:
INSERT INTO EMPLOYEE (Emp_no, E_name, E_address, E_ph_no, Dept_no, Dept_name, Job_id,
Salary)
INSERT INTO EMPLOYEE (Emp_no, E_name) VALUES (104, NULL); -- This will fail due to NOT NULL
constraint
d. Attribute Definitions:
Derived Attribute: Value derived from other attributes (e.g., Age from DOB).
Composite Attribute: Made of multiple components (e.g., Full Name = First Name + Last
Name).
a. Names of sailors who reserved both red and green boats (INTERSECT):
INTERSECT
b. Names of sailors who reserved red and green boats (UNION ALL):
UNION ALL
SELECT S_name FROM Sailors WHERE Boat_color = 'Green';
d. Aggregate Functions:
SELECT S_name, S_age FROM Sailors WHERE S_age = (SELECT MAX(S_age) FROM Sailors);
FROM Sailors
GROUP BY Rating
Name Courses
Alice Math
Alice Science
2NF: Remove partial dependencies. Example: If a table's non-key attribute depends only on
part of a composite key, decompose it.
9. Customer and Order Table Joins
a. INNER JOIN:
ON c.Customer_id = o.Customer_id;
ON c.Customer_id = o.Customer_id;
ON c.Customer_id = o.Customer_id;
ON c.Customer_id = o.Customer_id;
e. Triggers:
Definition: Automatically invoked SQL blocks on specific events like INSERT, UPDATE, DELETE.
Example:
BEGIN
END;
c. Oldest sailor:
SELECT S_name, S_age FROM Sailors WHERE S_age = (SELECT MAX(S_age) FROM Sailors);
UNION
Primary Key: Uniquely identifies each row. Only one allowed per table.
BEGIN
END;
BEGIN
END;
BEGIN
END;
d. Attribute Definitions:
Composite Attribute: Multi-part attribute (e.g., Full Address = Street + City + State).
IS
BEGIN
END;
b. Modify procedure:
IS
BEGIN
END;
d. ER Diagram Notations:
Entity: Rectangle
Attribute: Oval
Relationship: Diamond
a. Cursor Example:
DECLARE
BEGIN
OPEN emp_cursor;
LOOP
END LOOP;
CLOSE emp_cursor;
END;
b. Multiplication Program:
DECLARE
num NUMBER := 5;
result NUMBER := 1;
BEGIN
result := num * i;
END LOOP;
END;
IS
BEGIN
END;
b. Factorial Program:
DECLARE
num NUMBER := 5;
fact NUMBER := 1;
BEGIN
fact := fact * i;
END LOOP;
END;
c. Trigger to Update:
BEGIN
END;
a. Converting ER to Relational:
Relationships:
o Manages(P_no, SSN)
o Works_on(P_no, SSN)
1. Professors Table:
4. Name VARCHAR2(50),
5. Age NUMBER(3),
6. Rank VARCHAR2(20),
7. Specialty VARCHAR2(50)
8. );
9. Projects Table:
16. );
17. Relationships:
o Manages:
o P_no NUMBER,
o SSN VARCHAR2(11),
o );
o Works_On:
o P_no NUMBER,
o SSN VARCHAR2(11),
o );
Delete:
Truncate:
Drop:
);
Unique Key: Ensures unique values:
);
);
ENAME VARCHAR2(20),
JOB VARCHAR2(10),
DEPTNO NUMBER(3),
SAL NUMBER(7, 2)
);
DNAME VARCHAR2(10),
LOC VARCHAR2(10)
);
Queries:
ENAME VARCHAR2(20),
);
Drop a Column:
SELECT:
INSERT:
UPDATE:
1. Professors:
4. Name VARCHAR2(50),
5. Age NUMBER(3),
6. Rank VARCHAR2(20),
7. Specialty VARCHAR2(50)
8. );
9. Projects:
16. );
See 18.d.
See 18.e.
1. BUS:
2. TICKET:
3. PASSENGER:
Relationships:
1. Reservation:
2. Cancellation:
o Attributes: Cancellation_ID (Primary Key), Reservation_ID (Foreign Key), Date,
Reason.
Keys:
Candidate Key: A subset of attributes uniquely identifying a record (e.g., Ticket_ID in the
TICKET table).
Partial Key: A unique attribute within a weak entity set that requires a foreign key (e.g.,
Reservation_ID in the RESERVATION table).
DECLARE
CURSOR reservation_cursor IS
passenger_id Reservation.Passenger_ID%TYPE;
ticket_id Reservation.Ticket_ID%TYPE;
BEGIN
OPEN reservation_cursor;
LOOP
END LOOP;
CLOSE reservation_cursor;
END;
Table Creation:
Job VARCHAR2(10),
Address VARCHAR2(35),
Salary NUMBER(10, 2),
DOJ DATE
);
a. Insert Records:
INSERT INTO EMPLOYEE123 VALUES ('E001', 'Alice', 'Manager', 'New York', 75000, '2020-01-01');
INSERT INTO EMPLOYEE123 VALUES ('E002', 'Bob', 'Engineer', 'Los Angeles', 60000, '2019-03-15');
INSERT INTO EMPLOYEE123 VALUES ('E003', 'Charlie', 'Analyst', 'Chicago', 55000, '2021-07-10');
INSERT INTO EMPLOYEE123 VALUES ('E004', 'David', 'Manager', 'Houston', 80000, '2018-05-20');
INSERT INTO EMPLOYEE123 VALUES ('E005', 'Eva', 'HR', 'Seattle', 70000, '2020-09-25');
Update:
Delete:
c. Aggregate Functions:
Example:
e. Cursor Operations:
Refer to 23.b.
Table Creation:
Mer_ID VARCHAR2(10),
Age INTEGER,
);
e. Insert Records:
Refer to 18.d.