DBMS Manual
DBMS Manual
VSITR-IT
PRACTICAL-1
Syntax:
CREATE TABLE table_name(
column_1 data_type(size),
column_2 data_type(size),
column_3 data_type(size),
column_3 data_type(size)
);
Example:
CREATE TABLE Student(
S_ID int;
S_FirstName varchar(25),
S_LastName varchar(25),
S_Semester int,
S_Department varchar(5),
S_PhoneNumber varchar(10),
S_Email_ID varchar(30),
S_City varchar(15),
S_State varchar(15)
);
Syntax:
ALTER TABLE table_name
RENAME TO new_table_name;
Syntax:
ALTER TABLE table_name
RENAME COLUMN old_name TO new_name;
Example:
ALTER TABLE Student
RENAME COLUMN S_Pincode TO S_PCode;
Example:
ALTER TABLE Student
DROP COLUMN S_PCode;
To Delete All the Records from the Table, we use TRUNCATE command:
NOTE : This will not Affect the Structure of the Table i.e., Table and its Columns will not be Deleted only the data
inside it will get Deleted.
NOTE : This will Delete the whole Table along with its Columns and Data containing it.
Syntax:
CREATE TABLE Student(
S_ID int PRIMARY KEY;
S_FirstName varchar(25),
S_LastName varchar(25),
S_Semester int,
S_Department varchar(5),
S_PhoneNumber int,
S_Email_ID varchar(20),
S_City varchar(10),
S_State varchar(10),
OR
Example:
CREATE TABLE Student(
S_ID int ;
S_FirstName varchar(25),
S_LastName varchar(25),
S_Semester int,
S_Department varchar(5),
S_PhoneNumber int,
S_Email_ID varchar(20),
S_City varchar(10),
S_State varchar(10),
PRIMARY KEY (S_ID)
);
Syntax:
ALTER TABLE table_name
ADD PRIMARY KEY (column_name);
Example:
ALTER TABLE Student
ADD PRIMARY KEY (S_ID);
Example:
CREATE TABLE Parents_Detail(
S_ID int,
S_FatherName varchar(25),
S_MotherName varchar(25),
F_PhoneNumber int,
M_PhoneNumber int,
FOREIGN KEY (S_ID) REFERENCES Student(S_ID)
);
Syntax:
SELECT column1,column2, .... FROM table_name;
OR
SELECT * FROM table_name;
Example:
SELECT S_ID, S_FirstName, S_LastName FROM Student;
OR
SELECT * FROM Student;
Syntax:
INSERT INTO table_name
VALUES(value1,value2,value3, .....);
Example:
Syntax:
Example:
PRACTICAL-3
To Return the No. of Rows that Matches a Specified Criterion, we use COUNT()
function:
Syntax: SELECT COUNT(column_name) FROM table_name;
To Group Rows that have the same values, we use GROUP BY clause:
NOTE: The HAVING clause was added because the WHERE keyword cannot be used with aggregate functions.
PRACTICAL-4
UPPER() Function:
lower() Function:
CONCAT() Function:
LENGTH() Function:
Syntax:
SELECT LENGTH(column_name) FROM table_name;
Example:
SELECT FirstName, LENGTH(S_FirstName) AS Len_FirstName
FROM Student;
SUBSTR() Function:
INSTR() Function:
LPAD() Function:
RPAD() Function:
REPLACE() Function:
CURDATE() Function:
CURTIME() Function:
DATE() Function:
EXTRACT() Function:
NOTE: UNITS can be MICROSECOND, SECOND, MINUTE, HOUR, DAY, WEEK, MONTH, QUARTER, YEAR, etc.
DATE_ADD() Function:
NOTE: UNITS can be MICROSECOND, SECOND, MINUTE, HOUR, DAY, WEEK, MONTH, QUARTER, YEAR, etc.
DATEDIFF() Function:
Math Functions:
POWER() Function:
LOG() Function:
ABSOLUTE() Function:
SQUARE_ROOT() Function:
SINE() Function:
COSINE() Function:
MODULO() Function:
CEIL() Function:
Exponenent() Function:
GREATEST() Function:
LEASR() Function:
TRUNCATE() Function:
PRACTICAL-5
SUM() Function:
AVERAGE() Function:
Syntax: SELECT AVG(column_name) FROM table_name;
MIN() Function:
MAX() Function:
INNER JOIN:
SORATHIYA JENISH 23BEIT54020
DBMS
VSITR-IT
The INNER JOIN selects records that have matching values in both tables.
LEFT JOIN:
The LEFT JOIN returns all records from the left table (table1), and the matching records from the
right table (table2). The result is 0 records from the right side, if there is no match.
RIGHT JOIN:
The RIGHT JOIN returns all records from the right table (table2), and the matching records from
the left table (table1). The result is 0 records from the left side, if there is no match.
SELF JOIN:
A SELF JOIN is a regular join, but the table is joined with itself.
PRACTICAL-7
COMMIT:
Syntax: COMMIT;
SAVEPOINT:
Creates a savepoint within the transaction, allowing a partial rollback to this point.
ROLLBACK:
Rolls back the current transaction, undoing all changes made since the last COMMIT or
ROLLBACK.
Syntax: ROLLBACK;
ROLLBACK TO SAVEPOINT:
Rolls back the transaction to the specified savepoint, undoing changes made after the
savepoint was set.
Syntax: ROLLBACK;
Example: START TRANSACTION;
SAVEPOINT S1;
UPDATE Student SET S_FirstName = 'Meet' WHERE S_ID = 1;
SELECT * FROM Student WHERE S_FirstName Like ('M%');
COMMIT;
privilege_type: The type of permission you want to grant, such as SELECT, INSERT, UPDATE, etc.
object: The table, view, or database on which the privilege is being granted.
user: The user or role to whom the permission is granted.
WITH GRANT OPTION: (Optional) Allows the user to grant this privilege to others.
UNION Operator:
NOTE: The UNION operator selects only Distinct values, use UNION ALL operator to also select Duplicate values.
INTERSECT Operator:
EXCEPT(MINUS) Operator:
PRACTICAL-10
The first step in performing a join query is to clearly define the tables that will participate in
the join operation.
BTech_Students: Contains student information such as S_ID, Name, M_Name, and Favorite_Subject.
BTech_Mentors: Contains mentor details such as S_ID, M_Name, Department, and Phone_Number.
The next step is to identify how the tables are related by finding the common column.
In our case, Both BTech_Students and BTech_Mentors share the S_ID column. The join
condition will specify that rows from the BTech_Students table should match rows from
the BTech_Mentors table where the S_ID values are the same.
For our case, we’ll use an INNER JOIN since we only want to return records that exist in both
the BTech_Students and BTech_Mentors tables.
Determine which specific columns you want to include in the final result. This helps in
retrieving only the necessary data from each table, making the output more concise and
focused.
Now, combine all the components to form the final SQL query that will join the two tables
and display the desired columns.
Considering our case, using indexes on the S_ID column in both tables will enhance
performance.
This ensures that the database efficiently searches for matching rows between the two
tables.
Use the EXPLAIN command to analyze how the SQL query will be executed. This provides
insights into the database's execution plan, such as which indexes are used and how the
tables are accessed.
Finally, execute the query and verify that the results are correct, confirming that the join
operation was successful and the data was retrieved as expected.
NOTE: MySQL doesn’t directly support PL/SQL, which is used for the procedural programming for the oracle
databases.
INSERT Operation:
Example: DECLARE
v_name VARCHAR2(50) := 'Jenish';
BEGIN
INSERT INTO BTech_Students
VALUES (106, v_name, 'Mr. Shah', 'Mathematics');
COMMIT;
UPDATE Operation:
Example: BEGIN
UPDATE BTech_Students
SET Favorite_Subject = 'Physics'
WHERE S_ID = 101;
COMMIT;
DBMS_OUTPUT.PUT_LINE('Record updated successfully.');
EXCEPTION
WHEN OTHERS THEN
ROLLBACK;
DBMS_OUTPUT.PUT_LINE('An error occurred during update.');
END;
DELETE Operation:
Example: BEGIN
BTech_Students
Syntax: BEGIN
IF condition THEN
-- Statements
ELSIF condition THEN
-- Statements
ELSE
-- Statements
END IF;
END;
Example: DECLARE
student_subject VARCHAR(50);
BEGIN
DBMS_OUTPUT.PUT_LINE('All_Student_Names');
SELECT Favorite_Subject INTO student_subject
FROM BTech_Students
WHERE S_ID = 101;
Simple Loop:
Syntax: BEGIN
LOOP
-- statements
EXIT WHEN condition;
END LOOP;
END;
Example: DECLARE
student_name BTech_Students.Name%TYPE;
student_id BTech_Students.S_ID%TYPE := 101;
BEGIN
DBMS_OUTPUT.PUT_LINE('All_Student_Names');
LOOP
SELECT Name INTO student_name FROM BTech_Students WHERE S_ID = student_id;
DBMS_OUTPUT.PUT_LINE('S_ID: ' || student_id || ' Student Name: ' || student_name);
student_id := student_id + 1;
EXIT WHEN student_id > 105;
END LOOP;
END;
WHILE Loop:
Syntax: BEGIN
WHILE condition LOOP
-- statements
END LOOP;
END;
Example: DECLARE
student_name BTech_Students.Name%TYPE;
student_id BTech_Students.S_ID%TYPE := 101;
BEGIN
DBMS_OUTPUT.PUT_LINE('All_Student_Names');
WHILE student_id < 104 LOOP
SELECT Name INTO student_name FROM BTech_Students WHERE S_ID = student_id;
DBMS_OUTPUT.PUT_LINE('S_ID: ' || student_id || ' Student Name: ' || student_name);
student_id := student_id + 1;
SORATHIYA JENISH 23BEIT54020
DBMS
VSITR-IT
END LOOP;
END;
FOR Loop:
Syntax: BEGIN
FOR counter IN start_value .. end_value LOOP
-- statements
END LOOP;
END;
Example: DECLARE
student_name BTech_Students.Name%TYPE;
BEGIN
DBMS_OUTPUT.PUT_LINE('All_Student_Names');
FOR student_id IN 101..104 LOOP
SELECT Name INTO student_name FROM BTech_Students WHERE S_ID = student_id;
DBMS_OUTPUT.PUT_LINE('S_ID: ' || student_id || ' Student Name: ' || student_name);
END LOOP;
END;
Syntax: BEGIN
<<label>> -- label definition
-- statements
GOTO label; -- jumping to the label
END;
Example: DECLARE
student_name BTech_Students.Name%TYPE;
student_id BTech_Students.S_ID%TYPE := 101;
BEGIN
DBMS_OUTPUT.PUT_LINE('All_Student_Names');
<<start_loop>>
LOOP
IF student_id = 103 THEN
GOTO skip_student;
END IF;
<<skip_student>>
DBMS_OUTPUT.PUT_LINE('Skipped student with ID 103');
END;
Syntax: BEGIN
CASE expression
WHEN value1 THEN
-- statements
WHEN value2 THEN
-- statements
...
[ELSE
-- statements]
END CASE;
END;
Example: DECLARE
student_subject VARCHAR(50);
BEGIN
DBMS_OUTPUT.PUT_LINE('Favourite_Subject');
SELECT Favorite_Subject INTO student_subject
FROM BTech_Students
WHERE S_ID = 101;
CASE student_subject
WHEN 'Mathematics' THEN
DBMS_OUTPUT.PUT_LINE('The student likes Mathematics.');
WHEN 'Computer Science' THEN
DBMS_OUTPUT.PUT_LINE('The student likes Computer Science.');
WHEN 'Biology' THEN
DBMS_OUTPUT.PUT_LINE('The student likes Biology.');
ELSE
DBMS_OUTPUT.PUT_LINE('The student has a different favourite subject.');
END CASE;
END;
PRACTICAL-13
Implicit Cursors:
Implicit cursors are automatically created by PL/SQL when a SQL statement is executed. You
don't need to declare them explicitly.
Syntax: DECLARE
variable_name datatype;
BEGIN
SELECT column_name INTO variable_name
FROM table_name
WHERE condition;
END;
Example: DECLARE
v_title VARCHAR(100);
v_author VARCHAR(100);
BEGIN
DBMS_OUTPUT.PUT_LINE('Book_Info');
Explicit Cursors:
Explicit cursors are declared explicitly by the programmer when you need to process multiple
rows.
Syntax: DECLARE
CURSOR cursor_name IS
SELECT column_list FROM table_name WHERE condition;
LOOP
FETCH cursor_name INTO record_variable; -- Fetch data into record
EXIT WHEN cursor_name%NOTFOUND; -- Exit when no more rows
Example: DECLARE
v_title VARCHAR(100);
v_author VARCHAR(100);
v_result TEXT DEFAULT '';
CURSOR c_books IS
SELECT Title, Author FROM Books;
BEGIN
DBMS_OUTPUT.PUT_LINE('Book_Info');
OPEN c_books;
LOOP
SORATHIYA JENISH 23BEIT54020
DBMS
VSITR-IT
FETCH c_books INTO v_title, v_author;
IF v_title IS NULL THEN
EXIT;
END IF;
SET v_result = CONCAT(v_result, 'Title: ', v_title, ',\nAuthor: ', v_author, '\n\n');
END LOOP;
CLOSE c_books;
DBMS_OUTPUT.PUT_LINE(v_result);
END;
Books
UPDATE Books
SET Price = p_increase_amount
WHERE Book_ID = p_book_id;
SORATHIYA JENISH 23BEIT54020
DBMS
VSITR-IT
END Increase_Price;
DECLARE
v_increase_amount NUMBER := 5;
BEGIN
DBMS_OUTPUT.PUT_LINE('Updated_Price');
Increase_Price(1, v_increase_amount);
DBMS_OUTPUT.PUT_LINE('New Price: $' || v_increase_amount);
END;
In PL/SQL, a function allows you to encapsulate a piece of logic that can return a single value or a
table of values. Functions are similar to stored procedures, but they are designed to return a value
and can be used in SQL statements.
RETURN v_discounted_price;
END Get_Discounted_Price;
DECLARE
v_discounted_price DECIMAL(10, 2);
BEGIN
v_discounted_price := Get_Discounted_Price(1, 10);
DBMS_OUTPUT.PUT_LINE('Discounted Price');
DBMS_OUTPUT.PUT_LINE(v_discounted_price);
END;
PRACTICAL-15
Now we want to create a trigger that logs changes to a separate Books_Log table every time a new book is inserted
into the Books table, then:
Example: DELIMITER $$
DELIMITER ;
Trigger Logic:
After creating the trigger, whenever you insert a new record into the Books table, the Books_Log table
will automatically get a new entry.
The INSERT INTO Books_Log statement records the Book_ID, the action (INSERT), and the current
timestamp (NOW()) whenever a new book is added.