0% found this document useful (0 votes)
17 views44 pages

Dbms Lab

The document provides a comprehensive guide on creating and managing databases using SQL commands, including creating tables, adding constraints, and performing data manipulation operations like insert, update, and delete. It also covers querying databases with various conditions, implementing aggregate functions, and exploring subqueries and join operations. Additionally, it explains different types of joins such as natural, equi, and outer joins, along with practical examples for better understanding.

Uploaded by

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

Dbms Lab

The document provides a comprehensive guide on creating and managing databases using SQL commands, including creating tables, adding constraints, and performing data manipulation operations like insert, update, and delete. It also covers querying databases with various conditions, implementing aggregate functions, and exploring subqueries and join operations. Additionally, it explains different types of joins such as natural, equi, and outer joins, along with practical examples for better understanding.

Uploaded by

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

DBMS LAB MANUAL –

FINAL
CREATE A DATABASE TABLE, ADD CONSTRAINTS (PRIMARY KEY,
UNIQUE, CHECK, NOT NULL), INSERT ROWS, UPDATE AND DELETE
ROWS USING SQL DDL AND DML COMMANDS
Queries to Create Database:

1. mysql> create database red;

Query OK, 1 row affected (0.10 sec)

2. mysql> use red;

Database changed

Queries to Create Database Table:

3. mysql> CREATE TABLE Employee1( Id INT, NAME VARCHAR(30) NOT NULL, Email

VARCHAR(100),Age INT CHECK (Age>= 20), PRIMARY KEY (Id),UNIQUE(Email));

Query OK, 0 rows affected (0.14 sec)

Queries to Insert Value in Table:

4. mysql> INSERT INTO Employee1 values('1','AAA','[email protected]','25');

Query OK, 1 row affected (0.13 sec)

5. mysql> INSERT INTO Employee1 values('2','BBB','[email protected]','23');

Query OK, 1 row affected (0.07 sec)

“CHECK” CONSTRAINT VERIFICATION:

6. mysql> INSERT INTO Employee1

values('3','CCC','[email protected]','2'); ERROR 3819 (HY000): Check

constraint 'employee1_chk_1' is violated. “PRIMARY KEY” CONSTRAINT

VERIFICATION:

7. mysql> INSERT INTO Employee1 values('2','CCC','[email protected]','24');


8. INSERT INTO Employee1 values('3','CCC','[email protected]','24');

Query OK, 1 row affected (0.05 sec)

9. mysql> INSERT INTO Employee1 values('4','DDD','[email protected]','27');

Query OK, 1 row affected (0.07 sec)

“UNIQUE” CONSTRAINT VERIFICATION:

10. mysql> INSERT INTO Employee1

values('5','DDD','[email protected]','28'); ERROR 1062 (23000): Duplicate entry

'[email protected]' for key 'employee1.Email' ‘NULL’ CONSTRAINT

VERIFICATION:

11. mysql> INSERT INTO Employee1 values('5',NULL,'[email protected]','28');

ERROR 1048 (23000): Column 'NAME' cannot be null

12. mysql> INSERT INTO Employee1 values('5','EEE','[email protected]','28');

Query OK, 1 row affected (0.03 sec)

SELECT

13. mysql> select * from Employee1;

14. mysql> select NAME from Employee1;


DELETE

14.mysql> delete from Employee1 Where Age='28';

Query OK, 1 row affected (0.01 sec)

DROP COLUMNS:

15. .mysql> ALTER TABLE Employee1 DROP COLUMN Age;

Query OK, 0 rows affected (0.27 sec)

Records: 0 Duplicates: 0 Warnings: 0

6.mysql> select * from Employee1;

UPDATE

17.mysql> UPDATE Employee1 SET Email='[email protected]' WHERE Id=4;

Query OK, 1 row affected (0.06 sec)

Rows matched: 1 Changed: 1 Warnings:

18. mysql> select * from Employee1;


Ex No :2 CREATE A SET OF TABLES, ADD FOREIGN KEY CONSTRAINTS
AND INCORPORATE REFERENTIAL INTEGRITY

CHECK FOREIGN KEY CONSTRAINT


1. mysql> create database sec;

Query OK, 1 row affected (0.07 sec)

2. mysql> use sec;

Database changed

3. mysql> CREATE TABLE customer ( ID INT NOT NULL , Name varchar(50) NOT NULL,

City varchar(50) NOT NULL, PRIMARY KEY (ID) );

Query OK, 0 rows affected (0.26 sec)

4. mysql> CREATE TABLE contact (ID INT,Customer_Id INT, Customer_Info varchar(50)

NOT NULL, Type varchar(50) NOT NULL, INDEX par_ind (Customer_Id), CONSTRAINT

fk_customer FOREIGN KEY (Customer_Id) REFERENCES customer(ID));

Query OK, 0 rows affected (0.24 sec)

5. mysql> describe customer;

6. mysql> describe contact;


7. mysql> insert into customer values(101,'Shiva','Trichy');

Query OK, 1 row affected (0.09 sec)

8. mysql> insert into customer values(102,'Vishnu','Chennai');

Query OK, 1 row affected (0.02 sec)

9. mysql> insert into customer values(103,'Ram','Madurai');

Query OK, 1 row affected (0.02 sec)

10. mysql> select * from customer;

11. mysql> insert into contact values(1,'Male','Regular');

ERROR 1136 (21S01): Column count doesn't match value count at row 1

12. mysql> insert into contact values(1,101,'Male','Regular');

Query OK, 1 row affected (0.04 sec)

13. mysql> insert into contact values(2,102,'Female','Passerby');

Query OK, 1 row affected (0.05 sec)

14. mysql> insert into contact values(,103,'Male','Regular');

Query OK, 1 row affected (0.05 sec)

15.mysql> select * from contact;


16.mysql> insert into contact values(101,1,'Male','Passerby');

ERROR 1452 (23000): Cannot add or update a child row: a foreign key constraint fails

(`sec`.`contact`, CONSTRAINT `fk_customer` FOREIGN KEY (`Customer_Id`) REFERENCES

`customer` (`ID`))

17. mysql> insert into contact values(1,101,'Male','Passerby');

Query OK, 1 row affected (0.02 sec)

18. mysql> insert into contact values(2,102,'Male','Regular');

Query OK, 1 row affected (0.03 sec)

19.mysql> insert into contact values(3,103,'Male','Regular');

Query OK, 1 row affected (0.03 sec

20.mysql> select * from customer;

21.mysql> select * from contact;


Ex No : 3 QUERY THE DATABASE TABLES USING DIFFERENT ‘WHERE’
CLAUSE CONDITIONS AND ALSO IMPLEMENTAGGREGATE FUNCTIONS.

Aim :
To Query the database tables using different ‘where’ clause conditions and also implement
aggregate functions.

Procedure:

Where clause:
The SQL WHERE clause is used to specify a condition while fetching the data from a single
table or by joining with multiple tables. If the given condition is satisfied, then only it returns a
specific value from the table. You should use the WHERE clause to filter the records and
fetching only the necessary records.
The WHERE clause is not only used in the SELECT statement, but it is also used in the UPDATE,
DELETE statement, etc., which we would examine in the subsequent chapters.
Syntax
The basic syntax of the SELECT statement with the WHERE clause is as shown below.
SELECT column1, column2, columnN
FROM table_name
WHERE [condition]
You can specify a condition using the comparison or logical operators like >, <, =, LIKE,
NOT, etc. The following examples would make this concept clear.

Queries to use Aggregate Function

1. create database third1;

Query OK, 1 row affected (0.21 sec)

2. use third1;

Database changed

3. mysql> create table student1(studentName varchar(25), StudentId int,Age int, Address

Varchar(25), Department varchar(25), Fees int);

Query OK, 0 rows affected (0.12 sec)

4. mysql> describe student1;


5. mysql> insert into student1 values("Priya", 101, 29,"Mayiladuthurai","cse",20000);

Query OK, 1 row affected (0.04 sec)

6. mysql> insert into student1 values("Subha", 102, 28,"Chennai","ece",25000);

Query OK, 1 row affected (0.07 sec)

7. mysql> insert into student1 values("Jai", 103, 30,"Tirunelveli","eee",18000);

Query OK, 1 row affected (0.02 sec)


8. mysql> insert into student1 values("Raja", 104, 29, "Thanjavur", "Mech", 39000);

Query OK, 1 row affected (0.03 sec)

9. mysql> insert into student1 values("Suja", 105, 12, "Thanjavur", "Eighth", 15000);

Query OK, 1 row affected (0.02 sec)

10. mysql>select * from student1;

AGGREGATE FUNCTIONS

In addition to simply retrieving data, we often want to perform some computation or


summarization. SQL allows the use of arithmetic expressions. We now consider a powerful
class of constructs for computing aggregate values such as MIN and SUM.
1. Count: COUNT following by a column name returns the count of tuple in that column. If
DISTINCT keyword is used then it will return only the count of unique tuple in the column.
Otherwise, it will return count of all the tuples (including duplicates) count (*) indicates all
the tuples of the column.
Syntax: COUNT (Column name)
Example: SELECT COUNT (Sal) FROM emp;

2. SUM: SUM followed by a column name returns the sum of all the values in that column.
Syntax: SUM (Column name)
Example: SELECT SUM (Sal) From emp;
3. AVG: AVG followed by a column name returns the average value of that column values.
Syntax: AVG (n1, n2...)
Example: Select AVG (10, 15, 30) FROM DUAL;
4. MAX: MAX followed by a column name returns the maximum value of that column.
Syntax: MAX (Column name)
MIN: MIN followed by column name returns the minimum value of that column. Syntax: MIN
(Column name)
11. mysql> select avg(fees) result from student1;

12. mysql> select min(fees) result from student1;

13. mysql> select max(fees) result from student1;

14. mysql> select count(fees) result from student1;


15. mysql> SELECT SUM(fees) FROM student1 WHERE Address="Thanjavur";

16. mysql> select distinct Address from student1;

17. mysql> select count(*) from student1 where age<=20;

18. mysql> select count(*) from student1 where fees>20000;

Result:
Thus the database tables are queried using different ‘where’ clause conditions and also
implement aggregate functions.
Ex No :4 QUERY THE DATABASE TABLES AND EXPLORE SUB QUERIES
AND SIMPLE JOIN OPERATIONS

A subquery is a query embedded within another SQL query. It is often used to retrieve data that will be used in
the main query. Subqueries can be used in SELECT, INSERT, UPDATE, or DELETE operations.

Definition:

 A subquery is typically a SELECT statement placed within another SELECT, INSERT, UPDATE, or
DELETE statement.
 Subqueries are enclosed in parentheses ().

Syntax for a Simple Subquery:

A simple subquery returns a single value or a single column of values to be used by the outer query.

SELECT column_name
FROM table_name
WHERE column_name = (SELECT column_name FROM another_table WHERE condition);

Join Operation
A JOIN operation combines rows from two or more tables based on a related column between them. There
are several types of joins: INNER JOIN, LEFT JOIN, RIGHT JOIN, and FULL OUTER JOIN.
Definition:

 A JOIN allows you to retrieve data from multiple tables in a single query based on the relationships between them.

Syntax for Join Operation:

1. SELECT column_name(s)
FROM table1
JOIN table2
ON table1.column_name = table2.column_name;
//LEFT JOIN
2. SELECT employees.employee_id, employees.employee_name, departments.department_name
FROM employees
LEFT JOIN departments
ON employees.department_id = departments.department_id;
// RIGHT JOIN
3. SELECT column_name(s)
FROM table1
RIGHT JOIN table2
ON table1.column_name = table2.column_name;
// Full join
4. SELECT column_name(s)
FROM table1
FULL OUTER JOIN table2
ON table1.column_name = table2.column_name;
1. mysql> create database four;

Query OK, 1 row affected (0.75 sec)

2. mysql> use four;

Database changed

3. mysql> create table product (product_id int primary key, product_name

varchar(25),product_price int);

Query OK, 0 rows affected (0.06 sec)

4. mysql> desc product;

5. mysql> insert into product values(101,'mobile',20000);

Query OK, 1 row affected (0.09 sec)

6. mysql> insert into product values(102,'laptop',50000);

Query OK, 1 row affected (0.02 sec)

7. mysql> insert into product values(103,'Tablet',50000);

Query OK, 1 row affected (0.02 sec)

8. mysql> insert into product values(104,'TV',30000);

Query OK, 1 row affected (0.02 sec)

9. mysql> insert into product values(105,'Speaker',15000);

Query OK, 1 row affected (0.04 sec)


10. mysql> select * from product;

11. mysql> create table sale(sales_id int not null primary key auto_increment,product_id

int,sales_year int,sales_amount int);

Query OK, 0 rows affected (0.10 sec)

12. mysql> desc sale;

13. mysql> insert into sale values(1,101,2022,15000);

Query OK, 1 row affected (0.03 sec)

14. mysql> insert into sale values(2,102,2021,18000);

Query OK, 1 row affected (0.03 sec)

15. mysql> insert into sale values(3,103,2023,16800);

Query OK, 1 row affected (0.03 sec)

16. mysql> insert into sale values(4,104,2010,19000);

Query OK, 1 row affected (0.03 sec)

17. mysql> insert into sale values(5,105,2022,17400);

Query OK, 1 row affected (0.03 sec)


18. mysql> insert into sale values(6,106,2020,15300);

Query OK, 1 row affected (0.05 sec)

19. mysql> select * from sale;

20. mysql> SELECT * FROM product WHERE product_id=(SELECT product_id FROM

sale WHERE sales_amount>=15000 AND product_id=product.product_id);

21. mysql> SELECT * FROM product WHERE product_id=(SELECT product_id FROM

sale WHERE sales_amount>=19000 AND product_id=product.product_id);

22. mysql> SELECT * FROM product WHERE product_id IN (SELECT product_id

FROM sale);
23. mysql> SELECT * FROM product WHERE product_id NOT IN (SELECT product_id

FROM sale);

Empty set (0.00 sec)

JOIN QUERY

24. mysql> SELECT p.product_id,p.product_name,p.product_price FROM product p JOIN

sale ON p.product_id=sale.product_id WHERE sales_amount>=3000;

25. mysql> SELECT DISTINCT p.product_id,p.product_name,p.product_price FROM

product p JOIN sale ON p.product_id=sale.product_id;

26. mysql> SELECT DISTINCT p.product_id,p.product_name,p.product_price FROM

product p JOIN sale s ON p.product_id=s.product_id WHERE s.product_id IS NULL;

Empty set (0.01 sec)

Result:
Thus the database tables is queried with sub queries and simple join operations.
Ex No :5 QUERY THE DATABASE TABLES AND EXPLORE NATURAL, EQUI
AND OUTER JOINS.

CREATING TABLES FOR DOING JOIN AND NESTED QUERY OPERATIONS


Definition:
1. NATURAL JOIN

 A NATURAL JOIN automatically joins tables based on all columns that have the same name and data
type in both tables. It is a type of INNER JOIN where the join condition is based on the common columns,
and there’s no need to explicitly define the ON condition.
 Important: A NATURAL JOIN can result in fewer columns in the result set because it automatically
removes duplicate columns (those that are used for the join).
 Syntax:

SELECT column_name(s)
FROM table1
NATURAL JOIN table2;
2. EQUI JOIN
Definition:

 An EQUI JOIN is a type of JOIN where the join condition is based on equality (=) between columns of
the two tables. It is not a specific keyword in SQL, but rather a type of INNER JOIN that uses the equality
operator for the join condition.
 Note: EQUI JOIN is usually represented in SQL as an INNER JOIN or sometimes LEFT JOIN,
depending on the context, with a condition that uses = to match values.
 Syntax :

SELECT column_name(s)
FROM table1
JOIN table2
ON table1.column_name = table2.column_name;

1. mysql> create database five;

Query OK, 1 row affected (0.23 sec)

2. mysql> use five;

Database changed

3. mysql> create table table1(ID int, Name Varchar(30), Age int);

Query OK, 0 rows affected (0.39 sec)

4. mysql> create table table1(ID int, Name Varchar(30), Age int);

ERROR 1050 (42S01): Table 'table1' already exists

5. mysql> insert into table1 values(1,"Alice",23);


Query OK, 1 row affected (0.08 sec)

6. mysql> insert into table1 values(1,"Bob",28);

Query OK, 1 row affected (0.18 sec)

7. mysql> select * from table1;

8. mysql> insert into table1 values(1,"Charlie",28);

Query OK, 1 row affected (0.03 sec)


9. mysql> insert into table1 values(2,"Bob",28);

Query OK, 1 row affected (0.02 sec)

10. mysql> insert into table1 values(3,"Charlie",30);

Query OK, 1 row affected (0.02 sec)

11. mysql> create table table2(ID int, Address varchar(30),Salary int);

Query OK, 0 rows affected (0.07 sec)

12. mysql> insert into table2 values(2, "NewYork",50000);

Query OK, 1 row affected (0.06 sec)

13. mysql> insert into table2 values(3, "California",75000);

Query OK, 1 row affected (0.03 sec)

14. mysql> insert into table2 values(4, "Texas",59000);

Query OK, 1 row affected (0.03 sec)

15. mysql> select * from table2;


16. mysql> SELECT Table1.Name, Table2.Address FROM Table1 INNER JOIN Table2 ON

Table1.ID = Table2.ID;

17. mysql> SELECT Table1.ID, Table1.Name, Table1.Age, Table2.Address, Table2.Salary

FROM Table1 NATURAL JOIN Table2;

18. mysql> SELECT Table1.Name, Table2.Address FROM Table1 LEFT JOIN Table2 ON

Table1.ID = Table2.ID;

19. mysql> SELECT Table1.Name, Table2.Address FROM Table1 RIGHT JOIN Table2 ON
Table1.ID = Table2.ID;

Result:
Thus the database tables queried and explore natural, equi and outer joins.
FUNCTIONS AND PROCEDURES IN MYSQL
1. mysql> create database six;

Query OK, 0 rows affected (0.60 sec)

2. mysql> use six;

Database changed

3. mysql> delimiter //

4. mysql> CREATE PROCEDURE citycount (IN country CHAR(3), OUT cities INT)

-> BEGIN

-> SELECT COUNT(*) INTO cities FROM world.city

-> WHERE CountryCode = country;

-> END//

Query OK, 0 rows affected (0.60 sec)

5. mysql> delimiter ;

6. mysql> CALL citycount('JPN', @cities);

Query OK, 1 row affected (0.35 sec)

7. mysql> SELECT @cities;

8. mysql> CALL citycount('FRA', @cities);

Query OK, 1 row affected (0.00 sec)


9. mysql> SELECT @cities;

10. mysql> CREATE FUNCTION hello (s CHAR(20))

-> RETURNS CHAR(50) DETERMINISTIC

-> RETURN CONCAT('Hello, ',s,'!');

Query OK, 0 rows affected (0.10 sec)

11. mysql> SELECT hello('world');


DCL AND TCL COMMANDS IN MYSQL
1. mysql> CREATE DATABASE SEVEN;

2. mysql> use seven;

Database changed

3. mysql> CREATE TABLE Employee1( Id INT, NAME VARCHAR(30) NOT NULL, Email

VARCHAR(100),Age INT CHECK (Age>= 20), PRIMARY KEY (Id),UNIQUE(Email));

Query OK, 0 rows affected (2.25 sec)

4. mysql> INSERT INTO Employee1 values('1','AAA','[email protected]','25');

Query OK, 1 row affected (0.72 sec)

5. mysql> INSERT INTO Employee1 values('2','BBB','[email protected]','23');

Query OK, 1 row affected (0.17 sec)

6. mysql> INSERT INTO Employee1 values('3','CCC','[email protected]','29');

Query OK, 1 row affected (0.13 sec)

7. mysql> INSERT INTO Employee1 values('4','DDD','[email protected]','27');

Query OK, 1 row affected (0.12 sec)

8. mysql> INSERT INTO Employee1 values('5','EEE','[email protected]','28');

Query OK, 1 row affected (0.14 sec)

9. mysql> select * from Employee1;


10. GRANT SELECT, UPDATE ON Employee1 TO AAA;

Query OK, 0 rows affected (0.00 sec)

11. REVOKE UPDATE ON Employee1 FROM AAA;

Query OK, 0 rows affected (0.00 sec)

12. mysql> delete from Employee1 where ID=5;

Query OK, 1 row affected (0.12 sec)

13. mysql> commit;

Query OK, 0 rows affected (0.00 sec)

14. mysql> ROLLBACK;

Query OK, 0 rows affected (0.00 sec)’

15. mysql> select * from EMployee1;


TRIGGERS IN MYSQL

1. mysql> create database tr;


Query OK, 1 row affected (0.02 sec)
2. mysql> use tr;
Database changed
3. mysql> CREATE TABLE test1(a1 INT);

Query OK, 0 rows affected (0.06 sec)

4. mysql> CREATE TABLE test2(a2 INT);

Query OK, 0 rows affected (0.25 sec)

5. mysql> CREATE TABLE test3(a3 INT NOT NULL AUTO_INCREMENT PRIMARY KEY);

Query OK, 0 rows affected (0.08 sec)

6. mysql> CREATE TABLE test4(

-> a4 INT NOT NULL AUTO_INCREMENT PRIMARY KEY,

-> b4 INT DEFAULT 0

-> );

Query OK, 0 rows affected (0.06 sec)

7. mysql> delimiter |

8. mysql> CREATE TRIGGER testref BEFORE INSERT ON test1

-> FOR EACH ROW


-> BEGIN
-> INSERT INTO test2 SET a2 = NEW.a1;
-> DELETE FROM test3 WHERE a3 = NEW.a1;
-> UPDATE test4 SET b4 = b4 + 1 WHERE a4 = NEW.a1;
-> END;
-> |

Query OK, 0 rows affected (0.01 sec)


9. mysql> delimiter ;

10. mysql> INSERT INTO test3 (a3) VALUES

-> (NULL), (NULL), (NULL), (NULL), (NULL),

-> (NULL), (NULL), (NULL), (NULL), (NULL);

Query OK, 10 rows affected (0.04 sec)

Records: 10 Duplicates: 0 Warnings: 0

11. mysql> INSERT INTO test4 (a4) VALUES

-> (0), (0), (0), (0), (0), (0), (0), (0), (0), (0);

Query OK, 10 rows affected (0.03 sec)

Records: 10 Duplicates: 0 Warnings: 0

12. mysql> INSERT INTO test1 VALUES

-> (1), (3), (1), (7), (1), (8), (4), (4);

Query OK, 8 rows affected (0.05 sec)

Records: 8 Duplicates: 0 Warnings: 0

13. mysql> SELECT * FROM test1;

14. mysql> SELECT * FROM test2;


15. mysql> SELECT * FROM test3;

16. mysql> SELECT * FROM test4;


VIEWS AND INDEXES IN MYSQL

1. mysql> CREATE DATABASE mytest;

Query OK, 1 row affected (0.02 sec)

2. mysql> USE mytest;

Database changed

3. mysql> CREATE TABLE example (

-> col1 INT PRIMARY KEY,

-> col2 INT NOT NULL,

-> col3 INT NOT NULL,

-> col4 VARCHAR(20),

-> INDEX (col2, col3)

-> );

Query OK, 0 rows affected (0.09 sec)

4. mysql> SHOW INDEXES FROM example;


5. mysql> CREATE TABLE example2 (col1 INT PRIMARY KEY, col2 VARCHAR(20), col3

VARCHAR(20), col4 ARCHAR(20) );

Query OK, 0 rows affected (0.31 sec)

6. mysql> SHOW INDEXES FROM example2;

7. mysql> CREATE INDEX index1 ON example2 (col2,col3);

Query OK, 0 rows affected (0.16 sec)

Records: 0 Duplicates: 0 Warnings: 0

8. mysql> SHOW INDEXES FROM example2;

9. mysql> create table StudentInformation(Id int, Name varchar(20));

Query OK, 0 rows affected (0.06 sec)


10. mysql> CREATE VIEW view_Student AS SELECT Id,Name from StudInfo;

Query OK, 0 rows affected (0.03 sec)

11. mysql> SHOW CREATE VIEW view_Student;

12. mysql> CREATE TABLE CreatingTableUsingViewStudent AS select Id,Name from

view_Student;

Query OK, 0 rows affected (0.06 sec)

Records: 0 Duplicates: 0 Warnings: 0


XML DOCUMENT CREATION AND VALIDATION

1. CREATE TABLE person (


person_id INT NOT NULL PRIMARY KEY, fname VARCHAR(40) NULL,
lname VARCHAR(40) NULL,
created TIMESTAMP );

XML FILE PERSON.XML


<list>
<personperson_id="1"fname="Kapek"lname="Sainnouine"/>
<personperson_id="2"fname="Sajon"lname="Rondela"/>
<personperson_id="3"><fname>Likame</fname><lname>Örrtmons</lname></person>
<personperson_id="4"><fname>Slar</fname><lname>Manlanth</lname></person>
<person><fieldname="person_id">5</field><fieldname="fname">Stoma</field>
<fieldname="lname">Milu</field></person>
<person><fieldname="person_id">6</field><fieldname="fname">Nirtam</field>
<fieldname="lname">Sklöd</field></person>
<personperson_id="7"><fname>Sungam</fname><lname>Dulbåd</lname></person>
<personperson_id="8"fname="Sraref"lname="Encmelt"/>
</list>
INSERT VALUES USING LOADXMLDATAFILE
2. LOAD XML LOCAL INFILE 'c:/db/person.xml' //this is ths location of the xml data file INTO
TABLE person
ROWS IDENTIFIED BY '<person>';
3.MySQL>Select * from person;
VALIDATE XML USING EXTRACTVALUE FUNCTION

3. MySQL> SELECT ExtractValue('<?xml version="1.0" encoding="UTF-8"?>


CREATE DOCUMENT, COLUMN AND GRAPH BASED DATA USING
NOSQL DATABASE TOOLS.

Create database in mongodb


>Install Mongodb shell
>Connect with localhost
>Connection string: mongodb://localhost:27017
Create collection in mongodb

1. use <database_name>command

Create document in mongodb

2. mydbnew>db.details.insertOne({"website":"mywebsite"})
Display all documents

3. Mydbnew>Db.details.find()

CREATING CHART USING SAMPLE DATA

PROCEDURE:
Step 1: Log into MongoDB Atlas.
To access the MongoDB Charts application, you must be logged into Atlas

Step 2: Select your desired Atlas project, or create a new project.

If you have an Atlas Project with clusters containing data you wish to visualize,
Step 3: Select the project from the Context dropdown in the left navigation pane.

Step 4: Create an Atlas cluster. The MongoDB Charts application makes it easy to connect
Collections in your cluster as data sources. Data sources reference specific collections and
charts views that you can access in the Chart Builder to visualize the data in those collections
or charts views.

Step 5: Launch the MongoDB Charts application. In Atlas, click Charts in the navigation bar.

Step 6: Choose data from clusters


GUI BASED DATABASE APPLICATIONS

PROGRAM
import tkinter as tk import MySQL.connector from tkinter import *
def submitact():
user = Username.get() passw = password.get()
print(f"The name entered by you is {user} {passw}") logintodb(user, passw)
def logintodb(user, passw):
# If password is enetered by the # user
if passw:
db = MySQL.connector.connect(host ="localhost", user = user,
password = passw, db ="College")
cursor = db.cursor()
# If no password is enetered by the # user
else:
db = MySQL.connector.connect(host ="localhost", user = user,
db ="College") cursor = db.cursor()
# A Table in the database
savequery = "select * from STUDENT"
try:
cursor.execute(savequery) myresult = cursor.fetchall()
# Printing the result of the # query
for x in myresult: print(x)
print("Query Executed successfully")

except:
db.rollback() print("Error occurred")

root = tk.Tk() root.geometry("300x300") root.title("DBMS Login Page")


# Defining the first row
lblfrstrow = tk.Label(root, text ="Username -", ) lblfrstrow.place(x = 50, y = 20)
Username = tk.Entry(root, width = 35) Username.place(x = 150, y = 20, width = 100)
lblsecrow = tk.Label(root, text ="Password -") lblsecrow.place(x = 50, y = 50)
password = tk.Entry(root, width = 35) password.place(x = 150, y = 50, width = 100)
submitbtn = tk.Button(root, text ="Login",
bg ='blue', command = submitact) submitbtn.place(x = 150, y = 135, width = 55)
root.mainloop()
CASE STUDY USING ANY OF THE REAL LIFE DATABASE APPLICATIONS -
INVENTORY MANAGEMENT FOR A EMART GROCERY SHOP

DATABASE

TABLES:STOCK TABLE

CREATE TABLE

stock(

Prodid INT PRIMARY KEY, prodname VARCHAR2(50), quantity INT,

unitprice INT, reorder int

);

SALES TABLE

CREATE TABLE

sale(

prodid INT REFERENCES stock(prodid),

prodname VARCHAR2(50), unitprice INT,

50

salesqty INT,

datetime VARCHAR2(50)

);

SAMPLE CODING:

STOCK ENTRY FORM


STOCK ENTRY:

package conn;

import java.sql.Connection; import java.sql.DriverManager; import java.sql.PreparedStatement; import

javax.swing.JOptionPane;

import oracle.jdbc.OraclePreparedStatement;import oracle.jdbc.OracleResultSet;

public class stockentry extends javax.swing.JFrame {

Connection conn=null; OraclePreparedStatement pst=null; OracleResultSet rs=null;

private void btnInsert_clickActionPerformed(java.awt.event.ActionEvent evt) {

//TODO add your handling code here: try

Class.forName("oracle.jdbc.OracleDriver"); Connection conn =

DriverManager.getConnection("jdbc:oracle:thin:@localhost:1521:XE"

, "hemesh", "123");

String sql=” Insert into stock(prodid,prodname,quantity,unitprice,reorder)values(?,?,?,?,?)";

PreparedStatement pst=conn.prepareStatement(sql); pst.setString(1,txt_prodid.getText());

pst.setString(2,txt_prodname.getText()); pst.setString(3,txt_quantity.getText());

pst.setString(4,txt_unitprice.getText()); pst.setString(5,txt_reorder.getText());
pst.execute(); JOptionPane.showMessageDialog(null,"Successfully Inserted");

catch (Exception e)

JOptionPane.showMessageDialog(null, e);

private void btnUpdate_clickActionPerformed(java.awt.event.ActionEvent evt) {

//TODO add your handling code here: try

Class.forName("oracle.jdbc.OracleDriver"); Connection conn =

DriverManager.getConnection("jdbc:oracle:thin:@localhost:1521:XE", "hemesh", "123");

String sql="update stock set prodname=?,quantity=?,unitprice=?,reorder=? where prodid=?";

PreparedStatement pst=conn.prepareStatement(sql); pst.setString(1,txt_prodname.getText());

pst.setString(2, txt_quantity.getText()); pst.setString(3, txt_unitprice.getText()); pst.setString(4,

txt_reorder.getText()); pst.setString(5,txt_prodid.getText()); pst.executeUpdate();

JOptionPane.showMessageDialog(null,"Successfully Updated");

catch (Exception e)

JOptionPane.showMessageDialog(null,e);

STOCK SALES FORM


CODING: STOCK SALES

package stock; import

java.sql.Connection;import java.util.Date;

import java.sql.DriverManager; import java.text.SimpleDateFormat;import javax.swing.JOptionPane;

import java.sql.PreparedStatement;import java.sql.ResultSet;

public class stocksale extends javax.swing.JFrame {public stocksale() {

initComponents();additems();

private void

//TODO add your handling code here:Try

Date d = new Date();

SimpleDateFormat DATE_FORMAT = new SimpleDateFormat("dd-MM-yyyy 'at' HH:mm:ss a");

String date = DATE_FORMAT.format(d); int i=Integer.parseInt(txt_salesqty.getText());


Class.forName("oracle.jdbc.OracleDriver") Connectionconn=

DriverManager.getConnection("jdbc:oracle:thin:@localhost:1521:XE", "hemesh", “123");

String sql="update stock set quantity=quantity-'"+i+"' where prodid=?"; PreparedStatement

pst=conn.prepareStatement(sql);

pst.setString(1,jComboBox1.getSelectedItem().toString());pst.executeUpdate();

Stringsql1="Insertintosale(prodid,prodname,unitprice,salesqty,datetime) values(?,?,?,?,?)";

PreparedStatement pst1=conn.prepareStatement(sql1);

pst1.setInt(1,Integer.parseInt(jComboBox1.getSelectedItem().toString())); pst1.setString(2,

txt_prodname.getText());

pst1.setInt(3,Integer.parseInt( txt_unitprice.getText())); pst1.setInt(4,

Integer.parseInt(txt_salesqty.getText())); pst1.setString(5,date);

pst1.execute();

JOptionPane.showMessageDialog(null, "Sucessfully Inserted");

catch (Exception e)

JOptionPane.showMessageDialog(null, e);

private void jComboBox1ItemStateChanged(java.awt.event.ItemEvent evt) {

//TODO add your handling code here:try

Class.forName("oracle.jdbc.OracleDriver");

Connectionconn=DriverManager.getConnection("jdbc:oracle:thin:@localhost:1 521
:XE", "hemesh", "123");

String sql="select * from stock where prodid=?"; PreparedStatement pst=conn.prepareStatement(sql);

pst.setString(1, jComboBox1.getSelectedItem().toString()); ResultSet rs=pst.executeQuery();

if(rs.next())

txt_prodname.setText(rs.getString("prodname")

);

txt_unitprice.setText(rs.getString("unitprice")); txt_salesqty.setText(rs.getString("salesqty"));

public void additems()

try

Class.forName("oracle.jdbc.OracleDriver"); Connectionconn=

DriverManager.getConnection("jdbc:oracle:thin:@localhost:1521:XE", "hemesh", 123"); String

sql="select prodid from stock"; PreparedStatement pst=conn.prepareStatement(sql);ResultSet

rs=pst.executeQuery();

while(rs.next())
{
}
jComboBox1.addItem(rs.getInt("prodid"));
}
}

You might also like