CS3481 DBMS
CS3481 DBMS
Regulation 2021
(As Per Anna University, Chennai syllabus)
PAGE
S. NO. LIST OF EXPERIMENTS / PROGRAMS
NO.
1
1 DDL & DML COMMANDS
7
2 CONSTRAINTS
9
3 AGGREGATE FUNCTIONS
13
4 SUB QUERIES AND SIMPLE JOIN
15
5 NATURAL, EQUI, OUTER JOINS
20
6 FUNCTIONS & PROCEDURES
23
7 DCL & TCL COMMANDS
26
8 SQL TRIGGERS
COURSE OBJECTIVES:
To learn and implement important commands in SQL.
To learn the usage of nested and joint queries.
To understand functions, procedures and procedural extensions of databases.
To understand design and implementation of typical database applications.
To be familiar with the use of a front end tool for GUI based application development.
LIST OF EXPERIMENTS:
1. Create a database table, add constraints (primary key, unique, check, Not null), insert
rows, update and delete rows using SQL DDL and DML commands.
2. Create a set of tables, add foreign key constraints and incorporate referential integrity.
3. Query the database tables using different ‘where’ clause conditions and also implement
aggregate functions.
4. Query the database tables and explore sub queries and simple join operations.
5. Query the database tables and explore natural, equi and outer joins.
6. Write user defined functions and stored procedures in SQL.
7. Execute complex transactions and realize DCL and TCL commands.
8. Write SQL Triggers for insert, delete, and update operations in a database table.
9. Create View and index for database tables with a large number of records.
10. Create an XML database and validate it using XML schema.
11. Create Document, column and graph based data using NOSQL database tools.
12. Develop a simple GUI based database application and incorporate all the above-mentioned
features
13. Case Study using any of the real life database applications from the following list
a) Inventory Management for a EMart Grocery Shop
b) Society Financial Management
c) Cop Friendly App – Eseva
d) Property Management – eMall
e) Star Small and Medium Banking and Finance
● Build Entity Model diagram. The diagram should align with the business and functional goals
stated in the application.
● Apply Normalization rules in designing the tables in scope.
● Prepared applicable views, triggers (for auditing purposes), functions for enabling enterprise
grade features.
● Build PL SQL / Stored Procedures for Complex Functionalities, ex EOD Batch
Processing for calculating the EMI for Gold Loan for each eligible Customer.
● Ability to showcase ACID Properties with sample queries with appropriate settings
TOTAL: 45 PERIODS
COURSE OUTCOMES:
On completion of this course, the students will be able to:
CO1: Create databases with different types of key constraints
CO2: Construct simple and complex SQL queries using DML and DCL commands.
CO3: Use advanced features such as stored procedures and triggers and incorporate in GUI based
application development.
CO4: Create an XML database and validate with meta-data (XML schema).
CO5: Create and manipulate data using NOSQL database.
LABORATORY REQUIREMENT
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 data in 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 constraints apply 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 quick
2
A) Alter a Table – Add Column
3
C) Alter a Table – Rename Column
Rename Table
4
INSERT - insert data into a table
DELETE - Delete all records from a database table
UPDATE - updates existing data within a table
SELECT - retrieve data from a database
1.2.1 Insert Values into Tables:
5
SELECT operation from Table:
RESULT:
Thus the DDL, DML commands used to table was created with all constraints and executed
successfully.
6
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).
7
Without Foreign Key Table Creation:
RESULT:
Thus the foreign key used to different set of table was created and executed successfully.
8
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:
9
Combining AND & OR conditions
You can also combine the AND condition with the OR condition to test more complex conditions.
Aggregate function
SQL aggregation function is used to perform the calculations on multiple rows of a single
column of a table. It returns a single 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-numeric data types.
COUNT function uses the COUNT(*) that returns the count of all the rows in a specified table.
COUNT(*) considers duplicate and Null.
10
COUNT with WHERE
Average Function
SUM Function
11
MAX Function
MIN Function
RESULT:
Thus the database tables used to different ‘where’ clause conditions applied to various
aggregate functions are executed successfully.
12
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 also perform 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 equality operator 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 right side 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)
13
Sub Queries – (UPDATE COMMANDS)
RESULT:
Thus the SQL Program used to Database table was created and explored various sub queries
using insert, delete, update and select command and also performed join operations
successfully.
14
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 joined with the first table.
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.
15
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.
16
Full Outer Join
Full outer join combines left outer join and right outer join. This join returns all records/rows
from both tables. If there are no columns matching in both tables, 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 row on the left table will be
related to each row of the right table.
17
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 three JOINS to join a table to itself. Hence
Self-join is not a type of SQL join.
Syntax for ALL TYPES OF JOINS
18
Natural Join
RESULT:
Thus the SQL Program used to Database table was created and also various join queries
operations was executed successfully.
19
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
20
Maximum of TWO Numbers
21
PL/SQL Recursive Function
Drop Function
RESULT:
Thus the PL/SQL functions program was created and executed successfully.
22
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.
23
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
24
RESULT:
Thus the SQL program used to complex transactions DCL and TCL commands are executed
successfully.
25
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 into database 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.
26
26
RESULT:
Thus the PL/SQL Triggers program was created and executed successfully.
27
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 tables in the database.
You can add SQL statements and functions to a view and present the data as if the
data were coming from one single table.
A view is created with the CREATE VIEW statement.
28
Index Create Statement
The SQL statement below creates an index named "l_name" on the "Last_Name" column in
the "customer" table:
RESULT:
Thus the View and Index SQL Program using executed successfully.
29
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 an alternative 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 defines fixed and default values of elements and attributes.
XML schema also allows the developer to use data types.
30
OUTPUT:
RESULT:
Thus the XML database was created and validated successfully.
31
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.
32
11.1.2 Multiple Document Insertion Query
33
Column Based Database – NOSQL DATABASE TOOLS (CASSENDRA / SQLLite)
Column-oriented databases primarily work on columns and every column is treated
individually.
Values of a single column are stored contiguously.
Column stores data in column specific files.
In Column stores, query processors work on columns too.
All data within each column data file have the same type which makes it ideal for
compression.
Column stores can improve the performance of queries as it can access specific column data.
Example of Column-oriented databases : BigTable, Cassandra, SimpleDB etc.
OUTPUT:
34
OrientDB database is not only a Document database but also a Graph database. New concepts
such as Vertex and Edge are used to store the data in the form of graph. It applies polymorphism
on vertices. The base class for Vertex is V.
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'.
35
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 and executed successfully.
36
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 in another form.
10. Name the preview button as btnPreview.the checkbox for Reading and Painting are
named as chkReading and chkPainting
37
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 event that 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.
38
CREATING NAVIGATIONAL MENUS WHICH WILL HELP US TO NAVIGATE
CUSTOMER ENTRY SCREEN AND DISPLAY SCREENS 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 as frmCustomerDataEntry
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 is run.
39
40
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 as CustomerValidation.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 Customer Name 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 -
41
42
CONNECTING TO SQL SERVER, GETTING DATA AND GETTING ACQUAINTED
WITH ADO.NET COMPONENTS, LIKE CONNECTION, 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 data from 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
43
44
: INSERTING INTO DATABASE AND DYNAMIC CONNECTION STRING
45
RESULT:
Thus the Visual Studio C# Program used to Database Application GUI was designed successfully.
46
EX.NO:13 CASE STUDY
DATE:
AIM:
To design database using ER modeling, normalization constraints and to implement the
Operations of Bank Management 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 key is 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
47
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);
48
Insertion of Values to Tables
INSERT INTO SALESMAN VALUES (1000, ‗JOHN‘,‘BANGALORE‘,‘25 %‘);
INSERT INTO CUSTOMER1 VALUES (10,
‗PREETHI‘,‘BANGALORE‘, 100, 1000);
INSERT INTO ORDERS VALUES (50, 5000, ‗04-MAY-17‘, 10, 1000);SELECT * FROM
SALESMAN;
Queries:
49
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 WHERE SALESMAN_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);
50
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.
51