DBMS
DBMS
PARUL UNIVERSITY
Program: Online BCA
Course: database management
system
Skills Semester: 2
Internal Assignment: 1
Assignment Marks: 30
Internal assignment
Q.1 What is the difference between DBMS and RDBMS.
Ans : Let's start by comparing and contrasting DBMS (Database Management System) and
RDBMS (Relational Database Management System).
1. Definition:
-DBMS is software that creates, maintains and manipulates the data under it. In an effective
way, it lets users store and recover data. Such DBMS can be utilized for all types of database:
Hierarchical, network, or object-oriented databases.
-RDBMS is a special DBMS depending upon the relational model given by E.F. Codd. In
RDBMS all data are kept in the form of tables (relations). Each table consists of columns and
rows.
2. Data Storage:
-The data of a DBMS are kept in files. It does not impose any strict structure on itself, and
relationships between items may not need to be given explicitly.
-Data in an RDBMS is stored in tabular format. Each table is given a unique name and
comprises rows (records) and columns (attributes); these are specified using primary key
and foreign key to establish relationships.
3. Data Integrity and Normalization:
-DBMS does not follow the full concept of data normalization, so as to redundancy and
inconsistency of data.
-RDBMS support integrity constraints on the data and set up normalization rules for
customization-degree reduction and consistency maintenance of data.
4. Relationship support:
-There is no support offered by DBMS to provide relationships between different data items.
The user or the application programmer must take care of handling each relationship
individually.
-RDBMS allows defining relationships between the data tables using the foreign keys and
join operations.
5. Examples:
-File System, XML Database, old systems like dBase and FoxPro are examples of DBMS.
-MySQL, PostgreSQL, Oracle Database, Microsoft SQL Server, and SQLite are only a few
examples of RDBMS.
6. Security and ACID Properties:
-A DBMS might or might not support ACID (Atomicity, Consistency, Isolation, Durability)
properties that are required to ensure a reliable transaction process.
-RDBMS very strictly follows ACID properties for secured and trustworthy manipulation of
data, primarily in any multi-user environment.
7. Concurrency and multi-user support:
Most of the time, it uses a DBMS, which is typically a single-user system. It suits small
applications not requiring high-level data security or multi-user access.
RDBMS provides a multi-user access system with concurrency features using the ability to
read from and write to the same data simultaneously, so it is more suited for large and
complex applications.
When database normalization is done, it makes it more convenient for maintenance, as one
piece of information is kept only once. This reduces errors and inconsistencies.
Objectives of Normalization:
1. Eliminate duplication.
2. Have logical data dependencies.
3. Improve integrity and consistency of data.
4. Flexibility of Database and Easy maintenance.
Normal Forms:
There have been several levels of normalization (normal forms). Each level has some rules
and further extends the previous. The most popular normal forms are as follows:
1. First Normal Form (1NF):
A table is said to be in 1NF if:
• All attributes (columns) hold atomic (indivisible) values.
• They are distinct records.
2. Second Normal Form (2NF):
A table is said to be in 2NF if:
• It is in 1NF;
• Where All non-key attributes are entirely functionally depended on primary key.
3.\tThird Normal Form (3NF):
A table is in 3NF if:
• It is in 2NF;
• All non-key attributes are now not transitively dependent on the primary key. Example of
Normalization:
Consider an unnormalized table as follows:
StudentID StudentName Course CourseFee
101 Alice Math 500
101 Alice Science 600
102 Bob Math 500
Problems with this table:
• Redundant data: student name repeats itself.
• Update anomalies: if the course fee changes, it is necessary to update it at many places.
• Insertion anomaly: cannot insert a new course without enrolling a student.
• Deletion anomaly: if a student unenrolls from all courses, we would lose his information
about courses.
Stepwise Normalization:
1NF:
Split repeating groups into different rows:
StudentID StudentName Course CourseFee
101\tAlice\tMath\t500
101\tAlice\tScience\t600
102\tBob\tMath\t500
(Already 1NF, because all values are atomic.) 2NF:
Eliminate partial dependence:
Split into two tables:
Student Table:
StudentID StudentName
101\tAlice
102\tBob Enrollment Table:
StudentID Course CourseFee
101\tMath\t500
101\tScience\t600
102
3NF:\tMath\t500
Eliminate transitive dependency:
Split again:
Course Table:
Course CourseFee Math 500 Science 600 Final Tables:
1 Student Table
2 Course Table
3 Enrollment table (StudentID,Course) Now, all data is stored just once and relationships are
held by keys.
Q.3 What is the difference between Primary Key and Foreign Key.
Ans : In relational database systems, keys are essential in recognizing as well as relating
records. The Primary Key and the Foreign Key are two of the most commonly used types of
keys. While both are concerned with maintaining data integrity, they are used for different
purposes with different characteristics. Primary Key:-
A Primary Key serves the special purpose of identifying every record in a certain database
table. It prevents any two rows within a table from having the same value for whatever
primary key column(s) it holds. A primary key cannot have NULL values, and must hold
distinct values in all cases. Characteristics of Primary Key: •\tUniquely identifies each record
in a table. •\tCannot hold NULL values. • Each table may have just one primary key, but that
primary key may consist of a combination of columns (a composite key). • Only on the
designated primary key column(s) a unique index will be automatically created. Example:
consider the following Student table:
StudentID (Primary Key): Name: Age:
101: Alice: 20
102: Bob: 22
In this table, StudentID is the primary key that uniquely identifies every student.
Foreign Key:-
Another Foreign Key is a column that is referenced against a Primary Key within another
table (or collection of columns). It is the link between two tables. A foreign key does this by
enforcing that whatever value a column has will correspond with some current value inside
the other table, thus making foreign key reference integrity to hold.
Foreign Key Q's:
• A foreign key creates a link between two tables.
• It may contain duplicated values.
• In case, can contain NULL values unless otherwise stated.
• One or more foreign keys can exist in one table.
• It refers to a primary key in another table. Example:
Now, consider another table, Enrollment:
EnrollmentID StudentID(FK) Course
1\t101\tMath
2\t102\tSci
Here, the StudentID in the Enrollment table refers to StudentID in the Student table; this
ensures that only students in the Student table may enroll in a course.
Key Differences:
Feature\tPrimary Key\tForeign Key
Purpose\tServes to identify records in a table\tDefines relationship between tables
Uniqueness\tMust be unique\tCan have duplicates
NULLs\tNot Allowed\tAllow NULL unless stated otherwise Number in a tableOnly one
primary key is permitted in a table\tMultiple foreign keys permitted
Referencing\tNo reference to another table\tReferences primary key in another table
Enforces\tEntity Integrity\tReferential Integrity.
All of these include Atomicity, Consistency, Isolation, and Durability, which are the pillars of
any trustworthy database management systems. The importance of these is certainly in
manipulation of extremely valuable data and the maintenance of users' faith where the
question of accuracy and reliability is out of the question. For instance, in an Internet
shopping system, atomicity ensures that your purchase is either processed fully or not at all;
consistency guarantees;
Q. 5 Explain the purpose of Table and Write a simple query of Insert, Update, Delete and
Select query with example.
Ans :: A table in a database is analogous to a box meant for storing data. It consists of
information stored in columns and rows easily accessed, modified, and analyzed. A table has
one record or entry for every row and one attribute or field for every column. To further
illustrate, consider a table "students" with columns ID, Name, Age, and Grade. Each row in
this table corresponds to an individual student, with all information pertaining to him or her
neatly organized. Tables form the foundation of a database, where data is organized for
systematic and efficient storage.
Let's now move to four basic SQL queries-Insert, Update, Delete, and Select-with examples.
1. Insert Query: This is used to insert new records into a particular table. For instance, if we
want to insert a new student into the "Students" table, we can use the following query:
```sql
INSERT INTO Students (ID, Name, Age, Grade)
VALUES(1,'Aarav',15,'10th');
```
By executing this, a new entry is added to the table with ID 1, Name Aarav, Age 15, and Grade
10th.
2. Update Query: This query will modify any existing records in the table. In case the age of
Aarav was wrongly marked and now we wish to update it to 16, the following syntax can be
executed:
```sql
UPDATE Students
SET Age=16
WHERE ID=1;
```
In this case, the `WHERE` statement guarantees that only the record where ID=1 is modified.
3. Delete Query: This is used to delete records from a table. If we wish to delete Aarav's
record from the "Students" table, we can use:
```sql
DELETE FROM Students
WHERE ID=1;
```
Again, the `WHERE` clause ensures that only the record specified is deleted, thus avoiding
the unwanted deletion of other records.
4. Select Query: This collects data from a table. For instance, if we want to see all the students
of the "Students" table, we can use:
```sql
SELECT * FROM Students;
```
The `*` specifies all columns of the table, and the query will show all rows currently stored
in it.
Assuming that we want to get specific data, for instance, the names of 10th graders, the
query would be as follows:
```sql
SELECT Name FROM Students
WHERE Grade='10th';
```
These are very basic questions required for working with any database and controlling it.
The SQL queries empower us to insert fresh data, update or delete existing records, and
retrieve information for analysis or reports. It is an organized way for DBMS to administrate
and manage using tables and SQL queries with correctness and efficiency. Let me know if
you need me to elaborate on any of these concepts too!
Q. 6 Write an SQL query to create a table Employees with columns EmpID (INT, Primary
Key), FirstName (VARCHAR 50), LastName (VARCHAR 50), HireDate (DATE), and Salary
(FLOAT). After creating the table, insert an employee record with EmpID = 1, FirstName
= 'John', LastName = 'Smith', HireDate = '2024-01-01', and Salary = 50000. Update the
salary of the employee with EmpID = 1 to 55000. Then, write a query to retrieve the
FirstName, LastName, and Salary of the employee. Combine both the update and select
operations.
Ans ::
1. Create the table
CREATE TABLE Employees (
EmpID INT PRIMARY KEY,
FirstName VARCHAR(50),
LastName VARCHAR(50),
HireDate DATE,
Salary FLOAT );
2. Insert an employee record
INSERT INTO Employees (EmpID, FirstName, LastName, HireDate, Salary)
VALUES (1, 'John', 'Smith', '2024-01-01', 50000);
3. Update the employee's salary
UPDATE Employees
SET Salary = 55000
WHERE EmpID = 1;
Explanation:
• The CREATE TABLE command defines the structure of the Employees table.
• The INSERT INTO command adds a new employee.
• The UPDATE command modifies the salary.
• The SELECT command retrieves the employee’s first name, last name, and updated
salary.
Q.7 Add a new column Department (VARCHAR 30) to the Employees table using a DDL
command. After that, retrieve all employees' FirstName, LastName, and Department.
You have two tables, Employees and Departments. Insert a new department into the
Departments table with DeptID = 101 and DeptName = 'Sales'. Then, write a query to
join Employees and Departments on DeptID to retrieve FirstName, LastName, and
DeptName.
Ans ::
Here's the SQL code to handle your request step by step:
Q.8 Delete the employee with EmpID = 1 from the Employees table. After that, retrieve
all remaining employees' FirstName and LastName. 3 INSERT permission on the
Employees table to a user named UserA. Then, insert a new employee record with EmpID
= 2, FirstName = 'Alice', LastName = 'Brown', HireDate = '2024-02-01', and Salary =
60000.
Ans ::
Here's the SQL code to handle your request step by step:
1. Add a new column `Department` to the `Employees` table
This is done using a Data Definition Language (DDL) command:
```sql
ALTER TABLE Employees
ADD Department VARCHAR(30);
```
2. Retrieve all employees' `FirstName`, `LastName`, and `Department`
```sql
SELECT FirstName, LastName, Department
FROM Employees;
```
3. Insert a new department into the `Departments` table
Assuming the `Departments` table has columns `DeptID` and `DeptName`, the following
query inserts a new
department:
```sql
INSERT INTO Departments (DeptID, DeptName)
VALUES (101, 'Sales');
```
4. Join the `Employees` and `Departments` tables
To join the two tables on `DeptID` and retrieve `FirstName`, `LastName`, and
`DeptName`, you first need to
ensure the `Employees` table has a `DeptID` column to link with `Departments`. If it's
missing, add it with:
```sql
ALTER TABLE Employees
ADD DeptID INT;
```
Then, you can retrieve the requested data with a `JOIN` query:
```sql
SELECT Employees.FirstName, Employees.LastName, Departments.DeptName
FROM Employees
JOIN Departments
ON Employees.DeptID = Departments.DeptID;
```
Complete Example Workflow
Here's how it all fits together:
```sql
Q.9 SELECT permission from a user named UserA on the Employees table. Then, retrieve
all records from the Employees table to check the remaining permissions for the current
user. Add a column ManagerID (INT) to the Departments table. After that, join
Employees and Departments on DeptID and retrieve the FirstName, LastName,
DeptName, and ManagerID of all employees.
Ans ::
= Here’s the SQL code to achieve the tasks step by step:
1. Revoke SELECT permission from `UserA` on the `Employees` table
```sql
REVOKE SELECT ON Employees FROM UserA;
```
2. Retrieve all records from the `Employees` table
```sql
SELECT * FROM Employees;
```
This allows the current user to view all records in the `Employees` table and check the
remaining permissions.
3. Add a `ManagerID` column to the `Departments` table
```sql
ALTER TABLE Departments
ADD ManagerID INT;
```
4. Join `Employees` and `Departments` on `DeptID` and retrieve the required
information
```sql
SELECT Employees.FirstName, Employees.LastName, Departments.DeptName,
Departments.ManagerID
FROM Employees
JOIN Departments
ON Employees.DeptID = Departments.DeptID;
```
Complete Workflow
Below is the combined SQL script:
```sql
Q.10 Retrieve all employees who earn a salary greater than 50,000. Then, update the
salary of all employees retrieved by this query by increasing their salary by 10%.
Ans :: Here’s the SQL code to retrieve employees earning a salary greater than 50,000
and update their
salaries by increasing them by 10%:
1. Retrieve employees with a salary greater than 50,000
```sql
SELECT *
FROM Employees
WHERE Salary > 50000;
```
2. Update the salary of those employees
```sql
UPDATE Employees
SET Salary = Salary * 1.1
WHERE Salary > 50000;
```
Explanation:
- The `SELECT` query retrieves all employees whose salary is greater than 50,000. You
can view these records
before making any updates.
- The `UPDATE` query modifies the salaries of only those employees who meet the
condition `Salary > 50000`.
The `SET Salary = Salary * 1.1` increases their salary by 10%.