Dbms Procedure
Dbms Procedure
AIM:
To execute SQL commands for creating tables, retrieving the values, inserting, updating and
deleting values from the table.
PROCEDURE:
1. CreatingaDatabase
Syntax:
CREATEDATABASE<DB_NAME>;
Example:
CREATEDATABASETest;
The above command will create a database named Test, which will be an empty
schema without any table.
2. CreatingaTable
Create command can also be used to create tables. Now when we create a table, we
have to specify the details of the columns of the tables too. We can specify the names and
data types of various columns in the create command itself.
Syntax:
Example:
Data Manipulation Language (DML) statements are used for managing data in
database. DML commands are not auto-committed. It means changes made by DML
command are not permanent to database, it can be rolled back.
Syntax:
Example:
INSERT INTO Employee(EmployeeNo, EmployeeName, EmployeeSal, EmployeeCity,
EmployeeDob) Values(('1', 'Arvind', 5000, 'Mumbai','23-DEC-1992');
Other Options to insert records, using this technique all the table's columns are required.
The SQL SELECT statement is used to fetch the data from a database table which
returns this data in the form of a result table. These result tables are called result-sets.
Syntax :
Thebasicsyntax oftheSELECTstatementisasfollows −
SELECTcolumn1,column2,columnNFROMtable_name;
Here,column1,column2...arethefieldsofatablewhosevaluesyouwanttofetch.Ifyou
wantto fetchall the fieldsavailable in the
field,thenyoucanusethefollowingsyntax.
SELECT*FROMtable_name;
Example:
Syntax:
Example:
Syntax:
RESULT:
Thus, the SQL commands for creating tables, retrieving the values, inserting, updating and
deleting values from the table is executed successfully.
1. ALTERcommand
altercommandisusedforalteringthetablestructure,suchas,
ALTERCommand:AddanewColumn
Syntax:
ALTERTABLEtable_nameADD(column_namedatatype);
Example:
ALTER TABLE
ALTERCommand:AddmultiplenewColumns
UsingALTERcommandwecanevenaddmultiplenewcolumnstoanyexistingtable.
Syntax:
ALTERTABLEtable_nameADD(column_name1datatype1,column-name2datatype2, );
ALTERCommand:AddColumnwithdefaultvalue
ALTER command can add a new column to an existing table with a default value too.
The default value is used when no value is inserted in the column.
Syntax:
ALTERTABLEtable_nameADD(column-name1datatype1DEFAULTsome_value);
ALTERCommand:ModifyanexistingColumn
ALTER command can also be used to modify data type of any existing column.
Syntax:
ALTERTABLEtable_namemodify(column_namedatatype);
ALTERCommand:RenameaColumn
Syntax:
ALTERcommandcanalsobeusedtodroporremovecolumns.
Syntax:
ALTERTABLEtable_nameDROP(column_name);
2..TRUNCATEcommand
TRUNCATE command removes all the records from a table. But this command will
not destroy the table's structure. When we use TRUNCATE command on a table its
(autoincrement) primary key is also initialized.
Syntax:
TRUNCATETABLEtable_name;
Example:
3.DROPcommand
DROP command completely removes a table from the database. This command will
also destroy the table structure and the data stored in it.
Syntax:
DROP TABLEtable_name;
Example:
RESULT:
Thus, the SQL commands for creating tables, altering and dropping the table from a database
was executed successfully.
AIM:
To implement commands for data insertion using different ways, integrity constraints and
truncate commands
PROCEDURE:
Method 1: The first way specifies both the column names and the values to be inserted.
Syntax:
INSERT INTO table-name (column-names) VALUES (values) ;
Method 3:If you are adding the values for all the columns of the table, you do not need to
specify the column names in the SQL query. However, make sure the order of the values is in
the same order as the columns in the table.
2.INTEGRITY CONSTRAINTS:
• The Set of rules which is used to maintain the quality of information are known as
integrity constraints.
• Integrity constraints make sure about data intersection, update and so on.
Domain Constraint
• The Definition of an applicable set of values is known as domain constraint.
• Strings, character, time, integer, currency, date etc. Are examples of the data type of
domain constraints
• There can be a null value in the table apart from the primary key field.
• There can be a number of keys in an entity set but only one will be the primary key
out of all keys. In a relational table a primary key can have a unique as well as a null
value.
3.TRUNCATE:
TRUNCATE command removes all the records from a table. But this command will
not destroy the table's structure. When we use TRUNCATE command on a table its
(autoincrement) primary key is also initialized.
Syntax:
TRUNCATETABLEtable_name;
Example:
RESULT:
Thus the commands for data insertion using different ways, integrity constraints and truncate
has been implemented and executed successfully.
AIM:
PROCEDURE:
STEP1:Starttheprogram.
STEP 2: Create two different tables with its
essential attributes.
RESULT:
Thus the simple queries, nestedqueries ,sub queries and joins has been executed
successfully.
AIM:
PROCEDURE:
The following are the most commonly used SQL aggregate functions:
The SQL GROUP BY clause is used in collaboration with the SELECT statement to arrange
identical data into groups. This GROUP BY clause follows the WHERE clause in a SELECT
statement and precedes the ORDER BY clause.
Syntax:
FROM table_name
WHERE [ conditions ]
The ORDER BY keyword is used to sort the result-set in ascending or descending order.
The ORDER BY keyword sorts the records in ascending order by default. To sort the
records in descending order, use the DESC keyword.
Syntax;
RESULT:
Thus the aggregation functions, grouping and ordering commands to manipulate tables in a
database has been implemented and executes successfully.
AIM:
Data control language (DCL) is used to access the stored data. It is mainly used for
revoke and to grant the user the required access to a database. In the database, this language
does not have the feature of rollback.
It is a part of the structured query language (SQL).
It helps in controlling access to information stored in a database. It complements the
data manipulation language (DML) and the data definition language (DDL).
It is the simplest among three commands.
It provides the administrators, to remove and set database permissions to desired users
as needed.
These commands are employed to grant, remove and deny permissions to users for
retrieving and manipulating a database.
GRANT Command
It is employed to grant a privilege to a user. GRANT command allows specified users
to perform specified tasks
Syntax
GRANT privilege_name on objectname to user; Here, privilege names
are SELECT,UPDATE,DELETE,INSERT,ALTER,ALL objectname is
table name
user is the name of the user to whom we grant privileges
REVOKE Command
It is employed to remove a privilege from a user. REVOKE helps the owner to cancel
previously granted permissions.
Syntax
REVOKE privilege_name on objectname from user; Here, privilege
names are SELECT,UPDATE,DELETE,INSERT,ALTER,ALL
object name is table name user is the name of the user whose privileges are removing
5(a).IMPLEMENT DCL COMMANDS TO SET AND REVOKE PRIVILEGES
RESULT:
Thus the DCL Commands to set and revoke privileges has been executed
successfully.
AIM:
To execute Transactional Control Commands such as Commit, Rollback and Savepoint.
ALGORITHM:
STEP 4: Insert record values into the table or perform any kind of DML operation.
STEP 5: Create the SAVE POINTs for some set of statement on the transaction of database
object. STEP 6: Use the COMMIT command to save the effect of the previous command
operation except DDL command
STEP 7: Use the ROLLBACK TO SP_LABLE / ROLLBACK command for restore the
database status up to the save point STEP 8: Check the status of the database.
THEORY:
COMMIT command
COMMIT command is used to permanently save any transaction into the database.
To avoid that, we use the COMMIT command to mark the changes as permanent.
Syntax:
COMMIT;
ROLLBACK command
This command restores the database to last commited state. It is also used with
Syntax:
ROLLBACK TO savepoint_name;
SAVEPOINT command
SAVEPOINT command is used to temporarily save a transaction so that you can rollback to
that point when ever required.
Syntax:
SAVEPOINT savepoint_name;
RESULT:
Thus the Transactional Control Commands such as Commit, Rollback and Savepoint has
been executed successfully.
AIM:
To implement the conditional selection statement in PL/SQL block.
ALGORITHM:
STEP 1: Start the program.
STEP 2: Create the PL/SQL Block with necessary blocks. STEP
3: Declare the necessary variable in the declaration section
STEP 4: write the main program logics in the begin block.
STEP 5: if you want to access the table use the SQL statement.
STEP 6: if you want to solve any exception, write the exception name with WHEN
statement
STEP 7: Execute the PL/SQL block.
STEP 8: Give the input values or validate the information from the tables.
STEP 9: Stop the program.
The PL/SQL stands for Procedural Language extensions to Structured Query Language.
Basically, SQL is used to perform basic operations of creating a database, storing data in the
database, updating data in the database, retrieving the stored data of database, etc, whereas
PL/SQL is a fully Structured Procedural language which enables the developer to combine
the powers of SQL with its procedural statements.
PL/SQL Block
In a PL/SQL program, code is written in blocks. Each PL/SQL block has 3 sections, which
are:
1. Declare section
2. Begin section
3. Exception section
PL/SQL Block
PL/SQL block creates the structured logical blocks of code that describes the process to be
executed. Such a block consists of SQL statements and PL/SQL instructions that are then
passed to the oracle engine for execution. PL/SQL block consists of the following four sections:
• DECLARE Section:
PL/SQL code starts with a declaration section in which memory variables and other
oracle objects like cursor, triggers etc can be declared and if required can be initialized
as well. Once declared/initialised we can use them in SQL statements for data
manipulation. As it is not necessary that we would require variables etc in every
PL/SQL code, hence this section is an optional section.
• BEGIN Section:
This section contains the SQL and PL/SQL statements that are required to be executed
and contains the main logic. This section is responsible for handling the data retrieval
and manipulation, may be working with branching, can use looping and conditional
statements, etc.
• EXCEPTION Section:
This section is optional. It is mainly used to handle the errors that may occur between
BEGIN and EXCEPTION sections.
• END Section:
For example, if someone says, If I get 40 marks, I will pass the exam, else I will fail. In this
case condition is getting 40 marks, if its true then the person will pass else he/she will fail.
This can be logically implemented in PL/SQL block using decision making statements.
1. If Else statements
2. Case statement
PL/SQL: if Statement
The if statement, or the if...then statement can be used when there is only a single condition to
be tested. If the result of the condition is TRUE then certain specified action will be performed
otherwise if it is FALSE then no action is taken and the control of program will just move out
of the if code block.
Syntax:
if<test_condition> then
body of action
end if;
Using this statement group we can specify two statements or two set of statements, dependent
on a condition such that when the condition is true then one set of statements is executed and
if the condition is false then the other set of statements is executed.
It is used to check multiple conditions. Sometimes it is required to test more than one condition
in that case if...then...else statement cannot be used. For this purpose, if...then...elsif...else
statement is suitable in which all the conditions are tested one by one and whichever condition
is found to be TRUE, that block of code is executed. And if all the conditions result in FALSE
then the else part is executed.
In the following syntax, it can be seen firstly condition1 is checked, if it is true, the statements
following it are executed and then control moves out of the complete if block but if the
condition is false then the control checks condition2 and repeats the same process. If all the
conditions fail then the else part is executed.
Syntax:
if<test_condition1> then
body of action
elsif<test_condition2>then
body of action
elsif<test_condition3>then
body of action
...
...
...
else
body of action
end if;
If we try to describe the case statement in one line then, then we can say means "one out of
many". It is a decision making statement that selects only one option out of the multiple
available options.
It uses a selector for this purpose. This selector can be a variable, function or procedure that
returns some value and on the basis of the result one of the case statements is executed. If all
the cases fail then the else case is executed.
Syntax:
CASE selector when value1 then
Statement1; when value2 then
Statement2;
...
...
else statement; end
CASE;
RESULT:
Thus the conditional selection statement in PL/SQL block has been verified and executed
successfully.
AIM:
PROCEDURE:
PL/SQL - Cursors
A cursor is a pointer to this context area. PL/SQL controls the context area through a cursor.
A cursor holds the rows (one or more) returned by a SQL statement. The set of rows the cursor
holds is referred to as the active set.
There are two types of cursors.
• Implicit cursors
• Explicit cursors
Implicit Cursors
Implicit cursors are automatically created by Oracle whenever an SQL statement is executed,
when there is no explicit cursor for the statement. Programmers cannot control the implicit
cursors and the information in it.
Whenever a DML statement (INSERT, UPDATE and DELETE) is issued, an implicit cursor
is associated with this statement. For INSERT operations, the cursor holds the data that needs
to be inserted. For UPDATE and DELETE operations, the cursor identifies the rows that would
be affected.
In PL/SQL, you can refer to the most recent implicit cursor as the SQL cursor, which
always has attributes such as %FOUND, %ISOPEN, %NOTFOUND, and
%ROWCOUNT. The
SQL cursor has additional attributes, %BULK_ROWCOUNT and
%BULK_EXCEPTIONS, designed for use with the FORALL statement. The following
table provides the description of the most used attributes −
S.No Attribute & Description
1
%FOUND
Returns TRUE if an INSERT, UPDATE, or DELETE statement affected one or more rows or
a SELECT INTO statement returned one or more rows. Otherwise, it returns FALSE.
%NOTFOUND
%ISOPEN
3 Always returns FALSE for implicit cursors, because Oracle closes the SQL cursor
automatically after executing its associated SQL statement.
%ROWCOUNT
4 Returns the number of rows affected by an INSERT, UPDATE, or DELETE statement, or
returned by a SELECT INTO statement.
Any SQL cursor attribute will be accessed as sql%attribute_name as shown below in the
example.
Explicit Cursors
Explicit cursors are programmer-defined cursors for gaining more control over the context
area. An explicit cursor 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.
The syntax for creating an explicit cursor is −
Fetching the cursor involves accessing one row at a time. For example, we will fetch rows
from the above-opened cursor as follows − FETCH c_customers INTO c_id, c_name, c_addr;
Closing the Cursor
Closing the cursor means releasing the allocated memory. For example, we will close the
above-opened cursor as follows −
CLOSE c_customers;
RESULT:
Thus the manipulation of a table using Implicit and Explicit Cursors has been verified and
executed successfully.
AIM:
PROCEDURE:
TRIGGER :A Trigger is a stored procedure that defines an action that the database
automatically take when some database-related event such as Insert, Update or Delete occur.
:new :old
These two variables retain the new and old values of the column updated in the database.
The values in these variables can be used in the database triggers for data manipulation
Syntax:
[when
condition] begin
---statement end;
Create a trigger that insert current user into a username column of an existing table
2. Create a trigger for each row that insert the current user as user name into a table 3.
RESULT:
Thus the creation and dropping of triggers was performed and executed successfully
AIM:
PROCEDURE:
A procedure is a block that can take parameters (sometimes referred to as arguments) and be
invoked. Procedures promote reusability and maintainability. Once validated, they can be
used in number of applications. If the definition changes, only the procedure are affected, this
greatly simplifies maintenance. Modularized program development: · Group logically related
statements within blocks. · Nest sub-blocks inside larger blocks to build powerful programs.
· Break down a complex problem into a set of manageable well defined logical modules and
implement the modules with blocks.
ARGUMENT: It is the name of the argument to the procedure. Parenthesis can be omitted
if no arguments are present.
IN: Specifies that a value for the argument must be specified when calling the procedure ie.,
used to pass values to a sub-program. This is the default parameter.
OUT: Specifies that the procedure passes a value for this argument back to it‟s calling
environment after execution ie. used to return values to a caller of the sub-program. INOUT:
Specifies that a value for the argument must be specified when calling the procedure and that
procedure passes a value for this argument back to it‟s calling environment after execution.
RETURN: It is the data type of the function‟s return value because every function must
return a value, this clause is required.
PROCEDURES
FUNCTIONS
datatype
{is,as}
declaration; begin
RESULT:
Thus the procedures and function for various operations was developed and executed
successfully.
AIM:
PROCEDURE:
PL/SQL facilitates programmers to catch such conditions using exception block in the
program and an appropriate action is taken against the error condition.
SYNTAX:
DECLARE
<declarations section>
BEGIN
<executable command(s)>
EXCEPTION
exception1-handling-statements
exception2-handling-statements
exception3-handling-
statement........
END;
RESULT:
Thus the exceptions are handled in PL/SQL Program verified and executed
successfully.
AIM:
PROCEDURE:
NORMALIZATION
This process helps to design a logical data model known as conceptual data model.
1NF states that the domain of an attribute must include only atomic values and that value of
any
attribute in a tuple must be a single value from the domain of that attribute. Hence 1NF
disallows multivalued attributes, composite attributes. It disallows “relations within
relations”.
A relation is said to be in 2NF if it is already in 1NF and it has no partial dependency. 2NF is
based on the concept of full functional dependency.
A functional dependency x→y is partial dependency if A can be removed which does not
affect the dependencyie (x-
(A))→y holds.
A relation is in 2NF if it is in 1NF and every non-primary key attribute is fully and
functionally dependent on primary key.
A relation is in 1NF will be in the 2NF if one of the following conditions is satisfied:
2. No non-key attribute exist in relation ie all the attributes in the relation are components of
the primary key.
Every non-key attribute is functionally dependent on full set of primary key attributes.
An entity relationship diagram (ERD) shows the relationships of entity sets stored in a
database. An
These entities can have attributes that define its properties. Notation
RESULT:
Thus the database has been designed using ER Modeling and Normalization successfully.
AIM:
To design the payroll processing system in visual basic using ORACLE as backend
PROCEDURE :
NAMENULL? TYPE
EID NUMBER(10)
ENAME VARCHAR2(1 0)
DES VARCHAR2(10)
BASICPAY NUMBER(10)
HRA NUMBER(10)
DA NUMBER(10) MA
NUMBER(10)
GROSSPAY NUMBER(10)
DEDUCTION NUMBER(10)
NETPAY NUMBER(10)
4. Go to Start --> Settings --> Control Panel --> Administrative tools --> Data
Sources(ODBC) --> User DSN --> Add --> Select ORACLE database driver --> OK.
5. One new window will appear. In that window, type data source name as table name
created in ORACLE. Type user name as second csea.
1. In visual basic create tables, command buttons and then text boxes.
3. Projects --> Components --> Microsoft ADO Data Control 6.0 for OLEDB --> OK.
8. Choose general tab, select ODBC Data Sources name as the table name created in
ORACLE
15. Select the Data field and set the required field name created in table
RESULT:
Thus the payroll processing system was designed in Visual Basic using
ORACLE as backend and the output was verified
AIM:
PROCEDURE:
Indexes are special lookup tables that the database search engine can use to speed up data
retrieval. An index is a pointer to data in a table. An index in a database is very similar to an
index in the back of abook. An index helps speed up SELECT queries and WHERE clauses,
but it slows down data input, with UPDATE and INSERT statements. Indexes can be created
or dropped with no effect on the data. Index in sql is created on existing tables to retrieve the
rows quickly. When there are thousands of records in a table, retrieving information will take
a long time. When an index is created, it first sorts the data and then it assigns a ROWID for
each row.
An index can be created in a table to find data more quickly and efficiently.
The users cannot see the indexes, they are just used to speed up searches/queries
•table_name is the name of the table to which the indexed column belongs.
An index can be dropped using SQL DROP command. Care should be taken when dropping
an index because performance may be slowed or improved.
RESULT:
Thus the index has been created and dropped in a table has been implemented and executed
successfully.
Aim:
The objective is to introduce some features of non-relational or NoSQL databases
using MongoDB. MongoDB stores data in JSON objects which it calls documents and uses a
custom language for queries.
Installation
Option 1:
1. Set up a free cluster on Mongo Atlas by following the instructions here:
https://docs.atlas.mongodb.com/tutorial/deploy-free-tier-cluster/
2. Install the mongo shell on your machine and connect to your cluster following the
instructions on the dashboard.
Option 2:
1. Download and setup MongoDB for your OS following these instructions
https://www.mongodb.com/download-center/community
2. Start the server. Then use mongo shell to connect your
server: https://docs.mongodb.com/manual/mongo/ Preparation The
instructions below assume you have MongoDB installed and started on your local machine.
For Atlas, create a database and a user by following the instructions on the dashboard.
1. Once you have installed and started the Mogodb you can log into
your server as root. mongo -u root
2. To create a new database do use mongolab 3. Now create a new
user to access the database.
db.createUser({user:"e14xxx", pwd:"abc123", roles:[{role: "dbOwner" , db:"mongolab"}]})
4. Log out of the mongo shell and log back in using the user you created. mongo
localhost/mongolab -u e14xxx
Data Validation:
Document databases are a flexible alternative to the predefined schemas of relational
databases. Each document in a collection can have a unique set of fields, and those fields
can be added or removed from documents once they are inserted. Since the data fields can
be changed for each document in a collection, data validation is extremely important to
ensure queries run predictability.
To create the customer collection with a custom data validation function, enter the following
RESULT:
Thus the Data validation in NoSQL databases using MongoDB has been implemented and
executed successfully.