Dbms Lab
Dbms Lab
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:
Database changed
3. mysql> CREATE TABLE Employee1( Id INT, NAME VARCHAR(30) NOT NULL, Email
VERIFICATION:
VERIFICATION:
SELECT
DROP COLUMNS:
UPDATE
Database changed
3. mysql> CREATE TABLE customer ( ID INT NOT NULL , Name varchar(50) NOT NULL,
NOT NULL, Type varchar(50) NOT NULL, INDEX par_ind (Customer_Id), CONSTRAINT
ERROR 1136 (21S01): Column count doesn't match value count at row 1
ERROR 1452 (23000): Cannot add or update a child row: a foreign key constraint fails
`customer` (`ID`))
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.
2. use third1;
Database changed
9. mysql> insert into student1 values("Suja", 105, 12, "Thanjavur", "Eighth", 15000);
AGGREGATE FUNCTIONS
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;
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 ().
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.
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;
Database changed
varchar(25),product_price int);
11. mysql> create table sale(sales_id int not null primary key auto_increment,product_id
FROM sale);
23. mysql> SELECT * FROM product WHERE product_id NOT IN (SELECT product_id
FROM sale);
JOIN QUERY
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.
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;
Database changed
Table1.ID = Table2.ID;
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;
Database changed
3. mysql> delimiter //
4. mysql> CREATE PROCEDURE citycount (IN country CHAR(3), OUT cities INT)
-> BEGIN
-> END//
5. mysql> delimiter ;
Database changed
3. mysql> CREATE TABLE Employee1( Id INT, NAME VARCHAR(30) NOT NULL, Email
5. mysql> CREATE TABLE test3(a3 INT NOT NULL AUTO_INCREMENT PRIMARY KEY);
-> );
7. mysql> delimiter |
-> (0), (0), (0), (0), (0), (0), (0), (0), (0), (0);
Database changed
-> );
view_Student;
1. use <database_name>command
2. mydbnew>db.details.insertOne({"website":"mywebsite"})
Display all documents
3. Mydbnew>Db.details.find()
PROCEDURE:
Step 1: Log into MongoDB Atlas.
To access the MongoDB Charts application, you must be logged into Atlas
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.
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")
DATABASE
TABLES:STOCK TABLE
CREATE TABLE
stock(
);
SALES TABLE
CREATE TABLE
sale(
50
salesqty INT,
datetime VARCHAR2(50)
);
SAMPLE CODING:
package conn;
javax.swing.JOptionPane;
DriverManager.getConnection("jdbc:oracle:thin:@localhost:1521:XE"
, "hemesh", "123");
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);
JOptionPane.showMessageDialog(null,"Successfully Updated");
catch (Exception e)
JOptionPane.showMessageDialog(null,e);
java.sql.Connection;import java.util.Date;
initComponents();additems();
private void
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());
Integer.parseInt(txt_salesqty.getText())); pst1.setString(5,date);
pst1.execute();
catch (Exception e)
JOptionPane.showMessageDialog(null, e);
Class.forName("oracle.jdbc.OracleDriver");
Connectionconn=DriverManager.getConnection("jdbc:oracle:thin:@localhost:1 521
:XE", "hemesh", "123");
if(rs.next())
txt_prodname.setText(rs.getString("prodname")
);
txt_unitprice.setText(rs.getString("unitprice")); txt_salesqty.setText(rs.getString("salesqty"));
try
Class.forName("oracle.jdbc.OracleDriver"); Connectionconn=
rs=pst.executeQuery();
while(rs.next())
{
}
jComboBox1.addItem(rs.getInt("prodid"));
}
}