0% found this document useful (0 votes)
319 views161 pages

Database Management System

Structured Query Language (SQL) is a standard language used to manage data in relational database management systems (RDBMS). SQL allows users to define, manipulate, and control access to data in databases. It supports various data types including numeric, date/time, character, and binary types. Common numeric types include int, float, decimal, and money. Date/time types include datetime, smalldatetime, date, time, and datetime2. Character types include char, varchar, text, nchar, nvarchar, and ntext.

Uploaded by

Prateek Sharma
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)
319 views161 pages

Database Management System

Structured Query Language (SQL) is a standard language used to manage data in relational database management systems (RDBMS). SQL allows users to define, manipulate, and control access to data in databases. It supports various data types including numeric, date/time, character, and binary types. Common numeric types include int, float, decimal, and money. Date/time types include datetime, smalldatetime, date, time, and datetime2. Character types include char, varchar, text, nchar, nvarchar, and ntext.

Uploaded by

Prateek Sharma
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/ 161

Database Management System Practical File

Assignment 1

Commands Used: Create, Alter, Drop (Desc)

1. What is SQL? What are the various features of SQL?

Ans. Structured Query Language (SQL) is a specialized language for updating, deleting, and
requesting information from databases. SQL is an ANSI and ISO standard, and is the de facto
standard database query language. A variety of established database products support SQL,
including products from Oracle and Microsoft SQL Server. It is widely used in both industry and
academia, often for enormous, complex databases.

The various features of SQL are:

(i) SQL enables end user and system persons to deal with a number of database management
systems where it is available.
(ii) Applications written in SQL can be easily ported across systems. Such porting could be required
when the underlying DBMS needs to upgraded because of change in transaction volumes or
when a system developed in one environment is to be used on another.
(iii)SQL as a language is independent of the way it is implemented internally. A query returns the
same result regardless of whether optimizing has been done with indexes or not. This is because
SQL specifies what is required and not how it is to be done.
(iv) The language while being simple and easy to learn can cope with complex situations.
(v) The results to be expected are well defined in SQL.

Pratik Nag Page 1 Eno. 09990302012


Database Management System Practical File

2. Describe various Data types used in SQL.

Ans. various Data types used in SQL are:

Datatype Min Max Storage Type Notes


Bigint -2^63 2^63-1 8 bytes Exact
numeric
Int -2,147, 2,147,483,64 4 bytes Exact
483,648 7 numeric
Smallint -32,768 32,767 2 bytes Exact
numeric
Tinyint 0 255 1 bytes Exact
numeric
Bit 0 1 1 to 8 bit columns in Exact
the same table numeric
requires a total of 1
byte, 9 to 16 bits = 2
bytes, etc...
Decimal -10^38+1 10^38–1 Precision 1-9 = 5 Exact Decimal and numeric
bytes, precision 10- numeric data type is exactly the
99 = 9 bytes, same. Precision is the
precision 20-28 = 13 total number of digits.
bytes, precision 29- Scale is the number of
38 = 17 bytes decimals. For booth the
minimum is 1 and the
maximum is 38.
Numeric no
Money -2^63 / 2^63-1 / 8 bytes Exact
10000 10000 numeric
Smallmoney - 214,748.364 4 bytes Exact
214,748.364 7 numeric
8
Float -1.79E + 308 1.79E + 308 4 bytes when Approxi Precision is specified
precision is less than mate from 1 to 53.
25 and 8 bytes when numerics
precision is 25
through 53
Real -3.40E + 38 3.40E + 38 4 bytes Approxi Precision is fixed to 7.
mate
numerics
Datetime 1753-01-01 9999-12-31 8 bytes Date and If you are running SQL
00:00:00.00 23:59:59.997 time Server 2008 and need
0 milliseconds precision,
use datetime2(3)
instead to save 1 byte.
Smalldatetime 9900-01-01 2079-06-06 Date and
00:00 23:59 time

Pratik Nag Page 2 Eno. 09990302012


Database Management System Practical File

Date 0001-01-01 9999-12-31 Date and


time
Time 00:00:00.00 23:59:59.999 Date and Specifying the precision
00000 9999 time is possible. TIME(3) will
have milliseconds
precision. TIME(7) is
the highest and the
default precision.
Casting values to a
lower precision will
round the value.
Datetime2 0001-01-01 9999-12-31 Presicion 1-2 = 6 Date and Combines the date
00:00:00.00 23:59:59.999 bytes precision 3-4 = time datatype and the time
00000 9999 7 bytes precision 5-7 datatype into one. The
= 8 bytes precision logic is the
same as for the time
datatype.
Datetimeoffset 0001-01-01 9999-12-31 Presicion 1-2 = 8 Date and Is a datetime2 datatype
00:00:00.00 23:59:59.999 bytes precision 3-4 = time with the UTC offset
00000 - 9999 +14:00 9 bytes precision 5-7 appended.
14:00 = 10 bytes
Char 0 chars 8000 chars Defined width Characte Fixed width
r string
Varchar 0 chars 8000 chars 2 bytes + number of Characte Variable width
chars r string
Varchar(max) 0 chars 2^31 chars 2 bytes + number of Characte Variable width
chars r string
Text 0 chars 2,147,483,64 4 bytes + number of Characte Variable width
7 chars chars r string
Nchar 0 chars 4000 chars Defined width x 2 Unicode Fixed width
character
string
Nvarchar 0 chars 4000 chars Unicode Variable width
character
string
Nvarchar(max) 0 chars 2^30 chars Unicode Variable width
character
string
Ntext 0 chars 1,073,741,82 Unicode Variable width
3 chars character
string
Binary 0 bytes 8000 bytes Binary Fixed width
string
Varbinary 0 bytes 8000 bytes Binary Variable width
string
Varbinary(max) 0 bytes 2^31 bytes Binary Variable width
string

Pratik Nag Page 3 Eno. 09990302012


Database Management System Practical File

Image 0 bytes 2,147,483,64 Binary Variable width


7 bytes string
Sql_variant Other Stores values of various
SQL Server-supported
data types, except text,
ntext, and timestamp.
Timestamp Other Stores a database-wide
unique number that
gets updated every
time a row gets
updated.
Uniqueidentifier Other Stores a globally
unique identifier
(GUID).
Xml Other Stores XML data. You
can store xml instances
in a column or a
variable.
Cursor Other A reference to a cursor.
Table Other Stores a result set for
later processing.

Pratik Nag Page 4 Eno. 09990302012


Database Management System Practical File

3. Create table student with fields: Roll No. , Name, Course and Date of Birth and display its
structure.
Query:-

Create table student_09990302012(

Roll_Noint,

Name varchar(20),

Course varchar(10),

DOB date)

Screenshot:-

Create table student_09990302012(


Roll_Noint,
Name varchar(20),
Course varchar(10),
DOB date)

Pratik Nag Page 5 Eno. 09990302012


Database Management System Practical File

4. Modify the structure of student table as follows(display structure after every modification):
a. Add two new columns named Section and Contact No.
Query:-
ALTER table student_09990302012 ADD(Section char(10),Contact_Noint)

Screenshot:-

ALTER table student_09990302012 ADD(Section char(10),Contact_Noint)

Pratik Nag Page 6 Eno. 09990302012


Database Management System Practical File

b. Change the size of course column by 2 points.


Query:-
ALTER table student_09990302012 MODIFY(Course varchar(12))

Screenshot:-

ALTER table student_09990302012 MODIFY(Course varchar(12))

Pratik Nag Page 7 Eno. 09990302012


Database Management System Practical File

c. Remove the column Date of Birth.


Query:-
ALTER table student_09990302012 DROP(DOB)

Screenshot:-

ALTER table student_09990302012 DROP(DOB)

Query:-

Desc student_09990302012

Screenshot:-

Desc student_09990302012

Pratik Nag Page 8 Eno. 09990302012


Database Management System Practical File

5. Create table Employee with fields: EmpID, EmpName, Designation and Date of Joining
Query:-

Create table Employ_09990302012(

Emp_IDint,

Emp_Namevarchar(20),

Designation varchar(15),

DOJ date)

Screenshot:-

Create table Employ_09990302012(


Emp_IDint,
Emp_Namevarchar(20),
Designation varchar(15),
DOJ date)

Pratik Nag Page 9 Eno. 09990302012


Database Management System Practical File

6. Modify the structure of Employee table as follows(display structure after every modification):
a. Add a new column Salary.
Query:-
ALTER table Employ_09990302012 ADD(Salary int)

Screenshot:-

ALTER table Employ_09990302012 ADD(Salary int)

Pratik Nag Page 10 Eno. 09990302012


Database Management System Practical File

b. Change the size of Salary by 5 points and Name by 10 Points.


Query:-
ALTER table Employ_09990302012 MODIFY(Salary number(10),Emp_Namevarchar(30))

Screenshot:-

ALTER table Employ_09990302012 MODIFY(Salary


number(10),Emp_Namevarchar(30))

Pratik Nag Page 11 Eno. 09990302012


Database Management System Practical File

c. Remove the fields Designation and Date of Joining.


Query:-
ALTER table Employ_09990302012 DROP(Designation,DOJ)
Screenshot:-

ALTER table Employ_09990302012 DROP(Designation,DOJ)

Query:-

DESC Employ_09990302012

Screenshot:-

DESC Employ_09990302012

Pratik Nag Page 12 Eno. 09990302012


Database Management System Practical File

Assignment 2

Commands Used: Create, Select with where clause, Rename, Delete and

Wild Card Characters

1. What is a DML command? Explain with examples the different types of DML commands?
Ans.A Data Manipulation Language (DML) is a family of syntax elements similar to a
computer programming language used for inserting, deleting and updating data in a database.
Performing read-only queries of data is sometimes also considered a component of DML. Data
manipulation language comprises the SQL data change statements, which modify stored data but
not the schema or database objects. Manipulation of persistent database objects, e.g., tables or
stored procedures, via the SQL schema statements, rather than the data stored within them, is
considered to be part of a separate data definition language. In SQL these two categories are
similar in their detailed syntax, data types, expressions etc., but distinct in their overall function.

Data Manipulation Language (DML) statements are used for managing data within schema
objects. Some examples:

o SELECT - retrieve data from the a database


o INSERT - insert data into a table
o UPDATE - updates existing data within a table
o DELETE - deletes all records from a table, the space for the records remain

2. What are logical operators and the relational operators?


Ans.Logical operators

The logical operators AND, OR, and NOT operate according to the value returned by the truth
table. AND & OR are binary operators; NOT is a unary operator.

Relational operators

Relational operators are =,! =, <, >, <=, and >=. They allow for arbitrary comparisons of
complex expressions.

Pratik Nag Page 13 Eno. 09990302012


Database Management System Practical File

3. Create table customer with fields: CID, Lname,Fname,city,Pin,State


Query:-
Create table
Customer_09990302012(Customer_IDint,FNamevarchar(20),LNamevarchar(20),City
varchar(10),State varchar(20),Pin int);
Query:-
DESC Customer_09990302012;

Screenshot:-

DESC Customer_09990302012;

Pratik Nag Page 14 Eno. 09990302012


Database Management System Practical File

4. Insert 5 rows each using all the three methods(All Fields, Selected Fields and User interactive)
Query:-
All Fields
insert into Customer_09990302012 values
(101,'Rahul','Kumar','New Delhi','Delhi',990062)

insert into Customer_09990302012 values


(102,'Ryu','Kumar','New Delhi','Delhi',990062)

insert into Customer_09990302012 values


(103,'Rajiv','Kumar','Karnataka','Karnataka',220030)

insert into Customer_09990302012 values


(104,'Rakesh','Sahani','New Delhi','Delhi',990062)

insert into Customer_09990302012 values


(105,'Rajesh','Mehra','New Delhi','Delhi',990007)

Selected Field

insert into Customer_09990302012 (Customer_ID,FName,LName) values

(106,'Raj','Veer')

User interactive
Old 1: insert into Customer_09990302012 values
(&Customer_ID ,'&FName','&LName','&City','&State',&Pin)
New 1: insert into Customer_09990302012 values
(100 ,'Himanshu','Gautam','New Delhi','Delhi',990006)

Pratik Nag Page 15 Eno. 09990302012


Database Management System Practical File

Query:-
Select * from Customer_09990302012

Screenshot:-

Select * from Customer_09990302012

Pratik Nag Page 16 Eno. 09990302012


Database Management System Practical File

5. Display the contents of Customer Table


Query:-
Select * from Customer_09990302012
Screenshot:-

Select * from Customer_09990302012

Pratik Nag Page 17 Eno. 09990302012


Database Management System Practical File

6. Display CID and Fname of customers


Query:-
Select Customer_ID,FName,from Customer_09990302012

Screenshot:-

Select Customer_ID,FName,from Customer_09990302012

Pratik Nag Page 18 Eno. 09990302012


Database Management System Practical File

7. Display Fname,Lname and state of all the Customers


Query:-
Select FName,LName ,State from Customer_09990302012

Screenshot:-

Select FName,LName ,State from Customer_09990302012

Pratik Nag Page 19 Eno. 09990302012


Database Management System Practical File

8. Display all records from the customer table where state is Delhi
Query:-
Select * from Customer_09990302012 where (state = 'Delhi')

Screenshot:-

Select * from Customer_09990302012 where (state = 'Delhi')

Pratik Nag Page 20 Eno. 09990302012


Database Management System Practical File

9. Display fname,lname of customers concatenated with state and name it as “Customer and their
Location”.
Query:-
Select FName || LName || State as Customer from Customer_09990302012;

Screenshot:-

Select FName || LName || State as Customer from


Customer_09990302012;

Pratik Nag Page 21 Eno. 09990302012


Database Management System Practical File

10. Display the records of those customers whose pin is not entered.
Query:-
Select * from customer_09990302012 where PIN is Null

Screenshot:-

Select * from customer_09990302012 where PIN is Null

Pratik Nag Page 22 Eno. 09990302012


Database Management System Practical File

11. Display the states to which all the customer belongs.


Query:-
Select state from customer_09990302012

Screenshot:-

Select state from customer_09990302012

Pratik Nag Page 23 Eno. 09990302012


Database Management System Practical File

12. Select records of customer from Delhi having name Rajiv.


Query:-
Select * from Customer_09990302012 where Fname='Rajiv' and state='Delhi'

Screenshot:-

Select * from Customer_09990302012 where Fname='Rajiv' and


state='Delhi'

Pratik Nag Page 24 Eno. 09990302012


Database Management System Practical File

13. Display records of customer from state Delhi or Karnataka


Query:-
Select * from Customer_09990302012 where state='Delhi' or state='Karnataka'

Screenshot:-

Select * from Customer_09990302012 where state='Delhi' or


state='Karnataka'

Pratik Nag Page 25 Eno. 09990302012


Database Management System Practical File

14. Display details of Customer with CID=101.


Query:-
Select * from Customer_09990302012 where Customer_ID = '101'

Screenshot:-

Select * from Customer_09990302012 where Customer_ID = '101'

Pratik Nag Page 26 Eno. 09990302012


Database Management System Practical File

15. Display details of Customer except the customer with CID=101


Query:-
Select * from Customer_09990302012 where Customer_ID! = '101'

Screenshot:-

Select * from Customer_09990302012 where Customer_ID! = '101'

Pratik Nag Page 27 Eno. 09990302012


Database Management System Practical File

16. Use Alias Name to display CID as Customer Number


Query:-
Select Customer_ID asCustomer_Number from Customer_09990302012;

Screenshot:-

Select Customer_ID asCustomer_Number from


Customer_09990302012;

Pratik Nag Page 28 Eno. 09990302012


Database Management System Practical File

17. Retrieve all rows where CID is between 100 and 108 (both numbers included)
Query:-
SelectCustomer_ID from Customer_09990302012 whereCustomer_IDBetween 100 AND 108

Screenshot:-

SelectCustomer_ID from Customer_09990302012


whereCustomer_IDBetween 100 AND 108

Pratik Nag Page 29 Eno. 09990302012


Database Management System Practical File

18. Display those rows where state name begins with ‘D’
Query:-
Select State from Customer_09990302012 where state LIKE 'D%'

Screenshot:-

Select State from Customer_09990302012 where state LIKE 'D%'

Pratik Nag Page 30 Eno. 09990302012


Database Management System Practical File

19. Retrieve all rows where first name contains the word ‘RAJ’
Query:-
Select FName from Customer_09990302012 whereFName LIKE 'Raj%'

Screenshot:-

Select FName from Customer_09990302012 whereFName LIKE


'Raj%'

Pratik Nag Page 31 Eno. 09990302012


Database Management System Practical File

20. Retrieve all rows where name field contains the word’RA ESH’
Query:-
Select FName from Customer_09990302012 whereFName LIKE '%Ra_esh%'

Screenshot:-

Select FName from Customer_09990302012 whereFName LIKE


'%Ra_esh%'

Pratik Nag Page 32 Eno. 09990302012


Database Management System Practical File

21. Retrieve all rows where city is Delhi, Karnataka or Bangalore


Query:-
Select City from Customer_09990302012 where City in ('New Delhi','Bangalore','Karnataka')

Screenshot:-

Select City from Customer_09990302012 where City in ('New


Delhi','Bangalore','Karnataka')

Pratik Nag Page 33 Eno. 09990302012


Database Management System Practical File

22. Rename the table customer___ to cust___


Query:-
Rename customer_09990302012 to Cust_09990302012
Query:-
Select * from Cust_09990302012
Screenshot:-

Select * from Cust_09990302012

Pratik Nag Page 34 Eno. 09990302012


Database Management System Practical File

23. Delete all those rows of customers who state in Karnataka


Query:-
Delete from Cust_09990302012 where State = 'Karnataka'
Query:-
Select * from Cust_09990302012
Screenshot:-

Select * from Cust_09990302012

Pratik Nag Page 35 Eno. 09990302012


Database Management System Practical File

24. Delete those customers who do not have Pin Code.


Query:-
Delete from Cust_09990302012 where PIN is null
Query:-
Select * from Cust_09990302012
Screenshot:-

Select * from Cust_09990302012

Pratik Nag Page 36 Eno. 09990302012


Database Management System Practical File

25. Rename column city to address


Query:-
Alter Table Cust_09990302012 Rename Column City to Address
Query:-
Select * from Cust_09990302012
Screenshot:-

Select * from Cust_09990302012

Pratik Nag Page 37 Eno. 09990302012


Database Management System Practical File

26. Delete the customers who do not belong to Bangalore.


Query:-
Delete from Cust_09990302012 where State <> 'Bangalore'
Query:-
Select * from Cust_09990302012
Screenshot:-

Select * from Cust_09990302012

Pratik Nag Page 38 Eno. 09990302012


Database Management System Practical File

Assignment 3

Commands Used: Group by, Order by, Update, delete, rename.

1. Create table employee____ with following attributes: empid, fname, lname, bdate, address,
gender, salary,dept No..
Query:-
Create table
Employ_09990302012(Emp_IDint,Dept_Noint,FNamevarchar(20),LNamevarchar(20),DOB
Varchar(20),Address varchar(20),Gender Char(5),Salary int)
Query:-
DESC Employ_09990302012
Screenshot:-
Select * from Cust_09990302012

DESC Employ_09990302012

Pratik Nag Page 39 Eno. 09990302012


Database Management System Practical File

2. Insert 5 records in employee table.


Query:-
insert into Employ_09990302012 values
(101,1,'Rakesh','Sahani','09-05-9994','Khanpur','Male',20000)
insert into Employ_09990302012 values
(102,2,'Rahul','Kumar','04-01-9995','Khanpur','Male',10000)
insert into Employ_09990302012 values
(103,3,'Ram','Kapoor','25-05-9994','Janakpuri','Male',15000)
insert into Employ_09990302012 values
(104,4,'Angel','Kumari','06-05-9994','Janakpuri','Fmale',18000)
insert into Employ_09990302012 values
(105,5,'Kanika','Kumari','26-05-9995','Janakpuri','Fmale',12000)
Query:-
select * from employ_09990302012
Screenshot:-

Pratik Nag Page 40 Eno. 09990302012


Database Management System Practical File

select * from employ_09990302012

3. List name of all employees who work in Dept No. 5.


Query:-
select * from employ_09990302012 where Dept_No = 5
Screenshot:-

Pratik Nag Page 41 Eno. 09990302012


Database Management System Practical File

select * from employ_09990302012 where Dept_No = 5

4. Find names and salary of all the employees sorted according to their salary (ascending as well as
descending).Ascending By Default
Query:-
select * from employ_09990302012 order by Salary

Pratik Nag Page 42 Eno. 09990302012


Database Management System Practical File

Screenshot:-

select * from employ_09990302012 order by Salary

Query:-
select * from employ_09990302012 order by Salary DESC
Screenshot:-

select * from employ_09990302012 order by Salary DESC

5. List names of employees having salary between 10,000 and 20,000 using logical operators
Query:-
Select * from employ_09990302012 where SalaryBetween 10000 and 20000
Screenshot:-

Pratik Nag Page 43 Eno. 09990302012


Database Management System Practical File

Select * from employ_09990302012 where SalaryBetween 10000


and 20000

6. List name of employees who live in Janakpuri.


Query:-
Select * from employ_09990302012 where Address = 'Janakpuri'
Screenshot:-

Pratik Nag Page 44 Eno. 09990302012


Database Management System Practical File

Select * from employ_09990302012 where Address = 'Janakpuri'

7. List name of all female employees.


Query:-
Select * from employ_09990302012 where Gender = 'Fmale'
Screenshot:-

Pratik Nag Page 45 Eno. 09990302012


Database Management System Practical File

Select * from employ_09990302012 where Gender = 'Fmale'

8. List name of those employees whose DOB is before 09-05-9994.


Query:-
Select * from employ_09990302012 where DOB = '09-05-9994'
Screenshot:-

Pratik Nag Page 46 Eno. 09990302012


Database Management System Practical File

Select * from employ_09990302012 where DOB = '09-05-9994'

9. List all those employees whose first name is “Ram” and last Name is “Kapoor”.
Query:-
Select * from employ_09990302012 where Fname = 'Ram' And Lname = 'Kapoor'
Screenshot:-

Pratik Nag Page 47 Eno. 09990302012


Database Management System Practical File

Select * from employ_09990302012 where Fname = 'Ram' And


Lname = 'Kapoor'

10. Rename the column address to city.


Query:-
Alter Table Employ_09990302012 Rename Column Address to City
Query:-

Pratik Nag Page 48 Eno. 09990302012


Database Management System Practical File

Select * from employ_09990302012


Screenshot:-

Select * from employ_09990302012

11. Sort the employees in descending order of city.


Query:-
Select * from employ_09990302012 order by City DESC

Pratik Nag Page 49 Eno. 09990302012


Database Management System Practical File

Screenshot:-

Select * from employ_09990302012 order by City DESC

12. Update the person named “Ram” to “Vikas”.


Query:-
Update Employ_09990302012 Set Fname = 'Vikas' where Fname = 'Ram'

Pratik Nag Page 50 Eno. 09990302012


Database Management System Practical File

Query:-
Select * from employ_09990302012
Screenshot:-

Select * from employ_09990302012

13. Update city of Empid 101 to Jaipur.


Query:-
Update Employ_09990302012 Set City = 'Jaipur' where EMP_ID = 101
Pratik Nag Page 51 Eno. 09990302012
Database Management System Practical File

Query:-
Select * from employ_09990302012
Screenshot:-

Select * from employ_09990302012

14. Delete record of Empid 102.


Query:-
Delete from employ_09990302012 Where EMP_ID = 102

Pratik Nag Page 52 Eno. 09990302012


Database Management System Practical File

Query:-
Select * from employ_09990302012
Screenshot:-

Select * from employ_09990302012

15. Display those employees whose salary is not equal to 20,000.


Query:-

Pratik Nag Page 53 Eno. 09990302012


Database Management System Practical File

Select * from employ_09990302012 where Salary<> 20000


Screenshot:-

Select * from employ_09990302012 where Salary<> 20000

Pratik Nag Page 54 Eno. 09990302012


Database Management System Practical File

Assignment 4

Commands used : Grouping functions

1. What are the grouping functions and how do you define the different grouping
functions(min,max,count,avg,sum)
Ans. Group functions are built-in SQL functions that operate on groups of rows and return one
value for the entire group. These functions are: COUNT, MAX, MIN, AVG, SUM, DISTINCT

The various functions are:


 SQL COUNT (): This function returns the number of rows in the table that satisfies the
condition specified in the WHERE condition.
 SQL DISTINCT (): This function is used to select the distinct rows.
 SQL MAX (): This function is used to get the maximum value from a column.
 SQL MIN (): This function is used to get the minimum value from a column.
 SQL AVG (): This function is used to get the average value of a numeric column.
 SQL SUM():This function is used to get the sum of a numeric column.
2. How do you differentiate group by and order by?
Ans.
GROUP BY ORDER BY

GROUP BY will aggregate records by ORDER BY alters the order in which items
the specified column which allows you are returned. It can order the items either in
to perform aggregation functions on ascending or in descending order.
non-grouped columns (such as SUM,
COUNT, AVG, etc.).

Pratik Nag Page 55 Eno. 09990302012


Database Management System Practical File

3. Explain the having clause with an example?


Ans.TheSQL HAVING clause is used in combination with the SQL GROUP BY clause. It can
be used in an SQL SELECT statement to filter the records that a SQL GROUP BY returns.

The syntax for the SQL HAVING clause is:

SELECT column1, column2, ...column_n, aggregate_function (expression)

FROM tablesWHERE predicates

GROUP BY column1, column2, ...column_n

HAVING condition1 ... condition_n;

4. Create table employee____ with following attributes:


empno,ename(Not Null),salary(Not Null), Commission,Job, Deptno.
Query:-
Create table Employee_09990302012 (Emp_IDint,ENamevarchar(20) NOT NULL,Salaryint
NOT NULL,Commissionint,Jobvarchar(20), Dept_Noint)
Query:-
DESC Employee_09990302012
Screenshot:-

DESC Employee_09990302012

Pratik Nag Page 56 Eno. 09990302012


Database Management System Practical File

5. Insert 5 records in employee table.


Query:-

insert into Employee_09990302012 values


(1,'Rakesh Sahani',60000,5000,'Manager',1)
insert into Employee_09990302012 values
(2,'Ryu Kumar',55000,5000,'CEO',2)
insert into Employee_09990302012 values
(3,'Angel Kumari',50000,5000,'Manager',1)
insert into Employee_09990302012 values
(4,'Renu Kumari',50000,2000,'CEO',2)
insert into Employee_09990302012 values
(5,'Rakhi Kumari',48000,2000,'Staff',3)

Query:-

select * from Employee_09990302012


Screenshot:-

select * from Employee_09990302012

Pratik Nag Page 57 Eno. 09990302012


Database Management System Practical File

6. Find average and total salaries of all employees.


Query:-

select AVG(SALARY),COUNT(SALARY) From Employee_09990302012


Screenshot:-

select AVG(SALARY),COUNT(SALARY) From Employee_09990302012

7. Find average and total salaries of the employee.


Query:-

select AVG(SALARY),COUNT(SALARY) From Employee_09990302012 Group By Emp_ID


Screenshot:-

select AVG(SALARY),COUNT(SALARY) From Employee_09990302012 Group By


Emp_ID

Pratik Nag Page 58 Eno. 09990302012


Database Management System Practical File

8. Find minimum salary of Manager.


Query:-

select MIN(SALARY) From Employee_09990302012 Where Job = 'Manager'

Screenshot:-

select MIN(SALARY) From Employee_09990302012 Where Job = 'Manager'

9. Find the maximum salary of the Manager.


Query:-

select MAX(SALARY) From Employee_09990302012 Where Job = 'Manager'


Screenshot:-

select MAX(SALARY) From Employee_09990302012 Where Job = 'Manager'

Pratik Nag Page 59 Eno. 09990302012


Database Management System Practical File

10. How many employees are Managers.


Query:-

select Count(Job) From Employee_09990302012 Where Job = 'Manager'

Screenshot:-

select Count(Job) From Employee_09990302012 Where Job = 'Manager'

11. Add 50% salary to salary column and name it HRA.


Query:-

select Salary * .5,Salary as HRA From Employee_09990302012

Screenshot:-

select Salary * .5,Salary as HRA From Employee_09990302012

Pratik Nag Page 60 Eno. 09990302012


Database Management System Practical File

12. Count the total no. employees.


Query:-

select Count(*) From Employee_09990302012

Screenshot:-

select Count(*) From Employee_09990302012

13. Count the no. of departments available.


Query:-

select Count(Dept_No) From Employee_09990302012


Screenshot:-

select Count(Dept_No) From Employee_09990302012

Pratik Nag Page 61 Eno. 09990302012


Database Management System Practical File

14. Count the no. of employees in each department.


Query:-

select Count(Dept_No) From Employee_09990302012 Group By Dept_No


Screenshot:-

select Count(Dept_No) From Employee_09990302012 Group By Dept_No

15. Update the commission to 10%of salary for all the employees having salary greater than 50000.
Query:-

select Commission * .1 From Employee_09990302012 where Salary > 50000


Screenshot:-

select Commission * .1 From Employee_09990302012 where Salary > 50000

Pratik Nag Page 62 Eno. 09990302012


Database Management System Practical File

16. Find the minimum salary for each department.


Query:-

select MIN(salary),Dept_No from Employee_09990302012 group by Dept_No


Screenshot:-

select MIN(salary),Dept_No from Employee_09990302012 group by Dept_No

17. Update the salaries of all employees in marketing department by hiking it by 15%.
Query:-

Update Employee_09990302012 Set salary=Salary *.15 where Dept_No = 2


select * from Employee_09990302012
Screenshot:-

select * from Employee_09990302012

Pratik Nag Page 63 Eno. 09990302012


Database Management System Practical File

18. Find out the difference between highest and lowest salaries for each department.
Query:-
select max(salary),min(salary) from Employee_09990302012 Group by dept_no
Screenshot:-

select max(salary),min(salary) from Employee_09990302012 Group by dept_no

19. List average salary for all the departments which have more than 5 People.
Query:-

select AVG(salary),dept_no from Employee_09990302012 group by dept_no having


count(Emp_ID)>1
Screenshot:-

select AVG(salary),dept_no from Employee_09990302012 group by dept_no


having count(Emp_ID)>1

Pratik Nag Page 64 Eno. 09990302012


Database Management System Practical File

20. List jobs of all the employees where maximum salary is greater than or equal to 40000
Query:-

select job from Employee_09990302012 where salary>=40000


Screenshot:-

select job from Employee_09990302012 where salary>=40000

21. List jobs and no. of employees in each job. The result should be in descending order of the
number of employees
Query:-

select job, count(distinct job) from Employee_09990302012 group by job order by


count(Emp_ID)
Screenshot:-

select job, count(distinct job) from Employee_09990302012 group by job order by


count(Emp_ID)

Pratik Nag Page 65 Eno. 09990302012


Database Management System Practical File

22. List average salary of each job excluding managers


Query:-
selectavg(salary) from Employee_09990302012 where job <> 'Manager'
Screenshot:-

Select avg(salary) from Employee_09990302012 where job <> 'Manager'

Pratik Nag Page 66 Eno. 09990302012


Database Management System Practical File

23. List total, Max, Min, Average salary of employees job wise for dept no 2 and display only those
with average salary >10000
Query:-

select SUM(salary),MAX(salary),MIN(salary),AVG(salary) from Employee_09990302012


Group by Job Having AVG(salary)>10000
Screenshot:-

select SUM(salary),MAX(salary),MIN(salary),AVG(salary) from


Employee_09990302012 Group by Job Having AVG(salary)>10000

Pratik Nag Page 67 Eno. 09990302012


Database Management System Practical File

Practice Question - 1
1. Create a table employee with the following fields:
empno,ename,salary,commission,job,department number.

Query:-

create table emp_09990302012(Enoint,Enamevarchar(25),Salary


int,Commissionint,Jobvarchar(25),Dept_Noint)

DESC emp_09990302012
Screenshot:-

DESC emp_09990302012

Pratik Nag Page 68 Eno. 09990302012


Database Management System Practical File

2. Add five records to the table.


Query:-
insert into emp_09990302012 values (101,'Rahul',30000,NULL,'Clerk',10)
insert into emp_09990302012 values (201,'Shyam',40000,NULL,'Manager',20)
insert into emp_09990302012 values (301,'Rachna',35000,5000,'Executive',10)
insert into emp_09990302012 values (401,'Ramesh',40000,NULL,'Manager',30)
insert into emp_09990302012 values (501,'Swati',10000,1000,'Sales',10)

Query:-
select * from emp_09990302012

Screenshot:-

select * from emp_09990302012

Pratik Nag Page 69 Eno. 09990302012


Database Management System Practical File

3. Display the list of those employees who do not have commission.


Query:-
select * from emp_09990302012 where commission IS NULL
Screenshot:-

select * from emp_09990302012 where commission IS NULL

4. Give an alias name to 5% of salary and display empno,ename,salary and the alias name
HRA.Query:-
selectEno,Ename,Salary, (.05 * Salary) as "HRA" From emp_09990302012
Screenshot:-

Select Eno,Ename,Salary, (.05 * Salary) as "HRA" From emp_09990302012

Pratik Nag Page 70 Eno. 09990302012


Database Management System Practical File

5. Find out the minimum salary of a manager.


Query:-
select MIN(Salary) from emp_09990302012 where Job = 'Manager'
Screenshot:-

select MIN(Salary) from emp_09990302012 where Job = 'Manager'

6. Find out how many managers are there in an organization.


Query:-
select Count(Job) from emp_09990302012 where Job = 'Manager'
Screenshot:-

select Count(Job) from emp_09990302012 where Job = 'Manager'

Pratik Nag Page 71 Eno. 09990302012


Database Management System Practical File

7. Find out the maximum salary of the department where the maximum salary should be more than
30000.
Query:-
select MAX(Salary),Dept_no from emp_09990302012
group by dept_no having MAX(Salary)>30000

Screenshot:-

select MAX(Salary),Dept_no from emp_09990302012


group by dept_no having MAX(Salary)>30000

Pratik Nag Page 72 Eno. 09990302012


Database Management System Practical File

8. Find out the number of employees in each department in their ascending order of the number for
those department where atleast two employees work and that two in department 10.20,30.

Query:-
select count(*),Dept_no from emp_09990302012 where dept_no IN (10,20,30)
group by dept_no having count(*)>2 order by count(*) DESC

Screenshot:-

select count(*),Dept_no from emp_09990302012 where dept_no IN (10,20,30)


group by dept_no having count(*)>2 order by count(*) DESC

Pratik Nag Page 73 Eno. 09990302012


Database Management System Practical File

Assignment 5

Key constraints

1. What is a sql constraint?

Ans.SQL constraints are defined as the limitations that are used to limit the type of data that can
be added into a table.
 Constraints can be specified at two levels :
o Table level constraints, and
o Column level constraints
 Table Level Constraints are applied by the DBA whereas;The Column Level
constraints can be added after the table is created.
 The SQL constraints can be applied using Create Table Command and Alter Table
Command

2. What are the different types of SQL constraints? Explain each in detail:
Ans.
 Not null:The NOT NULL constraint enforces a column to NOT accept NULL values.

SYNTAX:
create table course_a023
(code number(10) primary key,namevarchar(10) not null,sectionvarchar(5))

 Unique: The UNIQUE constraint uniquely identifies each record in a database table. The
UNIQUE and PRIMARY KEY constraints both provide a guarantee for uniqueness for a
column or set of columns. A PRIMARY KEY constraint automatically has a UNIQUE
constraint defined on it.

SYNTAX:

Create table order_a023


(O_nonumber(10) not null,Constraintu_ono unique(o_no))

 Check: The CHECK constraint is used to limit the value range that can be placed in a
column.

SYNTAX:
Create table person
(P_idnumber(10),Check (p_id>0))

Pratik Nag Page 74 Eno. 09990302012


Database Management System Practical File

 Default: The DEFAULT constraint is used to insert a default value into a column.

SYNTAX:
Create table person
(P_idnumber(10) not null,Cityvarchar(20) default ‘delhi’)
 Primary key: The PRIMARY KEY constraint uniquely identifies each record in a
database table. Primary keys must contain unique values. A primary key column cannot
contain NULL values.

SYNTAX:
Create table person
(P_idnumber(10) not null primary key,Cityvarchar(20) default ‘delhi’)

 Foreign key: A FOREIGN KEY in one table points to a PRIMARY KEY in another
table.

SYNTAX:
PERSON TABLE

create table person_099


(p_id number(10) primary key,l_namevarchar(10),f_namevarchar(10) not null,
addressvarchar(20),city varchar(20))

ORDER TABLE

(O_idnumber(10) primary key,O_no number(10) not null,p_id number(10),


Constraint u_onounique(o_no),Constraint fk_oid foreign key(p_id) references
person_099(p_id))

 Here the primary key of person table is assigned as foreign key in the order table.

Pratik Nag Page 75 Eno. 09990302012


Database Management System Practical File

3.
a) create table course___ with fields: coursecode(primary key), coursename(not
null),section.

Query:-
create table course_09990302012(course_codeint primary key,course_namevarchar(20)
not null,Sectionvarchar(20))
Query:-
desc course_09990302012
Screenshot:-

desc course_09990302012

b) create table student___ with following attributes rollno, last name, first name,
address(unique), coursecode(foreign key)

Query:-
create tables student_09990302012(
Roll_Noint,Fnamevarchar(20),Lnamevarchar(20),Address
varchar(20)unique,Course_codeint,constraint Ass5 foreign key(Course_code) references
course_09990302012 (Course_code))
Query:-
desc Student_09990302012

Pratik Nag Page 76 Eno. 09990302012


Database Management System Practical File

Screenshot:-

desc Student_09990302012

Add the following constraints to the above tables:


1. Roll no. should be primary key using alter statement
Query:-

alter table Student_09990302012 add primary key (roll_no)

Query:-

desc Student_09990302012

Screenshot:-

desc Student_09990302012

Pratik Nag Page 77 Eno. 09990302012


Database Management System Practical File

2. Add constraint on firstname to not allow blank entries


Query:-

Alter table Student_09990302012 modify fname not null

Query:-

desc Student_09990302012

Screenshot:-

desc Student_09990302012

3. Add a check constraint on section i.e. it should be ‘m’ or ‘e’


Query:-

alter table course_09990302012 add check(section in('m','e'))

Screenshot:-

alter table course_09990302012 add check(section in('m','e'))

Pratik Nag Page 78 Eno. 09990302012


Database Management System Practical File

4. Drop constraint primary key of student table using alter command.


Query:-

alter table student_09990302012 drop primary key

Query:-

desc Student_09990302012

Screenshot:-

desc Student_09990302012

4.

Person table
p_id lastname firstname address city

1 hari om tilaknagar 10 delhi

2 shyam tomar base 23 bangalore

3 peter joseph staff 20 chandigarh

Pratik Nag Page 79 Eno. 09990302012


Database Management System Practical File

Orders table:
o_id orderno p_id

1 77895 3

2 44678 3

3 22456 2

4 24562 1

Specify the following constraints while creating table:-


1)p_id (person table)-primary key
2)first name-not null constraint
3)order no –unique constraint,not null
4)p_id(order table)-foreign key
5)o_id-primary key

Query:-

create table person_09990302012(p_id number(20) primary


key,Lastnamevarchar(20),Firstnamevarchar(20) not null,Addressvarchar(20),City varchar(20))

Query:-

Select * from person_09990302012

Screenshot:-

Select * from person_09990302012

Pratik Nag Page 80 Eno. 09990302012


Database Management System Practical File

Query:-

create table order_09990302012(o_id number(20) primary key,orderno number(20) not null,p_id


number(20),constraint fk_as5 foreign key(p_id) references person_09990302012(p_id),
constraint as5 unique (orderno));

Query:-

select * from order_09990302012

Screenshot:-

select * from order_09990302012

specify the following constraints while alter table:-


1)Add a default constraint on city as delhi

Query:-

alter table person_09990302012 modify city default 'delhi'

Screenshot:-

alter table person_09990302012 modify city default 'delhi'

Pratik Nag Page 81 Eno. 09990302012


Database Management System Practical File

2) Add check constraint on p_id>0

Query:-

Alter table person_09990302012 Add check(p_id>0)

Screenshot:-

Alter table person_09990302012 Add check(p_id>0)

3) Drop unique constraint order no from order table

Query:-

Alter table order_09990302012drop constraint as5

Screenshot:-

Alter table order_09990302012


drop constraint as5

Pratik Nag Page 82 Eno. 09990302012


Database Management System Practical File

4) Tryto delete a record from person table and find out the result

Query:-

delete from person_09990302012

Screenshot:-

delete from person_09990302012

5)Try to delete a record from order table and find out the result.

Query:-

Delete from order_09990302012 Where o_id=1

Screenshot:-

Delete from order_09990302012 Where o_id=1

Pratik Nag Page 83 Eno. 09990302012


Database Management System Practical File

6) Note down your observation from these two queries.


 In RDBMS while deleting a record we first delete the record from the child table and
then from parent table.

 To solve this problem we use the word “on delete cascade” whereis used along with
child table so that the records can be deleted from directly from the parent table.

Pratik Nag Page 84 Eno. 09990302012


Database Management System Practical File

Assignment 6

Numeric functions, Character function


1. What are scalar functions?

Ans.A scalar function is a function that, for each set of one or more scalar parameters,
returns a single scalar value. Examples of scalar functions include the LENGTH function,
and the SUBSTR function. Scalar functions can also be created that do complex
mathematical calculations on function input parameters. Scalar functions can be referenced
anywhere that an expression is valid within an SQL statement, such as in a select-list, or in a
FROM clause.
 Some of the scalar functions are:
o Absolute
o Length
o Upper
o Lower
o Ceil
o Floor
o Susbstr
o Instr
o Initcap
o Round, etc

2. What is a dual table?

Ans.The DUAL table is a special one-column table present by default in


all Oracle database installations. It is suitable for use in selecting a pseudo column such as
SYSDATE or USER. The table has a single VARCHAR2(1) column called DUMMY that
has a value of 'X'.

Pratik Nag Page 85 Eno. 09990302012


Database Management System Practical File

3. Give Syntax and Queries to demonstrate the use of following Numeric functions in System
Defined Table.

a. Absolute
Query:-
Select abs (-13.5) from dual
Screenshot:-

Select abs (-13.5) from dual

b. Ceil
Query:-
Select ceil (3.5) from dual
Screenshot:-

Select ceil (3.5) from dual

Pratik Nag Page 86 Eno. 09990302012


Database Management System Practical File

c. Floor
Query:-
Select floor(13.5) from dual
Screenshot:-

Select floor(13.5) from dual

d. Round
Query:-
Select round(13.52,1) from dual
Screenshot:-

Select round(13.52,1) from dual

Pratik Nag Page 87 Eno. 09990302012


Database Management System Practical File

e. Mod
Query:-
Select mod (3,5) from dual
Screenshot:-

Select mod (3,5) from dual

f. Power
Query:-
Select power(3,5) from dual
Screenshot:-

Select power(3,5) from dual

Pratik Nag Page 88 Eno. 09990302012


Database Management System Practical File

g. Tan
Query:-
Select tan(5) from dual
Screenshot:-

Select tan(5) from dual

h. Log
Query:-
Select log (3,10) from dual
Screenshot:-

Select log (3,10) from dual

Pratik Nag Page 89 Eno. 09990302012


Database Management System Practical File

i. Cos
Query:-
Select cos (15) from dual
Screenshot:-

Select cos (15) from dual

j. Sin
Query:-
Select sin(13) from dual
Screenshot:-

Select sin(13) from dual

Pratik Nag Page 90 Eno. 09990302012


Database Management System Practical File

k. Trunc
Query:-
Select trunc (13.235,2) from dual
Screenshot:-

Select trunc (13.235,2) from dual

l. Greatest
Query:-
Select greatest(1,3,5) from dual
Screenshot:-

Select greatest(1,3,5) from dual

Pratik Nag Page 91 Eno. 09990302012


Database Management System Practical File

m. Least
Query:-
Select least(1,3,5) from dual
Screenshot:-

Select least(1,3,5) from dual

n. Sqrt
Query:-
Select sqrt(13) from dual
Screenshot:-

Select sqrt(13) from dual

Pratik Nag Page 92 Eno. 09990302012


Database Management System Practical File

2. Create the following table and perform the below mentioned Queries

A B C
-12.5 2 3
-13.5 4 5
0.5 6 7
1.0 8 9
2.0 10 99

Query:-
Create table arth09990302012( A number(4,2),B int,Cint)
Query:-
select * from arth09990302012;

Screenshot:-

select * from arth09990302012;

Pratik Nag Page 93 Eno. 09990302012


Database Management System Practical File

a. Find the absolute value of Column A and name it as “Absolute A”


Query:-
select abs(A) as "absolute A" from arth09990302012;
Screenshot:-

select abs(A) as "absolute A" from arth09990302012;

b. Find the upper and lower values of Column A and column B respectively.
Query:-
select ceil(A),ceil(B),floor(A),floor(B) from arth09990302012;
Screenshot:-

select ceil(A),ceil(B),floor(A),floor(B) from arth09990302012;

Pratik Nag Page 94 Eno. 09990302012


Database Management System Practical File

c. Find the Power value using Absolute A as base and column C as its power.
Query:-
select power(abs(A),c) from arth09990302012;
Screenshot:-

select power(abs(A),c) from arth09990302012;

d. Find the square root of Column A


Query:-
selectsqrt(abs(A)) from arth09990302012;
Screenshot:-

Select sqrt(abs(A)) from arth09990302012;

Pratik Nag Page 95 Eno. 09990302012


Database Management System Practical File

e. Find the value of A mod B


Query:-
select mod(A,B) from arth09990302012;
Screenshot:-

select mod(A,B) from arth09990302012;

f. Find the sum of column A and Column B


Query:-
select A+B as "Sum" from arth09990302012;
Screenshot:-

select A+B as "Sum" from arth09990302012;

Pratik Nag Page 96 Eno. 09990302012


Database Management System Practical File

3. Give Syntax and Queries to demonstrate the use of following Character functions in
System Defined Table

a. Initcap
Query:-
Select initcap('Rakesh') from dual
Screenshot:-

Select initcap('Rakesh') from dual

b. Length
Query:-
Select length('Rakesh') from dual
Screenshot:-

Select length('Rakesh') from dual

Pratik Nag Page 97 Eno. 09990302012


Database Management System Practical File

c. Substr
Query:-
Select substr('RyuLove',4,4) from dual
Screenshot:-

Select substr('RyuLove',4,4) from dual

d. Instr
Query:-
Select instr('Rakesh','a') from dual
Screenshot:-

Select instr('Rakesh','a') from dual

Pratik Nag Page 98 Eno. 09990302012


Database Management System Practical File

e. Trim
Query:-
select trim(both 'u' from 'Rayu') from dual
Screenshot:-

select trim(both 'u' from 'Rayu') from dual

f. Ltrim
Query:-
selectltrim('Ryu Love','Ryu') from dual
Screenshot:-

Select ltrim('Ryu Love','Ryu') from dual

Pratik Nag Page 99 Eno. 09990302012


Database Management System Practical File

g. Rtrim
Query:-
Select rtrim('RyuLove','Love') from dual
Screenshot:-

Select rtrim('RyuLove','Love') from dual

h. Lpad
Query:-
Select lpad('Ryu',10,'*****') from dual
Screenshot:-

Select lpad('Ryu',10,'*****') from dual

Pratik Nag Page 100 Eno. 09990302012


Database Management System Practical File

i. Rpad
Query:-
Select rpad('Ryu',10,'*****') from dual
Screenshot:-

Select rpad('Ryu',10,'*****') from dual

j. Upper
Query:-
Select upper('rakesh') from dual
Screenshot:-

Select upper('rakesh') from dual

Pratik Nag Page 101 Eno. 09990302012


Database Management System Practical File

k. Lower
Query:-
Select lower('RAKESH') from dual
Screenshot:-

Select lower('RAKESH') from dual

l. Replace
Query:-
select replace('R','R','Ryu') from dual
Screenshot:-

select replace('R','R','Ryu') from dual

Pratik Nag Page 102 Eno. 09990302012


Database Management System Practical File

m. Translate
Query:-
select translate('rakesh','a','A') from dual
Screenshot:-

select translate('rakesh','a','A') from dual

4. Create the following table and perform the below mentioned Queries
Eid Ename Designation Salary City
101 Rajiv Manager 10000 Delhi
102 Sita Clerk 5000 Mumbai
103 Ram Manager 10000 Chennai
104 Ramesh Assistant 8000 Hyderabad
105 Shyam Clerk 5000 Delhi

Query:-
create table scemp_09990302012 (Eidint,Enamevarchar(20),Designation varchar(20),Salary
int,Cityvarchar(20))
Query:-
select * from scemp_09990302012

Pratik Nag Page 103 Eno. 09990302012


Database Management System Practical File

Screenshot:-

select * from scemp_09990302012

a. Convert the name of employees into uppercase.


Query:-
select upper(ename) from scemp_09990302012
Screenshot:-

select upper(ename) from scemp_09990302012

Pratik Nag Page 104 Eno. 09990302012


Database Management System Practical File

b. Capitalize the first letter of the designation of Employees.


Query:-
Select initcap(Designation) from scemp_09990302012
Screenshot:-

Select initcap(Designation) from scemp_09990302012

c. Convert the city column into lowercase letters.


Query:-
select lower(city) from scemp_09990302012
Screenshot:-

select lower(city) from scemp_09990302012

Pratik Nag Page 105 Eno. 09990302012


Database Management System Practical File

d. Trim zeroes from salary column.


Query:-
Select rtrim(salary,000) from scemp_09990302012
Screenshot:-

Select rtrim(salary,000) from scemp_09990302012

e. Pad the salary to 10 digits using Zeroes on right side.


Query:-
Select rpad(salary,10,00000) from scemp_09990302012
Screenshot:-

Select rpad(salary,10,00000) from scemp_09990302012

Pratik Nag Page 106 Eno. 09990302012


Database Management System Practical File

f. Remove 1 from eid column.


Query:-
select trim(both 1 from eid) from scemp_09990302012
Screenshot:-

select trim(both 1 from eid) from scemp_09990302012

g. Prefix 2 to eid column.


Query:-
Select lpad(eid,'4','2') from scemp_09990302012
Screenshot:-

Select lpad(eid,'4','2') from scemp_09990302012

Pratik Nag Page 107 Eno. 09990302012


Database Management System Practical File

h. Find the location of word “ei” in city column.


Query:-
Select instr(city,'e') from scemp_09990302012
Screenshot:-

Select instr(city,'e') from scemp_09990302012

i. Find the first two digits of city column.


Query:-
Select substr(city,1,2) from scemp_09990302012
Screenshot:-

Select substr(city,1,2) from scemp_09990302012

Pratik Nag Page 108 Eno. 09990302012


Database Management System Practical File

j. Find the length of designation column.


Query:-
select length(designation) from scemp_09990302012
Screenshot:-

select length(designation) from scemp_09990302012

Pratik Nag Page 109 Eno. 09990302012


Database Management System Practical File

Assignment 7

Date functions
1. What are date functions?

Ans. A data function is use to manipulate and extract the data from a particular column. A
date function will accept data as a input.

Some data functions are:

a. Add_months(d,n) :This function will return specify date after adding the number of
months to the date mention.
b. Last_day(d) : This function return the last date of the month specified in the function.
c. Months between (d2, d1): This function will return the number of months between the
D2 , D1.
d. Next_day(d,day) : This function will return the date of the first week i.e. is mention by
the day in the function.

2. Give Syntax and Queries to demonstrate the use of following Date functions in System
Defined Table
a. Add_months(d,n)
Query:-
Select add_months('99-mar-2013',3) from dual
Screenshot:-

Select add_months('99-mar-2013',3) from dual

Pratik Nag Page 110 Eno. 09990302012


Database Management System Practical File

b. Last_day(d)
Query:-
Select last_day('99-mar-2013') from dual
Screenshot:-

Select last_day('99-mar-2013') from dual

c. Months_between(d2,d1)
Query:-
Select Months_between('99-mar-2013','99-feb-2013') from dual
Screenshot:-

Select Months_between('99-mar-2013','99-feb-2013') from dual

Pratik Nag Page 111 Eno. 09990302012


Database Management System Practical File

d. Next_day(d,day)
Query:-
Select Next_day('99-mar-2013','Mon') from dual
Screenshot:-

Select Next_day('99-mar-2013','Mon') from dual

3. Create table task___ and perform the following operations

Task_name Start_date End_Date


Planning 01-april-9995 23-april-9995
Analysis 24-april-9995 14-may-9995
Design 15-may-9995 30-may-9995
Coding 01-june-9995 30-june-9995
Testing 01-july-9995 02-aug-9995
Query:-

create table task_09990302012(Task_namevarchar(30),Start_datedate,End_Date date)

Query:-

select * from task_09990302012

Pratik Nag Page 112 Eno. 09990302012


Database Management System Practical File

select * from task_09990302012

a. Increase End date by 3 months and name the column as new end date
Query:-
Select add_months(end_date,3) from task_09990302012

Select add_months(end_date,3) from task_09990302012

Pratik Nag Page 113 Eno. 09990302012


Database Management System Practical File

b. Find the last working day of every end date


Query:-
Select last_day(end_date),end_date from task_09990302012
Screenshot:-

Select last_day(end_date),end_date from task_09990302012

c. Calculate months between start date and end date.


Query:-
Select months_between(end_date,start_date) from task_09990302012
Screenshot:-

Select months_between(end_date,start_date) from task_09990302012

Pratik Nag Page 114 Eno. 09990302012


Database Management System Practical File

d. Find the next working day of every start date.


Query:-
Select next_day(start_date,'Monday') from task_09990302012
Screenshot:-

Select next_day(start_date,'Monday') from task_09990302012

4. Give Syntax and Queries to demonstrate the use of following Date functions in System
Defined Table

a. To_Char()
Query:-
Select to_char(12.73,'99.9') from dual
Screenshot:-

Select to_char(12.73,'99.9') from dual

Pratik Nag Page 115 Eno. 09990302012


Database Management System Practical File

Query:-
Select to_char(12.73,'$99.00') from dual
Screenshot:-

Select to_char(12.73,'$99.00') from dual

Query:-
Select to_char(21,'000099') from dual
Screenshot:-

Select to_char(21,'000099') from dual

Pratik Nag Page 116 Eno. 09990302012


Database Management System Practical File

Query:-
Select to_char(sysdate,'yyy/mm/dd') from dual
Screenshot:-

Select to_char(sysdate,'yyy/mm/dd') from dual

Query:-
Select to_char(sysdate,'Month DD,YYY') from dual
Screenshot:-

Select to_char(sysdate,'Month DD,YYY') from dual

Pratik Nag Page 117 Eno. 09990302012


Database Management System Practical File

Query:-
Select to_char(sysdate,'MON DDth,YYY') from dual
Screenshot:-

b. To_Date()
Query:-
Select to_date('2003/07/09','yyyy/mm/dd') from dual
Screenshot:-

Pratik Nag Page 118 Eno. 09990302012


Database Management System Practical File

Query:-
Select to_date('070803','MMDDYY') from dual
Screenshot:-

c. To_number
Query:-
Select to_number('1210.7','9999.9') from dual
Screenshot:-

Pratik Nag Page 119 Eno. 09990302012


Database Management System Practical File

Query:-
Select to_number('546','999') from dual
Screenshot:-

5. Create table employee with fields: Empid,DOB(char), doj, salary,deptname


Query:-
create table emp(Empid number(10),DOB char(10),DOJ date,Sal
number(20),Deptnamevarchar(20))
Query:-
select * from emp
Screenshot:-

Pratik Nag Page 120 Eno. 09990302012


Database Management System Practical File

a. Format DOJ to yyyy/mm/dd


Query:-
Select to_date(Doj,'yyyy/mm/dd') from emp
Screenshot:-

b. Format DOJ to Month dd,yyyy


Query:-
Select to_char(doj,'Month DD,YYYY') from emp
Screenshot:-

Pratik Nag Page 121 Eno. 09990302012


Database Management System Practical File

c. Format salary column to $


Query:-
Select to_char(sal,'$99999') from emp
Screenshot:-

d. Format salary column to $999.99


Query:-
Select to_char(sal,'$99999.99') from emp
Screenshot:-

Pratik Nag Page 122 Eno. 09990302012


Database Management System Practical File

e. Format salary column to $999.99


Query:-
Select to_char(salary,'$99999.99') from emp
Screenshot:-

f. Convert dob to date format


Query:-
Select to_date(dob) from emp
Screenshot:-

Pratik Nag Page 123 Eno. 09990302012


Database Management System Practical File

Assignment 8

Sub Queries
1. What is a sub query?
Ans.A subquery is a query within a query. In Oracle, you can create subqueries within your
SQL statements. These subqueries can reside in the WHERE clause, the FROM clause, or the
SELECT clause.

2. Find the difference between ANY and ALL?

Ans. ANY :The If any value from outcome of subquery is macthed then desired output will
beretrived.It checks with lowest value.

ALL :It will check for greatest value from outcome of subquery

3. Create table emp with fields empnmo,ename,salary,deptno,dname


Query:-
Create table emp09990302012 (emp_noint,enamevarchar(30),salary int,
dept_noint,dnamevarchar(30))
Query:-
select * from emp09990302012
Screenshot:-

select * from emp09990302012

Pratik Nag Page 124 Eno. 09990302012


Database Management System Practical File

4. Find the details of those employees whose salary is equal to maximum salary in the
organisation.
Query:-
select * from emp09990302012 where salary=(select max(salary)from emp09990302012)
Screenshot:-

select * from emp09990302012 where salary=(select max(salary)from emp09990302012)

5. Find the details of those employees who earn less than the average salary of the
organisation.
Query:-
select * from emp09990302012 where salary<(select avg(salary)from emp09990302012)
Screenshot:-

select * from emp09990302012 where salary<(select avg(salary)from emp09990302012)

Pratik Nag Page 125 Eno. 09990302012


Database Management System Practical File

6. Find all employees name of the department where ajay works.


Query:-
Select ename from emp09990302012 where dept_no=(select dept_no from emp09990302012
where ename='Rahul')
Screenshot:-

Select ename from emp09990302012 where dept_no=(select dept_no from emp09990302012


where ename='Rahul')

7. Find all the details of the employees who draw more than the average salary in the
organisation.
Query:-
select * from emp09990302012 where salary>(select avg(salary)from emp09990302012)
Screenshot:-

select * from emp09990302012 where salary>(select avg(salary)from emp09990302012)

Pratik Nag Page 126 Eno. 09990302012


Database Management System Practical File

8. Find all the details of the person who draws the second highest salary.
Query:-
select * from emp09990302012 where salary=(select max(salary)from emp09990302012
where salary<(select max(salary)from emp09990302012))
Screenshot:-

select * from emp09990302012 where salary=(select max(salary)from emp09990302012


where salary<(select max(salary)from emp09990302012))

9. Find the details of all the employees provided atleast 5 persons work in department 4 (use
Exists)
Query:-
select * from emp09990302012 where exists(select * from emp09990302012 where
dept_no=4 group by dept_no having count(*)>5)
Screenshot:-

select * from emp09990302012 where exists(select * from emp09990302012 where


dept_no=4 group by dept_no having count(*)>5)

Pratik Nag Page 127 Eno. 09990302012


Database Management System Practical File

Query:-
select * from emp09990302012 where exists(select * from emp09990302012 where
dept_no=4 group by dept_no having count(*)>0)
Screenshot:-

select * from emp09990302012 where exists(select * from emp09990302012 where


dept_no=4 group by dept_no having count(*)>0)

10. Find the details of those employees who earn more than the minimum salary of department
no 10.(Use Any)
Query:-
select * from emp09990302012 where salary>any(select salary from emp09990302012
where dept_no = 10)
Screenshot:-

select * from emp09990302012 where salary>any(select salary from emp09990302012 where


dept_no = 10)

Pratik Nag Page 128 Eno. 09990302012


Database Management System Practical File

Assignment 9

Views
1. What is view and types of views?

Ans.In SQL, a view is a virtual table based on the result-set of an SQL statement.

A view contains rows and columns, just like a real table. The fields in a view are fields from one
or more real tables in the database.

Types of views:

a. Updateable,
b. Non Updateable

2. Create a table employee with following attributes eno(Primary Key) ,ename ,ecity, salary,
deptno.
Query:-

create table em_09990302012(Enoint primary key,Enamevarchar(25),ecityvarchar(25),Salary


int,Dept_noint);

Query:-

select * from em_09990302012

Screenshot:-

Select * from em_09910302012

Pratik Nag Page 129 Eno. 09990302012


Database Management System Practical File

3. Create a view having ename and ecity.

Query:-

create view view_99 as select ename,ecity from em_09990302012

select * from view_99

Screenshot:-

select * from view_99

4. In the above view update the ecity to ‘Delhi’ where ename is ‘John’.Update View
Query:-

update view_99 set ecity = 'Delhi' where ename = 'John'

select * from view_99

Screenshot:-

select * from view_99

Pratik Nag Page 130 Eno. 09990302012


Database Management System Practical File

5. Insert a row in the view and see if it is visible in table created.

Query:-

insert into view_99 values ('Ajay','Mumbai')

Screenshot:-

insert into view_99 values ('Ajay','Mumbai')

We cannot insert value from the view into the base table because while inserting the value in the view the
primary key in the base table contains a null Hence, to insert value with help of primary key the view
must contain the primary key column.

Query:-

create view view_099 as select eno,ename,ecity from em_09990302012;

insert into view_099 values (6,'Ajay','Mumbai');

select * from view_099;

Screenshot:-

select * from view_991

Pratik Nag Page 131 Eno. 09990302012


Database Management System Practical File

6. Update the above view and increase the salary of all employees of department no. 10 by
Rs.1000.

Query:-

create view view_991 as select eno,ename,ecityecity ,Salary,Dept_no from em_09990302012;

update view_991 set Salary = salary+1000 where dept_no = 10

select * from view_991

Screenshot:-

select * from view_991

7. Create view having details of employee working in deptno=10

Query:-

create view view_299 as select * from em_09990302012 where dept_no = 10

Screenshot:-

create view view_299 as select * from em_09990302012 where dept_no = 10

Pratik Nag Page 132 Eno. 09990302012


Database Management System Practical File

8. Create a view having grouping functions like max(sal)and min(sal)

Query:-

create view view_399 as select max(salary) as "max_sal",min(salary) as "min_sal" from


em_09990302012

Screenshot:-

create view view_399 as select max(salary) as "max_sal",min(salary) as


"min_sal" from em_09990302012

9. Update the above view and set the max salary to 90000.Observe the result.

Query:-

update view_399 set "max_sal" = 9000

Screenshot:-

update view_399 set "max_sal" = 9000

Pratik Nag Page 133 Eno. 09990302012


Database Management System Practical File

10. Delete the view record created

Query:-

delete from view_299 where dept_no = 10

Screenshot:-

delete from view_299 where dept_no = 10

11. Delete the view created

Query:-

drop view view_299

Screenshot:-

drop view view_299

Pratik Nag Page 134 Eno. 09990302012


Database Management System Practical File

12.Observe the operations that are performed on views

INSERT:
While inserting record through a view we need a primary key of the base table for insertion in
the base table

DELETE:
While deleting a record we don’t need a primary key we can simply delete the desired record

UPDATE:
While updating a view if there are grouping functions or having clause , set operator and join
then the view cannot be updated

Pratik Nag Page 135 Eno. 09990302012


Database Management System Practical File

Assignment 10

Join
1. What are join ? What are different types of joins ?

Ans. SQL joins are used to query data from two or more tables, based on a relationship between certain
columns in these tables.Different SQL JOINs are:

 JOIN: Return rows when there is at least one match in both tables.
 LEFT JOIN: Return all rows from the left table, even if there are no matches in the right table.
 RIGHT JOIN: Return all rows from the right table, even if there are no matches in the left table.
 FULL JOIN: Return rows when there is a match in one of the tables.

2. Create the Department table with fields: Dno(Primary Key), Dname, Dloc.Insert the
following values

Dno Dname Dloc

10 Accounts New York

20 Research Dallas

30 Sales Chicago

40 Operation Boston

50 Payroll Dallas

Query:-

create table dept099(Dnoint primary key,Dnamevarchar(20),Dlocvarchar(20))

select * from dept099;

Screenshot:-

select * from dept099;

Pratik Nag Page 136 Eno. 09990302012


Database Management System Practical File

3. Create the Employee table with fields: Empno(Primary Key), Ename, Salary, Commission,
Deptno(Foreign Key),Mgrid.Insert the following values.

Empno Ename Salary Commission Deptno Mgrid

75 Jones 20000 20

76 Martin 30000 30 75

77 Blake 40000 1400 30 75

78 Ford 10000 20

79 Tummes 20000 5000 10 78

Query:-

create table empl099(Empnoint primary key,enamevarchar(25),salary int,commissionint,dnoint,mgridint,


constraint de foreign key(dno) references dept099)

Query:-

select * from empl099

Screenshot:-

select * from empl099

Pratik Nag Page 137 Eno. 09990302012


Database Management System Practical File

4. Create the SalGrade table with fields: Grade, Lowsal, Highsal.Insert the following values

Grade Lowsal highsal

A 10000 99000

B 20000 29000

C 30000 50000

Query:-

create table salgrade099(Grade varchar(10),Lowsalint,Highsalint);

Query:-

select * from salgrade099;

Screenshot:-

select * from salgrade099;

Pratik Nag Page 138 Eno. 09990302012


Database Management System Practical File

5. Display Empno, Salary,Deptname of employees.


Query:-
select empl099.empno,empl099.salary,dept099.dname from dept099
inner join empl099 on dept099.dno = empl099.dno
Screenshot:-

select empl099.empno,empl099.salary,dept099.dname from dept099


inner join empl099 on dept099.dno = empl099.dno

6. Use table Alias & solve the above query again.


Query:-
Select e.empno,e.salary,d.dname from dept099 d
inner join empl099 e on d.dno = e.dno
Screenshot:-

Select e.empno,e.salary,d.dname from dept099 d


inner join empl099 e on d.dno = e.dno

Pratik Nag Page 139 Eno. 09990302012


Database Management System Practical File

7. Display Empno.,ename,Grade and salary that should be greater than lowsal and less than
highsal.
Query:-
Select e.empno,e.ename,e.salary,s.grade from empl099 e
inner join salgrade099 s on e.salary>s.lowsal
ande.salary<s.highsal
Screenshot:-

Select e.empno,e.ename,e.salary,s.grade from empl099 e


inner join salgrade099 s on e.salary>s.lowsal
ande.salary<s.highsal

8. Retrieve name of employees where employee is also a manager.


Query:-
Select e.ename from empl099 e
join empl099 ev on e.empno=ev.mgrid
Screenshot:-

Select e.ename from empl099 e


join empl099 ev on e.empno=ev.mgrid

Pratik Nag Page 140 Eno. 09990302012


Database Management System Practical File

9. Display Empno, Ename, Dname ,Dloc of all the employees using outer join.
Query:-
Select e.empno,e.ename,d.dname,d.dloc from dept099 d
right join empl099 e on d.dno=e.dno
Screenshot:-

Select e.empno,e.ename,d.dname,d.dloc from dept099 d


right join empl099 e on d.dno=e.dno

10. Display Empno, Ename, Dname ,Dloc of all the departments using outer join.
Query:-
Select e.empno,e.ename,d.dname,d.dloc from dept099 d
left join empl099 e on d.dno=e.dno
Screenshot:-

Select e.empno,e.ename,d.dname,d.dloc from dept099 d


left join empl099 e on d.dno=e.dno

Pratik Nag Page 141 Eno. 09990302012


Database Management System Practical File

11. Retrieve employee no, employee name of all the employees along with their manager no and
manager name if any.
Query:-
Select e.ename,e.empno,ev.mgrid
from empl099 e
join empl099 ev on e.empno=ev.mgrid
Screenshot:-

Select e.ename,e.empno,ev.mgrid
from empl099 e
join empl099 ev on e.empno=ev.mgrid

12. Display a cross join employee and Department table.


Query:-
select * from empl099
cross join dept099
Screenshot:-

Pratik Nag Page 142 Eno. 09990302012


Database Management System Practical File

Assignment 99

Revision
1. Create table student with following fields: RollNo(PK), Sname, DOB(not Null), Coursecode(FK), Year of
admission.
Query:-
create table stu099(RollNoint primary key,Snamevarchar(20),DOB date Not Null,ccodeint,constraint rev
foreign key(ccode) references course099,Year_of_admission date)
Query:-
select * from stu099;
Screenshot:-

select * from stu099;

2. Create table course with fields:ccode(PK), cname(Unique),fee.


Query:-
create table course099(ccodeint primary key,cnamevarchar(20) Unique,feeint);
select * from course099;
Screenshot:-

select * from course099;

Pratik Nag Page 143 Eno. 09990302012


Database Management System Practical File

3. Find the age of the students.


Query:-
select (sysdate-dob)/365 as age from stu099

Screenshot:-

select (sysdate-dob)/365 as age from stu099

4. Find the roll no, Name, course name of every student.


Query:-
Select rs.rollno,rs.sname,c.cname from stu099 rs
inner join course099 c on rs.ccode = c.ccode

Screenshot:-

Select rs.rollno,rs.sname,c.cname from stu099 rs


inner join course099 c on rs.ccode = c.ccode

Pratik Nag Page 144 Eno. 09990302012


Database Management System Practical File

5. Find the details of the students who are in BBACAM course.


Query:-
select rs.rollno,rs.sname,rs.dob,rs.ccode,rs.year_of_admission,c.cname,c.fee from stu099 rs
inner join course099 c
onrs.ccode = c.ccode
where cname = 'BBA-CAM'
Screenshot:-

select rs.rollno,rs.sname,rs.dob,rs.ccode,rs.year_of_admission,c.cname,c.fee
from stu099 rs
inner join course099 c
onrs.ccode = c.ccode
where cname = 'BBA-CAM'

6. Find the list of students who have taken admission in the 2012.
Query:-
select rs.rollno,rs.sname,rs.dob,rs.ccode,rs.year_of_admission,c.cname,c.fee from stu099 rs
inner join course099 c
onrs.ccode = c.ccode
where year_of_admission = 2012
Screenshot:-

select rs.rollno,rs.sname,rs.dob,rs.ccode,rs.year_of_admission,c.cname,c.fee
from stu099 rs
inner join course099 c
onrs.ccode = c.ccode
where year_of_admission = 2012

Pratik Nag Page 145 Eno. 09990302012


Database Management System Practical File

7. Find the number of students who have taken admission year wise.
Query:-
select count(*),rs.year_of_admission from stu099 rs
inner join course099 c
onrs.ccode = c.ccode
group by year_of_admission

Screenshot:-

select count(*),rs.year_of_admission from stu099 rs


inner join course099 c
onrs.ccode = c.ccode
group by year_of_admission

8. Find the details of the students who are in the course in which student with roll no 101 studies.
Query:-
Select * from stu099 where ccode=(select course099.ccode from course099,stu099
where course099.ccode=stu099.ccode
and stu099.rollno=101)

Screenshot:-

Select * from stu099 where ccode=(select course099.ccode from


course099,stu099
where course099.ccode=stu099.ccode
and stu099.rollno=101)

Pratik Nag Page 146 Eno. 09990302012


Database Management System Practical File

Assignment 12

Set Operators
1. What are different set operators?

Ans.The different set operators are union,unionall,intersection,minus.

All the below operators are binary operators that are used to perform different set operation using two
tables.

Union - In this duplicate values are eliminated & we get all the records of both tables.

Union all- In this duplicate records are there & records of both the table are displayed.

Intersection- In this only common record of both the table are diaplayed.

Minus - It returns the difference of two select query i.e the second query is substracted by first query.

2. Create table emp_banglore & emp_delhi with following attributes :eno (primary key),name,salary,dno.
Insert 5 records in emp_banglore and 3 records in emp_delhi.
Query:-
create table emp_bang099(enoint primary key,namevarchar(20),salint,dnoint);
Query:-
select * from emp_bang099;
Screenshot:-

select * from emp_bang099;

Pratik Nag Page 147 Eno. 09990302012


Database Management System Practical File

Query:-
create table emp_delhi099(enoint primary key,namevarchar(20),salint,dnoint);

Query:-
select * from emp_delhi099;
Screenshot:-

select * from emp_delhi099;

3. Find out the list of all employees who are working in Delhi & Bangalore.
Query:-
select * from emp_bang099
union
select * from emp_delhi099
Screenshot:-

select * from emp_bang099


union
select * from emp_delhi099

Pratik Nag Page 148 Eno. 09990302012


Database Management System Practical File

4. Find out the list of all employees who are working in both the locations.
Query:-
select * from emp_bang099
intersect
select * from emp_delhi099
Screenshot:-

select * from emp_bang099


intersect
select * from emp_delhi099
intersect
select * from emp_delhi099

5. Find out the list of all employees who are working only in banglore.
Query:-
select * from emp_bang099
minus
select * from emp_delhi099
Screenshot:-

select * from emp_bang099


minus
select * from emp_delhi099

Pratik Nag Page 149 Eno. 09990302012


Database Management System Practical File

Assignment13

Index
1. What are Indexes?Why are they required in DBMS
Ans. Index an order list of the contents of column/columns of a table. SQL uses a B-tree structure in
indexes to store and retrieve data. Pointers are used to point these data nodes. Database starts its search
at the top node and follows the pointers. Index is a table access strategy that increases speed of
searching.
Types of index:-

 Primary index(simple index) :-one column

Create index emp1 on employee(eno)

 Secondary index(composite index) :-many columns

Create index emp1 on employee(eno,ename)

 Unique index:-Indexes that do not allow duplicate values for indexed column.

Create unique index empu on employee(ename)

 Duplicate index:- Indexes that allow duplicate values for indexed column

2. Create a table employee with fields: empid, Empname, Address,contact,salary,deptno


Query:-
create table emply099(empidint,Empnamevarchar(20), Address varchar(20),contact
int,salaryint,deptnoint)
Screenshot:-

Desc emply099

Pratik Nag Page 150 Eno. 09990302012


Database Management System Practical File

3. Insert 6 values in employee table as follows:


Empid Empname Address Contact Salary Deptno
1 Arnav Tilak Nagar 32145698 10000 10
2 Vimal Janakpuri 78965412 12000 10
3 Rahul Dwarka 98764123 20000 20
4 Jatin Rohini 58965416 18000 30
5 Mukul Pitampura 25874134 20000 30
6 Parul Mayapuri 98564718 17000 10
Query:-
select * from emply099;
Screenshot:-

select * from emply099;

4. Create an unique index on the above table using empname.


Query:-
create unique index empl on emply099(empname)
Screenshot:-

create unique index empl on emply099(empname)

Pratik Nag Page 151 Eno. 09990302012


Database Management System Practical File

5. Create an index on the above table using salary and deptno.


Query:-
create unique index emp on emply099(salary,deptno)
Screenshot:-

create unique index emp on emply099(salary,deptno)

6. View all the indices on the server.


Query:-
select * from user_indexes
Screenshot:-

Pratik Nag Page 152 Eno. 09990302012


Database Management System Practical File

7. View all the constraints on the server.


Query:-
select * from user_constraints
Screenshot:-

8. View all the tables on the server.


Query:-
select * from tabs
Screenshot:-

Pratik Nag Page 153 Eno. 09990302012


Database Management System Practical File

9. View the current date of the server.


Query:-
Select sysdate from dual
Screenshot:-

10. Drop the index containing empname.


Query:-
drop index empl
Screenshot:-

Pratik Nag Page 154 Eno. 09990302012


Database Management System Practical File

Assignment 14

DCL
1. What is a DCL command explains in detail?
Ans.Data Control Language (DCLDCL Commands are used to enforce database security in a multiple
user database environment. Only Database Administrator's or owner's of the database object can
provide/remove privileges on a database object. Two types of DCL commands are:
 GRANT - gives user's access privileges to database
 REVOKE - withdraw access privileges given with the GRANT command

SQL GRANT Command

SQL GRANT is a command used to provide access or privileges on the database objects to the users.

The Syntax for the GRANT command is:

GRANT privilege_name
ON object_name
TO {user_name |PUBLIC }

 privilege_name is the access right or privilege granted to the user. Some of the access rights are
ALL, EXECUTE, and SELECT,update

 object_name is the name of an database object like TABLE, VIEW, STORED PROC and
SEQUENCE.
 user_name is the name of the user to whom an access right is being granted.
Eg:-create user username identified by password

 PUBLIC is used to grant access rights to all users.

For Example: GRANT SELECT ON employee TO user1;

SQL REVOKE Command:

The REVOKE command removes user access rights or privileges to the database objects.

The Syntax for the REVOKE command is:

REVOKE privilege_name
ON object_name
FROM {user_name |PUBLIC |role_name}

Pratik Nag Page 155 Eno. 09990302012


Database Management System Practical File

2. Create a table student_name with the following fields:-Rollno,name,course,date of birth and subject.
Query:-
create table student_rakesh(rollnoint primary key,namevarchar(20),course varchar(20),dob
date,ccodeint)
Query:-
select * from student_pratik
Screenshot:-

select * from student_pratik

3. Grant the priviledge of select and update to a user.


Query:-
grant select
on
student_pratik
to
bca2
Screenshot:-

grant select
on
student_pratik
to
bca2

Pratik Nag Page 156 Eno. 09990302012


Database Management System Practical File

Query:-
grant update
on
student_pratik
to
bca2
Screenshot:-

grant update
on
student_pratik
to
bca2

4. Take away the give priviledge from the user by using the revoke command.
Query:-
revoke select
on
student_pratik
from
bca2
Screenshot:-

revoke select
on
student_pratik
from
bca2

Pratik Nag Page 157 Eno. 09990302012


Database Management System Practical File

Query:-

revoke update
on
student_pratik
from
bca2

Screenshot:-

revoke update
on
student_pratik
from
bca2

Pratik Nag Page 158 Eno. 09990302012


Database Management System Practical File

Assignment 15

TCL
1. What are the different TCL command and explain each of them.
Ans. Transaction Control(TCL) statements are used to manage the changes made by DML
statements. It allows statements to be grouped together into logical transactions.

o COMMIT - save work done


o SAVEPOINT - identify a point in a transaction to which you can later roll back
o ROLLBACK - restore database to original since the last COMMIT

A transaction is a sequence of SQL statements that oracle treats as single unit.

COMMIT

Eg:-delete from customer where state=’Delhi’; commit;

Deletes record permanently.

ROLLBACK: - If we want to undo the delete operation then by issuing rollback:-

Delete from customer where state =’Delhi’; ROLLBACK;

SAVEPOINT: - If we want to rollback to a certain point, a save point may be issued

Save point d1;

Delete from customer where state=’Delhi’

Save point d2

Delete from customer where state=’Kerala’;

Rollback to d2;

Commit;

This will undo the second delete operation.

Pratik Nag Page 159 Eno. 09990302012


Database Management System Practical File

2. Use any table that you have created and delete a record from that table. Issue a save point after the
delete operation.
Query:-
select * from empl099
Screenshot:-

select * from empl099

Query:-
delete from empl099 where dno=10;
save point d1
Screenshot:-

delete from empl099 where dno=10;


save point d1

Pratik Nag Page 160 Eno. 09990302012


Database Management System Practical File

3. Delete two records from a table where after first record deletion use a savepoint and after the second
record deletion use the rollback to the savepoint.
Query:-
delete from empl099 where dno=20;
save point d2
Screenshot:-

delete from empl099 where dno=20;


save point d2

4. Perform a transaction and used a commit operation.


Query:-
delete from empl099 where dno=30;
save point d2
rollback to d1
commit
Screenshot:-

delete from empl099 where dno=30;


save point d2
rollback to d1
commit

Pratik Nag Page 161 Eno. 09990302012

You might also like