dbms
dbms
dbms
2. What are the advantages of using a Database Management System (DBMS) over a
traditional file processing system?
1. Data Redundancy and Inconsistency Reduction: DBMS minimizes data redundancy and
ensures data consistency by centralizing data storage and management.
2. Data Integrity and Security: DBMS enforces data integrity constraints and provides
robust security measures to protect data from unauthorized access.
3. Data Independence: DBMS allows data to be modified without affecting the application
programs, providing logical and physical data independence.
4. Efficient Data Access: DBMS uses sophisticated algorithms to efficiently retrieve and
manipulate data, improving performance and speed.
Normalization
Normalization is the process of organizing data in a database to reduce redundancy and
improve data integrity. It involves dividing a database into two or more tables and defining
relationships between them. The main types of normalization are:
1. First Normal Form (1NF)
2. Second Normal Form (2NF ) 3 Third Normal Form (3NF)
3. Differentiate between the Primary Key and Foreign Key.
Primary Key
Uniqueness: A primary key uniquely identifies each record in a table.
Null Values: A primary key cannot contain null values.
Single Table: It is defined within a single table.
Purpose: Ensures that each record in the table is unique and can be referenced.
Foreign Key
Relationship: A foreign key is a field (or collection of fields) in one table that uniquely
identifies a row of another table.
Null Values: A foreign key can contain null values.
Multiple Tables: It is used to establish a link between two tables.
Purpose: Enforces referential integrity by ensuring that the value in the foreign key
column matches a value in the primary key column of the referenced table.
Q1
a) Create a table EMPLOYEE with attributes EmpID, Name, Salary, and Dept.
CREATE TABLE EMPLOYEE (
EmpID INT PRIMARY KEY,
Name VARCHAR(100),
Salary DECIMAL(10, 2),
Dept VARCHAR(50)
);
b) Display employees whose salary is greater than 50,000.
SELECT * FROM EMPLOYEE
WHERE Salary > 50000;
2 The REVOKE command is used to remove specific privileges from users or roles.
REVOKE privilege_name ON object_name FROM {user | role};
1. Write a PL/SQL program to check whether a specific employee exists in the EMPLOYEE
table based on the employee number. If not, display a message "Employee Not Found."
DECLARE
emp_id EMPLOYEE.EmpID%TYPE;
emp_count NUMBER;
BEGIN
-- Set the employee number to check
emp_id := 101;
SELECT COUNT(*)
INTO emp_count
FROM EMPLOYEE
WHERE EmpID = emp_id;
IF emp_count = 0 THEN
DBMS_OUTPUT.PUT_LINE('Employee Not Found');
ELSE
DBMS_OUTPUT.PUT_LINE('Employee Exists');
END IF;
END;
Q. Write a PL/SQL program to calculate the total salary of employees in a specific
department.
DECLARE
dept_id EMPLOYEE.DeptID%TYPE;
total_salary NUMBER;
BEGIN
dept_id := 102; SELECT SUM(Salary)
INTO total_salary
FROM EMPLOYEE
WHERE DeptID = dept_id;
DBMS_OUTPUT.PUT_LINE('Total Salary for Department ' || dept_id || ' is: ' || total_salary);
END;
Q. Consider the table EMP (EmpID, Name, Salary, Department, JoiningDate). Write SQL
queries to:
a)Display employees who joined after January 1, 2020.
SELECT * FROM EMP
WHERE JoiningDate > '2020-01-01';
b) Display the names of employees who have more than 5 years of experience.
SELECT Name
FROM EMP
WHERE DATEDIFF(YEAR, JoiningDate, GETDATE()) > 5;
d) Find the department with the highest average salary.
SELECT Department, AVG(Salary) AS AverageSalary
FROM EMP
GROUP BY Department
ORDER BY AverageSalary DESC
LIMIT 1;
3. Create a table BOOK with the following attributes: BookID, Title, Author, Publisher,
Price. Write SQL queries to:
CREATE TABLE BOOK (
BookID INT PRIMARY KEY,
Title VARCHAR(255),
Author VARCHAR(255),
Publisher VARCHAR(255),
Price DECIMAL(10, 2)
);
a) Add a new book to the table.
INSERT INTO BOOK (BookID, Title, Author, Publisher, Price)
VALUES (1, 'The Great Gatsby', 'F. Scott Fitzgerald', 'Scribner', 10.99);
b) Update the price of a book based on its ID.
UPDATE BOOK
SET Price = 12.99
WHERE BookID = 1;
c) Delete books older than 5 years.
DELETE FROM BOOK
WHERE PublicationDate < DATEADD(YEAR, -5, GETDATE());