Print Dbmslab2023 V
Print Dbmslab2023 V
YEAR :
BRANCH :
REGISTER NUMBER :
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
aggregatefunctions.
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
EX.NO 1: DDL,DML,DCL,TCL COMMANDS
//1.CREATE COMMAND
//TABLE CREATION:
SQL>create table exp1_accounts(Accountno number(5),Customername varchar2(10),Balance
number(5),Accountstatus varchar2(10),Branchname varchar(15));
Table created.
//TABLE DESCRIPTION:
SQL> desc exp1_accounts;
Name Null? Type
---------------------- -------- -------------------
ACCOUNTNO NUMBER(5)
CUSTOMERNAME VARCHAR2(10)
BALANCE NUMBER(5)
ACCOUNTSTATUS VARCHAR2(10)
BRANCHNAME VARCHAR2(15)
//INSERT RECORDS:
SQL> insert into exp1_accounts values(101,'smith',500,'closed','chennai');
1 row created.
SOL>insert into exp1_accounts values(102,'williams',600,'active','trichy');
1row created.
SQL> insert into exp1_accounts values(103,'jones',700,'closed','madurai');
1 row created.
SQL> insert into exp1_accounts values(104,'henry',800,'inactive','chennai');
1 row created.
SQL>insert into exp1_accounts values(105,'turner',900,'closed','kerela');
1 row created
//selects or view all records from table:
SQL> select *from exp1_accounts;
ACCOUNTNO CUSTOMERNAME BALANCE ACCOUNTSTATUS BRANCHNAME
---------- ---------- ---------- ---------- ---------------
101 smith 500 closed chennai
102 williams 600 active trichy
103 jones 700 closed madurai
104 henry 800 inactive chennai
105 turner 900 closed kerela
2)ALTER COMMAND:
a) Add New Column to the existing table using alter command
SQL> alter table exp1_accounts add password varchar2(10);
Table altered.
//DESCRIPTION OF A TABLE:
SQL> desc exp1_accounts;
Name Null? Type
---------------------- -------- -------------------
ACCOUNTNO NUMBER(5)
CUSTOMERNAME VARCHAR2(10)
BALANCE NUMBER(5)
ACCOUNTSTATUS VARCHAR2(10)
BRANCHNAME VARCHAR2(15)
PASSWORD VARCHAR2(10)
//TRUNACATE
SQL> truncate table exp1_accounts;
Table truncated.
//DROP command
SQL> drop table IT_exp1_account;
Table dropped.
//Rename a table
SQL> alter table exp1_accounts rename to temptable1;
Table altered.
//RENAME TABLE
ALTER TABLE customers RENAME TO contacts;
//CREATION OF TABLE
SQL>create table exp1_students(sid number(5),sname varchar2(20),sage number(10),sarea
varchar2(20),sdept varchar2(20));
Table created.
//insert command
//INSERTION OF VALUES INTO THE TABLE
SQL> insert into exp1_students values(101,'ashwin',19,'annanagar','eee');
1 row created.
SQL> insert into exp1_students values(102,'bhavesh',18,'nungambakkam','cse');
1 row created.
SQL> insert into exp1_students values(103,'pruthvik',20,'annanagar','ece');
1 row created.
SQL> insert into exp1_students values(104,'charith',20,'kilpauk','mech');
1 row created.
//UPDATE command
SQL>update exp1_students set sage=25 where sid=101;
1 row updated.
SQL>update exp1_students set sage=78 where sid=102;
1 row updated.
//TABLE CREATION
//ROLLBACK COMMAND DEMO
SQL>create table exp1_persons(pid number(5),firstname varchar2(10),lastname
varchar2(10),address varchar2(10),age number(5));
Table created.
Privilege Description
SELECT Ability to perform SELECT statements on the table.
INSERT Ability to perform INSERT statements on the table.
UPDATE Ability to perform UPDATE statements on the table.
DELETE Ability to perform DELETE statements on the table.
REFERENCES Ability to create a constraint that refers to the table.
ALTER Ability to perform ALTER TABLE statements to change the table
definition.
INDEX Ability to create an index on the table with the create index statement.
ALL All privileges on table.
Example:
SQL>GRANT SELECT,INSERT,UPDATE,DELETE ON suppliers TO smithj;
SQL>GRANT ALL ON suppliers TO smithj;
SQL>GRANT SELECT ON suppliers TO public;
Revoke command
//Revoke Privileges on Table Example:
REVOKE DELETE ON suppliers FROM anderson;
REVOKE ALL ON suppliers FROM anderson; REVOKE
ALL ON suppliers FROM public;
SELECT:
This command helps you to select the attribute based on the condition described by the
WHERE clause.
Syntax:
SELECT expressions FROM TABLES WHERE conditions;
For example:
SQL> SELECT FirstName FROM Student WHERE RollNo>15;
EXP.NO : 2 INTEGRITY CONSTRAINTS
1)primary key 2)foreign key 3)check 4)unique 5)not null
//insert tuples/records
SQL> insert into exp2_employee values(101,'tamil',15000);
1 row created.
SQL> insert into exp2_employee values(102,'hayes',14000);
1 row created.
SQL> insert into exp2_employee values(103,'william',12000);
1 row created.
//Create table
SQL>create table exp3_student(sid number(5) primary key,sname varchar2(20),sdept
varchar2(20),mark number(4));
Table created.
//ARITHMETIC OPERATION
SQL> select sname,mark+10 from exp3_student;
SNAME MARK+10
-------------------- ----------
hayes 50
william 60
jhon 70
smith 80
william 90
//CONCATENATION OPERATOR
SQL> select sname || 'is a'||sdept||'engineer' as profession from exp3_student;
PROFESSION
---------------------------
hayes is a cse engineer
william is a eee engineer
jhon is a ece engineer
smith is a ece engineer
william is a cse engineer
//distinct operation
SQL> select distinct sdept from exp3_student;
SDEPT
-----------
cse
eee
ece
//Between operator
SQL> select *from exp3_student where sid between 100 and 102;
//In predicate
SQL> select *from exp3_student where sid in(102,103);
SID SNAME SDEPT MARK
---------- -------------------- -------------------- ----------
102 william eee 50
103 smith ece 70
SQL> select *from exp3_student;
SID SNAME SDEPT MARK
---------- -------------------- -------------------- ----------
100 hayes cse 40
102 william eee 50
101 jhon ece 60
103 smith ece 70
105 william cse 80
//AND operator
SQL> select *from exp3_student where sid>102 and sname='smith';
SID SNAME SDEPT MARK
---------- -------------------- -------------------- ----------
103 smith ece 70
//OR operator
SQL> select *from exp3_student where sid>102 or sname='smith';
SID SNAME SDEPT MARK
---------- -------------------- -------------------- ----------
103 smith ece 70
105 william cse 80
//not in predicate
SQL> select *from exp3_student where sid not in(102,103);
SID SNAME SDEPT MARK
---------- -------------------- -------------------- ----------
//AGGREGATE FUNCTIONS(min,max,count,sum,avg)
SQL> select *from exp3_student;
SID SNAME SDEPT MARK
---------- -------------------- -------------------- ----------
//max function
SQL> select max(mark) as Result from exp3_student;
RESULT
----------
80
//average function
SQL> select avg(mark) as Result from exp3_student;
RESULT
----------
60
//count function
SQL> select count(*) as Result from exp3_student;
RESULT
----------
5
cse 120
eee 50
ece 130
SDEPT TOTAL
-------------------- ----------
cse 120
//having clause
SQL> select sdept,sum(mark) as total from exp3_student group by sdept having
sum(mark)<=50;
SDEPT TOTAL
-------------------- ----------
eee 50
EX.NO4 : SUB QUERY AND SIMPLE JOIN OPERATIONS
//JOINS DEMO
//table1
SQL> create table exp4_student1(sid number(5),sname varchar2(20));
Table created.
//table1 structure
SQL> desc exp4_student1;
Name Null? Type
----------- -------- ----------------------------
SID NUMBER(5)
SNAME VARCHAR2(20)
//create table2
SQL> create table exp4_student2(sid number(5),sdept varchar2(10),marks number(5));
Table created.
//table2 structure
SQL> desc exp4_student2;
Name Null? Type
----------------------------------------- -------- ----------------------------
SID NUMBER(5)
SDEPT VARCHAR2(10)
MARKS NUMBER(5)
//table2 records
SQL> insert into exp4_student2 values(100,'cse',70);
1 row created.
SQL> insert into exp4_student2 values(101,'ece',80);
1 row created.
SQL> insert into exp4_student2 values(102,'eee',90);
1 row created.
100 cse 70
101 ece 80
102 eee 90
EQUI JOIN(The join condition or the comparison operator present in the WHERE
clause of the select statement)
100 cse 70
101 ece 80
102 eee 90
//first view all sid from second table
SQL> select sid from exp4_student2;
SID
----------
100
101
102
//select sid from table2 and pass to table1 to view student name
SQL> select sname from exp4_student1 where exp4_student1.sid in(select sid from
exp4_student2);
SNAME
--------------------
tamil
jhon
hayes
SQL> select sname,sid from exp4_student1 where exp4_student1.sid in(select sid from
exp4_student2);
SNAME SID
-------------------- ----------
tamil 100
jhon 101
hayes 102
//some keyword
SQL> select sid,sdept,marks from exp4_student2 where marks >some (select marks from
exp4_student2 where sdept='cse');
SID SDEPT MARKS
---------- ---------- ----------
102 eee 90
101 ece 80
SQL> select sid,sdept,marks from exp4_student2 where marks >=some (select marks
from exp4_student2 where sdept='cse');
SID SDEPT MARKS
---------- ---------- ----------
102 eee 90
101 ece 80
100 cse 70
SQL> select sid,sdept,marks from exp4_student2 where marks >=any (select marks
from exp4_student2 where sdept='cse');
SID SDEPT MARKS
---------- ---------- ----------
102 eee 90
101 ece 80
100 cse 70
//all keyword
SQL> select sid,sdept,marks from exp4_student2 where marks >all (select marks
from exp4_student2 where sdept='cse');
SQL> select sid,sdept,marks from exp4_student2 where marks <all (select marks
from exp4_student2 where sdept='cse');
no rows selected
SQL> select sid,sdept,marks from exp4_student2 where marks <=all (select marks
from exp4_student2 where sdept='cse');
SID SDEPT MARKS
---------- ---------- ----------
100 cse 70
EX.NO 5 TRIGGERS
Connected to:
Personal Oracle Database 11g Release 11.2.0.1.0 - Production
With the Partitioning, OLAP, Data Mining and Real Application Testing options
//insert 3records
SQL> insert into tbl1 values(106,'hayes',12000);
1 row created.
SQL> insert into tbl1 values(107,'jhon',11000);
1 row created.
SQL> insert into tbl1 values(108,'smith',10000);
1 row created.
Trigger types
1.statement level trigger
2.Row level trigger
1.STATEMENT LEVEL TRIGGER -DEMO
//trigger creation
SQL> create or replace trigger stmt_level_demo
2 before update
3 on tbl1
4 begin
5 DBMS_OUTPUT.PUT_LINE('Statement level Trigger executed');
6 end;
7 /
Trigger created.
//trigger execution
SQL> select *from tbl1;
EMPID EMPNAME EMPSALARY
---------- -------------------- ----------
106 hayes 12000
107 jhon 11000
108 smith 10000
//trigger output
SQL> update tbl1 set empname='william';
Statement level Trigger executed.3 rows updated.
------------------------------------------------------------------------------------------------------------
2.ROW LEVEL TRIGGER
//Example1
//trigger creation
SQL> create or replace trigger row_level_demo
2 before update
3 on tbl1
4 for each row
5 begin
6 DBMS_OUTPUT.PUT_LINE('Row Level trigger executed');
7 end;
8 /
Trigger created.
Table 2: employee_history
SQL>CREATE TABLE employee_history(emp_id NUMBER, emp_Name
VARCHAR2(20),salary NUMBER);
Table created.
//Trigger Creation
SQL> CREATE OR REPLACE TRIGGER salary_history_trigger
2 BEFORE UPDATE OF salary
3 ON emp2
4 FOR EACH ROW
5 BEGIN
6 INSERT INTO employee history VALUES (:old.emp_id,:old.emp_name,:old.salary);
7 END;
8 /
Trigger created.
//Trigger Execution
SQL> UPDATE emp2 SET salary=1500 WHERE emp_id=100;
1 row updated.
Note:When ever we update records in emp2 table,the old values of emp2 table are saved
into the emp_history by using triggers.
EX.NO 6 VIEWS AND INDEX
a) Simple View
- If a view is created based on single table
- It should not have any group function, aggregate function etc..
//create table
SQL> create table exp9_employees(empid number(5),empname varchar2(15),salary
int,mobileno number(10));
Table created.
SQL> desc exp9_employees;
Name Null? Type
--------------- -------- --------------
EMPID NUMBER(5)
EMPNAME VARCHAR2(15)
SALARY NUMBER(38)
MOBILENO NUMBER(10)
//record insertion
SQL> insert into exp9_employees values(100,'tamil',25000,9994392172);
1 row created.
SQL> insert into exp9_employees values(101,'vinoth',11000,9894383533);
1 row created.
SQL> insert into exp9_employees values(103,'ram',12000,9791069676);
1 row created.
SQL> insert into exp9_employees values(104,'kumar',14000,9999988888);
1 row created.
SQL> insert into exp9_employees values(105,'ananth',12000,9791067684);
1 row created.
SQL> insert into exp9_employees values(106,'arun',11000,9898756456);
1 row created.
//view creation
SQL> create or replace view tempview1
2 as
3 select empid,empname from exp9_employees where empid in(100,101,103);
View created.
//view execution
SQL> select *from tempview1;
EMPID EMPNAME
---------- ---------------
100 tamil
101 vinoth
103 aravind
//view updation
SQL> update tempview1 set empname='kumar' where empid=100;
1 row updated.
//now convert the above self join operations into view(sef join execution with help of
view)
//COMPLEX VIEW CREATION
SQL> create or replace view cmpview1
2 as
3 select e1.empid,e1.empname,e2.mobileno,e2.deptid
4 from exp9_employees e1,exp9_employees e2
5 where e1.empid=e2.deptid order by deptid;
View created.
EMPID EMPNAME
---------- ---------------
100 tamil
101 vinoth
103 ram
104 kumar
105 ananth
106 arun
5 rows selected.
//when update view,it shows error,bz its read only view
SQL> update tempview2 set empname='bala' where empid=100;
update tempview2 set empname='bala' where empid=100
ERROR at line 1:
ORA-42399: cannot perform a DML operation on a read-only view
//before index
SQL> select *from exp9_emp1;
EMPID ENAME JOB SALARY
---------- --------------- -------------------- ----------
100 tami hr 15000
101 anbu manager 12000
102 ramesh programmer 14000
103 jhon tester 13000
104 ashok leader 16000
NOTE: we can check difference between normal execution and index execution answers if
we insert more than 100 records. Time delay will get reduce if we use,index concepts.
EX.NO7 USER DEFINED FUNCTIONS AND PROCEDURES IN
ORACLE
//PROCEDURES DEMO
//EXAMPLE1 //PROCEDURE CREATION
SQL> create procedure si29(p number, n number, r number)
2 is s number;
3 begin
4 s:=(p*n*r)/100;
5 dbms_output.put_line('SI='||s);
6 end;
7/
Procedure created.
//PROCEDURE CALLING
SQL> declare p number:=&p;
2 n number:=&n;
3 r number:=&r;
4 begin
5 si29(p,n,r);
6 end;
7/
//Output
Enter value for p: 1000
old 1: declare p number:=&p;
new 1: declare p number:=1000;
SI=60
PL/SQL procedure successfully completed.
---------------------------------------------------------------------------------------------------------------
//EXAMPLE 2
//CREATIONS OF PROCEUDRE
SQL>CREATE OR REPLACE PROCEDURE welcome_msg (p_name IN VARCHAR2)
IS
BEGIN
dbms_output.put_line ("Welcome"||p_name);
END;
/
//Ouput
welcome Guru99
//EXAMPLE 3
//CREATION OF PROCEDURES FOR TABLES AND READ TABLE VALUES
//CREATE TABLE
SQL> create table ititems(itemid number(3), actualprice number(5), ordid number(4),
prodid number(4)); Table created.
//INSERT 4RECORDS
SQL> insert into ititems values(101, 2000, 500, 201);
1 row created.
SQL> insert into ititems values(102, 3000, 1600, 202);
1 row created.
SQL> insert into ititems values(103, 4000, 600, 202);
1 row created.
//execution procedure
SQL> exec procedurename1(103);
//OUTPUT:
Actual price is 4000
PL/SQL procedure successfully completed.
--------------------------------------------------------------------------------------------------------------
//EXAMPLE4 //PROCEDURE FOR ‘OUT’ PARAMETER – CREATION,
EXECUTION
SQL> set serveroutput on;
//procedure creation
SQL> create procedure zzz (a in number, b out number) is identity number;
2 begin
3 select ordid into identity from ititems where itemid=a;
4 if identity<1000 then
5 b:=100;
6 end if;
7 end;
8 / Procedure created.
//CALLING PROCEDURE FROM CALLER PROGRAM
SQL> declare
2 a number;
3 b number;
4 begin
5 zzz(101,b);
6 dbms_output.put_line('The value of b is '|| b);
7 end;
8/
OUTPUT
The value of b is 100
PL/SQL procedure successfully completed.
------------------------------------------------------------------------------------------------
//PROCEDURE FOR ‘INOUT’ PARAMETER – CREATION, EXECUTION
//EXAMPLE5
//procedure creation
SQL> create procedure itit( a in out number) is begin
2 a:=a+1;
3 end;
4/
Procedure created.
//calling procedure
SQL> declare
2 a number:=7;
3 begin
4 itit(a);
5 dbms_output.put_line('The updated value is '||a);
6 end;
7/
OUTPUT
The updated value is 8
PL/SQL procedure successfully completed.
-----------------------------------------------------------------------------------------------------------
FUNCTION CREATION DEMO
//Example1//CREATION FUNCTIONS
SQL> create or replace function facto29 (n number)
2 return number is a number;
3 f number:=1;
4 begin
5 for i in 1..n loop
6 f:=f*i;
7 end loop;
8 return f;
9 end;
10 /
Function created.
//FUNCTION CALLING
SQL> declare n number:=&n;
2 a number;
3 begin
4 a:=facto29(n);
5 dbms_output.put_line('factorial = '||a);
6 end;
7/
//EXAMPLE2
//FUNCTION CREATION(DEMO2)
SQL> CREATE OR REPLACE FUNCTION welcome_msgJune(p_name IN
VARCHAR2)RETURN VARCHAR2
2 is
3 begin
4 return('welcome'||p_name);
5 end;
6 /
Function created.
//FUNCTION CALLING
SQL> declare
2 lv_msg varchar2(50);
3 begin
4 lv_msg:=welcome_msgjune('guru99');
5 dbms_output.put_line(lv_msg);
6 end;
7/
output
welcome guru99
PL/SQL procedure successfully completed.
What is MongoDB?
➢ MongoDB is an open source platform written in C++ and has a very easy setup
environment.
➢ It is a cross-platform, document-oriented and non-structured database.
MongoDB provides high performance, high availability, and auto-scaling.
➢ It is a NoSQL database and has flexibility with querying and indexing. MongoDB
has very rich query language resulting in high performance.
FEATURES OF MONGODB
• Indexing: Indexes are used to boost the performance of queries.
• Schemaless: This feature makes it a flexible database to query any depth of
Document nesting.
• Sharding(Dividing and store): Using Sharding MongoDB, distribute the
big datasets over multiple nodes for high throughputs.
• Replication: Through Replication the copy of data is replicated over
multiple nodes to provide high redundancy.
2.Read Operations(select )
//to view documents from collections
gcecse> db.students.find() or //db.students.find().pretty()
[
{
_id: ObjectId("6465c42a20ff566800ba8a9d"),
rollno: 1,
name: 'hayes'
},
{
_id: ObjectId("6465ca1820ff566800ba8a9e"),
rollno: 2,
name: 'jhon'
},
{
_id: ObjectId("6465ca7320ff566800ba8a9f"),
rollno: 3,
name: 'william'
}]
//4.Delete operations
//before deletion documents
gcecse> db.students.find()
[
{
_id: ObjectId("6465c42a20ff566800ba8a9d"),
rollno: 1, name: 'hayes'
},
{ _id: ObjectId("6465ca1820ff566800ba8a9e"),
rollno: 2, name: 'jhon'
},
{ _id: ObjectId("6465ca7320ff566800ba8a9f"),
rollno: 3, name: 'arun'
}
]
//after deletion
gcecse> db.students.find()
[
{
_id: ObjectId("6465c42a20ff566800ba8a9d"),
rollno: 1, name: 'hayes'
},
{
_id: ObjectId("6465ca7320ff566800ba8a9f"),
rollno: 3, name: 'arun'
}
]
//deletion based on name column
gcecse> db.students.deleteOne({name:"arun"})
{ acknowledged: true, deletedCount: 1 }
gcecse> db.students.find()
[
{ _id: ObjectId("6465c42a20ff566800ba8a9d"),
rollno: 1, name: 'hayes
}
]
MongoDB Distributed System Architecture
EX.NO 9 DATABASE CONNECTION FOR WINDOWS APPLICATION
Aim:
To Create Real Life Database applications and demonstrate database connection.
1. Connection Class
We use connection classes to link to the database.
These connection classes similarly manage connections and connection pooling in
ADO.NET.
Example:
SqlConnection con = new
SqlConnection(ConfigurationManager.ConnectionStrings[“connection”].Connection
String);
con.Open();
–
–
con.Close();
2. Command Class
Various commands that are executed by the Command Class.
The Command class deliver methods for storing and executing SQL statements and Stored
Actions. Such as.
ExecuteReader():
Typically be an SQL select statement that covers one or more select statements and Stored
Procedures. This technique returns a DataReader object that can be used to fill a DataTable
object or used straight for production reports and so forth.
ExecuteNonQuery():
This method returns an integer that is the sum of rows affected by the query.
Performs a command that variations the data in the database, such as an update, delete, or
insert statement, or a Kept Way that covers one or more of these statements.
ExecuteScalar():
Its a kind of query returns a total count of rows or an intended value. This method
returns a single value only.
ExecuteXMLReader: Obtains data from an SQL Server 2000 database (SqlClient classes
only) using the XML stream. And Returns an XML Reader object.
3. DataReader Class()
✓ DataReader class is used in the conjunction with the Command class that performs
an SQL Select statement and then access resumed rows.
✓ The DataReader major operation is used to retrieve data.
4. DataAdapter Class
✓ The DataAdapter is supremely useful when consuming data-bound controls in the
Windows Forms, but it can likewise be used to bring an easy way to accomplish the
connection in between your application and core database tables, views and Stored
Procedures.
✓ The major job of the DataAdapter is to connect the DataSets to databases.
5. DataSet Class
✓ DataSet is essentially a collection of DataTable objects. In turn, each object contains
a group of the DataColumn and DataRow objects.
✓ DataSet also shields a kindred collection that can be used to label relations among
Data Table Objects. The DataSet is the core of ADO.NET.
//Procedure to execution
//CONNECT TO DATABASE(userid :sa password: cse)
//SOURCE CODE
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using System.Data.SqlClient; //add this namespace
namespace test1
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
//UPDATE RECORD
// DELETE RECORDS
//SEARCH RECORDS
Result:
Thus Real Life Database applications is Created and Performed CRUD operations.
EX.NO10 CREATE XML DATABASE AND VALIDATE USING XML
SCHEMA
Aim:
Create xml database and validate the xml db file using DTD or Schema.
Need for validating XML Document(check valid xml and well formed xml)
✓ XML validation is a process done to check for a syntax error in an XML document
to ensure that the document is well-written with the standard rules using either DTD
or schema.
✓ A complete XML file is considered to be valid XML document unless it has correct
syntax and constraints defined in it.
✓ Validation Process is dealt with XML Parser in two different ways.One is Well-
defined XML Document and another one is Valid XML Document.
✓ Validation is optional but when it is about data integrity it is highly recommended.
✓ In most of the cases, validation of XML is done through schemas, not by DTD.
An XSD ( XML Schema Definition Language) file defines the structure of the XML file, i.e
which elements in which order, how many times, with which attributes, how they are nested,
etc. Without an XSD, an XML file is a relatively free set of elements and attributes.
Structure of the Project(create console c# application and add xsd file and xml file)
Student.xsd(schema file)
<?xml version="1.0" encoding="utf-8"?>
<xsd:schema xmlns:xsd="http://www.w3.org/2001/XMLSchema">
<xsd:element name="Students">
<xsd:complexType>
<xsd:sequence>
<xsd:element name="Student" minOccurs="1" maxOccurs="4">
<xsd:complexType>
<xsd:sequence>
<xsd:element name="Name" minOccurs="1" maxOccurs="1"/>
<xsd:element name="Gender" minOccurs="1" maxOccurs="1"/>
<xsd:element name="TotalMarks" minOccurs="1" maxOccurs="1"/>
</xsd:sequence>
</xsd:complexType>
</xsd:element>
</xsd:sequence>
</xsd:complexType>
</xsd:element>
</xsd:schema>
3. Each Student element should contain the following 3 elements in the order specified.
i) Name
ii) Gender
iii) TotalMarks
Step 3: Add a new XML file to the project. Name it Data.xml. Copy and paste the following
XML.
OUTPUT
Result:
Thus the xml database is created and validated using xml schema.