Assignment 2
Assignment 2
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.
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.
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.
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);
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:
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;
Example of 2NF:
Consider a table named "StudentCourses" with columns: StudentID,
CourseID, CourseName, and Instructor. Here, (StudentID, CourseID) is the
composite primary key.
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.
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:
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.
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."
Another example is if you want to retrieve customers with a last name of exactly
three characters:
(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%';
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
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');
UPDATE Student
SET marks = 96
WHERE name = 'Rahul';
(v) To list student names who have marks less than 40:
SELECT name
FROM Student
WHERE marks < 40;
SELECT *
FROM Student
WHERE place <> 'Mumbai';
(i) Create table "student" with proper datatypes and RollNo as primary key:
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.
Q13) Describe database privileges. Write down the procedure for granting &
revoking privileges in database objects to the users. Database privileges
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:
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:
Example: