Professional Documents
Culture Documents
Full Manual Databse Practices
Full Manual Databse Practices
Full Manual Databse Practices
This is to Certify that the bona fide record of practical work done in
the Computer Laboratory of Surya School of Engineering & Technology
during the academic year 2023-2024
Staff
Ex.No. Date Name of the Experiment Page no. Signature
Ex.no. 1a
Data Definition Language
Aim
To create, alter and drop table by using SQL and view them,
enforce the primary key, foreign key, check, unique and not null
constraints
SQL Commands
This constraint helps to set a limit value placed for a field. When
we define a check constraint on a single column, it allows only the restricted
values on that field.
A view can contain all rows of a table or select rows from a table.
A view can be created from one or many tables which depends on the written
SQL query to create a view.
RESULT
Thus the SQL commands for create, alter, drop and Constraints are
implemented successfully
Ex.no. 1b
Data Manipulation Language
Aim:
To insert, Delete, Update the details in a table and
implement aggregate functions, set operations, Nested queries using SQL
commands
SQL Commands
AGGREGATE FUNCTIONS
SET OPERATIONS
1. Union
The SQL Union operation is used to combine the result of two or more SQL
SELECT queries.
SELECT * FROM First UNION SELECT * FROM Second;
2. Union All
Union All operation is equal to the Union operation. It returns the set without
removing duplication and sorting the data.
3. Intersect
It is used to combine two SELECT statements. The Intersect operation returns
the common rows from both the SELECT statements.
Nested Queries
1. IN
2. NOT IN
Select all Developers who earn more than all the Managers
4. ANY
SELECT * FROM employees WHERE role = 'Developer' AND salary > ANY
(SELECT salary FROM employees WHERE role = 'Manager');
RESULT
Thus the SQL commands for insert, delete, Update, aggregate operations,
Set operations and Nested Queries are implemented successfully
Ex.no. 1c
Transaction Control Language
Aim:
To Commit, Roll back and save point in a table using SQL
commands
SQL Commands
(i) COMMIT command
The COMMIT command is used to permanently save any transaction to
the database. When any DML commands like INSERT, UPDATE, DELETE
commands are used, the changes made by these commands are not permanent.
COMMIT;
(ii) ROLLBACK command
The ROLLBACK command restores the database to the last commited state. It
is used with SAVEPOINT command to jump to a particular savepoint location.
ROLL BACK TO save point name;
(iii) SAVEPOINT command
The SAVEPOINT command is used to temporarily save a transaction so that
you can rollback to the point whenever required. The different states of our table can
be saved at anytime using different names and the rollback to that state can be done
using the ROLLBACK command.
SAVEPOINT savepoint_name;
Example showing COMMIT, SAVEPOINT and ROLLBACK in the student table
having the following data:
INSERT INTO Student VALUES (107, 'Beena', 'F', 20 , 'Cochin'); COMMIT;
RESULT
Thus the SQL commands for commit, Rollback and save point in a table
was implemented successfully
Ex.no. 2a
Distributed Database Design
Aim
To implement and design the distributed data base
Distributed database
A distributed database is basically a database that is not limited to one system, it is
spread over different sites, i.e, on multiple computers or over a network of
computers. A distributed database system is located on various sites that don‟t share
physical components.
1. Data Replication
Data replication is the process of storing separate copies of the database at two
or more sites
Some commonly used replication techniques are −
Snapshot replication
Near-real-time replication
Pull replication
Fragmentation
Fragmentation is the task of dividing a table into a set of smaller tables. The
subsets of the table are called fragments. Fragmentation can be of three types:
horizontal, vertical, and hybrid
Vertical Fragmentation
In vertical fragmentation, the fields or columns of a table are grouped into
fragments.
For example, let us consider that a University database keeps records of all
registered students in a Student table having the following schema.
STUDENT
Now, the fees details are maintained in the accounts section. In this case, the
designer will fragment the database as follows −
Hybrid Fragmentation
In hybrid fragmentation, a combination of horizontal and vertical
fragmentation techniques are used.
RESULT
Aim
To implement Row level and statement level triggers using SQL commands
Statement-level triggers
CREATE TRIGGER
We‟ll use the table customers from the sample database for the demonstration:
A row-level trigger
row-level trigger fires once for each row that is affected by a triggering event.
For example, if deletion is defined as a triggering event for a particular table, and a
single DELETE statement deletes five rows from that table, the trigger fires five
times, once for each row.
We‟ll use the customers table from the sample database for demonstration:
CREATE OR REPLACE TRIGGER customers_update_credit_trg
ON customers
BEGIN
' cannot increase to more than double, the current credit ' || :OLD.credit_limit);
END IF;
END;
Because row-level triggers execute within the context of a single row, you can
access the old and new column values
-- carry an action
END;
RESULT
Thus the Row level and statement level triggers are implemented
successfully
Ex.no. 2c
Accessing a relational data base using PHP, Python and R
Aim
To accessing a relational data base using PHP, Python and R
//HTML Code
<html>
<head>
<title>Login Form</title>
</head>
<body>
<label>Username: </label>
<label>Password: </label>
</form>
</body>
</html>
<?php
$utext = $_REQUEST["user"];
$ptext = $_REQUEST["pass"];
$flag = false;
$hostname = "localhost";
$username = "root";
$password = "123456";
while($x = mysqli_fetch_array($result))
$flag = true;
if($flag)
else
?>
Database Connection
Following is the example of connecting with MySQL database "TESTDB"
#!/usr/bin/python
import MySQLdb
cursor.execute(sql)
INSERT Operation
It is required when you want to create your records into a database table.
executes SQL INSERT statement to create a record into EMPLOYEE table
#!/usr/bin/python
import MySQLdb
READ Operation
READ Operation on any database means to fetch some useful information
from the database.
Once our database connection is established, you are ready to make a query
into this database. You can use either fetchone() method to fetch single record
or fetchall() method to fetech multiple values from a database table.
The following procedure queries all the records from EMPLOYEE table
having salary more than 1000 −
#!/usr/bin/python
import MySQLdb
Update Operation
UPDATE Operation on any database means to update one or more records,
which are already available in the database.
The following procedure updates all the records having SEX as 'M'. Here, we
increase AGE of all the males by one year.
Example
#!/usr/bin/python
import MySQLdb
DELETE Operation
DELETE operation is required when you want to delete some records from
your database. Following is the procedure to delete all the records from EMPLOYEE
where AGE is more than 20 −
Example
#!/usr/bin/python
import MySQLdb
Connecting R to MySql
Once the package is installed we create a connection object in R to connect to
the database. It takes the username, password, database name and host name as input.
# Create a connection Object to MySQL database.
# We will connect to the sampel database named "sakila" that comes with MySql
installation.
mysqlconnection = dbConnect(MySQL(), user = 'root', password = '', dbname = 'sakila',
host = 'localhost')
# Store the result in a R data frame object. n = 5 is used to fetch first 5 rows.
data.frame = fetch(result, n = 5)
print(data.fame)
When we execute the above code, it produces the following result −
RESULT
Thus accessing a relational data base using PHP, Python and R using mySql
was done successfully
Ex.no. 3a
Creating XML Documents, Document type Definition and XML
Schema
Aim
XML - DTD
The XML Document Type Declaration, commonly known as DTD, is a way to
describe XML language precisely. DTDs check vocabulary and validity of the
structure of XML documents against grammatical rules of appropriate XML
language.
Internal DTD
A DTD is referred to as an internal DTD if elements are declared within the
XML files. To refer it as internal DTD, standalone attribute in XML declaration must
be set to yes. This means, the declaration works independent of an external source.
<?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>
Start Declaration − Begin the XML declaration with the following statement.
<?xml version = "1.0" encoding = "UTF-8" standalone = "yes" ?>
External DTD
In external DTD elements are declared outside the XML file. They are
accessed by specifying the system attributes which may be either the legal .dtd file or
a valid URL. To refer it as external DTD, standalone attribute in the XML declaration
must be set as no. This means, declaration includes information from the external
source.
Definition Types
You can define XML schema elements in the following ways
Simple Type
Simple type element is used only in the context of the text. Some of the
predefined simple types are: xs:integer, xs:boolean, xs:string, xs:date. For example −
<xs:element name = "phone_number" type = "xs:int" />
Complex Type
A complex type is a container for other element definitions. This allows you to
specify which child elements an element can contain and to provide some structure
within your XML documents. For example −
<xs:element name = "Address">
<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>
Global Types
With the global type, you can define a single type in your document, which
can be used by all other references. For example, suppose you want to generalize
the person and company for different addresses of the company. In such case, you can
define a general type as follows −
<xs:element name = "AddressType">
<xs:complexType>
<xs:sequence>
<xs:element name = "name" type = "xs:string" />
<xs:element name = "company" type = "xs:string" />
</xs:sequence>
</xs:complexType>
</xs:element>
RESULT
Ex.no. 3b
XML Queries
Thus Creating XML Documents, Document type Definition and XML Schema
using mySql was done successfully
Aim
XQuery
XQuery is About Querying XML. XQuery is a language for finding and
extracting elements and attributes from XML documents.
XQuery Example
for $x in doc("books.xml")/bookstore/book
where $x/price>30
order by $x/title
return $x/title
XQuery 1.0 and XPath 2.0 share the same data model and support the same
functions and operators. If you have already studied XPath you will have no problems
with understanding XQuery.
"books.xml":
<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>
doc("books.xml")
path expression is used to select all the title elements in the "books.xml" file:
doc("books.xml")/bookstore/book/title
we want to add some HTML elements to the result. We will put the result in
an HTML list - together with some text:
<html>
<body>
<h1>Bookstore</h1>
<ul>
{
for $x in doc("books.xml")/bookstore/book
order by $x/title
return <li>{data($x/title)}. Category: {data($x/@category)}</li>
}
</ul>
</body>
</html>
<html>
<body>
<h1>Bookstore</h1>
<ul>
<li>Everyday Italian. Category: COOKING</li>
<li>Harry Potter. Category: CHILDREN</li>
<li>Learning XML. Category: WEB</li>
<li>XQuery Kick Start. Category: WEB</li>
</ul>
</body>
</html>
we are selecting and filtering elements with either a Path expression or with a
FLWOR expression.
The for clause binds a variable to each item returned by the in expression. The for
clause results in iteration. There can be multiple for clauses in the same FLWOR
expression.
To loop a specific number of times in a for clause, you may use the to keyword:
for $x in (1 to 5)
return <test>{$x}</test>
Output:
<test>1</test>
<test>2</test>
<test>3</test>
<test>4</test>
<test>5</test>
for $x at $i in doc("books.xml")/bookstore/book/title
return <book>{$i}. {data($x)}</book>
Output:
It is also allowed with more than one in expression in the for clause. Use comma to
separate each in expression:
Output:
<test>x=10 and y=100</test>
<test>x=10 and y=200</test>
<test>x=20 and y=100</test>
<test>x=20 and y=200</test>
RESULT
Thus creating XML Queries like adding selecting text to a table using SQL
commands was done successfully
Ex.no. 4a
Creating Database using MongoDB
Aim
To create a database using MongoDB
RESULT
Ex.no. 4b
Creating Database using DynamoDB
Step 2: In the navigation pane on the left side of the console, choose Dashboard.
Step 3: On the right side of the console, choose Create Table.
RESULT
Thus creating Databases using DynamoDB was done through step by step
process successfully
Ex.no. 5
Implementing Access control in Relational Databases
Aim
To Implement Access control in Relational Databases
RELATIONAL DATABASES
A relational database stores data in relations which are expected to satisfy
some simple mathematical properties. Roughly speaking, a relation can be thought of
as a table, and is often shown as such. The columns of the table are called attributes
and the rows are called tuples
The relation instance gives us the tuples of the relation at a given instant. For
example, consider the following relation scheme for the EMPLOYEE relation
Let the domain of the NAME, DEPT, RANK, OFFICE, and SUPERVISOR
attributes be character strings, and the domain of the SALARY attribute be integers.
INSERT Statements
The EMPLOYEE table is initially empty.
This query applied to instance of EMPLOYEE given above returns the data shown below
The view COMPUTER SCI DEPT will be automatically modied to include Turing, as
shown below
RESULT
Thus implementing access control in relational database was done successfully