0% found this document useful (0 votes)
0 views

Database Management Answers

The document provides SQL queries for creating a 'students' table, inserting records, deleting rows based on age, updating grades, and displaying student information using PL/SQL. It includes explanations for each query, detailing the purpose and functionality of the SQL commands. The examples cover various operations such as constraints, data types, and cursor usage.
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
0 views

Database Management Answers

The document provides SQL queries for creating a 'students' table, inserting records, deleting rows based on age, updating grades, and displaying student information using PL/SQL. It includes explanations for each query, detailing the purpose and functionality of the SQL commands. The examples cover various operations such as constraints, data types, and cursor usage.
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 2

Q1: Write an SQL query to create a table named "students" with columns "id", "name", "age", and "grade".

Answer:
CREATE TABLE students (
id INT PRIMARY KEY,
name VARCHAR(50) NOT NULL,
age INT CHECK (age > 0),
grade DECIMAL(3,2) CHECK (grade BETWEEN 0 AND 4.0)
);
Explanation: The id column is a PRIMARY KEY ensuring uniqueness. The name column is VARCHAR(50) and NOT
NULL. The age column has a CHECK constraint for positive values. The grade column is DECIMAL(3,2) with a CHECK
constraint for valid range.

Q2: Write an SQL query to insert a new row into the "students" table with id=1, name="John", age=20, and grade=3.5.
Answer:
INSERT INTO students (id, name, age, grade) VALUES (1, 'John', 20, 3.5);
Explanation: The INSERT INTO statement adds a new record to the students table. The VALUES clause specifies the
data. 'John' is a string, requiring quotes. id, age, and grade are numbers, so they do not need quotes.
Inserting multiple records:
INSERT INTO students (id, name, age, grade) VALUES (2, 'Alice', 22, 3.8), (3, 'Bob', 19, 2.9), (4, 'Eve', 21, 3.2);

Q3: Write an SQL query to delete all rows from the "students" table where age is less than 18.
Answer:
DELETE FROM students WHERE age < 18;
Explanation: The DELETE FROM statement removes records where age < 18. Without the WHERE clause, all rows
would be deleted.
Checking records before deletion:
SELECT * FROM students WHERE age < 18;
Using TRUNCATE to remove all records:
TRUNCATE TABLE students;

Q4: Write an SQL query to update the grade of the student with id=1 to 4.0.
Answer:
UPDATE students SET grade = 4.0 WHERE id = 1;
Explanation: The UPDATE statement modifies the grade of the student with id=1. The WHERE clause ensures only this
record is updated.
Updating multiple columns:
UPDATE students SET grade = 4.0, age = 21 WHERE id = 1;
Updating multiple records:
UPDATE students SET grade = grade + 0.5 WHERE grade < 3.0;
Checking after update:
SELECT * FROM students WHERE id = 1;

Q5: Write a PL/SQL block to display the name and age of all students who have a grade greater than 3.0.
Answer:
DECLARE
CURSOR student_cursor IS SELECT name, age FROM students WHERE grade > 3.0;
student_name students.name%TYPE;
student_age students.age%TYPE;
BEGIN
OPEN student_cursor;
LOOP
FETCH student_cursor INTO student_name, student_age;
EXIT WHEN student_cursor%NOTFOUND;
DBMS_OUTPUT.PUT_LINE('Name: ' || student_name || ', Age: ' || student_age);
END LOOP;
CLOSE student_cursor;
END;
/

Explanation: The cursor student_cursor selects students with grade > 3.0. Variables store fetched data. The cursor
opens, loops through records, and prints student details. Finally, the cursor is closed.

You might also like