0% found this document useful (0 votes)
29 views

Database practices lab Record NEW

Data base lab practices record new for 2025 regulations

Uploaded by

shshhshsajsjsjsj
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
29 views

Database practices lab Record NEW

Data base lab practices record new for 2025 regulations

Uploaded by

shshhshsajsjsjsj
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 58

DMI COLLEGE OF ENGINEERING

Palanchur, Chennai - 600123

DEPARTMENT OF COMPUTER SCIENCE AND

ENGINEERING

CP4152 – DATABASE PRACTICES

M.E DEGREE EXAMINATION

SEMESTER I

ACADEMIC YEAR 2023-2024


DMI COLLEGE OF ENGINEERING
Palanchur, Chennai - 600123

BONAFIDE CERTIFICATE

UNIVERSITY REGISTER NUMBER

Certified that this is bonafide record of partial work done by


Mr./Miss………………………….……. of Computer Science and Engineering
Department, in the CP4152 – DATABASE PRACTICES during the academic year 2023 –
24 and submitted for the University Practical Examination conducted on at
DMI COLLEGE OF ENGINEERING, PALANCHUR – 600123.

Staff In-charge Head of the Department

Internal Examiner External Examiner


Page
Sl.No. Date Signature
No.

2. a

2. b

2. c

2. d

2. e

4. a

4. b

10

11
EX NO:1 Data Definition Language

AIM:

PROCEDURE:
Queries:

i) Create, Alter and Drop:-


SQL QUERY:
Creating Database:
SQL> CREATE DATABASE Banking;

SQL> SHOW DATABASES;

+ +
| Database |
+ +
| information_schema |
| AMROOD |
| mysql |
| orig |
| test |
| Banking |
+ +

CREATE TABLE table_name(


column1 datatype,
column2 datatype,
column3 datatype,
.....
columnN datatype,
PRIMARY KEY( one or more columns )
);

Creating table:

SQL> CREATE TABLE CUSTOMERS(


ID INT NOT NULL,
NAME VARCHAR (20) NOT NULL,
AGE INT NOT NULL,
ADDRESS CHAR (25) ,
SALARY DECIMAL (18, 2),
PRIMARY KEY (ID)
);
Altering table:
Syntax (Add column):
ALTER TABLE table_name
ADD column_name datatype;

Example:
ALTER TABLE Customers
ADD Email varchar(255);
Syntax (Drop column):
ALTER TABLE table_name
DROP COLUMN column_name;

Example:
ALTER TABLE Customers
DROP COLUMN Email;

Drop Table:
Syntax:
DROP TABLE table_name;

Example:
SQL> DROP TABLE CUSTOMERS;
Query OK, 0 rows affected (0.01 sec)

SQL> DESC CUSTOMERS;


ERROR 1146 (42S02): Table 'TEST.CUSTOMERS' doesn't exist

Drop DB:
Syntax:
DROP DATABASE DatabaseName;
Example:
SQL> DROP DATABASE Banking;
ii) Enforce Primary Key, Foreign Key, Check, Unique and Not Null Constraints
Syntax: (Setting primary key and NOT NULL):
CREATE TABLE CUSTOMERS(
ID INT NOT NULL,
NAME VARCHAR (20) NOT NULL,
AGE INT NOT NULL,
ADDRESS CHAR (25) ,
SALARY DECIMAL (18, 2),
PRIMARY KEY (ID)
);

For already created table,


ALTER TABLE CUSTOMER ADD PRIMARY KEY (ID);

PRIMARY KEY constraint on multiple columns,


CREATE TABLE CUSTOMERS(
ID INT NOT NULL,
NAME VARCHAR (20) NOT NULL,
AGE INT NOT NULL,
ADDRESS CHAR (25) ,
SALARY DECIMAL (18, 2),
PRIMARY KEY (ID, NAME)
);

Foreign key:
Example
Consider the structure of the following two tables.
CUSTOMERS table
CREATE TABLE CUSTOMERS(
ID INT NOT NULL,
NAME VARCHAR (20) NOT NULL,
AGE INT NOT NULL,
ADDRESS CHAR (25) ,
SALARY DECIMAL (18, 2),
PRIMARY KEY (ID)
);

ORDERS table
CREATE TABLE ORDERS (
ID INT NOT NULL,
DATE DATETIME,
CUSTOMER_ID INT references CUSTOMERS(ID),
AMOUNT double,
PRIMARY KEY (ID)
);
If the ORDERS table has already been created and the foreign key has not yet been set, the use the
syntax for specifying a foreign key by altering a table.
ALTER TABLE ORDERS
ADD FOREIGN KEY (Customer_ID) REFERENCES CUSTOMERS (ID);

Unique:
CREATE TABLE CUSTOMERS(
ID INT NOT NULL,
NAME VARCHAR (20) NOT NULL,
AGE INT NOT NULL UNIQUE,
ADDRESS CHAR (25) ,
SALARY DECIMAL (18, 2),
PRIMARY KEY (ID)
);

the AGE column is set to UNIQUE, so that you cannot have two records with the same age.
Check:
CREATE TABLE CUSTOMERS(
ID INT NOT NULL,
NAME VARCHAR (20) NOT NULL,
AGE INT NOT NULL CHECK (AGE >= 18),
ADDRESS CHAR (25) ,
SALARY DECIMAL (18, 2),
PRIMARY KEY (ID)
);

Added a CHECK with AGE column, so that you cannot have any CUSTOMER who is below 18
years.

iii) Views:
CREATE VIEW:
Syntax:
CREATE VIEW view_name AS
SELECT column1, column2.....
FROM table_name
WHERE [condition];

Example:
CUSTOMERS table having the following records
create a view from the CUSTOMERS table. This view would be used to have customer name and age
from the CUSTOMERS table.
SQL > CREATE VIEW CUSTOMERS_VIEW AS
SELECT name, age
FROM CUSTOMERS;

SQL > SELECT * FROM CUSTOMERS_VIEW;

Result:
EX NO:2 a Data Manipulation Language - Insert, Delete, Update

Aim

Procedure:
Queries:
Insert:
Syntax:
INSERT INTO TABLE_NAME (column1, column2, column3,...columnN)
VALUES (value1, value2, value3,...valueN);

Example:
INSERT INTO CUSTOMERS (ID,NAME,AGE,ADDRESS,SALARY)
VALUES (1, 'Ramesh', 32, 'Ahmedabad', 2000.00 );

INSERT INTO CUSTOMERS (ID,NAME,AGE,ADDRESS,SALARY)


VALUES (2, 'Khilan', 25, 'Delhi', 1500.00 );

INSERT INTO CUSTOMERS (ID,NAME,AGE,ADDRESS,SALARY)


VALUES (3, 'kaushik', 23, 'Kota', 2000.00 );

INSERT INTO CUSTOMERS (ID,NAME,AGE,ADDRESS,SALARY)


VALUES (4, 'Chaitali', 25, 'Mumbai', 6500.00 );

INSERT INTO CUSTOMERS (ID,NAME,AGE,ADDRESS,SALARY)


VALUES (5, 'Hardik', 27, 'Bhopal', 8500.00 );

INSERT INTO CUSTOMERS (ID,NAME,AGE,ADDRESS,SALARY)


VALUES (6, 'Komal', 22, 'MP', 4500.00 );

INSERT INTO CUSTOMERS


VALUES (7, 'Muffy', 24, 'Indore', 10000.00 );

Delete:
Syntax:
DELETE FROM table_name
WHERE [condition];
Example:
SQL> DELETE FROM CUSTOMERS
WHERE ID = 6;

To DELETE all the records from the CUSTOMERS table


SQL> DELETE FROM CUSTOMERS;

Update:
Syntax:
UPDATE table_name
SET column1 = value1, column2 = value2. .. , columnN = valueN
WHERE [condition];

Example:
SQL> UPDATE CUSTOMERS
SET ADDRESS = 'Pune'
WHERE ID = 6;

SQL> UPDATE CUSTOMERS


SET ADDRESS = 'Pune', SALARY = 1000.00;
EX NO:2 b Data Manipulation Language - Cartesian Product, Equi Join, Left Outer Join, Right
Outer Join and Full Outer Join

Aim

Procedure:
CARTESIAN JOIN or CROSS JOIN:
Syntax:

SELECT table1.column1, table2.column2...


FROM table1, table2 [, table3 ]

Example:

SQL> SELECT ID, NAME, AMOUNT, DATE


FROM CUSTOMERS, ORDERS;

This would produce the following result −


+ + + + +
| ID | NAME | AMOUNT | DATE |
+ + + + +
| 1 | Ramesh | 3000 | 2009-10-08 00:00:00 |
| 1 | Ramesh | 1500 | 2009-10-08 00:00:00 |
| 1 | Ramesh | 1560 | 2009-11-20 00:00:00 |
| 1 | Ramesh | 2060 | 2008-05-20 00:00:00 |
| 2 | Khilan | 3000 | 2009-10-08 00:00:00 |
| 2 | Khilan | 1500 | 2009-10-08 00:00:00 |
| 2 | Khilan | 1560 | 2009-11-20 00:00:00 |
| 2 | Khilan | 2060 | 2008-05-20 00:00:00 |
| 3 | kaushik | 3000 | 2009-10-08 00:00:00 |
| 3 | kaushik | 1500 | 2009-10-08 00:00:00 |
| 3 | kaushik | 1560 | 2009-11-20 00:00:00 |
| 3 | kaushik | 2060 | 2008-05-20 00:00:00 |
| 4 | Chaitali | 3000 | 2009-10-08 00:00:00 |
| 4 | Chaitali | 1500 | 2009-10-08 00:00:00 |
| 4 | Chaitali | 1560 | 2009-11-20 00:00:00 |
| 4 | Chaitali | 2060 | 2008-05-20 00:00:00 |
| 5 | Hardik | 3000 | 2009-10-08 00:00:00 |
| 5 | Hardik | 1500 | 2009-10-08 00:00:00 |
| 5 | Hardik | 1560 | 2009-11-20 00:00:00 |
| 5 | Hardik | 2060 | 2008-05-20 00:00:00 |
| 6 | Komal | 3000 | 2009-10-08 00:00:00 |
| 6 | Komal | 1500 | 2009-10-08 00:00:00 |
| 6 | Komal | 1560 | 2009-11-20 00:00:00 |
| 6 | Komal | 2060 | 2008-05-20 00:00:00 |
| 7 | Muffy | 3000 | 2009-10-08 00:00:00 |
| 7 | Muffy | 1500 | 2009-10-08 00:00:00 |
| 7 | Muffy | 1560 | 2009-11-20 00:00:00 |
| 7 | Muffy | 2060 | 2008-05-20 00:00:00 |
+ + + + +

LEFT JOIN

Syntax:
SELECT table1.column1, table2.column2...
FROM table1
LEFT JOIN table2
ON table1.common_field = table2.common_field;

Example
SQL> SELECT ID, NAME, AMOUNT, DATE
FROM CUSTOMERS
LEFT JOIN ORDERS
ON CUSTOMERS.ID = ORDERS.CUSTOMER_ID;
Right Join:
Syntax:
SELECT table1.column1, table2.column2...
FROM table1
RIGHT JOIN table2
ON table1.common_field = table2.common_field;

Example:
SQL> SELECT ID, NAME, AMOUNT, DATE
FROM CUSTOMERS
RIGHT JOIN ORDERS
ON CUSTOMERS.ID = ORDERS.CUSTOMER_ID;

Full join:
Syntax:
SELECT table1.column1, table2.column2...
FROM table1
FULL JOIN table2
ON table1.common_field = table2.common_field;
Example
SQL> SELECT ID, NAME, AMOUNT, DATE
FROM CUSTOMERS
FULL JOIN ORDERS
ON CUSTOMERS.ID = ORDERS.CUSTOMER_ID;
EX NO:2 c Data Manipulation Language – Aggregate functions

Aim

Procedure:
Procedure:
Syntax:
SELECT <FUNCTION NAME> (<PARAMETER>) FROM <TABLE NAME>
Example: table : Employee

Eid Ename Age City Salary

E001 ABC 29 Pune 20000

E002 PQR 30 Pune 30000

E003 LMN 25 Mumbai 5000

E004 XYZ 24 Mumbai 4000

E005 STU 32 Bangalore 25000

AVG Function:
Select AVG(salary) from Employee
AVG(salary)= 16800

COUNT Function:
Select COUNT(*) from Employee where Salary > 20000;
Count(salary)=1

MAX Function:
Select MAX(salary) from Employee;
Max(salary)=30000

SUM Function:
Select SUM(salary) from Employee
SUM (salary)= 84000
EX NO:2 d Data Manipulation Language – Set Operation

Aim

Procedure:
SET Operators in SQL
1. UNION
2. UNION ALL
3. INTERSECT
4. MINUS

Table 1: t_employees

ID Name Department Salary Year_of_Experience

1 Aakash Singh Development 72000 2

2 Abhishek Pawar Production 45000 1

3 Pranav Deshmukh HR 59900 3

4 Shubham Mahale Accounts 57000 2

5 Sunil Kulkarni Development 87000 3

6 Bhushan Wagh R&D 75000 2

7 Paras Jaiswal Marketing 32000 1

Table 2: t2_employees

ID Name Department Salary Year_of_Experience

1 Prashant Wagh R&D 49000 1

2 Abhishek Pawar Production 45000 1

3 Gautam Jain Development 56000 4

4 Shubham Mahale Accounts 57000 2

5 Rahul Thakur Production 76000 4

6 Bhushan Wagh R&D 75000 2

7 Anand Singh Marketing 28000 1


Table 3: t_students

ID Name Hometown Percentage Favourite_Subject

1 Soniya Jain Udaipur 89 Physics

2 Harshada Sharma Kanpur 92 Chemistry

3 Anuja Rajput Jaipur 78 History

4 Pranali Singh Nashik 88 Geography

5 Renuka Deshmukh Panipat 90 Biology

6 Swati Kumari Faridabad 93 English

7 Prachi Jaiswal Gurugram 96 Hindi

Table 4: t2_students

ID Name Hometown Percentage Favourite_Subject

1 Soniya Jain Udaipur 89 Physics

2 Ishwari Dixit Delhi 86 Hindi

3 Anuja Rajput Jaipur 78 History

4 Pakhi Arora Surat 70 Sanskrit

5 Renuka Deshmukh Panipat 90 Biology

6 Jayshree Patel Pune 91 Maths

7 Prachi Jaiswal Gurugram 96 Hindi

Union:
Syntax:
mysql> SELECT *FROM t_employees UNION SELECT *FROM t2_employees;
ID Name Department Salary Year_of_Experience

1 Aakash Singh Development 72000 2

2 Abhishek Pawar Production 45000 1

3 Pranav Deshmukh HR 59900 3

4 Shubham Mahale Accounts 57000 2

5 Sunil Kulkarni Development 87000 3

6 Bhushan Wagh R&D 75000 2

7 Paras Jaiswal Marketing 32000 1

1 Prashant Wagh R&D 49000 1

3 Gautam Jain Development 56000 4

5 Rahul Thakur Production 76000 4

7 Anand Singh Marketing 28000 1

Union All:
Syntax:
mysql> SELECT *FROM t_employees UNION ALL SELECT *FROM t2_employees;

ID Name Department Salary Year_of_Experience

1 Aakash Singh Development 72000 2

2 Abhishek Pawar Production 45000 1

3 Pranav Deshmukh HR 59900 3

4 Shubham Mahale Accounts 57000 2

5 Sunil Kulkarni Development 87000 3

6 Bhushan Wagh R&D 75000 2


7 Paras Jaiswal Marketing 32000 1

1 Prashant Wagh R&D 49000 1

2 Abhishek Pawar Production 45000 1

3 Gautam Jain Development 56000 4

4 Shubham Mahale Accounts 57000 2

5 Rahul Thakur Production 76000 4

6 Bhushan Wagh R&D 75000 2

7 Anand Singh Marketing 28000 1

Intersect:
Syntax:
mysql> SELECT *FROM t_employees INTERSECT SELECT *FROM t2_employees;

ID Name Hometown Percentage Favourite_Subject

2 Abhishek Pawar Production 45000 1

4 Shubham Mahale Accounts 57000 2

6 Bhushan Wagh R&D 75000 2

Minus:
Syntax:
mysql> SELECT *FROM t_employees MINUS SELECT *FROM t2_employees;

ID Name Department Salary Year_of_Experience

1 Aakash Singh Development 72000 2

3 Pranav Deshmukh HR 59900 3

5 Sunil Kulkarni Development 87000 3

7 Paras Jaiswal Marketing 32000 1


EX NO:2 e Data Manipulation Language – Nested Queries

Aim

Procedure:
Example
Find the names of employee who have regno=103

The query is as follows −


select E.ename from employee E where E.eid IN (select S.eid from salary S where S.regno=103);

Student table
The student table is created as follows −

create table student(id number(10), name varchar2(20),classID number(10), marks varchar2(20));


Insert into student values(1,'pinky',3,2.4);
Insert into student values(2,'bob',3,1.44);
Insert into student values(3,'Jam',1,3.24);
Insert into student values(4,'lucky',2,2.67);
Insert into student values(5,'ram',2,4.56);
select * from student;

Output
You will get the following output −

Id Name classID Marks

1 Pinky 3 2.4

2 Bob 3 1.44

3 Jam 1 3.24

4 Lucky 2 2.67

5 Ram 2 4.56

Teacher table
The teacher table is created as follows −

Example

Create table teacher(id number(10), name varchar(20), subject varchar2(10), classID number(10),
salary number(30));
Insert into teacher values(1,’bhanu’,’computer’,3,5000);
Insert into teacher values(2,'rekha','science',1,5000);
Insert into teacher values(3,'siri','social',NULL,4500);
Insert into teacher values(4,'kittu','mathsr',2,5500);
select * from teacher;

Output
You will get the following output −

Id Name Subject classID Salary

1 Bhanu Computer 3 5000

2 Rekha Science 1 5000

3 Siri Social NULL 4500

4 Kittu Maths 2 5500

Class table
The class table is created as follows −

Example

Create table class(id number(10), grade number(10), teacherID number(10), noofstudents


number(10));
insert into class values(1,8,2,20);
insert into class values(2,9,3,40);
insert into class values(3,10,1,38);
select * from class;

Output
You will get the following output −

Id Grade teacherID No.ofstudents

1 8 2 20

2 9 3 40

3 10 1 38

Now let’s work on nested queries


Example 1

Select AVG(noofstudents) from class where teacherID IN(


Select id from teacher
Where subject=’science’ OR subject=’maths’);

Output
You will get the following output −

20.0
EX NO:3 Transaction Control Language

Aim

Procedure:
Procedure:
Commit, Rollback and Save Points

Commit:
Syntax:
COMMIT;

Example:

SQL> DELETE FROM CUSTOMERS


WHERE AGE = 25;
SQL> COMMIT;

ROLLBACK:
Syntax:
Rollback;

SQL> DELETE FROM CUSTOMERS


WHERE AGE = 25;
SQL> ROLLBACK;
Savepoint:
SQL> SAVEPOINT SP1;
Savepoint created.
SQL> DELETE FROM CUSTOMERS WHERE ID=1;
1 row deleted.
SQL> SAVEPOINT SP2;
Savepoint created.
SQL> DELETE FROM CUSTOMERS WHERE ID=2;
1 row deleted.
SQL> SAVEPOINT SP3;
Savepoint created.
SQL> DELETE FROM CUSTOMERS WHERE ID=3;
1 row deleted.

SQL> ROLLBACK TO SP2;


Rollback complete.
EX NO:4 a Triggers – Statement Level

AIM:

PROCEDURE:
Syntax:

Step 1 : Create table named Employee


create table Employee
( emp_id number,
ename varchar2(100)
);

Step 2 : Insert values in employee table


insert into Employee values (1,‟Amit‟);
insert into Employee values (2,‟Pradnya‟);

Step 3 : Write the Code for Trigger

create or replace trigger T_Statement_Level


before update —Before updating the table named Employee trigger needs to be fired
on Employee
begin
DBMS_OUTPUT.PUT_LINE(„Trigger Fired Before Update‟);
end;

Step 4 : Testing of Trigger


update Employee
set ename=‟Rahul‟;

Output:

Trigger fired Before Update

2 rows updated

not satisfying condition,

update Employee
set ename=‟Rahul‟
where emp_id=5;

In above example emp_id=5 is not present in the Employee table. But trigger will be fired once and
output will be,

Trigger fired Before Update

0 rows updated

Result:
EX NO:4 b Triggers – Row Level

AIM:

PROCEDURE:
Syntax:

Step 1 : Lets create Employee table

create table Employee

( emp_id number,

ename varchar2(100)

);

insert into Employee values (1,‟Amit‟);

insert into Employee values (2,‟Pradnya‟);

Step 2 : Lets create a trigger using for each row statement.

create or replace trigger T1_Row_level_example

before update —Before updating the employee table

on Employee

for each row

begin

DBMS_OUTPUT.PUT_LINE(„Row Level trigger executed‟);

end;

Step 3 : Testing the trigger

To test the trigger we require to update the employee table,

Update Employee set Ename=‟Rahul‟;

The output

Row Level trigger executed

Row Level trigger executed

Updated two rows


Step 4 : Test trigger without processing rows

Lets fire the second statement,

Update Employee set Ename=‟Rahul‟ where 1=2;

The 1=2 is always false condition so this will not execute the trigger.

The output

0 rows updated
EX NO:5 Accessing a Relational Database using Python

AIM:

PROCEDURE:
Connecting with MySQL database "TESTDB"

#!/usr/bin/python

import MySQLdb

# Open database connection


db = MySQLdb.connect("localhost","testuser","test123","TESTDB" )

# prepare a cursor object using cursor() method


cursor = db.cursor()

# execute SQL query using execute() method.


cursor.execute("SELECT VERSION()")

# Fetch a single row using fetchone() method.


data = cursor.fetchone()
print "Database version : %s " % data

# disconnect from server


db.close()

Creating Database Table: Employee

#!/usr/bin/python

import MySQLdb

# Open database connection


db = MySQLdb.connect("localhost","testuser","test123","TESTDB" )

# prepare a cursor object using cursor() method


cursor = db.cursor()

# Drop table if it already exist using execute() method.


cursor.execute("DROP TABLE IF EXISTS EMPLOYEE")

# Create table as per requirement


sql = """CREATE TABLE EMPLOYEE (
FIRST_NAME CHAR(20) NOT NULL,
LAST_NAME CHAR(20),
AGE INT,
SEX CHAR(1),
INCOME FLOAT )"""

cursor.execute(sql)

# disconnect from server


db.close()

Inserting values:
#!/usr/bin/python

import MySQLdb

# Open database connection


db = MySQLdb.connect("localhost","testuser","test123","TESTDB" )

# prepare a cursor object using cursor() method


cursor = db.cursor()

# Prepare SQL query to INSERT a record into the database.


sql = """INSERT INTO EMPLOYEE(FIRST_NAME,
LAST_NAME, AGE, SEX, INCOME)
VALUES ('Mac', 'Mohan', 20, 'M', 2000)"""
try:
# Execute the SQL command
cursor.execute(sql)
# Commit your changes in the database
db.commit()
except:
# Rollback in case there is any error
db.rollback()

# disconnect from server


db.close()

Queries:
#!/usr/bin/python

import MySQLdb

# Open database connection


db = MySQLdb.connect("localhost","testuser","test123","TESTDB" )

# prepare a cursor object using cursor() method


cursor = db.cursor()

sql = "SELECT * FROM EMPLOYEE \


WHERE INCOME > '%d'" % (1000)
try:
# Execute the SQL command
cursor.execute(sql)
# Fetch all the rows in a list of lists.
results = cursor.fetchall()
for row in results:
fname = row[0]
lname = row[1]
age = row[2]
sex = row[3]
income = row[4]
# Now print fetched result
print "fname=%s,lname=%s,age=%d,sex=%s,income=%d" % \
(fname, lname, age, sex, income )
except:
print "Error: unable to fecth data"

# disconnect from server


db.close()

Output:
fname=Mac, lname=Mohan, age=20, sex=M, income=2000
EX NO: 6 Creating XML Documents, DTD, XML Schema

AIM:

PROCEDURE:
Syntax:
Simple document creation:

<?xml version = "1.0"?>


<contact-info>
<name>Ganesh</name>
<company>DMI College</company>
<phone>(91) 12345-64567</phone>
</contact-info>

Creating DTDs:
Syntax
<!DOCTYPE element DTD identifier
[
declaration1
declaration2
........
]>

Example
<?xml version = "1.0" encoding = "UTF-8" standalone = "yes" ?>
<!DOCTYPE address [
<!ELEMENT address (name,company,phone)>
<!ELEMENT name (#PCDATA)>
<!ELEMENT company (#PCDATA)>
<!ELEMENT phone (#PCDATA)>
]>

<address>
<name>Tanmay Patil</name>
<company>TutorialsPoint</company>
<phone>(011) 123-4567</phone>
</address>

XML Schema:

Example:
<?xml version = "1.0" encoding = "UTF-8"?>
<xs:schema xmlns:xs = "http://www.w3.org/2001/XMLSchema">
<xs:element name = "contact">
<xs:complexType>
<xs:sequence>
<xs:element name = "name" type = "xs:string" />
<xs:element name = "company" type = "xs:string" />
<xs:element name = "phone" type = "xs:int" />
</xs:sequence>
</xs:complexType>
</xs:element>
</xs:schema>
Global type:
Example:

<xs:element name = "Address1">


<xs:complexType>
<xs:sequence>
<xs:element name = "address" type = "AddressType" />
<xs:element name = "phone1" type = "xs:int" />
</xs:sequence>
</xs:complexType>
</xs:element>

<xs:element name = "Address2">


<xs:complexType>
<xs:sequence>
<xs:element name = "address" type = "AddressType" />
<xs:element name = "phone2" type = "xs:int" />
</xs:sequence>
</xs:complexType>
</xs:element>
EX NO: 7 XML Query

AIM:

PROCEDURE:
Query:
<?xml version="1.0" encoding="UTF-8"?>

<bookstore>

<book category="COOKING">
<title lang="en">Everyday Italian</title>
<author>Giada De Laurentiis</author>
<year>2005</year>
<price>30.00</price>
</book>

<book category="CHILDREN">
<title lang="en">Harry Potter</title>
<author>J K. Rowling</author>
<year>2005</year>
<price>29.99</price>
</book>

<book category="WEB">
<title lang="en">XQuery Kick Start</title>
<author>James McGovern</author>
<author>Per Bothner</author>
<author>Kurt Cagle</author>
<author>James Linn</author>
<author>Vaidyanathan Nagarajan</author>
<year>2003</year>
<price>49.99</price>
</book>

<book category="WEB">
<title lang="en">Learning XML</title>
<author>Erik T. Ray</author>
<year>2003</year>
<price>39.95</price>
</book>

</bookstore>
OUTPUT

View the "books.xml" file in your browser.

<bookstore>
<book category="cooking">
<title lang="en">Everyday Italian</title>
<author>Giada De Laurentiis</author>
<year>2005</year>
<price>30.00</price>
</book>
<book category="children">
<title lang="en">Harry Potter</title>
<author>J K. Rowling</author>
<year>2005</year>
<price>29.99</price>
</book>
<book category="web">
<title lang="en">XQuery Kick Start</title>
<author>James McGovern</author>
<author>Per Bothner</author>
<author>Kurt Cagle</author>
<author>James Linn</author>
<author>Vaidyanathan Nagarajan</author>
<year>2003</year>
<price>49.99</price>
</book>
<book category="web" cover="paperback">
<title lang="en">Learning XML</title>
<author>Erik T. Ray</author>
<year>2003</year>
<price>39.95</price>
</book>
</bookstore>
EX NO: 8 Creating DB using MangoDB

AIM:

PROCEDURE:
Syntax:
use DATABASE_NAME;

>use mydb
switched to db mydb

To check your currently selected database, use the command db


>db
mydb

If you want to check your databases list, use the command show dbs.
>show dbs
local 0.78125GB
test 0.23012GB

Inserting data:
>db.movie.insert({"xxx":"Newone"})

>show dbs
local 0.78125GB
mydb 0.23012GB
test 0.23012GB
EX NO: 9 Code to access MangoDB

AIM:

PROCEDURE:
Code using Java:

code to connect to MongoDB

Get the database and collection:

MongoDatabase db = mongoClient.getDatabase("sample_guides");
MongoCollection<Document> coll = db.getCollection("planets");

Retrieve specific documents in the planets collection:

Bson filter = eq("hasRings", true);


MongoCursor<Document> cursor = coll.find(filter).iterator();

Iterate Over the Result:

try {
while (cursor.hasNext()) {
System.out.println(cursor.next().toJson());
}
} finally {
cursor.close();
}

Complete Code:
import com.mongodb.client.*;
import com.mongodb.client.model.Filters.*;
import org.bson.Document;
import org.bson.conversions.Bson;
public class CrudRead {
public static void main(String[] args) {
String uri = "mongodb+srv://<user>:<password>@<cluster-
url>?retryWrites=true&writeConcern=majority";
try (MongoClient mongoClient = MongoClients.create(uri)) {
// database and collection code goes here
MongoDatabase db = mongoClient.getDatabase("sample_guides");
MongoCollection<Document> coll = db.getCollection("planets");
// find code goes here
Bson filter = eq("hasRings", true);
MongoCursor<Document> cursor = coll.find(filter).iterator();
// iterate code goes here
try {
while (cursor.hasNext()) {
System.out.println(cursor.next().toJson());
}
} finally {
cursor.close();
}
}
}
}
Output:

{... 'name': 'Uranus', 'hasRings': True, ...}


{... 'name': 'Neptune', 'hasRings': True, ... }
{... 'name': 'Jupiter', 'hasRings': True, ... }
{... 'name': 'Saturn', 'hasRings': True, ... }
EX NO: 10 Code to access Neo4j

AIM:

PROCEDURE:
Code:

To Show DB:

neo4j@system> SHOW DATABASE neo4j;

Output:
+
+
| name | type | aliases | access | address | role | writer | requestedStatus | currentStatus
| statusMessage | default | home | constituents |
+
+
| "neo4j" | "standard" | [] | "read-write" | "localhost:7687" | "primary" | TRUE | "online" |
"online" | "" | TRUE | TRUE | [] |
+
+

1 row available after 100 ms, consumed after another 6 ms

CREATE DATABASE:
neo4j@system> CREATE DATABASE sales;

output:
0 rows available after 108 ms, consumed after another 0 ms

USE DATABASE:
use <database-name>

DROP DATABASE

neo4j@system> DROP DATABASE sales;


0 rows available after 82 ms, consumed after another 1 ms

Configuration of a remote DBMS:

CREATE USER alice SET PASSWORD 'secret'


CREATE ROLE remote
GRANT ACCESS ON DATABASE neo4j TO remote
GRANT MATCH {*} ON GRAPH neo4j TO remote
GRANT ROLE remote TO alice

Example:
# accept non-local connections
server.default_listen_address=0.0.0.0

# configure ssl for bolt


dbms.ssl.policy.bolt.enabled=true
dbms.ssl.policy.bolt.base_directory=certificates/bolt
dbms.ssl.policy.bolt.private_key=private.key
dbms.ssl.policy.bolt.public_certificate=public.crt
dbms.ssl.policy.bolt.client_auth=NONE

# enforcing ssl connection


server.bolt.tls_level=REQUIRED
EX NO: 11 Implementing Access Control in Relational Databases

AIM:

PROCEDURE:
Queries:

The GRANT Statement:

Syntax:

GRANT privileges
[ON relation]
TO users
[WITH GRANT OPTION]

Example:
To one:

GRANT UPDATE ON ORDER_BACKLOG TO JONES WITH GRANT OPTION

To public-

GRANT INSERT ON ORDER_BACKLOG TO PUBLIC WITH GRANT OPTION

Grant authority to public allocations-

GRANT SELECT ON TABLE Q.STAFF TO PUBLIC


GRANT SELECT ON TABLE Q.STAFF TO PUBLIC AT ALL LOCATIONS

The REVOKE Statement:

REVOKE [IF EXISTS]


priv_type [(column_list)]
[, priv_type [(column_list)]] ...
ON [object_type] priv_level
FROM user_or_role [, user_or_role] ...
[IGNORE UNKNOWN USER]

REVOKE [IF EXISTS] ALL [PRIVILEGES], GRANT OPTION


FROM user_or_role [, user_or_role] ...
[IGNORE UNKNOWN USER]

REVOKE [IF EXISTS] PROXY ON user_or_role


FROM user_or_role [, user_or_role] ...
[IGNORE UNKNOWN USER]

REVOKE [IF EXISTS] role [, role ] ...


FROM user_or_role [, user_or_role ] ...
[IGNORE UNKNOWN USER]

user_or_role: {
user (see Section 6.2.4, ―Specifying Account Names‖)
| role (see Section 6.2.5, ―Specifying Role Names‖
}
Output:
Revoked.

You might also like