Dbms th-2
Dbms th-2
CREATE: This command is used to create a new table, view, index, or other database object.
Example:
CREATE TABLE employees (
id INT PRIMARY KEY,
name VARCHAR(100),
department VARCHAR(50)
);
○
ALTER: ALTER is used to modify an existing database object like a table. Example:
ALTER TABLE employees ADD COLUMN salary DECIMAL(10,2);
○
DROP: DROP is used to delete an existing database object like a table or index. Example:
DROP TABLE employees;
○
DELETE: DELETE is used to remove rows from a table based on certain conditions. Example:
DELETE FROM employees WHERE id = 1;
○
COMMIT: COMMIT is used to permanently save any changes made during the current
transaction. Example:
COMMIT;
○
2. SQL Constraints: SQL constraints are rules enforced on data columns to ensure data
integrity. Some common constraints include:
○
○
○
4. Logical Operators: SQL uses logical operators to combine conditions:
○
○
○
5. Transaction Properties:
○ Atomicity: Ensures that either all operations within a transaction are completed
or none are.
○ Consistency: Ensures that the database remains in a valid state before and
after the transaction.
○ Isolation: Ensures that transactions operate independently of each other.
○ Durability: Ensures that once a transaction is committed, its changes are
permanent even in case of system failure.
7. What are the different levels of lock granularity? Explain with example
Lock granularity refers to the level at which database locking is applied to control concurrent
access.
Example:
SELECT * FROM employees WHERE id = 1 FOR UPDATE;
○
5. Column-Level Lock: Locks a specific column in a table.
Example:
Distributed Database:
Distributed Processing:
Distributed processing refers to the use of multiple computers to process data stored in a
distributed database.
Example Diagram:
Each server holds a portion of the database, and users can access data from any server.
The Two-Phase Commit (2PC) Protocol ensures that a distributed transaction is either
committed fully or aborted across multiple databases.
Phases:
Example Scenario:
Distributed Transparency refers to the ability of a distributed database system to hide the
complexities of data distribution from users, making it appear as a single unified system.
Aggregate functions perform calculations on multiple rows and return a single value.
1.
2.
3.
5.
Common Functions:
CURRENT_DATE: Returns today’s date.
SELECT CURRENT_DATE;
1.
2.
3.
4.
5.
15. List types of Joins and explain any two with example.
Types of Joins:
Example:
SELECT employees.id, employees.name, departments.dept_name
FROM employees
INNER JOIN departments ON employees.dept_id = departments.dept_id;
●
● Returns all rows from the left table and matching rows from the right table.
Example:
SELECT employees.id, employees.name, departments.dept_name
FROM employees
LEFT JOIN departments ON employees.dept_id = departments.dept_id;
●
○ If an employee has no department, dept_name will be NULL.
1.
2.
LENGTH() – Returns the length of a string.
SELECT LENGTH('SQL Query'); -- Output: 9
3.
4.
5.
6.
Example:
SELECT * FROM employees WHERE name LIKE 'A%'; -- Names starting with 'A'
●
Example:
SELECT * FROM products WHERE price BETWEEN 100 AND 500;
●
●
Example:
SELECT * FROM employees WHERE manager_id IS NULL;
●
Example:
SELECT * FROM employees WHERE EXISTS (SELECT * FROM departments WHERE
employees.dept_id = departments.dept_id);
●
○ INT (Integer)
○ FLOAT (Floating-point)
○ DECIMAL(10,2) (Fixed decimal precision)
2. String Data Types:
○ DATE (YYYY-MM-DD)
○ TIME (HH:MM:SS)
○ DATETIME (YYYY-MM-DD HH:MM:SS)
4. Boolean Data Type:
Definition Deletes the entire table, including Deletes all rows but keeps table
structure structure
Rollback No No
Possible?
○
2. Comparison Operators: (=, !=, <, >, <=, >=)
Example:
SELECT * FROM products WHERE price > 1000;
○
3. Logical Operators: (AND, OR, NOT)
Example:
SELECT * FROM employees WHERE salary > 50000 AND department = 'IT';
○
4. Bitwise Operators: (&, |, ^, <<, >>)
Example:
SELECT 5 & 3; -- Output: 1 (Bitwise AND)
○
A transaction in SQL is a sequence of one or more database operations that are executed as a
single unit of work. Transactions ensure that the database remains consistent even in cases of
failures.
1. Atomicity:
○ Ensures the database remains in a valid state before and after the transaction.
○ Example: A product’s stock count must be updated correctly when a purchase is
made.
3. Isolation:
○ Two transactions update the same data, and one update is lost.
Example:
-- Transaction 1
-- Transaction 2
○ If both transactions read the same balance and update it without considering the
other’s changes, one update is lost.
2. Dirty Read:
○ A transaction reads the same row twice, but another transaction modifies it in
between.
Example:
SELECT balance FROM accounts WHERE id = 1;
Optimistic Concurrency Control (OCC) assumes transactions rarely conflict and allows them
to proceed without locks, verifying at the end if conflicts exist.
Phases of OCC:
Example:
○ Ensures users do not know if data is being replicated across different locations.
○ Example: Customer data available in different branches but appearing as a single
entity.
3. Fragmentation Transparency:
○ Ensures users do not need to know if data is stored in fragments across multiple
locations.
○ Example: A product table is split by region, but users retrieve it as a whole.
4. Failure Transparency:
Example:
SELECT city FROM customers
UNION
●
ii. UNION ALL (Keeps Duplicates)
Example:
SELECT city FROM customers
UNION ALL
●
Example:
SELECT city FROM customers
INTERSECT
●
● Returns rows in the first query that do not exist in the second.
Example:
SELECT city FROM customers
MINUS
●
Here are the correct answers:
1. UNIQUE constraint ensures that all values in a column are unique.
3. (a) DROP – The SQL keyword DROP is used to delete a Table structure.
4. (b) % and _ – LIKE operator uses % (matches any sequence of characters) and _
(matches a single character) for pattern matching.
7. (a) Transaction Log – Using Transaction Log, we can recover our database
transaction.
11.False – TO_DATE function converts a string to a date format but does not return
today's date. (SYSDATE returns today’s date in SQL).
Here are the correct answers:
12.Cross join is also known as Cartesian Join.
13.False – UNION operator removes duplicate rows; UNION ALL includes duplicates.
15.NOT NULL constraint ensures that column’s value should not be NULL.
16.False – DML stands for Data Manipulation Language, not Data Multiplication
Language.
17.(D) DELETE – The SQL keyword DELETE is used to delete table’s content (Data).
18.(a) BETWEEN works similar to AND, and IN operator works similar to OR.
24.False – INTERSECT operator returns only common rows and removes duplicates.