Dbms 2
Dbms 2
2. Describe the data types available for attributes with syntax and
examples.
SQL supports several data types, including:
Numeric:
o INT: Integer values.
sql
Copy code
age INT;
o DECIMAL(p, s): Fixed-point numbers.
sql
Copy code
price DECIMAL(10, 2);
String:
o VARCHAR(n): Variable-length strings.
sql
Copy code
name VARCHAR(50);
o TEXT: Long text data.
sql
Copy code
description TEXT;
Date/Time:
o DATE: Stores dates.
sql
Copy code
birth_date DATE;
Boolean:
o BOOLEAN: Stores TRUE or FALSE values.
sql
Copy code
is_active BOOLEAN;
3. Explain the CREATE TABLE command in SQL and write the syntax and
example.
The CREATE TABLE command is used to create a new table in a database.
Syntax:
sql
Copy code
CREATE TABLE table_name (
column1 datatype,
column2 datatype,
...
);
Example:
sql
Copy code
CREATE TABLE Employees (
employee_id INT PRIMARY KEY,
first_name VARCHAR(50),
last_name VARCHAR(50),
hire_date DATE
);
11. Write the EXISTS and UNIQUE Functions in SQL with syntax and
example.
EXISTS: Checks for the existence of rows returned by a subquery.
Syntax:
sql
Copy code
SELECT column
FROM table
WHERE EXISTS (SELECT 1 FROM another_table WHERE condition);
Example:
sql
Copy code
SELECT name FROM Students WHERE EXISTS (SELECT 1 FROM Enrollments
WHERE student_id = Students.student_id);
UNIQUE: Typically used in table definitions to enforce uniqueness; in queries,
use DISTINCT.
Example:
sql
Copy code
SELECT DISTINCT course_name FROM Enrollments;
12. What are the different types of joining the tables with syntax and
example?
Different types of joins in SQL include:
INNER JOIN: Returns matching rows from both tables.
sql
Copy code
SELECT * FROM A INNER JOIN B ON A.id = B.id;
LEFT JOIN: Returns all rows from the left table and matched rows from the
right.
sql
Copy code
SELECT * FROM A LEFT JOIN B ON A.id = B.id;
RIGHT JOIN: Returns all rows from the right table and matched rows from
the left.
sql
Copy code
SELECT * FROM A RIGHT JOIN B ON A.id = B.id;
FULL OUTER JOIN: Returns all rows when there is a match in either table.
sql
Copy code
SELECT * FROM A FULL OUTER JOIN B ON A.id = B.id;
14. Illustrate the GROUP BY and HAVING clauses with syntax and
example.
GROUP BY groups rows that have the same values in specified columns,
allowing aggregate functions to be applied.
Syntax:
sql
Copy code
SELECT column1, aggregate_function(column2)
FROM table
GROUP BY column1;
Example:
sql
Copy code
SELECT product, SUM(amount) AS total_sales FROM sales GROUP BY product;
HAVING filters groups based on a condition after aggregation.
Syntax:
sql
Copy code
SELECT column1, aggregate_function(column2)
FROM table
GROUP BY column1
HAVING condition;
Example:
sql
Copy code
SELECT product, SUM(amount) AS total_sales FROM sales GROUP BY product
HAVING SUM(amount) > 400;
15. What is a trigger? Explain the mechanism of a trigger with syntax
and example.
A trigger is a special type of stored procedure that automatically executes in
response to certain events on a specified table, such as INSERT, UPDATE, or
DELETE.
Syntax:
sql
Copy code
CREATE TRIGGER trigger_name
AFTER INSERT ON table_name
FOR EACH ROW
BEGIN
-- Trigger logic
END;
Example:
sql
Copy code
CREATE TRIGGER after_employee_insert
AFTER INSERT ON Employees
FOR EACH ROW
BEGIN
INSERT INTO AuditLog (action, employee_id) VALUES ('INSERT',
NEW.employee_id);
END;
17. Define a view. Discuss the SQL CREATE VIEW statement with
example.
A view is a virtual table based on the result of a query. It can simplify complex
queries, encapsulate complex joins, and restrict access to data.
Syntax:
sql
Copy code
CREATE VIEW view_name AS
SELECT column1, column2
FROM table
WHERE condition;
Example:
sql
Copy code
CREATE VIEW ActiveEmployees AS
SELECT first_name, last_name
FROM Employees
WHERE is_active = TRUE;
21. What is ALTER and DROP command? Give syntax and example.
ALTER: Used to modify an existing database object.
Syntax:
sql
ALTER TABLE table_name
ADD column_name datatype;
Example:
sql
Copy code
ALTER TABLE Employees ADD email VARCHAR(100);
DROP: Used to delete a database object.
Syntax:
sql
Copy code
DROP TABLE table_name;
Example:
sql
Copy code
DROP TABLE Employees;
23. Illustrate First, Second, and Third Normal Forms with examples.
First Normal Form (1NF): Ensures that all attributes contain atomic
values.
o Example: A table of student courses should not have a multi-valued
column.
Second Normal Form (2NF): Achieved when it is in 1NF and all non-key
attributes are fully functionally dependent on the primary key.
o Example: In a table with StudentID and CourseID, ensure no partial
dependencies.
Third Normal Form (3NF): Achieved when it is in 2NF and there are no
transitive dependencies.
o Example: If a student’s department is stored in the same table, it
should be moved to a separate table to eliminate dependency.
27. What is JDBC? Write the JDBC classes and interfaces with examples.
JDBC (Java Database Connectivity) is an API in Java for connecting and
executing queries with databases.
Key Classes and Interfaces:
DriverManager: Manages database drivers.
Connection: Represents a connection to a database.
Statement: Used to execute SQL queries.
ResultSet: Represents the result set of a query.
Example:
java
Copy code
Connection conn =
DriverManager.getConnection("jdbc:mysql://localhost:3306/mydb", "user",
"password");
Statement stmt = conn.createStatement();
ResultSet rs = stmt.executeQuery("SELECT * FROM Employees");
while (rs.next()) {
System.out.println(rs.getString("first_name"));
}