Comprehensive Guide to DBMS Concepts
Comprehensive Guide to DBMS Concepts
DDL commands define and manage the structure of a database, whereas DML commands manipulate the data within those structures. DDL commands include CREATE, ALTER, and DROP, which create, modify, and delete the structures of database objects, respectively. For example, CREATE TABLE Student(id NUMBER, name VARCHAR2(20)) establishes a new table structure . Conversely, DML commands like INSERT, UPDATE, and DELETE are used to add, modify, or remove data within existing tables. INSERT INTO Employee VALUES(101, 'Amit', 30000) is an instance of DML operation that inserts a record into a table . Thus, DDL deals with schema manipulation, while DML involves data manipulation within those schemas.
Backup strategies are essential for safeguarding database data against various types of failure such as transaction failure, system crash, media failure, and application failure. Backup types include: 1) Full Backup, which creates a complete copy of the entire database, securing all data at a specific point in time . 2) Incremental Backup, which only saves data that has changed since the last backup, optimizing storage space and time . 3) Differential Backup, which backs up data changed since the last full backup, balancing full and incremental processes . These backups protect against data loss due to unexpected failures like power loss, hardware malfunction, software bugs, or human errors, ensuring data availability and continuity .
Aggregate functions in SQL perform calculations on multiple rows of a single column and return a single value. These functions include SUM, AVG, COUNT, and MAX/MIN . For example, SUM(Salary) calculates the total of all salary values in the Employee table . They are often used with the GROUP BY clause to provide summary data; for example, grouping employees by department to calculate the total salary per department. This enhances data analysis by allowing the aggregation of data into meaningful summaries .
Set operators in SQL are used to combine results from multiple queries into a single result set. UNION merges the results of two queries, returning unique records from both . UNION ALL also combines results but includes all duplicates, beneficial for complete dataset combination rather than deduplicated results . INTERSECT returns only those records that appear in both queries, useful for finding common items across datasets . MINUS yields records present in the first query but not in the second, effective for identifying differences between datasets . These operators facilitate complex data retrieval and are applied depending on whether you need to unite, exclude, or find commonalities between datasets.
Sequences in SQL are important for generating unique numeric identifiers, often used for primary keys. They operate independently of table data, increasing values with each use based on specified increments. For example, CREATE SEQUENCE Cust_seq START WITH 1 INCREMENT BY 1 initializes a sequence starting at 1, incrementing by 1 for each subsequent value . Sequences ensure that identifiers are unique and efficiently manage surrogate keys across tables without requiring complex logic in the application code . They are particularly effective in multi-user environments or when generating values for parallel insertions.
Creating a view in SQL involves defining a virtual table based on the result set of a query. The syntax is CREATE VIEW view_name AS SELECT column1, column2 FROM table WHERE condition. For example, CREATE VIEW Pune_Cust AS SELECT Cust_id, Cust_name FROM Customer WHERE Cust_city='Pune' creates a view for customer data in Pune . Views simplify complex queries by abstracting commonly used query patterns, enhance security by providing restricted data access, and support logical data segmentation, easing maintenance and enhancing consistency across user queries .
PL/SQL functions enhance database performance and maintainability by encapsulating reusable chunks of code that can be invoked as needed. This approach supports modularity, reduces code duplication, and improves efficiency by streamlining repetitive calculations. An example is the function get_bonus, which calculates a 10% bonus based on a given salary: CREATE OR REPLACE FUNCTION get_bonus(salary NUMBER) RETURN NUMBER IS BEGIN RETURN salary*0.10; END . This function can be reused across different parts of an application, minimizing errors and saving development time, ultimately improving performance and maintainability through concise code organization .
The ACID properties in database transactions ensure reliability and data integrity. They include: 1) Atomicity, which ensures that all parts of a transaction are completed successfully or none at all, maintaining an all-or-nothing execution . 2) Consistency, which guarantees that a transaction brings the database from one valid state to another, preserving database integrity . 3) Isolation, which ensures that transactions are executed independently, preventing them from affecting each other . 4) Durability, which ensures that once a transaction is committed, its changes are permanent, even in the event of a system failure . These properties collectively maintain the stability and correctness of the database system, even under concurrent operations.
A PL/SQL program can identify the largest of three numbers using conditional IF-ELSE logic. Consider the following program: DECLARE a NUMBER := &a; b NUMBER := &b; c NUMBER := &c; largest NUMBER; BEGIN IF a>b AND a>c THEN largest:=a; ELSIF b>c THEN largest:=b; ELSE largest:=c; END IF; DBMS_OUTPUT.PUT_LINE('Largest number is: '||largest); END . This program evaluates each condition sequentially to determine the largest number. This kind of logic is beneficial in database applications for tasks requiring comparison-based evaluations, such as ranking items, conditional data processing, or making decisions based on SQL query results .
DML commands in SQL are used to modify the data stored in database tables without altering the structure of the database itself. Examples of DML commands include: 1) INSERT, which adds new records to a table, with syntax like INSERT INTO Employee VALUES(101, 'Amit', 30000). 2) UPDATE, which changes existing records, for example, UPDATE Employee SET Salary=35000 WHERE Emp_ID=101 . 3) DELETE, which removes records from a table, such as DELETE FROM Employee WHERE Emp_ID=101 . 4) SELECT, which retrieves data, for instance, SELECT * FROM Employee WHERE Salary>25000 . Each of these commands enables specific operations on the data to facilitate various requirements for data manipulation.