Dbms Answer
Dbms Answer
In SQL (Structured Query Language), schema change statements are used to modify the structure of a database schema, which
includes changing tables, columns, constraints, indexes, and other objects. These statements fall under the Data Definition Language
(DDL) category.
1. CREATE
Used to create new database objects like tables, views, indexes, schemas, etc.
Syntax:
Example:
2. ALTER
Used to modify an existing database object, such as a table or column. It is the most common schema change command.
a. Add a Column:
Example:
b. Modify a Column:
Example (MySQL):
c. Rename a Column:
d. Drop a Column:
Example:
3. DROP
Used to delete an entire database object, such as a table, view, index, or even the entire schema.
a. Drop a Table:
Example:
4. RENAME
Syntax
Example:
5. TRUNCATE
Not exactly a schema change, but often discussed with DDL commands. It deletes all records from a table but retains the structure.
Syntax:
Example:
6. COMMENT
Example:
Used to manage indexes, which improve query performance but also affect the schema design.
Create Index:
Drop Index:
INSERT is used to add a single tuple to a relation. We must specify the relation name and a
list of values for the tuple.
While adding values for all the columns of the table, need not specify the column names in
the SQL query. The values should be listed in the same order in which the corresponding
attributes were specified in the CREATE TABLE command.
The INSERT INTO syntax would be as follows:
The DELETE command removes tuples from a relation. The WHERE clause, selects the
tuples to be deleted. Tuples are explicitly deleted from only one table at a time.
The deletion propagates to tuples in other relations if referential triggered actions(on delete
cascade) are specified in the referential integrity constraints of the DDL.
A missing WHERE clause specifies that all tuples in the relation are to be deleted;
however, the table remains in the database as an empty table. The DROP TABLE command
to remove the table definition.
Example:
DELETE FROM EMPLOYEE
WHERE Lname=‘Brown’;
Definition:
A correlated nested query (also called a correlated subquery) is a subquery that depends on the outer
query for its values. It is executed repeatedly, once for each row selected by the outer query.
Key Characteristics:
Example: Find employees who earn more than the average salary of their department
Tables:
SQL Query:
ii) To List the Names of Managers Who Have at Least One Dependent
Explanation:
i) Join in SQL
A JOIN in SQL is used to combine rows from two or more tables based on a related column between them
(usually a foreign key relationship).
Types of JOINs:
Type Description
INNER JOIN Returns only the matching rows between the tables
LEFT JOIN Returns all rows from the left table and matching rows from the right
RIGHT JOIN Returns all rows from the right table and matching rows from the left
FULL JOIN Returns all rows when there is a match in one of the tables
Table:
Query:
HAVING Clause
HAVING is used to filter the results of groups created by GROUP BY. It's like a WHERE clause, but for groups (not
individual rows).
A trigger is a stored procedure in a database that automatically executes (fires) in response to certain events
on a particular table or view.
Think of it as an automatic action that happens when you INSERT, UPDATE, or DELETE data.
Triggers Used:
Trigger Events:
Syntax (Generic):
Tables:
SQL Trigger:
5. What is VIEW? And explain the concept of Views in SQL with examples
Views in SQL are virtual tables based on the result of an SQL query. They do not store data themselves but
provide a way to look at and manipulate the result of a query as if it were a table.
Example:
In this example, a view called HighSalaryEmployees is created based on the result of a query that selects
employees with a salary greater than 50,000. Once the view is created, you can query it as if it were a table:
Benefits of Views:
● Making sure that the semantics of the attributes is clear in the schema
● Reducing the redundant information in tuples
● Reducing the NULL values in tuples
● Disallowing the possibility of generating spurious tuples
Making sure that the semantics of the attributes is clear in the schema
6. What are Functional Dependencies? And explain the following Normal forms
with relation example for each. i) 2NF ii) 3NF iii) BCNF
A functional dependency (FD) is a relationship between two sets of attributes in a relation (table). It expresses
a constraint between attributes such that:
If two rows have the same value for attribute A, they must have the same value for attribute B.
Example:
Table: StudentCourse
1. It is in 2NF.
2. There is no transitive dependency (i.e., non-prime attribute should not depend on another non-prime
attribute).
Example:
Table: Employee
1 Alice D1 Sales
2 Bob D2 HR
3 Carol D1 Sales
1. It is in 3NF.
2. For every non-trivial functional dependency X → Y, X must be a super key.
Example:
Table: ProfessorSubject
P1 Math Science
P2 Physics Science
P1 Math Science
The ACID properties ensure that database transactions are processed reliably.
They are:
1. Atomicity: Ensures that all operations within a transaction are completed. If any part of the transaction
fails, the entire transaction is rolled back, and the database remains unchanged.
Example: If a bank transfer transaction debits one account but fails to credit another, the entire transaction
is reversed.
2. Consistency: Ensures that a transaction brings the database from one valid state to another. The
database must meet all the rules and constraints after the transaction.
Example: If a transaction violates any constraints, such as a foreign key constraint, the database should be
rolled back.
3. Isolation: Ensures that transactions are executed in isolation from one another. Intermediate results of a
transaction are not visible to other transactions until the transaction is committed.
Example: If two users try to update the same account balance simultaneously, isolation ensures that both
transactions are handled in a serializable manner.
4. Durability: Ensures that once a transaction is committed, it remains so, even in the event of a system
crash. The results of the transaction are permanently stored in the database.
Example: Once a bank transfer transaction is committed, it is reflected in the database even if the system
crashes immediately afterward.
Concurrency control is essential in a database management system (DBMS) to ensure the consistency,
integrity, and isolation of data when multiple transactions are executed concurrently.
When multiple users or applications access and modify the database at the same time, problems like data
inconsistency, lost updates, dirty reads, or uncommitted data can occur. Concurrency control mechanisms
help prevent these issues.
Assume a bank database with a table Accounts having the following record:
Account_No Balance
1001 5000
This is a case of lost update problem, where the update from T1 is overwritten by T2.
With Concurrency Control (Serial Execution)
Either:
Or:
Concurrency control ensures correct execution of multiple transactions occurring simultaneously without
violating data integrity. One major approach is locking, where access to data items is controlled using locks.
Locks are variables associated with data items to indicate whether a transaction can access them.
Binary Locks
To allow greater concurrency, the multiple-mode lock approach is used. This allows:
States of LOCK(X):
Unlocked
Read-locked (Shared Lock): Multiple transactions can read.
Write-locked (Exclusive Lock): Only one transaction can read/write.
Locking Rules
A transaction must:
Conversion of Locks
1. Growing Phase:
o Can acquire any number of locks.
o No unlocks allowed.
2. Shrinking Phase:
o Can release locks.
o No new locks allowed.
Create
Read
Update
Delete
1. Create (Insert)
db.students.insertOne({
name: "Alice",
age: 21,
course: "Computer Science"
});
db.students.insertMany([
{ name: "Bob", age: 22, course: "Math" },
{ name: "Charlie", age: 20, course: "Physics" }
]);
2. Read (Query)
db.students.find({});
3. Update
db.students.updateOne(
{ name: "Alice" },
{ $set: { age: 22 } }
);
db.students.updateMany(
{ course: "Math" },
{ $set: { grade: "A" } }
);
4. Delete