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

Assignment 2

Uploaded by

Vedant Gade
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
12 views

Assignment 2

Uploaded by

Vedant Gade
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 19

Assignment 2: DBMS

Q 1) List DCL commands. (2M)


1) GRANT: This command is used to grant specific privileges to a user or role.
It allows the specified user or role to perform certain actions on database
objects.

2) REVOKE: The REVOKE command is used to revoke privileges that were


previously granted to a user or role. It restricts the specified user or role
from performing certain actions on database objects.

3) DENY: While not supported in all database systems, the DENY command is
used to explicitly deny a user or role the ability to perform certain actions
on database objects. It's a stricter form of access control than REVOKE.

4) COMMIT: Although not traditionally considered a DCL command, COMMIT


is used to make permanent changes to the database that were made within
a transaction. It's used in conjunction with the Data Manipulation Language
(DML) commands.

5) ROLLBACK: Similar to COMMIT, ROLLBACK is not strictly a DCL command,


but it's important for managing data consistency. It is used to undo the
changes made within a transaction and return the database to its previous
state.

6) SAVEPOINT: SAVEPOINT creates a point within a transaction to which you


can later roll back. It's useful for rolling back only part of a transaction while
leaving other changes intact.
Q2) Define Normalization and list its types

Normalization is a database design process that aims to organize data in a


relational database efficiently and reduce data redundancy. The primary goal of
normalization is to eliminate data anomalies, such as insertion, update, and
deletion anomalies, by ensuring that each piece of data is stored in only one
place.

1) First Normal Form (1NF): In 1NF, the table must have a primary key, and
each column in the table must store only atomic (indivisible) values. There
should be no repeating groups or arrays within a column.

2) Second Normal Form (2NF): A table is in 2NF if it is in 1NF and all non-key
attributes are fully functionally dependent on the entire primary key. This
means that each non-key attribute depends on the entire primary key, not
just part of it.

3) Third Normal Form (3NF): A table is in 3NF if it is in 2NF and there are no
transitive dependencies between non-key attributes and the primary key.
In other words, no non-key attribute should depend on another non-key
attribute.

Q3)Define the terms: i)Candidate Key ii)Primary Key

i)Candidate Key:
A candidate key in a relational database is a minimal set of attributes (columns)
that uniquely identifies each record (row) in a table. In other words, it's a column
or a combination of columns that can uniquely identify each individual row in a
table without any redundancy. Each table can have one or more candidate keys.
It's important to note that while multiple candidate keys might exist, only one of
them will be chosen as the primary key.
ii)Primary Key:
The primary key in a relational database is a specific candidate key chosen to
uniquely identify each record in a table. It's a column or a set of columns that has
the following properties:

1) Uniqueness: The values in the primary key column(s) must be unique for
each row in the table, ensuring that no two rows have the same values in
the primary key.

2) Non-nullability: The primary key column(s) cannot contain null values, as


null values are not considered unique identifiers.

3) Irreducibility: The primary key is minimal, meaning that no subset of the


primary key columns can serve as a unique identifier on its own.

Q4)List any four ddl commands:

CREATE TABLE:
Syntax:
CREATE TABLE table_name (
column1 datatype,
column2 datatype,
...
PRIMARY KEY (one_or_more_columns)
);

ALTER TABLE
Syntax:
ALTER TABLE table_name
ADD column_name datatype;
DROP TABLE:

Syntax:
DROP TABLE table_name;
CREATE INDEX:
Syntax:
CREATE INDEX index_name
ON table_name (column_name);

Q5) Enlist DML commands:


1) SELECT: Retrieves data from one or more tables based on specified
conditions.
2) INSERT: Adds new records (rows) into a table.
3) UPDATE: Modifies existing records in a table based on specified conditions.
4) DELETE: Removes records from a table based on specified conditions.
5) MERGE: Combines INSERT, UPDATE, and DELETE operations in a single
statement, often used to synchronize data between two tables.

Q6)Explain difference between delete and truncate command with example

1)DELETE Command:

The DELETE command is used to remove specific rows from a table based
on specified conditions. It is a DML (Data Manipulation Language)
command that can be quite selective, allowing you to delete specific
records that match a certain criteria. This command also generates
individual entries in the database's transaction log for each row deleted,
making it slower compared to TRUNCATE for large operations.

Example:
Suppose you have a table named "Employees" and you want to delete
employees who have resigned. You could use the DELETE command like
this:

Syntax:

DELETE FROM Employees WHERE EmploymentStatus = 'Resigned';

2)TRUNCATE Command:
The TRUNCATE command is used to remove all rows from a table in a much
faster and efficient manner compared to DELETE. It is also a DDL (Data
Definition Language) command, which means it's used to modify the
structure of the database. Because of its efficiency, it doesn't generate
individual entries in the transaction log for each row, making it faster than
DELETE, especially for large datasets. However, it doesn't allow the use of a
WHERE clause for specifying conditions.

Example:
Using the same "Employees" table, if you want to remove all the records,
you could use the TRUNCATE command like this:

Syntax:
TRUNCATE TABLE Employees;

Q7)State and explain 2 NF and 3NF with example:

!)Second Normal Form (2NF):

2NF is a level of database normalization that builds upon the concepts of


First Normal Form (1NF). A table is in 2NF if it is in 1NF and no non-prime
attribute (an attribute that is not part of any candidate key) is partially
dependent on any candidate key.

In simpler terms, a table is in 2NF if it doesn't have partial dependencies,


meaning that each non-key attribute depends on the entire candidate key,
not just part of it.

Example of 2NF:
Consider a table named "StudentCourses" with columns: StudentID,
CourseID, CourseName, and Instructor. Here, (StudentID, CourseID) is the
composite primary key.

StudentID CourseID CourseName Instructor


101 1 Math Professor A
101 2 Physics Professor B
102 1 Math Professor C

In this table, the combination of (StudentID, CourseID) is the candidate key.


However, the "Instructor" column depends only on "CourseID" (part of the
candidate key), not on the entire key. This violates 2NF.

To bring the table to 2NF, you can split it into two separate tables:
"Courses" and "StudentCourseRelations". The "Courses" table would have
CourseID, CourseName, and Instructor columns, and the
"StudentCourseRelations" table would have StudentID and CourseID
columns.

Third Normal Form (3NF):


3NF is the next level of database normalization that builds upon 2NF. A
table is in 3NF if it is in 2NF and if no non-prime attribute is transitively
dependent on any superkey.

In simpler terms, a table is in 3NF if all non-key attributes depend only on


the candidate keys and not on other non-key attributes.
Example of 3NF:
Consider a table named "EmployeeInfo" with columns: EmployeeID,
ProjectID, ProjectManager, and ManagerPhoneNumber. Here,
(EmployeeID, ProjectID) is the composite primary key.

EmployeeID ProjectID ProjectManager


101 1 John Smith
102 1 John Smith
103 2 Jane Doe
In this table, the "ProjectManager" and "ManagerPhoneNumber" columns
are functionally dependent on "ProjectID," which is not a candidate key.
This results in a transitive dependency, violating 3NF.

To bring the table to 3NF, you could separate it into three tables: "Employees"
(EmployeeID, ProjectID), "Projects" (ProjectID, ProjectManager), and "Managers"
(ProjectManager, ManagerPhoneNumber).

Q8)List the SQL operations and explain range searching operations between and
pattern matching operator ‘like’ with example

SQL Operations:

i. SELECT: Retrieves data from one or more tables.


ii. INSERT: Adds new records (rows) into a table.
iii. UPDATE: Modifies existing records in a table.
iv. DELETE: Removes records from a table.
v. BETWEEN: Used for range searching between two values.
vi. LIKE: Used for pattern matching within a string.

Range Searching using BETWEEN:


The BETWEEN operator is used to retrieve rows with values within a specified
range. It's often used with numeric, date, or timestamp data types.
Syntax:

SELECT column1, column2, ...


FROM table_name
WHERE column_name BETWEEN value1 AND value2;

Example:
Suppose you have a table named "Sales" with columns "OrderID," "OrderDate,"
and "Amount." You want to retrieve orders with amounts between $100 and
$500.

SELECT OrderID, OrderDate, Amount


FROM Sales
WHERE Amount BETWEEN 100 AND 500;
This query will retrieve all rows where the "Amount" falls within the range of
$100 to $500.

Pattern Matching using LIKE:


The LIKE operator is used to search for a specified pattern within a string. It's
often used with text or character-based columns.

% represents zero or more characters.


_ represents a single character.
Syntax:
SELECT column1, column2, ...
FROM table_name
WHERE column_name LIKE pattern;

Example:
Suppose you have a table named "Customers" with columns "CustomerID,"
"FirstName," and "LastName." You want to retrieve customers whose last name
starts with "Smi."

SELECT CustomerID, FirstName, LastName


FROM Customers
WHERE LastName LIKE 'Smi%';
This query will retrieve all rows where the "LastName" starts with "Smi." The %
wildcard is used to match any number of characters after "Smi."

Another example is if you want to retrieve customers with a last name of exactly
three characters:

SELECT CustomerID, FirstName, LastName


FROM Customers
WHERE LastName LIKE '___';
In this query, the _ wildcard is used to match a single character, and since there
are three underscores, it will match any last name with exactly three characters.
Q9)Write a command to crate table student (rollno, Stud_name, branch, class,
DOB, City, Contact_no) and write down queries for following:
(i) Insert one row into the table
(ii) Save the data
(iii) Insert second row into the table
(iv) Undo the insertion of second row
(v) Create save point S1.
Insert one row into the table

Create the "student" table:

CREATE TABLE student (


rollno INT PRIMARY KEY,
Stud_name VARCHAR(50),
branch VARCHAR(50),
class VARCHAR(20),
DOB DATE,
City VARCHAR(50),
Contact_no VARCHAR(15)
);
Step 2: Perform the queries:

(i) Insert one row into the table:

INSERT INTO student (rollno, Stud_name, branch, class, DOB, City,


Contact_no)
VALUES (1, 'John Doe', 'Computer Science', '10th', '2000-05-15', 'New York',
'123-456-7890');

(ii) Save the data:


In SQL, you don't explicitly "save" data after an INSERT operation. The data
is immediately inserted into the table.

(iii) Insert second row into the table:


INSERT INTO student (rollno, Stud_name, branch, class, DOB, City,
Contact_no)
VALUES (2, 'Jane Smith', 'Electrical Engineering', '12th', '1999-08-20', 'Los
Angeles', '987-654-3210');

(iv) Undo the insertion of the second row:


To undo the insertion of the second row, you can use the DELETE command
to remove the row that you inserted:

DELETE FROM student WHERE rollno = 2;


(v) Create save point S1:

Savepoints are typically used in the context of transactions to allow you to


set a point to which you can later roll back. However, note that not all
databases support savepoints. In this example, I'll demonstrate using a
savepoint using standard SQL syntax:
SAVEPOINT S1;

(vi) Insert one row into the table:

INSERT INTO student (rollno, Stud_name, branch, class, DOB, City,


Contact_no)
VALUES (3, 'Alice Johnson', 'Mechanical Engineering', '11th', '2001-02-10',

Q10)Consider the structure for book table as Book-Master (bookid, bookname,


author, no_of copies, price) Write down SQL queries for following:
(i) Write a command to create Book_master table.
(ii) Get authorwise list of all books.
(iii) Display all books whose price is between 500 & 800.
(iv) Display all books with details whose name start withD‟.
(v) Display all books whose price is above 700.
(vi) Display all books whose number of copies are less than 10.

i) Create the Book_Master table:

CREATE TABLE Book_Master (


bookid INT PRIMARY KEY,
bookname VARCHAR(100),
author VARCHAR(50),
no_of_copies INT,
price DECIMAL(10, 2)
);

(ii) Get authorwise list of all books:

SELECT author, bookname


FROM Book_Master
ORDER BY author;

(iii) Display all books whose price is between 500 and 800:

SELECT *
FROM Book_Master
WHERE price BETWEEN 500 AND 800;
(iv) Display all books with details whose name starts with 'D':

SELECT *
FROM Book_Master
WHERE bookname LIKE 'D%';

(v) Display all books whose price is above 700:

SELECT *
FROM Book_Master
WHERE price > 700;

(vi) Display all books whose number of copies are less than 10:

SELECT *
FROM Book_Master
WHERE no_of_copies < 10;

Q11)Consider the table Student (name, marks, dept, age, place, phone,
birthdate). Write SQL query for following.
i)To list students having place as ‘Pune’ or ‘Jalgaon’
ii) To list students having same department (dept) as that of ‘Rachana’
iii) To change marks of ‘Rahul’ from 81 to 96.
iv) To list student name and marks from ‘Computer’ dept.
v) To list student name who have marks less than 40.
vi) To list students who are not from ‘Mumbai

(i) To list students having place as 'Pune' or 'Jalgaon':

SELECT name
FROM Student
WHERE place IN ('Pune', 'Jalgaon');

(ii) To list students having the same department (dept) as that of 'Rachana':

SELECT name
FROM Student
WHERE dept = (SELECT dept FROM Student WHERE name = 'Rachana');

(iii) To change marks of 'Rahul' from 81 to 96:

UPDATE Student
SET marks = 96
WHERE name = 'Rahul';

(iv) To list student name and marks from 'Computer' dept:


SELECT name, marks
FROM Student
WHERE dept = 'Computer';

(v) To list student names who have marks less than 40:

SELECT name
FROM Student
WHERE marks < 40;

(vi) To list students who are not from 'Mumbai':

SELECT *
FROM Student
WHERE place <> 'Mumbai';

Q12) i) Write a command to create table student(RNO,name marks, dept) with


proper datatypes and RNo as primary key
ii) Write a command to create and drop sequence

(i) Create table "student" with proper datatypes and RollNo as primary key:

CREATE TABLE student (


RollNo INT PRIMARY KEY,
name VARCHAR(50),
marks INT,
dept VARCHAR(50)
);

In this command, I've assumed that "RNO" is the Roll Number, "name" is the
student's name, "marks" is an integer field representing marks, and "dept" is the
department.

(ii) Create and drop sequence:

To create a sequence (used for generating auto-incremented values):

CREATE SEQUENCE seq_student_id START WITH 1 INCREMENT BY 1;


To drop the sequence:

DROP SEQUENCE seq_student_id;

Q13) Describe database privileges. Write down the procedure for granting &
revoking privileges in database objects to the users. Database privileges

Database privileges are permissions or rights granted to users or roles in a


database system to perform specific actions or operations on database objects
such as tables, views, procedures, and functions. These privileges control who can
access, modify, or manipulate data and database structures.

Common types of database privileges include:


1) SELECT: Allows users to retrieve data from tables and views.
2) INSERT: Allows users to add new records to tables.
3) UPDATE: Allows users to modify existing records in tables.
4) DELETE: Allows users to remove records from tables.
5) CREATE: Allows users to create new database objects (tables, views, etc.).
6) ALTER: Allows users to modify the structure of existing database objects.
7) DROP: Allows users to delete database objects.
8) GRANT: Allows users to grant privileges to other users.
9) REVOKE: Allows users to revoke privileges from other users.

Granting Privileges:

GRANT: To grant privileges to a user, you use the GRANT statement followed by
the privileges and the object on which you're granting the privileges.

Syntax:

GRANT privilege(s) ON object TO user;


Example:

GRANT SELECT, INSERT ON employees TO alice;


WITH GRANT OPTION: You can use the WITH GRANT OPTION clause to allow the
user receiving the privileges to further grant those same privileges to others.

Example:
GRANT SELECT ON customers TO bob WITH GRANT OPTION;
Revoking Privileges:

REVOKE: To revoke privileges from a user, you use the REVOKE statement
followed by the privileges and the object on which you're revoking the privileges.

Syntax:

REVOKE privilege(s) ON object FROM user;


Example:

REVOKE INSERT, UPDATE ON orders FROM charlie;


CASCADE: You can use the CASCADE keyword with the REVOKE statement to
automatically revoke privileges from dependent objects.

Example:

REVOKE SELECT ON employees FROM alice CASCADE;

You might also like