DBMS 2021 REGULATION LAB MANUAL
DBMS 2021 REGULATION LAB MANUAL
PAGE
S. NO. LIST OF EXPERIMENTS / PROGRAMS
NO.
1
1 DDL & DML COMMANDS
3
2 CONSTRAINTS
5
3 AGGREGATE FUNCTIONS
7
4 SUB QUERIES AND SIMPLE JOIN
9
5 NATURAL, EQUI, OUTER JOINS
12
6 FUNCTIONS & PROCEDURES
15
7 DCL & TCL COMMANDS
19
8 SQL TRIGGERS
AIM:
To create a database table, add constraints (primary key, unique, check, Not null), insert
rows, update and delete rows using SQL DDL and DML commands.
DDL COMMANDS
Constraints are used to limit the type of data that can go into a table. This ensures the accuracy
and reliability of the datain the table. If there is any violation between the constraint and the data
action, the action is aborted.
Constraints can be column level or table level. Column level constraints apply to a column, and
table level constraintsapply to the whole table.
The following constraints are commonly used in SQL:
NOT NULL - Ensures that a column cannot have a NULL value
UNIQUE - Ensures that all values in a column are different
PRIMARY KEY - A combination of a NOT NULL and UNIQUE. Uniquely identifies each row in a
table
CHECK - Ensures that the values in a column satisfies a specific condition
FOREIGN KEY - Prevents actions that would destroy links between tables
DEFAULT - Sets a default value for a column if no value is specified
CREATE INDEX - Used to create and retrieve data from the database
very quickly
A) Alter a Table – Add Column
Rename Table
RESULT:
Thus the DDL, DML commands used to table was created with all constraints and executed
successfully.
EX.NO:2 CONSTRAINTS
DATE:
AIM:
To create a set of tables, add foreign key constraints and incorporate referential integrity.
CONSTRAINTS:
In SQL, we can create a relationship between two tables using the FOREIGN KEY constraint.
Here, the customer_id field in the Orders table is FOREIGN KEY which references the id field in
the Customers table. This means that the value of the customer_id (of the Orders table) must be
a value from the id column (of the Customers table).
RESULT:
Thus the foreign key used to different set of table was created and executed successfully.
EX.NO:3 AGGREGATE FUNCTIONS
DATE:
AIM:
To query the database tables using different 'where' clause conditions and also implement
aggregate functions.
PROCEDURE:
The SQL WHERE clause is used to filter the results and apply conditions in a SELECT, INSERT, UPDATE, or
DELETE statement.Syntax - The syntax for the WHERE clause in SQL is:
Aggregate function
SQL aggregation function is used to perform the calculations on multiple rows of a single column
of a table. It returns asingle value. It is also used to summarize the data.
COUNT FUNCTION
COUNT function is used to Count the number of rows in a database table. It can work on both
numeric and non-numericdata types.
COUNT function uses the COUNT(*) that returns the count of all the rows in a specified table.
COUNT(*) considersduplicate and Null.
COUNT with WHERE
Average Function
SUM Function
MAX Function
MIN Function
RESULT:
Thus the database tables used to different 'where' clause conditions applied to various aggregate
functions are executedsuccessfully.
EX.NO:4 SUB QUERIES AND SIMPLE JOIN OPERATIONS
DATE:
AIM:
To create Database table and explore various sub queries using insert, delete, update and
select command and alsoperform join operations.
PROCEDURE:
In SQL a Sub query can be simply defined as a query within another query. In other words
we can say that a Sub query is a query that is embedded in WHERE clause of another SQL query.
Important rules for Sub queries:
You can place the Sub query in a number of SQL clauses: WHERE clause, HAVING clause, FROM
clause. Sub queries can be used with SELECT, UPDATE, INSERT, DELETE statements along with expression
operator. It could be equalityoperator or comparison operator such as =, >, =, <= and Like operator.
A sub query is a query within another query. The outer query is called as main query and
inner query is called as sub query.
The sub query generally executes first when the sub query doesn't have any co-relation with
the main query, when there is a co-relation the parser takes the decision on the fly on which query to
execute on precedence and uses the output of the sub query accordingly. Sub query must be enclosed
in parentheses. Sub queries are on the rightside of the comparison operator.
ORDER BY command cannot be used in a Sub query. GROUPBY command can be used to
perform same function as ORDER BY command.
Use single-row operators with single row Sub queries. Use multiple-row operators with multiple-
row Sub queries.
Sub Queries – (Create Duplicate Table – USING INSERT & SELECT COMMANDS)
RESULT:
Thus the SQL Program used to Database table was created and explored various sub queries
using insert, delete, updateand select command and also performed join operations successfully.
EX.NO:5 NATURAL, EQUI AND OUTER JOINS
DATE:
AIM:
To query the database tables and explore natural, equi and outer joins.
PROCEDURE:
SQL Joins are used to fetch/retrieve data from two or more data tables, based on a join
condition. A join condition is a relationship among some columns in the data tables that take
part in SQL join. Basically, database tables are related to each other with keys. We use this
keys relationship in SQL Joins.
Types of SQL Joins
In SQL Server we have only three types of joins. Using these joins we fetch the data from
multiple tables based on condition.
Inner Join / EQUI Join
Inner join returns only those records/rows that match/exist in both the tables. The inner join
generally depends upon FORM or the WHERE clause in which the data of the first table is
joined using another table using the terms 'inner join' followed by the second table to be
Outer Join
Outer join is also called Right join and the primary reason a right join would be used is when we
are joining more than two tables from the database. In these use-cases, using a right join
method is preferable because it can avoid restructuring our whole query to join one table.
Outside of this thing, the right joins are used very rarely due to their complexity, so for such
Simple joins, it's better to use a left join than a right as it will be easier for our query to be
read and understood by others while developing a DBMS query. We have three types of
Outer Join.
Left Outer Join
Left outer join returns all records/rows from the left table and from the right table returns
only matched records. If there are no columns matching in the right table, it returns NULL
values.
Cross Join
Cross join is a cartesian join means the cartesian product of both the tables. This join does not
need any condition to join two tables. This join returns records/rows that are multiplication of
record numbers from both the tables means each rowon the left table will be related to each
row of the right table.
Self Join
Self-join is used to join a database table to itself, particularly when the table has a foreign key
that references its own Primary Key. Basically, we have only three types of joins: Inner join,
Outer join, and Cross join. We use any of these threeJOINS to join a table to itself. Hence Self-
join is not a type of SQL join.
Syntax for ALL TYPES OF JOINS
Natural Join
RESULT:
Thus the SQL Program used to Database table was created and also various join queries
operations was executedsuccessfully.
EX.NO:6 FUNCTIONS & PROCEDURES
DATE:
AIM:
To create a user defined function and stored procedure using SQL.
PROCEDURE:
The PL/SQL Function is very similar to PL/SQL Procedure. The main difference between
procedure and a function is, a function must always return a value, and on the other hand a
procedure may or may not return a value. Except this, all the other things of PL/SQL procedure
are true for PL/SQL function too
Drop Function
RESULT:
Thus the PL/SQL functions program was created and executed successfully.
EX.NO:7 DCL & TCL COMMANDS
DATE:
AIM
To execute complex transactions and also realize various DCL and TCL commands.
PROCEDURE:
Data Control Language Commands
DCL is short name of Data Control Language which includes commands such as GRANT and
mostly concerned with rights,permissions and other controls of the database system.
GRANT - allow users access privileges to the database
REVOKE - withdraw users access privileges given by using the GRANT command
Queries:
Their schemas are as follows , Departments ( dept _no , dept_ name , dept_location
);Employees ( emp_id , emp_name , emp_salary );
Q1: Develop a query to grant all privileges of employees table into departments table
Q2: Develop a query to grant some privileges of employees table into departments table
Ans: SQL> Grant select, update , insert on departments to departments with grant option;
Grant succeeded.
Q3: Develop a query to revoke all privileges of employees table from departments
table
Ans: SQL> Revoke all on employees from departments; Revoke succeeded.
Q4: Develop a query to revoke some privileges of employees table from departments
table
Ans: SQL> Revoke select, update , insert on departments from departments;
Revoke succeeded.
COMMIT
SAVE POINT
ROLLBACK
RESULT:
Thus the SQL program used to complex transactions DCL and TCL commands are executed
successfully.
EX.NO:8 SQL TRIGGERS
DATE:
AIM:
To write SQL Triggers for insert, delete, and update operations in a database table.
PROCEDURE:
Trigger is invoked by Oracle engine automatically whenever a specified event
occurs. Trigger is stored intodatabase and invoked repeatedly, when specific condition match.
Triggers are stored programs, which are automatically executed or fired when some
event occurs.
Triggers are written to be executed in response to any of the following events.
A database manipulation (DML) statement (DELETE, INSERT, or UPDATE).
A database definition (DDL) statement (CREATE, ALTER, or DROP).
A database operation (SERVERERROR, LOGON, LOGOFF, STARTUP, or SHUTDOWN).
Triggers could be defined on the table, view, schema, or database with which the event
is associated.
RESULT:
Thus the PL/SQL Triggers program was created and executed successfully.
EX.NO:9 VIEW AND INDEX
DATE:
AIM:
To create a view and index for database tables with a large number of records.
PROCEDURE:
CREATE VIEW Statement
In SQL, a view is a virtual table based on the result-set of an SQL statement.
A view contains rows and columns, just like a real table. The fields in a view are fields
from one or more
real tablesin the database.
You can add SQL statements and functions to a view and present the data as if the
data were coming from onesingle table.
A view is created with the CREATE VIEW statement.
RESULT:
Thus the View and Index SQL Program using executed successfully.
Ex No:10 XML DATABASE
DATE:
AIM:
To create an XML database to validate it using XML Schema.
PROCEDURE:
The XML schemas are used to represent the structure of XML document.
The goal or purpose of XML schema is to define the building blocks of an XML
document. These can be used as analternative to XML DTD.
The XML schema language is called as XML Schema Definition (XSD) language.
XML schema defines elements, attributes, elements having child elements, order of
child elements. It also definesfixed and default values of elements and attributes.
XML schema also allows the developer to use data types.
OUTPUT:
RESULT:
Thus the XML database was created and validated successfully.
EX.NO:11 NOSQL DATABASE TOOLS
DATE:
AIM:
To create a Document, column and Graph based data using NOSQL Database tool.
PROCEDURE:
Document Oriented Database – NOSQL DATABASE TOOLS (MONGO DB)
A collection of documents
Data in this model is stored inside documents.
A document is a key value collection where the key allows access to its value.
Documents are not typically forced to have a schema and therefore are flexible and
easy to change.
Documents are stored into collections in order to group different kinds of data.
Documents can contain many different key-value pairs, or key-array pairs, or even
nested documents.
Here is a comparison between the classic relational model and the document model
Example of Document Oriented databases : MongoDB, CouchDB etc.
OUTPUT:
Vertex Creation
Execute the following query to create a vertex without 'name' and on the base class V.
If the above query is executed successfully, you will get the following output.
Execute the following query to create a new vertex class named v1, then create vertex in that class.
Execute the following query to create a new vertex of the class named v1, defining its properties
such as brand = 'Maruti'and name = 'Swift'.
Edge Creation
Execute the following query to create an edge E between two vertices #9:0 and #14:0.
If the above query is executed successfully, you will get the following output.
RESULT:
Thus the NOSQL database tools are used to Document based, Column Based and Graph based
databases are created andexecuted successf
EX.NO:12 GUI BASED DATABASE APPLICATION
DATE:
AIM:
To create a Simple GUI based database application and incorporate all the features.
PROCEDURE:
CREATING A SIMPLE CUSTOMER SCREEN WHICH TAKES CUSTOMER NAME, COUNTRY, GENDER, HOBBY
ANDSTATUS
1.Create a Windows Form and in the 'Text' Properties of the Form write: Customer Data Entry Screen
2. Add Labels from the Tool Box and Add corresponding Text Boxes for Name and Country field.
3. Use radioButton when you want to select a single option from multiple choices.
4. To keep the radio Buttons in a group, first Drag a GroupBox and inside it drag the radioButtons.
5. Use checkbox when you want to select multiple options from a set of choices.
6. Give unique names for each radioButtons and checkboxes
7. In the following form radioButton for Male is named as radioMale and for Female as radioFemale
8. Similarly radioButton for Married is named as radioMarried and for Unmarried as radioUnmarried.
9. Finally, a Preview Button is added at the bottom of the form which when clicked will show the
given data inanother form.
10. Name the preview button as btnPreview.the checkbox for Reading and Painting are named
as chkReading andchkPainting.
CREATING A PREVIEW SCREEN THAT WILL DISPLAY DATA ENTERED IN TO THE CUSTOMER DATA ENTRY
SCREEN
1.In the Customer Data Entry form, double click the button Preview and write down the functionality of the
clicking eventthat is show a form which will contain 5 labels for the titles and another 5 Labels to show the
data that was given as input.
2. Write a user-defined function SetValues (. . .) that sets the value of the given input to the Labels.
CREATING NAVIGATIONAL MENUS WHICH WILL HELP US TO NAVIGATE CUSTOMER ENTRY SCREEN
AND DISPLAYSCREENS EASILY
1. To create a user friendly interface we can use something called MDIParent Form and name
it as MDICustomer.
2
it.
3. In the Design View of MDIParent, Click File and you will find some menu options
4. Edit a menu name and give it Enter Customer 5. Then double click the Enter Customer menu
6. When this menu is clicked you have to invoke the Customer Data Entry Form which is
named here asfrmCustomerDataEntry
7. To keep the form within the MDIParent select the obj.MdiParent = this;
8. To show the form use the function Show ()
9. In the program.cs file specify the form MDICustomer. This will invoke the MDICustomer form
when the application isrun.
REUSING CODE BY CREATING CLASSES AND OBJECTS AND FORM VALIDATION
1.An important feature of programming is to avoid the reusability of code.
2. To write the validations of different types, create a new class.
3. View Solution Explorer Select your project Right Click Add New Item Add a new
Class and name it asCustomerValidation.cs
4. In CustomerValidation.cs you can define various functions with different names and parameters. An
example is givenbelow –
In frmCustomerDataEntry:
1.In the Preview button Click event we have write some code to specify that if the user does not enter any
CustomerName then an error message should be displayed.
2. We have to call the CheckCustomerName (string) function in the CustomerValidation class.
3. This code is specified in the try-catch block below -
CONNECTING TO SQL SERVER, GETTING DATA AND GETTING ACQUAINTED WITH ADO.NET
COMPONENTS, LIKECONNECTION, COMMAND AND DATA READER
As C# is a Microsoft Product, it is better to use a Microsoft Data management software which is SQL
Server.
•ADO.NET (ActiveX Data Object) helps to connect the User Interface to the Database and also sends and
retrieves datafrom SQL Server
•To Create a Database connection you have to follow the steps below –
1. e:
".\
4. To create a new Table in the Database you have to select your new connection and under it there is
an option Tables.Right Click on the Tab
5. Save this and give a name (here CustomerDB is given) to the Table
ve datatypes
: INSERTING INTO DATABASE AND DYNAMIC CONNECTION STRING
RESULT:
Thus the Visual Studio C# Program used to Database Application GUI was designed successfully.
EX.NO:13 CASE STUDY
DATE:
AIM:
To design database using ER modeling, normalization constraints and to implement the
Operations of BankManagement System using the visual basic as front end and oracle as back
end todesign a forms.
PROCEDURE:
Database design using E-R model and Normalization
SALESMAN (Salesman_id, Name, City, Commission) CUSTOMER (Customer_id, Cust_Name, City,
Grade, Salesman_id)
ORDERS (Ord_No, Purchase_Amt, Ord_Date, Customer_id, Salesman_id)
Write SQL queries to
1. Count the customers with grades above Bangalore's average.
2. Find the name and numbers of all salesmen who had more than one customer.
3. List all salesmen and indicate those who have and don't have customers in their cities (Use
UNION operation.)
4. Create a view that finds the salesman who has the customer with the highest order of a day.
5. Demonstrate the DELETE operation by removing salesman with id 1000. All his orders must
also be deleted.
Representing Relationships
• 1:1 Relationships. The key of one relation is stored in the second relation. Look at examplequeries
to determine which keyis queried most often.
• 1:N Relationships. Parent - Relation on the "1" side. Child - Relation on the "Many" side.
• Represent each Entity as a relation. Copy the key of the parent into the child relation.
• M:N Solution: Introduce a third Intersection relation and copy keys from original two relations.
• Relationships. Many to Many relationships can not be directly implemented in relations.
•
ER MODEL DIAGRAM
CREATE TABLE SALESMAN (SALESMAN_ID NUMBER (4), NAME VARCHAR2 (20), CITY VARCHAR2 (20),
COMMISSION VARCHAR2 (20), PRIMARY KEY (SALESMAN_ID));
CREATE TABLE CUSTOMER1 (CUSTOMER_ID NUMBER (4), CUST_NAME VARCHAR2 (20), CITY
VARCHAR2 (20), GRADE NUMBER (3), PRIMARY KEY (CUSTOMER_ID), SALESMAN_ID REFERENCES
SALESMAN (SALESMAN_ID) ON DELETE SET NULL);
CREATE TABLE ORDERS (ORD_NO NUMBER (5), PURCHASE_AMT NUMBER (10, 2), ORD_DATE DATE,
PRIMARY KEY (ORD_NO), CUSTOMER_ID REFERENCES CUSTOMER1 (CUSTOMER_ID) ON DELETE
CASCADE, SALESMAN_ID REFERENCES SALESMAN (SALESMAN_ID) ON DELETE CASCADE);
Queries:
SELECT GRADE, COUNT (DISTINCT CUSTOMER_ID) FROM CUSTOMER1 GROUP BY GRADE HAVING
GRADE > (SELECTAVG(GRADE) FROM CUSTOMER1 WHERE CITY='BANGALORE');
1.Find the name and numbers of all salesmen who had more than one customer.
SELECT SALESMAN_ID, NAME FROM SALESMAN A WHERE 1 < (SELECT COUNT (*) FROM
CUSTOMER1 WHERESALESMAN_ID=A.SALESMAN_ID);
2. List all salesmen and indicate those who have and don't have customers in their cities
(UseUNION operation.) SELECT SALESMAN.SALESMAN_ID, NAME, CUST_NAME, COMMISSION FROM
SALESMAN, CUSTOMER1 WHERE SALESMAN.CITY = CUSTOMER1.CITY UNION SELECT SALESMAN_ID,
NAME, 'NO MATCH', COMMISSION FROM SALESMAN WHERE NOT CITY = ANY (SELECT CITY FROM
CUSTOMER1) ORDER BY 2 DESC;
3. Create a view that finds the salesman who has the customer with the highest
order of a day.
CREATE VIEW ELITSALESMAN AS SELECT B.ORD_DATE, A.SALESMAN_ID, A.NAME FROM
SALESMAN A, ORDERS B WHERE A.SALESMAN_ID = B.SALESMAN_IDAND
B.PURCHASE_AMT=(SELECT MAX (PURCHASE_AMT) FROM ORDERS C
WHERE C.ORD_DATE = B.ORD_DATE);
4. Demonstrate the DELETE operation by removing salesman with id 1000. All his orders
must also be deleted.
Use ON DELETE CASCADE at the end of foreign key definitions while creating child table
orders and then execute the following: DELETE FROM SALESMAN WHERE
SALESMAN_ID=1000;
RESULT:
Thus the SQL Program used to databases are designed using ER modeling, normalization constraints
and the Operations of Bank Management System using visual basic as front end and oracle as back
end to design forms successfully.