Dbms Record Final-1
Dbms Record Final-1
Dbms Record Final-1
Date:
AIM:
To execute a table in the database.
1. CREATE :
Purpose:
To create a table in the database.
Syntax:
Create table <table name> (column definition);
2. DESC:
Purpose:
To display the table structure.
Syntax:
Desc<table name>
3.ALTER:
Purpose:
To alter the structure of the table in the database.
Syntax:
Alter table<table name> add(column definitions);
Alter table <tablename>modify(column definitions);
Alter table <table name> rename to<table name>;
Alter table <table name> rename column<old column name>to<new column
name>
Alter table<table name>drop column<column name>;
Alter table <table name>add constraint name>primary key(column 1,
column2……..,column n-1);
Alter table <table name>drop constraint<constraint name>;
4. DROP:
Purpose:
To remove the table from the database.
Syntax:
Drop table<table name>
5.TRUNCATE:
Purpose:
To remove the entire content of the table from the database.
Syntax:
Truncate tanle<table name>
EXAMPLE QUERIES:
OUTPUT:
Table created.
OUTPUT:
NAME NULL? TYPE
----------------------------- -------------------------------------------------
ENO NUMBER(3)
ENAME VARCHAR2(20)
DNO NUMBER(3)
OUTPUT:
Table altered.
SQL>desc employee.
OUTPUT:
NAME NULL? Type
----------------------------------- -----------------------------------------------
ENO NUMBER(3)
ENAME VARCHAR2(30)
DNO NUMBER(3)
OUTPUT:
Table created.
SQL>desc employee;
OUTPUT:
Table droped.
Result:
Aim:
To execute DML commands.
01. INSERT:
Purpose:
To add records to the table
Syntax:
1.Direct method-only one record can be inserted in the field at a time
Insert into<table name>values<values for all columns>
2.Null method-we can skip some field
Insert into<table name>(column name)values(values for columns)
3.Macro method-More than one value can be inserted in the field at a time.
Insert into<table name>values<&column names>
02. SELECT:
Purpose:
To reterieve or view records with in the table
Syntax:
Select * from<table name>
Select *from <table name>where(condition)
Select (column name)from<table name>
03. UPDATE:
Purpose:
To modify records with in the table
Syntax:
Update<table name>set(column name)=(value)
Update<table name>set(column name)=(value)where(condition)
04. DELETE:
Purpose:
To modify records from a table
Syntax:
Delete from<table name>
Delete from<table name>where(condition)
Example queries:
SQL >.insert into employee values ( &eno, ’&ename’ ,&dno ,&sal, ‘&dname’);
Output:
1 row created
Output:
1 row created
03. Display all records from employee table
SQL>select * from employee;
Output:
ENO ENAME DNO SAL DNAME
1 D.Abinaya 111 8000 IT
2 P.Ratha 111 8900 IT
3 D.Geetha 222 9000 CSE
4 K.lalitha 222 8000 CSE
5 L.priya 333 10000 ECE
6 K.Jaya 333 9000 ECE
7 M.Sasi 444 8000 EEE
8 N.Raja 444 9400 EEE
9 G.Ganga 555 8000 MECH
10 P.Manoj 555 9000 MECH
10 rows selected.
Output:
ENAME DNO
- -------- ------- -------- --------
L.Priya 333
Result:
Thus DML Queries were executed.
Ex:No: 3 BASIC SELECT STATEMENT
Date:
Aim:
To execute Basic Select statement.
Example queries:
1. Write command to list name, emp_id and sal of employee whose salary>15000.
SQL>Select name, eno, sal from employee where (salary >15000);
OUTPUT:
OUTPUT:
OUTPUT:
OUTPUT:
15-jun- IT
234 abinaya D 45000
11
27- IT
212 MARY S SELVAN 45000
SEP-12
6. Write command to list employee joined before 30 Jun 17 and after 31 Dec 17.
SQL>Select * from employee where DOJ not between 30-Jun-17 and 31-Dec-17;
OUTPUT:
OUTPUT:
OUTPUT:
Result:
Thus Basic Select statements were executed.
Ex:No: 4 ADVANCED SELECT STATEMENT
Date:
Aim:
To execute Advanced Select statement.
Example queries:
OUTPUT:
max(salary)
25000
2. Using aggregate function write query to display employee’s salary who’s salary is
greater than average if its employees whose hire date is before 27-09-2017.
Command: Select salary from employee where salary> (select avg(Salary) from
employee where (hire date<’21-Feb-18’));
OUTPUT:
salary
35000
35000
45000
3. Find the job with lowest average salary.
SQL>Select*from employee where salary < (Select min (avg (Salary))from employee
group by department);
OUTPUT:
employ employee_number fname lname mname dob c salary department
1 10 bond john j 20-JUL-98 hr 15000 EEE
4. Find the employees who earn salary same as the min salary of the department.
fname
bond
5. Command to create new table from already existing table.
7. Create table emp1 with the employees who works in hourly basis and create another
table emp2 with who works in contract basis.
SQL> select fname from emp1 union select fname from emp2;
OUTPUT:
fname
john
mark
mark
9. Write command to display the employee who have same name.
concat(fname,concat(mname,lname))
johnsammartieni
markjemrain
markjemrain
Result:
Thus Advanced Select statements were executed.
Ex No:5 INTEGRITY AND CONSTRAINS
Date:
Aim:
To study the various constraints available in the SQL query language.
Description:
DOMAIN INTEGRITY CONSTRAINTS
Domain integrity validates data for a column of the table and it also means the definition
of a valid set of values for an attribute.
It can be enforced using:
Foreign key constraint.
Check constraint.
NOT NULL.
Default constraint.
These definitions ensure that a specific attribute will have a right and proper value
in the database.
ENTITY INTEGRITY CONSTRAINTS
OUTPUT:
DOMAIN INTEGRITY CONSTRAINTS
NOT NULL CONSTRAINT
SQL> create table empl (ename varchar2(30) not null, eid varchar2(20) not null);
Table created.
SQL> insert into empl values ('abcde',11);
1 row created.
SQL> insert into empl values ('fghij',12);
1 row created.
SQL> insert into empl values ('klmno',null);
insert into empl values ('klmno',null)
*
ERROR at line 1:
ORA-01400: cannot insert NULL into ("ITA"."EMPL"."EID")
SQL> select * from empl;
ENAME EID
------------------------------ --------------------
abcde 11
fghij 12
Example queries:
1. Write command to create student relationship with attributes with not null
constraints.
Command: create table emp (id int not null,name varchar(25), dept varchar(25));
Output:
Command: Createtable supply (name varchar (10) NOT NULL, id int NOT
NULL, addr varchar (10) NOT NULL, primary key(name,id));
Output:
Name Null? Type
NAME NOT NULL VARCHAR2(10)
ID NOT NULL NUMBER(38)
ADDR NOT NULL VARCHAR2(10)
3. Alter student table with check constraints in the attribute age where age>=18.
Output:
4. Create table in the name of voter’s list for a part location using default constraint.
Command:Createtable voters(numint,loc varchar(10) default ‘chennai’);
Output:
5. Show the difference in the entity integrity constraint called primary & unique key.
Command:
Primary key: Createtable prime(no number(10), addr varchar(10), primary key(no,
addr));
6. Create table customer with attributes p_id, name, location & age. Similarly another
table called order with the attributes o_id, o_no&p_id.
Command:Createtable cust(p_id number(10) primary key, name varchar(10), loc
varchar(10),age number(10));
Result:
Thus the various constraints available were executed.
Ex: No: 6 JOIN OPERATIONS
Date:
Aim:
To know the use of join operator in SQL.
Join:
A join is used to combine rows from multiple tables. A join is performed whenever
two or more tables is listed in the form clause of an SQL statement.
Table structure:
SQL>desc suppliers;
Name NULL? Type
------ --------- --------- -----------------
Supplier_id number(5)
Supplier_name varchar2(25)
SQL>desc order;
Supplier:
Supid supname
------- ------ --------- ------
12 abi
13 chindu
14 nithi
15 selvi
Order:
Oid supid odate
------- ------- ---------- ------- --------- -------
111 12 1/9/09
222 23 8/9/09
333 14 6/9/09
444 25 3/9/09
555 15 17/9/09
01. Natural joins:
Output:
Supid supname Oid supid odate
-------- ---------- ----------- --------------- ------------- ---------- ------
12 abi 111 12 1/9/09
14 nithi 333 14 6/9/09
15 selvi 555 15 17/9/09
02.Outer Join:
Output:
SuPID SuPNAME Odate
--------- --------- --------- --------- -------- ----------
12 abi 1/9/09
14 nithi 6/9/09
15 selvi 17/9/09
8/9/09
3/9/09
Output:
Output:
EXAMPLE QUERIES :
1. Create db student with attributes & another database advisor with set of attributes
& come up with a relationship table in which the db contains student name with
corresponding advisor.
FNAME
JOHN
MARK
bond
dejane
3. Display the name, city & birth month of the employees with following cosiderations:
1. employee address contains e_name, country & city details, e_id.
2. employee payroll contains e_id,e_dept, e_birthdate&doj.
OUTPUT:
FNAME DOB CITY
JOHN 20-MAY-27 Chennai
MANOJ 5-JUL-10 Bangalore
MAOPI 1-JAN-12 Kolkata
MARKIL 5-MAR-13 Delhi
MARK 1-APR-11 Mumbai
bond 20-JUL-98 Rajastan
dejane 20-JUN-93 Hyderabad
4. List emp_id& gender for all married empls& include the names of any charity
to which the employee donate via the company program.
EID GENDER
RA01 Male
RA03 Female
RA05 Male
5. Get employee name, project name, order by first name from employee detail &
project detail for those employee who have assigned project already.
6. Get employee name, project name, order by first name from employee detail &
project detail for those employee who have not assigned project.
8. Get all project name even they have not matching any emp_id in the left table order
by first name from emp_detail&project_detail.
ENAME PROJECT_NAME
Ni san Machine Learning
Ni san Biometrics
Result:
Thus all joins operations were executed.
Ex No: 7 SQL FUNCTIONS
Date :
Aim:
To execute simple query using SQL Functions.
SQL Function:
Set of instructions which will do some specific task
It may or may not return the value
It’s the module.
Types
Single row function
1. Data function
2. Numeric function or arithmetic or mathematical Function
3. Character function
4. General function or Miscellaneous function
Group function
1. max( )
2. min( )
3. sum( )
4. avg( )
5. std dev( )
6. variance( )
01 .Date functions
05. Write a query to find the next day in a month of date specified
10. Write a query to round off particular date to month preceding it.
11. Write a query to round off particular date to day preceding it.
19. Write a query to retain decimal part and truncate fractional part in a number
27. Write a query to round off a number to the specified decimal places
30. Write a query to accept a character as input and return as output the initial
character
in upper case .
31. Write a query to accept a character as input and return as output character in
lower case.
32. Write a query to accept a character as input and output the character in upper
case.
SQL>select upper (‘manomani’) from dual ;
UPPER(‘MA
------------
MANOMANI
33. Write a query to truncate specified number of characters to left from specified
string.
SQL>select ltrim(‘IloveIndia’,’India’) from dual ;
LTRIM(‘IL
---------
IloveIndia
34. Write a query to truncate specified number of characters to right from specified
string.
38. Write a query to extract specified numbers of characters and replace them with
new characters
From a specified string.
39. Write a query to extract specified numbers of characters from a specified string.
04.Miscellaneous functions
47. Write a query to find the average value of an item in a column of data.
49. Write a query to find the minimum value of an item in a column of data.
50. Write a query to find the maximum value of an item in a column of data.
Result:
Thus all the SQL Function queries were executed.
Ex: No: 8 SUB QUERIES
Date:
Aim:
To execute the Sub queries
Sub Queries:
Query within a query is sub query
The result will be based on innermost query
Syntax:query(query)
Types:
1.Single row subqueries(using single row operators like <,<=,>,>=)
2.Multiple row subqueries(using multiple row operators like in,all,any)
SQL>select name from emp where deptno=(select deptno from emp where
name=’miller’);
SQL>select max(sal)from emp where sal<( select max(sal)from emp where sal<( select
max(sal)from emp));
Using aggregating function subquery whose sal is greater than avg of its employees
whose hiredate is before 1.4.81
To avoid single row subqueries that return more than 1 row we use distinct clause
with subqueries.
SQL>select name from emp where deptno=(select distinct deptno from dept where
deptno=emp.deptno);
Subquery returned more than one value.This is illegal when the subquery follows
=,!=,<=,>=,<,>to rectify instead of ‘=’ place ‘in’.
List the names of employees who earn lowest salary in each dept
SQL>select name,sal,deptno from emp where sal in(select min(sal)from emp group by
deptno);
Correlated SubQuery
A query which uses values from the outer query is called as correlated subquery.
SQL>select no ,deptno,name,sal from emp where sal<(select max(sal)from emp e where
no=e.no);
Exists Operator
Find the employees who earn the same as the min sal for dept
SQL>select *from emp where sal in(select min(sal) from emp group by deptno);
SQL> select name from emp where sal<any(select sal from emp where deptno=30);
SQL> select name from emp where sal>all(select sal from emp where deptno=30);
Table structure:
SQL>desc emp_det;
SQL>desc pro_det;
SQL>desc work_in;
7 rows selected
Output:
3 rows selected
Output:
4 rows selected
Example Queries:
01.Find the names of all employees who do work in department where geetha is
working.
SQL>select ename from emp_det where dno not in (select dno from emp_det where
ename = ‘geetha’);
Output:
ENAME
------ ------
Abi
Deepak
Anjali
Kiruthi
Chindu
4 rows selected
02. Find names of employees who are working in the same department with Shirley.
SQL>select ename,dno from emp_det where dno=(select dno from emp_det where
ename=’shirley’)order by ename;
Output:
ENAME DNO
--------------- ---------------
Geetha 3
Shirley 3
2 rows selected
03. Find the names of employees who are working in DBMS project.
SQL>select ename from emp_det where eno in(select eno from work_in where
pno=(select pno from pro_det where pname=’DBMS’))order by ename
Output:
ENAME
---- -------
Abi
Deepak
2 rows selected
04. Find names and basic salary of those employees of the department with dno2
who get more salary the highest paid employee of the department with dno 10.
SQL>select ename,basic_salary from emp_det where dno = 2 and basic_salary > (select
max(basic_salary)from emp_det where dno=10)order by ename;
Output:
ENAME BASIC_SALARY
---------------- ------------------ ---------------- --------------------------
Anjali 10000
Kiruthi 10000
2 rows selected
Output:
ENAME JOB_STATUS BASIC_SALARY
---------------- ------------------ ---------------- --------------------------
Chindu professor 7800
1 row selected
06. Find the names of all projects in which employees are working.
SQL>select pno,pname from pro_det where exists(select pno from work_in where
work_in.pno=pro_det.pno)
Output:
PNO PNAME
1 DBMS
2 COMPILER
2 rows selected
09. List the salary where basic is less than the average salary.
Output:
ENO ENAME ADDRESS BASIC_SALARY JOB_STATUS DNO
-------- -------------- --------------- ----------------- ------------- ---------- -----------
1 abi erode 8000 manager 10
2 deepak erode 8500 manager 10
4 geetha abc nagar 7800 professor 3
5 shirley kk nagar 7888 assistant 3
7 chindu mmnagar 7800 professor 2
Result:
Thus Sub queries were executed.
Ex: No: 9 VIEWS
Date:
Aim:
To perform operations on views.
Views:
A view is nothing more than a SQL statement that is stored in the database with
an associated name. A view is actually a composition of a table in the form of a
predefined SQL query. 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 depend on the written SQL query
to create a view.
Views, which are a type of virtual tables, allow users to do the following −
Structure data in a way that users or classes of users find natural or intuitive.
Restrict access to the data in such a way that a user can see and (sometimes)
modify exactly what they need and no more.
Summarize data from various tables which can be used to generate reports.
CREATING VIEWS
Database views are created using the CREATE VIEW statement. Views can be
created from a single table, multiple tables or another view. To create a view, a user must
have the appropriate system privilege according to the specific implementation.
SYNTAX
CREATE VIEW view_name AS
SELECT column1, column2.....
FROM table_name
WHERE [condition];
CHECK OPTION
SYNTAX
CREATE VIEW CUSTOMERS_VIEW AS
SELECT column1, column2.....
FROM table_name
WHERE [condition];
WITH CHECK OPTION;
UPDATING A VIEW
A view can be updated under certain conditions which are given below −
DROPPING VIEWS
Drop the view if it is no longer needed.
SYNTAX
drop view view_name;
Example Queries:
1. Create a view for salesman who belong to the city Chennai.
2. Query to find the salesman of the city Chennai who achieves the commission more
than 13%.
3. Query to create view to get count of how customers we have at each level of a grade.
Result:
Thus various Views were executed.
Ex: No: 10 BASIC PL/SQL
Date:
Aim:
To write PL/SQL programs using procedures and functions.
Description:
PL/SQL Programming:
Procedural Language/Structured Query Language (PL/SQL) is an extension of
SQL.
PL/SQL is a block-structured language, meaning that PL/SQL programs are
divided and written in logical blocks of code. Each block consists of three sub-
parts:
DECLARE- This section starts with the keyword DECLARE. It is an optional
section and defines all variables, cursors, subprograms, and other elements to be
used in the program.
EXECUTABLE COMMANDS- This section is enclosed between the keywords
BEGIN and END and it is a mandatory section. It consists of the executable
PL/SQL statements of the program. It should have at least one executable line of
code, which may be just a NULL command to indicate that nothing should be
executed.
EXCEPTION HANDLING- This section starts with the keyword EXCEPTION.
This section is again optional and contains exception(s) that handle errors in the
program.
Syntax:
[(Parameter [,parameter])]
IS
[declaration_section]
BEGIN
executable_section
[EXCEPTION
exception _section]
END [procedure_name];
Problems:
Output:
SQL>set serverout on;
SQL>/
Procedure created.
SQL>exe pro1(4)
1
2
3
4
PL/SQL procedure successfully completed.
02.Create a table employee which has the following columns eno,ename,sal and
write a procedure to display the employee details of given employee number.
Procedure:
Main program:
declare
empno emp.eno%rowtype;
begin
empno :=&empno;
p3(empno);
end;
Output:
SQL>set serverout on;
SQL>/
SQL>execp2(4);
Factorial of 4 is :24
PL/SQL procedure successfully completed.
Main program:
declare
a number;
b number;
begin
a:=&a;
b:=&b;
dbms_output.putline(‘Before swapping a=’||a|| b =’||b);
swap(a,b);
dbms_output.putline(‘After swapping a=’||a|| b =’||b);
end;
Output:
Enter value for a: 3
Old 5:a:=&a;
New 5:a:=3;
Enter the value for b: 5
Old 6:b:=&b;
New 6:b:=5;
Before swapping a = 3 b = 5
After swapping a = 5 b = 3
PL/SQL procedure successfully completed.
04.Write a procedure to input a value from the user and display it.
Output:
Output:
Enter value for a: 5
old 6: a:=&a;
new 6: a:=5;
Enter value for b: 7
old 7: b:=&b;
new 7: b:=7;
Enter value for c: 1
old 8: c:=&c;
new 8: c:=1;
The greatest of the three is 7
PL/SQL procedure successfully completed.
06 .Write a procedure to print numbers from 1 to 5 using simple loop
SQL> set serveroutput on;
SQL> declare
2 a number:=1;
3 begin
4 loop
5 dbms_output.put_line (a);
6 a:=a+1;
7 exit when a>5;
8 end loop;
9 end;
10 /
Output:
1
2
3
4
5
PL/SQL procedure successfully completed.
1
2
3
4
PL/SQL procedure successfully completed.
Output:
1
2
3
4
5
PL/SQL procedure successfully completed.
02. Functions:
Syntax:
Result:
Thus procedure and functions were executed.
Ex.No:11a DESIGN AND IMPLEMENTATION OF STUDENTS
Date: INFORMATION SYSTEM
Aim:
To design a simple form in Visual Basic using Oracle as backend.
Table creation:
SQL> create table vb1(Name varchar2(20),Rollno number(3),Maths number(3),English
number(3),Physics number(3),Chemistry number(3));
Table created.
Coding:
1.ADD
Private Sub ADD_Click()
Text1.SetFocus
Adodc1.Recordset.AddNew
End Sub
2.DELETE
Private Sub DELETE_Click()
If MsgBox("DELETE IT?", vbOKCancel) = vbOK Then
Adodc1.Recordset.DELETE
MsgBox "One row deleted"
End If
Text1.Text = " "
Text2.Text = " "
Text3.Text = " "
Text4.Text = " "
Text5.Text = " "
Text6.Text = " "
End Sub
3.EXIT
Private Sub EXIT_Click()
Unload Me
End Sub
4.SAVE
Private Sub SAVE_Click()
If MsgBox("SAVE IT?", vbOKCancel) = vbOK Then
Adodc1.Recordset.Update
Else
Adodc1.Recordset.CancelUpdate
End If
End Sub
OUTPUT:
Result:
Thus the Students Information System using Visual Basic has been created.
Ex.No:11b DESIGN AND IMPLEMENTATION OF LIBRARY
Aim:
To design the library details in Visual Basic using Oracle as backend.
Algorithm for creating table:
1. Create library table with following fields
Name Type
------------------------------- -------- ----
BOOK NO NUMBER (15)
BOOKNAME VARCHAR2 (15)
BOOKAUTHOR VARCHAR2 (15)
BOOKRATE NUMBER (8)
NCOPY NUMBER (8)
ISSUE DATE DATE
RETURN DATE DATE
2. Insert all the possible values into the library table.
3. Enter commit command.
Algorithm for ADO Connection:
After creating the table in Oracle, Go to start menu.
1. Start Control Panel Administrative ToolsData Sources (ODBC)User
DSNAdd Select Microsoft ODBC for OracleFinishOK
2. One new window will appear. In that window type Data Source Name as table
name created in Oracle. Type user name as the user name entered in SQL+,
Server as 172.31.4.4 and then click O.K.
Algorithm for ADODC in Visual Basic:
1. In Visual Basic create the labels, command buttons and their text boxes.
2. In Visual Basic go to Project menuComponentsMicrosoft ADO Data
Control 6.0 for OLEDBOK
3. Now drag and drop ADODC Data Control available in toolbox into the form.
4. Right click in ADODC Data Control then click the ADODC properties.
5. Choose General tab, useODBC Data source name as the table name created in
Oracle.
6. Choose Use Connection String: Orclclick Buildselect Oracle provider for
OLE
ClickNext
Data Source: Orcl
User Name: as entered in SQL+ login
Password: student
7. Choose Authentication tab and select username, password as entered for SQL+.
8. Choose Record sourceselect Command type as adcmdTable.
9. Select Table or Stored procedure name as table created in Oracle.
10. Click ApplyO.K
11. Select the Data Source as ADODC1
12. Select the Data Field and set the required field name created in table
Coding:
1. ADD
Private Sub ADD_Click ()
Text1.SetFocus
Adodc1.Recordset.AddNew
End Sub
2. SAVE
Private Sub SAVE_Click ()
If MsgBox ("SAVE IT?", vbOKCancel) = vbOK Then
Adodc1.Recordset.Update
Else
Adodc1.Recordset.CancelUpdate
End If
End Sub
3. DELETE
Private Sub DELETE_Click ()
If MsgBox ("DELETE IT?", vbOKCancel) = vbOK Then
Adodc1.Recordset.Delete
MsgBox "One row deleted"
End If
Text1.Text = ""
Text2.Text = ""
Text3.Text = ""
Text4.Text = ""
Text5.Text = ""
Text6.Text = ""
Text7.Text = ""
Text8.Text = ""
Text9.Text = ""
Text10.Text = ""
End Sub
4. FIND
Private Sub FIND_Click ()
Dim N As String
N = InputBox ("Enter the book")
Adodc1.Recordset.Find "book_no=" & N
If Adodc1.Recordset.BOF or Adodc1.Recordset.EOF Then
MsgBox "Record not found"
End If
End Sub
5. MOVE
FIRST
Private Sub FIRST_Click ()
Adodc1.Recordset.MoveFirst
End Sub
PREVIOUS
Private Sub PREVIOUS_Click ()
Adodc1.Recordset.MovePrevious
End Sub
NEXT
Private Sub NEXT_Click ()
Adodc1.Recordset.MoveNext
End Sub
LAST
Private Sub LAST_Click ()
Adodc1.Recordset.MoveLast
End Sub
6. UPDATE
Private Sub UPDATE_Click ()
Adodc1.Recordset.Update
End Sub
7. EXIT
Private Sub EXIT_Click ()
Unload Me
End Sub
OUTPUT:
Result:
Thus the library details were designed in Visual Basic using Oracle as backend
and executed successfully.