DBMS Full File
DBMS Full File
Solution:
SQL was developed by IBM in 1970s for use in System R, it is an ISO and ANSI standard.
Structured Query Language (SQL) is a language that provides an interface to relational database
systems.
SQL is a computer language for storing, manipulating and retrieving data stored in relational database.
All relational database management systems like MySQL, Oracle, Sybase, Informix, Postgres and SQL.
Server use SQL as standard database language.
Features of SQL
SQL can be used by a range of users, including those with little or no programming experience.
It is a non procedural language.
It reduces the amount of time for creating and maintaining database systems.
It is English like language.
Solution:
DATE()
The date is displayed in YYYY-MM-DD format. This format is default for the DATE data type. The
DATE data type can store values from 0001-01-01 through 9999-12-31.
CHAR(size)
CHAR is a fixed length string data type, so any remaining space in the field is padded with blanks.
CHAR takes up 1 byte per character. So, a CHAR(100) field (or variable) takes up 100 bytes in disk,
regardless of the string it holds.
VARCHAR(size)
VARCHAR takes up 1 byte length data type. So it holds only the characters you assign to it.
VARCHAR takes up 1 byte per character, +2 bytes to hold length information. For example, if you set a
VARCHAR(100) data type=’Jen’, then it would take up 3 bytes (for J, E and N) plus 2 bytes, or 5 bytes
in all.
INTEGER
A 32-bit signed integer value. The range of INTEGER is -2147483648 to 2147483647. The int data type
is the primary integer data type in SQL server. The bigint data type is intended for use when integer
values might exceed the range that is supported by the int data type.
NUMBER(p,s)
Number allows a decimal component whereas integer doesn’t. If you try to store 3.43 in an integer, it
will just store 3. Number allows for much larger values than integer does. p is a precision value; s is a
scale value. For e.g, number(6,2) is a number that has 4 digits before the decimal and 2 digits after the
decimal.
Q3. Create table student with fields: Roll No. , Name, Course and Date of Birth and display its structure.
Solution:
Create table stud_data_095 (rollno number(10), name varchar(20), course varchar(30), dob date);
Desc stud_data_095;
Q4. Modify the structure of student table as follows(display structure after every modification):
Solution:
Desc stud_data_095;
Desc stud_data_095;
Solution:
Solution:
Or
Q5. Create table Employee with fields: EmpID, EmpName, Designation and Date of Joining
Solution:
Create table employee095 (eid number(10), ename varchar(20), designation varchar(30), doj date);
Desc employee095;
Q6. Modify the structure of Employee table as follows (display structure after every modification):
Solution:
Solution:
Desc employee095;
Solution:
Alter table employee095 drop(designation, doj);
Desc employee095;
ASSIGNMENT 2
Q1. Create table customer with fields: CID, Fname, Lname, City, State, pin.
Solution:
Create table customer_095 (CID number(10), Fname varchar(10), Lname varchar(10), City varchar(10),
State varchar(10), pin number(8));
Desc customer_095;
Q2. Insert 5 rows each using all the three , methods (All fields, Selected Fields and User interactive).
Solution:
1. All fields
Insert into customer_095 values ( 98, 'Neha', 'Sharma', 'Faridabad', 'Haryana', 121002);
2. Selected fields
Insert into customer_095 (CID, Fname, Lname, City, State)values ( 100, 'Rajesh', 'Kapoor', 'Bangalore',
'Karnataka');
3. User interactive
Insert into customer_095 values (&CID, '&Fname', '&Lname', '&City', '&State', '&pin');
Solution:
Solution:
Solution:
Solution:
Q7. Display fname, lname of customers concatenated with state and name it as “Customer and their
Location”.
Solution:
Select Fname||' '||Lname||' '||State as "Customer and their location" from customer_095;
Q8. Display the records of those customers whose pin is not entered.
Solution:
Solution:
Solution:
Solution:
Solution:
Solution:
Solution:
Q15. Retrieve all rows where CID is between 98 and 100(both numbers included).
Solution:
Solution:
Q17. Retrieve all rows where first name contains the word’RAJ’.
Solution:
Solution:
Solution:
Solution:
Solution:
Solution:
Solution:
Solution:
Solution:
Create table emp_info_095 (empid number(10), fname varchar(20), lname varchar(20), bdate date,
gender varchar(8), salary number(8,2), dept_no number(2), address varchar(20));
Solution:
Solution:
Solution:
1. ASCENDING
2. DESCENDING
Solution:
Solution:
Solution:
Q8. List name of those employees whose bdate is before 2 Nov. 1996.
Solution:
Solution:
Solution:
Solution:
Solution:
Solution:
Solution:
Solution:
Select * from emp_info_095 where salary<>20000;
ASSIGNMENT 4
Q1. Create table employee____ with following attributes:
Solution:
Create table emp095 (eno number(10), ename varchar(20) not null, salary number(10) not null,
commission number(10), job varchar(20), dno number(5));
Desc emp095;
Solution:
Solution:
Solution:
Solution:
Solution:
Solution:
Solution:
Solution:
Solution:
Q12. Find the salary of lowest paid employee for each department.
Solution:
Solution:
Q14. Find out the difference between highest and lowest salaries for each department.
Solution:
Solution:
a) Create table course with fields: Coursecode (primary key), Coursename (not null), Section
b) Create table student with following attributes Rollno, First name, Last name, Address(unique),
Coursecode(foreign key)
SOLUTION:
a) Create table course_095(coursecode number(10), cname varchar(10) not null, section varchar(10), constraint
codepk primary key(coursecode));
Desc course_095;
b) create table student_095(rollno number(10), fname varchar(10), lname varchar(10), address varchar(20),
coursecode number(10), constraint addch unique(address), constraint cdfk foreign key(coursecode)
References course_095(coursecode));
Desc student_095;
Add the following constraints to the above tables:
SOLUTION:
Q2.
Person table:
P_id Firstname Lastname Address city
Orders table:
o_id orderno p_id
1 77895 3
2 44678 3
3 22456 2
4 24562 1
CREATE PERSON095:
Create table person095 (pid number(5), fname varchar(10) not null, lname varchar(10), address varchar(15),
city varchar(15), constraint pid_pk primary key(pid));
desc person095;
CREATE ORDER_095:
create table order_095(oid number(5), orderno number(10) not null, pid number(5), constraint oid__pk
primary key(oid), constraint orderno_uk unique(orderno), constraint pid_fk foreign key(pid) references
person095(pid));
desc order_095;
SOLUTION:
1. alter table person095 modify City default 'Delhi';
6. We cannot delete a record from the table in which a primary key is present which is referred by a foreign
key of another table.
So, we cannot delete a record from person table because PID is a primary key in person table which is
referred by a foreign key PID in order table.
ASSIGNMENT 6
Q1. Give Syntax and Queries to demonstrate the use of following Numeric functions in System Defined
Table:
1. Absolute
2. Ceil
3. Floor
4. Round
5. Mod
6. Power
7. Tan
8. Cos
9. Sin
10. Log
11. Trunc
12. Greatest
13. Least
14. Sqrt
SOLUTION:
1. Select abs(-18.7) from dual;
2. Select ceil(18.3) from dual;
SOLUTION:
Q4. 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
1. Convert the name of employees into uppercase
2. Capitalize the first letter of the designation of Employees
3. Convert the city column into lowercase letters
4. Trim zeroes from salary column
5. Pad the salary to 10 digits using Zeroes on right side
6. Remove 1 from eid column
7. Prefix 2 to eid column
8. Find the location of word “el” in city column
9. Find the first two digits of city column
10. Find the length of designation column
SOLUTION:
Create table employee_sonam095 (eid number(10),ename varchar(15),designation varchar(15),salary
number(10),city varchar(15));
Desc employee_sonam095;
insert into employee_sonam095 values(101,'Rajiv','Manager',10000,'Delhi');
insert into employee_sonam095 values(102,'Sita','Clerk',5000,'Mumbai');
insert into employee_sonam095 values(103,'Ram','Manager',10000,'Chennai');
insert into employee_sonam095 values(104,'Ramesh','Assistant',8000,'Hyderabad');
insert into employee_sonam095 values(105,'Shyam','Clerk',5000,'Delhi');
SOLUTION:
Create table department_sonam095(dno number(5), dname varchar(15), dlocation varchar(15), constraint dnopk
primary key(dno), constraint dname_uk unique(dname));
Desc department_sonam095;
Q2. Create table Employee with fields:
SOLUTION:
create table employee__sonam095(eno number(5), ename varchar(20) not null, salary number(10), Designation
varchar(20), Dno number(5), constraint eno_pk primary key(eno), constraint dno_fk foreign key(dno) references
department_sonam095(dno));
Desc employee__sonam095;
Q3. Insert 5 records in both tables
Department
10 Sales Noida
20 Finance Gurgaon
30 HR Delhi
40 Marketing Delhi
50 IT Gurgaon
Employee
SOLUTION:
DEPARTMENT TABLE:
Q4. Display the details of the employee who draws maximum salary.
SOLUTION:
select * from employee__sonam095 where salary=(select max(salary)from employee__sonam095);
Q5. Display the details of those employees whose salary is greater than the average salary in the organization.
SOLUTION:
Q6. Display the details of the employee whose Department location is Noida.
SOLUTION:
Select * from employee__sonam095 where dno =(select dno from department_sonam095 where dlocation='Noida');
SOLUTION:
SOLUTION:
update employee__sonam095 set salary=salary+1000 where designation='Manager';
SOLUTION:
SOLUTION:
alter table department_sonam095 drop column dlocation;
ASSIGNMENT 8
1. Create a table employee with following attributes eno(Primary Key) ,ename ,ecity, salary, deptno.
SOLUTION:
Create table employee__095(eno number(10) primary key,ename char(20),ecity char(10),salary
number(30),deptno number(20));
Desc employee__095;
2. Insert 5 records.
SOLUTION:
Insert into employee__095 values(101,'John','Jaipur',20000,10);
Insert into employee__095 values(102,'Derek','Dehradun',50000,20);
Insert into employee__095 values(103,'Sam','Shillong',12000,10);
Insert into employee__095 values(104,'Ashley','Ahemdabad',23000,20);
Insert into employee__095 values(105,'Ash','Amritsar',12000,20);
4. In the above view update the ecity to ‘Delhi’ where ename is ‘John’.
SOLUTION:
Update v1 set ecity='Delhi' where ename='John';
SOLUTION:
Insert into v1 values('Amit','Hyderabad');
If the table contains the primary key then we cannot insert values through a view.
6. Create view containing ename,city,deptno and salary and update the view by increasing the salary
of all employees of department no 10 by Rs.1000.
SOLUTION:
Create view v2 as select ename,ecity,deptno,salary from employee__095;
SOLUTION:
Create view v_3 as select * from employee__095 where deptno=10;
SOLUTION:
Create view v4 as select max(salary) as maximum, min(salary) as minimum from employee__095;
SOLUTION:
Drop view v4;
ASSIGNMENT 9
Q1. Give Syntax and Queries to demonstrate the use of following Date functions in System Defined Table
a. Add_months(d,n)
b. Last_day(d)
c. Months between(d2,d1)
d. Next_day(d,day)
SOLUTION:
OR
a. Increase End date by 3 months and name the column as new end date
b. Find the last working day of every end date
c. Calculate months between start date and end date.
d. Find the next working day of every start date.
SOLUTION:
Desc task095;
Insert into task095 values('Planning','01-april-1995','23-april-1995');
Insert into task095 values('Analysis','24-april-1995','14-may-1995');
Insert into task095 values('Design','15-may-1995','30-may-1995');
Insert into task095 values('Coding','01-june-1995','30-june-1995');
Insert into task095 values('Testing','01-july-1995','02-august-1995');
Select * from task095;
a. To_Char()
b. To_Date()
c. To_number()
SOLUTION:
20 Research Dallas
30 Sales Chicago
40 Operation Boston
50 Payroll Dallas
SOLUTION:
Create table department_sonam(dno number(10) primary key, dname varchar(15), dloc varchar(15));
Desc department_sonam;
Q2. Create the Employee table with fields: Empno(Primary Key), Ename, Salary, Commission,
Deptno(Foreign Key),Mgrid
Insert the following values
75 Jones 20000 20
76 Martin 30000 30 75
78 Ford 10000 20
SOLUTION:
Create table employee_sonam(empno number(10) primary key, ename varchar(15), salary number(10),
commission number(10), deptno number(10), mgrid number(10), foreign key(deptno) References
department_sonam (dno));
Desc employee_sonam;
Q3. Create the SalGrade table with fields: Grade, Lowsal, Highsal
B 20000 29000
C 30000 50000
SOLUTION:
SOLUTION:
SOLUTION:
Q6. Display Empno., ename,Grade and salary that should be greater than lowsal and less than highsal
SOLUTION:
SOLUTION:
Q8. Display Empno, Ename, Dname ,Dloc of all the employees using outer join
SOLUTION:
SOLUTION:
Q10. Retrieve employee no, employee name of all the employees along with their manager no and
manager name if any
SOLUTION:
SOLUTION:
Q2. Create table student with following fields: RollNo(PK), Sname, DOB(not Null), Coursecode(FK),
Year of admission
SOLUTION:
Create table student095_sonam(rollno number(10) primary key,sname varchar(10),dob date not null,
coursecode number(10) ,year_of_addmission number(10),foreign key (coursecode) references
course095(ccode));
desc student095_sonam;
SOLUTION:
SOLUTION:
Q5. Find the roll no, Name, course name of every student
SOLUTION:
Q6. Find the details of the students who are in BBACAM course
SOLUTION:
Q7. Find the list of students who have taken admission in the same year
SOLUTION:
Select * from student095_sonam order by year_of_addmission;
Q8. Find the number of students who have taken admission year wise
SOLUTION:
Q9. Find the details of the students who are in the course in which student with roll no 101 studies
SOLUTION:
Select * from student095_sonam where coursecode=(select coursecode from student095_sonam where
rollno=101);
Q10. Find the details of the students who have paid the fees for the costliest course
SOLUTION:
Select * from student095_sonam where coursecode=(select ccode from course095 where fee=(select
max(fee) from course095));