DBMS Lab Programs (Print)
DBMS Lab Programs (Print)
OUTPUT:
ID NAME AGE SALARY
101 Ramua 20 2000
SOURCE CODE:
OUTPUT:
RIGHT JOIN:
SOURCE CODE:
SQL> select id,name,salary,odate from customer RIGHT JOIN orders ON
customer.i d=orders.cid;
OUTPUT:
ID NAME SALARY ODATE
101 Ramya 2000 29-DEC-18
OUTPUT:
ID NAME SALARY ODATE
101 Ramya 2000 29-DEC-18
101 Ramya 2000 10-NOV-18
101 Ramya 2000 10-SEP-18
101 Ramya 2000 15-APR-18
101 Ramya 2000 24-NOV-17
102 Dhivya 2300 29-DEC-18
102 Dhivya 2300 10-NOV-18
102 Dhivya 2300 10-SEP-18
102 Dhivya 2300 15-APR-18
102 Dhivya 2300 24-NOV-17
UNION:
SOURCE CODE:
SQL> select id,name,salary,odate from customer left join orders on customer.id=orders.cid
UNION select id,name,salary,odate from customer RIGHT join orders1 5 on
customer.id=orders.cid;
OUTPUT:
UNION ALL:
SOURCE CODE:
SQL> select id,name,salary,odate from customer left join orders on customer.id
=orders.cid UNION ALL select id,name,salary,odate from customer RIGHT join orders15
on customer.id=orders.cid;
OUTPUT:
ID NAME SALARY ODATE
101 Ramya 2000 29-DEC-18
102 Dhivya 2300 10-NOV-18
103 Srividhya 3000 10-SEP-18
104 Priya 2500 15-APR-18
SOURCE CODE:
Table1: Customer
SQL> create table customer(id int,name varchar(20),age int,address
varchar(20),salary float); Table created.
SQL> select * from customer;
OUTPUT:
SOURCE CODE:
Table2: Orders
OUTPUT:
OID ODATE CID AMOUNT
1 29-DEC-18 101 2000
2 10-NOV-18 102 2300
3 10-SEP-18 103 3000
4 15-APR-18 104 2000
6 24-NOV-17 107 2000
AGGREGATE FUNCTION:
SOURCE CODE:
OUTPUT:
Count(*)
5
SOURCE CODE:
3200
SOURCE CODE:
OUTPUT:
MIN(SALARY)
2000
SOURCE CODE:
SQL> select avg(salary)from customer;
OUTPUT:
AVG(SALARY)
2500
SOURCE CODE:
OUTPUT:
SUM(SALARY)
13000
SUBQUERIES:
SOURCE CODE:
SQL> select * from customer where id IN(select id from customer where salary>2000);
OUTPUT:
ID NAME AGE ADDRESS SALARY
102 Dhivya 21 Bombay 2300
SOURCE CODE:
SQL> select * from customer where id NOT IN(select id from customer where salary>2000);
OUTPUT:
ID NAME AGE ADDRESS SALARY
Harish Kolkata
Ashish Durgapur
Pratik Delhi
Dhanraj Bihar
Query:
SQL>CREATE VIEW MarksView AS
SELECT StudentDetails.NAME, StudentDetails.ADDRESS, StudentMarks.MARKS FROM
StudentDetails, StudentMarks WHERE StudentDetails.NAME =StudentMarks.NAME;
SQL>SELECT * FROM MarksView;
OUTPUT:
NAME ADDRESS MARKS
Harsh Kolkata 90
Pratik Delhi 80
Dhanraj Bihar 95
Ram Rajasthan 85
CREATE OR REPLACE VIEW:
Syntax:
CREATE OR REPLACE VIEW view_name AS SELECT column1,coulmn2,..
FROM table_name WHERE condition;
Query:
SQL>CREATE OR REPLACE VIEW MarksView AS SELECT StudentDetails.NAME,
StudentDetails.ADDRESS, StudentMarks.MARKS, StudentMarks.AGE FROM
StudentDetails, StudentMarks WHERE StudentDetails.NAME = StudentMarks.NAME;
SQL> SELECT * FROM MarksView;
OUTPUT:
NAME ADDRESS MARKS AGE
Harsh Kolkata 90 19
Partik Delhi 80 19
Dhanraj Bihar 95 21
Ram Rajasthan 85 18
INSERTING A ROW IN A VIEW:
Syntax:
Query:
SQL>INSERT INTO DetailsView(NAME, ADDRESS)
VALUES("Suresh","Gurgaon"); SQL>SELECT * FROM DetailsView;
OUTPUT:
NAME ADDRESS
Harsh Kolkata
Ashish Durgapur
Pratik Delhi
Dhanraj Bihar
Suresh Gurgaon
Syntax:
DELETE FROM view_name WHERE condition;
Query:
DELETE FROM DetailsView WHERE NAME="Suresh";
OUTPUT:
NAME ADDRESS
Harsh Kolkata
Ashish Durgapur
Partik Delhi
Dhanraj Bihar
Query:
SQL>CREATE TABLE students (ID number(10), NAME char(20));
Now insert values into table
SQL> INSERT into students VALUES(sequence_1.nextval,'Rajesh'); INSERT into students
VALUES(sequence_1.nextval,'Suresh');
OUTPUT:
ID NAME
1 Rajesh
2 Suresh
Query:
SQL>CREATE SYNONYM Geektabl FOR Server1.GFG.Geeeksh.Geektab; GO
Find the output in Server2 by using synonym.
SQL>SELECT ID, Name FROM Geektable;
OUTPUT :
ID NAME
1 Nisha
2 Mira
3 Punit
4 Ram
Program :
DECLARE
total_rows number(2);
BEGIN
UPDATE customers
SET salary = salary + 500;
IF sql%notfound THEN dbms_output.put_line('no customers selected');
ELSIF sql%found THEN
total_rows := sql
%rowcount;
dbms_output.put_line( total_rows || ' customers selected ');
END
IF;
END
;
/
OUTPUT:
6 customers selected
PL/SQL procedure successfully completed.
SQL>Select * from customers;
EXPLICIT CURSORS:
DECLARE
c_id customers.id%type;
c_name customer.name
%type; c_addr
customers.address%type;
CURSOR c_customers is
SELECT id, name, address
FROM customers; BEGIN
OPEN
c_customers;
LOOP
FETCH c_customers into c_id, c_name,
c_addr; EXIT WHEN c_customers
%notfound;
dbms_output.put_line(c_id || '' || c_name || '' ||
c_addr); END LOOP;
CLOSE
c_customers;
END;
/Output:
ID NAME ADDRESS
1 Ramesh Ahmadabad
2 Khilan Delhi
3 Kaushik Kota
4 Chaitali Mumbai
5 Hardik bhopal
6 Komal MP
PROGRAM:
set serveroutput on;
c := a+b;
dbms_output.put_line('Sum of two nos= '|| c); END Sum;
OUTPUT:
Procedure created.
For calling the procedure created following code will be executed:
set serveroutput on; DECLARE
x
num
ber;
y
num
ber;
BEGIN
x := &x;
y := &y; Sum(x,y);
END;
OUTPUT:
Enter value for x: 10
PROGRAM:
c := a+b;
RETURN c;
END;
OUTPUT:
Function Created.
no1
number;
no2
number;
result
number;
BEGIN
no1 :=
&no1;
no2 :=
&no2;
result := Sum(no1,no2);
dbms_output.put_line(‘Sum of two nos=’||result);
END;
OUTPUT:
Enter value for
no1:5 Enter
value for no2:5
PROGRAM:
create or replace trigger up_classd before update on customer for
each row DECLARE
BEGIN
if updating then
DBMS_OUTPUT.PUT_LINE('new value
is'||:new.stotal); DBMS_OUTPUT.PUT_LINE('old
value is'||:old.stotal);
end if;
END;
OUTP
UT:
SQL>/Trigger created.
SQL> select * from customer;
SID SNAME SALARY
1 Aarthi 300
2 Ramya 500
3 Sakthi 670
1 Aarthi 500
2 Ramya 500
3 Skathi 670
OUTPUT:
Trigger created.
SQL> delete from customer
where sid=1; row deleted
1 row deleted.
SQL> select * from customer;
OUTPUT:
SQL>/ Trigger created.
SQL> select * from classb;
1 Ramya IT 450
2 Ezhil IT 460
PROGRAM:
SQL>declare
2 c_id customer.id%type:=5;
3 c_name customer.name%type;
4 c_addr customer.address%type;
5 begin
6 select name,address into
c_name,c_addr 7 from customer
8 where id=c_id;
9 dbms_output.put_line('name:'||c_name);
10 dbms_output.put_line('address:'||c_addr);
11 exception
12 when no_data_found then
13 dbms_output.put_line('no such customer!
'); 14 when others then
15 dbms_output.put_line('error!');
16 end;
17 /
Pre-defined Exceptions
SQL> select * from customer;
Output:
name :priya
address : eee
PL/SQL procedure successfully completed.
User-defined Exceptions
DECLARE
c_id customers.id%type :=
&cc_id; c_name
customers.name%type;
c_addr customers.address%type;
-- user defined
exception ex_invalid_id
EXCEPTION; BEGIN
IF c_id <= 0
THENRAISE
ex_invalid_id;
ELSE
SELECT name, address INTO c_name,
c_addr FROM customers
WHERE id = c_id;
DBMS_OUTPUT.PUT_LINE ('Name: '|| c_name);
DBMS_OUTPUT.PUT_LINE ('Address: ' || c_addr); END IF;
EXCEPTION
WHEN ex_invalid_id THEN
dbms_output.put_line('ID must be greater than zero!');
WHEN no_data_found THEN
dbms_output.put_line('No such customer!
'); WHEN others THEN
dbms_output.put_line('Error!');
END;
/
OUTPUT:
Enter value for cc_id: -6 (let's enter a value -6) old 2: c_id customers.id%type :=
&cc_id;
new 2: c_id customers.id
%type := -6; ID must be greater
than zero!
PL/SQL procedure successfully completed.
Double click on Button to open code window
To insert Record:
Include Header file
using System;
using System.Data;
using System.
Configuration; using
System. Collections;
using System. Web;
using System.
Web.Security; using
System. Web.UI;
using System. Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System. Web.UI.HtmlControls;
using System.
Data.SqlClient;
using System. IO;
Declare Variables
string connstr =
ConfigurationManager.ConnectionStrings["ApplicationServices"].ToString();
SqlConnection conn = new SqlConnection();
string strQuery;
SqlCommand
cmd;
SqlDataAdapter sda = new SqlDataAdapter();
In Button -> Click function
conn.ConnectionString =
connstr; conn.Open();
string insertquery = "insert into Sample (ID,Name) values (@ID,@Name)"; cmd = new
SqlCommand(insertquery, conn); cmd.Parameters.AddWithValue("@ID", TextBox1.Text );
cmd.Parameters.AddWithValue("@Name", TextBox2.Text );
cmd.ExecuteNonQuery();
TextBox1.Text = "";
TextBox2.Text = "";
TextBox1.Focus();
Output
CREATE A TABLE IN ORACLE
SQL>create table account(cname varchar(20),accno number(10),balance
number); Table Created
SQL> insert into account values('&cname',&accno,&balance); Enter value for cname: Mathi
Private Sub
ACCOUNT_Click()
Form2.Show
End Sub
Private
Sub
EXIT_C
lick()
Unload
Me End
Sub
Private Sub
TRANSACTION_C
lick()
Form3.
Show
End
Sub
DELETE_Click()
Adodc1.Recordset.DELETE MsgBox "record deleted"
Adodc1.Recordset.MoveNext If Adodc1.Recordset.EOF =
True Then Adodc1.Recordset.MovePrevious
Private Sub
EXIT_Click()
Unload Me
End
Sub
Private
Sub
HOME
_Click()
Form1.
Show
End
Sub
Private
Sub
INSERT_Click()
Adodc1.Recordset.AddNew End Sub
Form3.Show
End
Sub
Private Sub UPDATE_Click() Adodc1.Recordset.UPDATE MsgBox "record
updated successfully"
End Sub
SOURCE CODE FOR FORM 3
Private Sub
ACCOUNT_Click()
Form2.Show
End Sub
Private Sub
CLEAR_Click()
Text1.Text = ""
DEPOSIT_Click()
Dim s As String s = InputBox("enter the amount to be deposited")
Text2.Text = Val(Text2.Text) + Val(s) A = Text2.Text MsgBox "CURRENT BALANCE
IS Rs"
+ Str(A) Adodc1.Recordset.Save
Adodc1.Recordset.UPDATE End Sub
Form1.Sh
ow End
Sub
Private
Sub
WITHDRAW_Click()
Dim s As String s = InputBox("enter the amount to be deleted")
Text2.Text = Val(Text2.Text) - Val(s) A = Text2.Text MsgBox "current balance is Rs" +
Str(A) Adodc1.Recordset.Save
Adodc1.Recordset.U
PDATE End Sub
Output
SYNTAX:
Import MySQL. Connector
Db_connection=mysql.connector.connect ( Host=”hostname”,User=”username”,
Passwd=”password” )
EXAMPLE:
Import MySQL. Connector
Db_connection = mysql.connector.connect ( Host=”localhost”,User=”root”, Passwd=”root”)
Print (db_connection)
OUTPUT:
Here OUTPUT shows the connection created successfully Creating Database in MySQL
using Python:
SYNTAX:
# executing cursor with execute method and pass SQL QUERY db_cursor.execute
("CREATE DATABASE my_first_db")
# get list of all databases db_cursor.execute ("SHOW DATABASES") #print all databases
For db in db_cursor:
Print (db)
OUTPUT
SYNTAX:
User="root",
Passwd="root", Database="my_first_db")
db_cursor = db_connection.cursor ()
#here creating database table as student'
db_cursor.execute ("CREATE TABLE student (id INT, name VARCHAR (255))") #Get
database table'
db_cursor.execute ("SHOW
TABLES") For table in db_cursor:
Print (table)
OUTPUT:
(‘Student',)
SYNTAX
ALTER TABLE student MODIFY id INT PRIMARY KEY
Example,
Import MySQL. Connector
db_connection = mysql.connector.connect (Host="localhost",User="root", passwd="root",
Database="my_first_db")
SYNTAX:
INSERT INTO employee (id, name, salary) VALUES (01, "John", 10000)
Example:
OUTPUT:
2 Records Inserted
1. Click the Data in Excel
1. In the From ODBC dialog, choose your data source name (DSN). If you haven't
configured your ODBC driver yet, you can enter an SQL statement that will be executed
right after establishing a connection to the data source. Click OK.
1. select Database and enter your credentials in the dialox bog, then click Connect.
4.Click Load.
OUTPUT:
PROCEDURE:
Open ODBC Data Sources (32-bit) and/or ODBC Data Sources (64-bit)
Setup the detail here, and then you can just pick it off the list later.
Step1: In a word document, click on the select recipients and in the drop down menu click on
Step 2: Next in the select data source, click on the New source.
Step 3: Then in the data connection wizard, click on the other/advanced and then click next
Step 5:Then click the Maria DB ODBC 3.0 driver in the create new data source
Step 6: In the Data link properties, click the connection and select
the Use connection string in it and click build.
Steps 7: Next, click on the file data source in the select data source
Step 8: Then in the create new data source click on the MYSQL
Step 9: Now type the file data source name that you want and then click browse and click
next.
Step 11: Then in the MYSQL connector/ODBC give the user id, password and the database
Step 12: Then in the select data source give the DSN name that you have given for the file
data source name
Step 13: Then in the data link properties give the user name, password and then tick the
check box allow saving password and click the test connection
Step 14: Next, in the data connection wizard click the table that you gave and click next
OUTPUT: