DBMS Lab Viva Questions & Answers
DBMS Lab Viva Questions & Answers
Normalization organizes database tables to minimize redundancy and dependency, leading to improvements in data integrity. By dividing larger tables into smaller ones and defining relationships between them, it avoids anomalies during data operations. For example, in a non-normalized table, a product's supplier information may be repeated multiple times, but normalization would separate this into a 'Product' and 'Supplier' table linked by a Supplier ID. Benefits include reduced data redundancy, ensuring data integrity by eliminating update anomalies, and optimized database performance .
Data types in SQL define the kind of values that can be stored in a column, which facilitates appropriate database operations and optimizes storage. Each column in a database table is assigned a data type, such as INT for integers, VARCHAR for variable-length strings, DATE for dates, and FLOAT for floating-point numbers. This definition is critical because it determines how SQL manages data storage, retrieval, and processing effectively. For example, assigning an INT data type properly allows SQL to perform arithmetic operations during queries efficiently, while VARCHAR facilitates text operations .
Indexes in SQL improve the speed of data retrieval operations on a table. They function similarly to an index in a book, allowing the database engine to find data quickly without scanning the entire table. However, they can also slow down data insertion and update operations since the index itself must be updated. Therefore, indexes should be applied to columns frequently used in WHERE clauses, JOIN conditions, and ORDER BY statements to enhance performance in query operations. For example, creating an index on a 'customer_id' column in a sales database can substantially speed up customer-specific queries .
The ALTER command is used to modify the structure of an existing table. This includes adding or deleting columns, changing the data type of a column, or renaming columns or the table itself. An example use case is when a new requirement necessitates the addition of a new column to store data not previously accounted for. The command format for adding a column is: ALTER TABLE table_name ADD column_name datatype. It can also be used to drop a column if it is no longer needed: ALTER TABLE table_name DROP COLUMN column_name. These modifications help maintain the database’s adaptability to evolving data needs .
Data integrity in a relational database is maintained through constraints that enforce rules on the data in tables. Specific SQL constraints include PRIMARY KEY, which ensures that each row in a table is unique and not null; FOREIGN KEY, which maintains referential integrity between tables by ensuring that a value in one table corresponds to a value in another; UNIQUE, which ensures that all values in a column are different; NOT NULL, which prevents null values in a column; and CHECK, which enforces data validation rules on columns. Together, these constraints help in preserving data accuracy and consistency across the database .
Subqueries, or nested queries, are used in SQL to perform operations that require multiple steps, such as filtering results based on aggregated data. They allow breaking a complex problem into smaller, logical parts, which simplifies query construction and increases readability. An example of a subquery is: SELECT name FROM students WHERE id IN (SELECT student_id FROM scores WHERE score > 80). This subquery selects student IDs from the 'scores' table where the score exceeds 80, and the outer query retrieves corresponding student names from the 'students' table. Subqueries are significant because they enable dynamic query inputs and conditions, supporting advanced data retrieval and analysis .
SQL JOIN operations are essential for enhancing database functionality by allowing data to be retrieved from multiple tables based on logical relationships. They are integral to relational databases because they enable queries that reflect complex real-world relationships and conditions. JOINS, such as INNER JOIN, LEFT JOIN, and FULL JOIN, allow combining rows from two or more tables based on related columns. For example, in a database with 'orders' and 'customers' tables, an INNER JOIN can be used to list orders alongside customer information where customer ID matches in both tables, thereby providing a comprehensive view of transactions .
Views in SQL are used in scenarios where a specific representation of data is required without altering the actual tables. They are particularly useful for simplifying complex queries, providing data security by restricting access to certain columns, and presenting aggregated data in a user-friendly manner. For example, a view can be created to show only the names and salaries of employees, which hides detailed information. Advantages of views include ease of reuse of SQL logic, reduced SQL query complexity for users, and enhanced security by showing only necessary data to specific users .
DELETE, TRUNCATE, and DROP are SQL commands with notably different implications. DELETE is used to remove specific rows from a table and allows for filtration through WHERE clauses, affecting only selected data; it also supports transaction rollback. TRUNCATE, on the other hand, removes all rows from a table without logging individual row deletions but keeps the table structure for future use. It is faster than DELETE since it does not generate row-level locks. DROP, the most drastic, removes the table structure completely, along with all its data, making it irreversible and non-recoverable without backups. The choice among them depends on whether only the data or the structure too should be discarded .
The GRANT command in SQL is significant for implementing and managing database security. It is used to give specific privileges on database objects to users, such as SELECT, INSERT, UPDATE, DELETE on tables or EXECUTE on procedures. By controlling access, it helps protect sensitive data from unauthorized users and regulates user actions within the database, which is crucial for maintaining data confidentiality, integrity, and availability. For instance, granting only SELECT privileges to a user for a table restricts them to data retrieval without altering it, thereby ensuring controlled data access .