DBMS Record
DBMS Record
Introduction
Oracle Database (commonly referred to as Oracle DBMS, Oracle Autonomous
Database, or simply as oracle) is a proprietary multi-model database
management system produced and marketed by Oracle Corporation.
Procedure
1.Double click on the oracle logo setup file. Then oracle installer window open
as shown in below.
2.Click on the next in the welcome to install wizard for the oracle logo database
edition .select the accept the license agreement and click on NEXT.
3.Select the path location and click on next.
4.Then select the password filled and enter the password and click on NEXT
BUTTON.
5.In next window click on install BUTTON as shown in below .THEN installation
procedure started.
6.Click on finish button and installation wizard is completed.
Aim: SQL Query to create a employee table and insert 5 records and display
the table.
Introduction
CREATE command
This is used to create a table in SQL.This is a part of Data Definition Language.A
table cosists of rows and columns.So while creating we have to provide all the
information to SQL about the names of columns,types of data to be stored in
column,size of the data etc.
Syntax
CREATE TABLE table_name(column1 datatype(size),column2 datatype(size)…);
INSERT command
This is used to insert data into table’s columns.This is part of Data Manipulation
Language.
Syntax
INSERT INTO table_name(column1,coulumn2,….) VALUES(value1,value2,…);
or
INSERT INTO table_name VALUES(value1,value2…);
SELECT command
This is used to select data from a table.
Syntax :
SELECT * from table_name;
or
SELECT column1,coulum2,….FROM table_name;
QUERY
CREATE TABLE employee(id number(3),name varchar(20),salary
number(6),age);
table created.
INSERT INTO employee VALUES(1,’ram’,100000,26);
1 row inserted.
INSERT INTO employee VALUES(2,’bharath’,90000,27);
1 row inserted.
INSERT INTO employee VALUES(3,’chandini’,95000,26);
1 row inserted.
INSERT INTO employee VALUES(4,’lokesh’,100000,30);
1 row inserted.
INSERT INTO employee VALUES(5,’jahnavi’,80000,25);
1 row inserted.
UPDATE command
This is a Data manipulation command that is used to modify the record present
in the existing table.It updateds the record by using a WHERE clause that
specifies the condition with an UPDATE statement.If the condition is not
specified,all the rows will be affected.
Syntax
UPDATE table_name
SET column1=value1,column2=value2,…
WHERE CONDITION;
QUERY
CREATE TABLE employee(id number(3),name varchar(20),salary
number(6),age);
table created.
INSERT INTO employee VALUES(1,’ram’,50000,26);
1 row inserted.
INSERT INTO employee VALUES(2,’bharath’,40000,27);
1 row inserted.
INSERT INTO employee VALUES(3,’chandini’,35000,26);
1 row inserted.
INSERT INTO employee VALUES(4,’lokesh’,60000,30);
1 row inserted.
INSERT INTO employee VALUES(5,’jahnavi’,30000,25);
1 row inserted.
UPDATE employee
SET salary=salary+5000
WHERE salary<40000;
2 rows updated.
Introduction
ALTER command
This is used to modify the structure in the existing table.It is used to add,drop
and rename columns.
ALTER TABLE-ADD
It is used to add columns into the existing table.Sometimes we may require to
add additional information,in that case we do not require to create the whole
database again,ADD comes to our rescue.
Syntax
ALTER TABLE table_name ADD(column1 datatype,column2 datatype,..);
ALTER TABLE-DROP
It is used to drop the columns in a table.Deleting the unwanted columns from
the table.
Syntax
ALTER TABLE table_name DROP COLUMN column_name;
QUERY
ALTER TABLE-RENAME
It is used to rename the columns and table in existing table.
QUERY
SELECT *FROM student;
QUERY
SELECT *FROM student;
ID NAME MARKS AGE
1 RAM 100 17
2 VISHAL 95 18
3 BHARATH 96 17
4 LOKESH 97 17
QUERY
SELECT * FROM student;
ID NAME MARKS
1 Ram 95
2 Vishal 85
3 Bharath 80
4 Chandini 70
5 Jahnavi 75
6 Chaitanya 60
7 Sravani 85
UPDATE student
SET grade=’B’
WHERE marks BETWEEN 81 AND 90;
3 rows updated.
UPDATE student
SET grade=’C’
WHERE marks BETWEEN 71 AND 80;
3 rows updated.
UPDATE student
SET grade=’D’
WHERE marks BETWEEN 1 AND 60;
1 row updated.
8.FLOOR(Float value):Floor returns the largest integer value not greatest than
an number specified as an argument.
Syntax: select floor(float value) from dual;
QUERY
SELECT ABS(-92) FROM DUAL;
OUTPUT:
ABS(-92)
92
Introduction
Group functions: They are mathematical functions to operate on set of records
to give one result.
1.AVG(column_name):It gives average value in a column.
syntax:SELECT AVG(column_name) FROM table_name;
a)QUERY
SELECT *FROM employee;
ID NAME SALARY AGE
1 Ram 50000 26
2 Bharath 40000 27
3 Chandini 40000 26
4 Lokesh 60000 20
5 Jahnavi 35000 25
QUERY
CREATE TABLE customers(id number NOT NULL,name varchar(20));
Table created.
QUERY
ALTER TABLE student MODIFY COLUMN name VARCHAR(20) NOT NULL;
Table altered.
Introduction
WHILE loop
The while loop in PL/SQL is used to check the entry condition,and if the entry
condition is true,only then the loop is executed.if the condition is false,the
control does not enter the loop and control transfer after loop
Syntax
WHILE(condition)
LOOP
statements;
updation;
END LOOP;
FOR loop
The for loop in PL/SQL provides implicit variable declaration,implicit
incrementation of the variable by one and implicit exit also.While writing the
loop statement,the variable is declared implicitly.The range consists of the
starting value,from where the values of the iterating variable begins,and the
end value,which determines the last value which the variable can have.In each
iteration,the variable is invcremented by one.
syntax
FOR variable IN start_value .. end_value
LOOP
Statements;
END LOOP;
a)DECLARE
i number:=1;
j number;
BEGIN
WHILE (i<=10)
LOOP
j:=1
WHILE(j<=10)
LOOP
dbms_output.put_line(i || ' x ' || j || ' = ' || (i * j));
j:=j+1;
END LOOP
i:=i+1;
END LOOP;
END;
/
OUTPUT
1x1=1
1x2=2
.
.
.
.
.
10 x 9 = 90
10 x 10 = 100
b)
BEGIN
FOR i IN 1..10 LOOP
FOR j IN 1..10 LOOP
DBMS_OUTPUT.PUT_LINE(i || ' * ' || j || ' = ' || (i * j));
END LOOP;
END LOOP;
END;
/
Output
1x1=1
1x2=2
.
.
.
.
.
10 x 9 = 90
10 x 10 = 100
Aim: PL/SQL procedure to display the highest marks in student table.
Introduction
A procedure is a subprogram unit that consists of a group of PL/SQL statements
that performs a specific task. Procedures are standalone blocks of a program
that can be stored in database.It is mainly used to execute the process in
PL/SQL.Every procedure in PL/SQL has its own unique name by which it can be
referred to and called.
Syntax for creating procedure
CREATE OR REPLACE PROCEDURE procedure_name(parameters if any)
AS
variables;
BEGIN
statements;
END;
/
Program
CREATE OR REPLACE PROCEDURE highest_marks
IS
highest students.marks%TYPE;
BEGIN
SELECT MAX(marks) INTO highest FROM students;
dbms_output.put_line(‘The highest marks:’||highest);
END;
/
procedure created.
--The above created procedure can be called from EXECUTE command.
EXECUTE highest_marks;
OUTPUT
The highest marks:100
--It can also be called from another PL/SQL block.
BEGIN
highest_marks;
END;
/
OUTPUT
The highest marks:100
Aim: PL/SQL procedure to take the employee id and amount to update the
salary of employee in employee table.
Introduction
A procedure is a subprogram unit that consists of a group of PL/SQL statements
that performs a specific task. Procedures are standalone blocks of a program
that can be stored in database. It is mainly used to execute the process in
PL/SQL.Each procedure in PL/SQL has its own unique name by which it can be
reffered to and called
Syntax for creating procedure
CREATE OR REPLACE PROCEDURE procedure_name(parameters if any)
AS
variables;
BEGIN
statements;
END;
/
Program
CREATE OR REPLACE PROCEDURE raise_salary(e IN NUMBER,amt IN NUMBER,s
OUT NUMBER)
AS
BEGIN
UPDATE employee
SET salary=salary+amt
WHERE id=e;
SELECT salary INTO s FROM employee WHERE id=e;
END;
/
procedure created.
DECLARE
s number(6);
BEGIN
raise_salary(1,5000,s);
dbms_output.put_line(‘After updating the salary of employee of ID-1:’||s);
END;
/
OUTPUT
After updating the salary of employee of ID-1:105000
Aim:PL/SQL function to perform calculations.
Introduction
Function is a named PL/SQL block that return a single values, mainly used to
compute and return a value. There are standalone block that can be stored in
the database.Functions use RETURN keyword to return the value,and the
datatype of this is defined at the time of creation
Syntax for creating function
CREATE OR REPLACE FUNCTION function_name(parameter if any)
RETURN datatype
IS
variables;
BEGIN
statements;
RETURN <expression>;
END;
/
Program
CREATE OR REPLACE FUNCTION cal(a NUMBER,b NUMBER,op CHAR)
RETURN NUMBER
IS
BEGIN
IF op=’+’ THEN
RETURN a+b;
ELSIF op=’-‘ THEN
RETURN a-b;
ELSIF op=’/’ THEN
RETURN a/b;
ELSE
RETURN a*b;
END IF;
END;
/
Function created.
--The above created function can be called by SELECT statement
SELECT cal(10,20,’+’) FROM DUAL;
OUTPUT
30
--It can be also be called from another block
DECLARE
result number(3);
BEGIN
result:=cal(10,20,’-‘);
dbms_output.put_line(result);
END;
/
OUTPUT
-10
Aim:PL/SQL function to find the factorial of number using recursion.
Introduction
Function is a named PL/SQL block that return a single values, mainly used to
compute and return a value. There are standalone block that can be stored in
the database.Function use return keyword to return the value and datatype of
this value is declared at the time of creation.
A program or function can call other functions.When a function calls itself,it is
referred to as recursive call and the process is known as recursion.
Syntax for creating function
CREATE OR REPLACE FUNCTION function_name(parameter if any)
RETURN datatype
IS
variables;
BEGIN
statements;
RETURN <expression>;
END;
/
Program
CREATE OR REPLACE FUNCTION fact(n NUMBER)
RETURN NUMBER
IS
BEGIN
IF n=0 THEN
RETURN 1;
ELSE
RETURN n*fact(n-1);
END IF;
END;
/
Function created.
--The above created function can be called from SELECT statement.
SELECT fact(5) FROM DUAL;
OUTPUT
120
--It can also be called from another block.
DECLARE
result NUMBER;
n NUMBER:
BEGIN
n:=&a;
result:=fact(n);
dbms_output.put_line(‘The factorial is:’||result);
END;
/
OUTPUT
Enter value for n:4
The factorial is:24
Aim:PL/SQL program to display the number of rows affected by updating salary
in employee table.
Introduction
When a SQL statement is executed, Oracle creates a memory area known as
context area. A cursor is pointer to this context area. A cursor is a pointer to
this context area. Cursors are used to referred to a program to fetch and
process the rows returned by the SQL statement, one at a time.
There are two types of cursors:
1.Implicit Cursor.
2.Explicit Cursor.
Implicit Cursors
Program
DECLARE
total NUMBER;
BEGIN
UPDATE employee
SET salary=salary+1000
IF SQL%FOUND THEN
total:=SQL%ROWCOUNT;
dbms_output.put_line(total| ‘rows updated’);
END IF;
IF SQL%NOTFOUND THEN
dbms_output.put_line( ‘No rows updated’);
END IF;
END;
/
OUTPUT
4 rows updated
Aim:PL/SQL program to display the employee id and employee name in
employee table using cursors.
Introduction
When a SQL statement is executed, Oracle creates a memory area known as
context area. A cursor is pointer to this context area. A cursor is a pointer to
this context area. Cursors are used to referred to a program to fetch and
process the rows returned by the SQL statement, one at a time.
There are two types of cursors:
1.Implicit Cursor.
2.Explicit Cursor.
Explicit Cursors
The Explicit cursors are defined by the programmers to gain more control over
the context area. These cursors should be defined in the declaration section of
the PL/SQL block. It is created on a SELECT statement which returns more than
one row.
Steps:
You must follow these steps while working with an explicit cursor.
1. Declare the cursor to initialize in the memory.
2. Open the cursor to allocate memory.
3. Fetch the cursor to retrieve data.
4. Close the cursor to release allocated memory.
Declare the cursor
It defines the cursor with a name and the associated SELECT statement.
CURSOR name IS SELECT statement;
Open the cursor
It is used to allocate memory for the cursor and make it easy to fetch the rows
returned by the SQL statements into it.
OPEN cursor_name;
Fetch the cursor
It is used to access one row at a time. You can fetch rows from the above-
opened cursor as follows:
FETCH cursor_name INTO variable_list;
Close the cursor
It is used to release the allocated memory.
The following syntax is used to close the opened cursor
ClOSE cursor_name;
Program
DECLARE
CURSOR emp_cur IS SELECT id,name from employee;
emp_rec emp_cur%ROWTYPE;
BEGIN
OPEN emp_cur;
LOOP
FETCH emp_cur INTO emp_rec;
EXIT WHEN emp_cur%NOTFOUND;
Dbms_output.put_line(emp_rec.id||’ ‘||emp_rec.name);
END LOOP;
CLOSE emp_cur;
END;
/
Output
1 RAM
2 VISHAL
3 BHARATH
4 LOKESH
Aim:PL/SQL trigger to display the salary changes after updating salary in
employee table.
Introduction
Triggers
In PL/SQL, triggers are special types of stored procedures that automatically
execute or fire in response to certain events on a particular table or view. They
are used to enforce business rules, maintain data integrity, or automatically
generate derived column values.
Types of Triggers
1.After insert
2.After delete
3.After update
4.Before insert
5.Before delete
6.Before update
Advantages of Triggers
1.Event logging
2.Enforcing referential integrity
3.Auditing.
Syntax for creating trigger
CREATE [OR REPLACE] TRIGGER trigger_name
{BEFORE | AFTER | INSTEAD OF} {INSERT | UPDATE | DELETE}
ON table_name
[FOR EACH ROW]
[WHEN (condition)]
BEGIN
-- PL/SQL code
END;
/
ID NAME SALARY DEP
1 RAM 106000 10
2 VISHAL 96000 30
3 91000 20
BHARATH
4 LOKESH 91000 30
Program
CREATE OR REPLACE TRIGGER display_salary_changes
BEFORE DELETE OR INSERT OR UPDATE ON employee
FOR EACH ROW
WHEN (NEW.id > 0)
DECLARE sal_diff number;
BEGIN
sal_diff := :NEW.salary - :OLD.salary;
dbms_output.put_line('Old salary: ' || :OLD.salary);
dbms_output.put_line('New salary: ' || :NEW.salary);
dbms_output.put_line('Salary difference: ' || sal_diff);
END;
/
Trigger created.
UPDATE employee
SET salary=salary+4000
WHERE id=1;
OUTPUT
Old salary:106000
New salary:110000
Salary difference:4000
Aim:PL/SQL trigger to store the deleted records of student table to another
table.
Introduction
Triggers
In PL/SQL, triggers are special types of stored procedures that automatically
execute or fire in response to certain events on a particular table or view. They
are used to enforce business rules, maintain data integrity, or automatically
generate derived column values.
Types of Triggers:
1.After insert
2.After delete
3.After update
4.Before insert
5.Before delete
6.Before update
Advantages of Triggers:
1.Event logging
2.Enforcing referential integrity
3.Auditing.
Syntax for creating trigger
CREATE [OR REPLACE] TRIGGER trigger_name
{BEFORE | AFTER | INSTEAD OF} {INSERT | UPDATE | DELETE}
ON table_name
[FOR EACH ROW]
[WHEN (condition)]
BEGIN
-- PL/SQL code
END;
/
ID NAME MARKS AGE
1 RAM 100 17
2 VISHAL 95 18
3 BHARATH 96 17
4 LOKESH 97 17
PROGRAM
CREATE TABLE deleted_history(id number,name varchar(20),age
number,attendance float);
Table created.
CREATE OR REPLACE TRIGGER history
BEFORE DELETE ON student
FOR EACH ROW
BEGIN
INSERT INTO deleted_history
VALUES(:OLD.id,:OLD.name,:OLD.age:OLD.ATTENDANCE);
END;
/
Trigger created.
DELETE FROM student WHERE attendance<75;
1 row deleted.
SELECT * FROM deleted_history;
Output
Introduction
Mongo DB is built on a scale-out architecture that has become popular with
developers of all kinds for developing scalable applications with evolving data
schemas. As a document database, Mongo DB makes it easy it easy for
developers to store structured or unstructured data. It uses a JSON-like format
to store documents.
Procedure
1.Go to Mongo DB Download Center to download Mongo DB Community
Server.
2.when the download is complete open the msi file and click the next button in
the startup.
3.Now accept the End-User License Agreement and click the next button.
4.Now select the complete option to install all the program features. Here, if
you can want to install only selected program features and want to select the
location of the installation, then use the customoption.
5.select “Run service as Network Service user” and copy the path of the data
directory. Click Next.
6.Click the install button to start the installation process.
7.After clicking on the install button installation of MongoDB begins.
8.Now click the Finish button to complete the installation process.
9.Now we go to the locations where MongoDB installed in your system and
copy the path upto bin.
10.Now, to create an environment variable open system properties. Goto
system variable and then go to path, Edit Environment variable and paste the
copied link to your environment system and click OK.