DBMS - Reference for Placement
DBMS - Reference for Placement
1. Create table student with required attributes which should get only mbbs and btech
as its degree.
create table student(rno int, name varchar(20), degree varchar(20), primary key(rno,name),
check( degree in(‘mbbs’,’btech’)));
2. Write a SQL query to Create a foreign key and primary key for a table
create table student(rno int primary key, name varchar(20), degree varchar(20))
create table studentacademic(rno int, name varchar(20), cgpa float, foreign key(rno)
references student);
//Here rno of studentacademic table act as foreign key and rno of student table act as
primary key
3. Display the names of students who have 4 characters
select * from student where name like ‘_ _ _ _ ‘ ;
4. Display the names of the students starting with p
select * from student where name like ‘p%’;
5. Display the names of the students whose name contains a characters ‘ya’
select * from student where name like ‘%ya%’;
6. To display the student id and student name of the student whose name has letters 'va'
from the table student
select studid, name from student where name like ‘%ev%’;
7. Display the total salary of the employee who belongs to cse department
//Consider a table employee contains attributes eno, ename, gender, department and salary
eno ename gender department salary
1 anbu male ece 25000
2 rajesh male cse 22000
3 abi female cse 40000
4 anand male ece 30000
5 keerthana female eee 18000
Explanation:
In the above query, emp1 and emp2 are the alias of the table employee.(i.e) both tables
emp1 and emp2 contains the same data which is in employee table
First, all the salaries in table emp2 is compared with the first salary of emp1 table. Count is
incremented if it is greater. If that count matches with n-1, then the all the details in row 1
of emp1 is displayed.
If count doesn’t match, all the salaries in table emp2 is compared with the second salary of
emp1 table. Count is incremented if it is greater. If that count matches with n-1, then the all
the details in row 2 of emp1 is displayed and so on.
Example:
Consider I want to find the 4th largest salary of the employee:
emp2
Salary
25000 emp1
22000
40000 25000
30000
18000
Here each salary in emp2 is compared with 1 st salary of emp1 table (i.e) 25000. When
comparing, two salaries 40000 and 30000 are greater than 25000. Hence the count is 2. But
it is not equal to 4-1.
emp2
Salary emp1
25000
22000
22000
40000
30000
18000
Here each salary in emp2 is compared with 2nd salary of emp1 table(i.e) 22000. When
comparing, three salaries 25000, 40000 and 30000 are greater than 22000. Hence the count
is 3. It is equal to 4-1.
Since it is equal, the value in emp1 is the 4th largest salary. Hence the entire details of the
employee getting salary 22000 is displayed.
27. Create a query that returns only two columns – called sid and name – ordered by
name in descending order.
select sid,name from student order by name desc;
28. You will need to concatenate sid and name to display them in one column.(Use concat
operation)
select concat(sid,name) as studdetails from student;
29. Create a table employee with required attributes such that salary of one of the
employee is made as null. Create a view for that employee table such that your view
should not contain that null value.
create view eview as select eno,ename,salary from employee where salary is not
null with check option;
30. Add one column cgpa to an existing table student.
Alter table student add cgpa float;
Fill the detail of cgpa to any of the student.
update student set cgpa =8.4 where sid=3
31. Difference between DBMS and RDBMS.
DBMS RDBMS
DBMS stores data as a file. RDBMS stores data in the form of tables. It
is the advanced version of DBMS
DBMS supports single user only. It supports multiple users
DBMS does not support the integrity RDBMS supports the integrity constraints
constants. at the schema level.
DBMS does not support Normalization RDBMS can be Normalized
DBMS system, stores data in either a RDBMS uses a tabular structure
navigational or hierarchical form
Examples of DBMS are a file system, XML, Example of RDBMS is MySQL, Oracle,
Windows Registry, etc SQL Server, etc.
Table Relation
Table may contain duplicate values Relation must not contain duplicate values
Table is not necessarily a relation always Relation is a table always
In table, row order & column order are In relation, row and column order is not
mandatory considered
33. Different types of keys with examples.
Refer notes and internet.
34. Application of primary and foreign key.
There may be several foreign keys for a primary key.
Similar to the fact that, there may be several child for a parent in tree.
35. Joins concepts and its types
Joins can be applied on two tables only if there is a common attribute among those two
tables.
Types: self join, inner join(or) natural join, outer join(left,right,full) – See examples
Theta join: It is a join which can be performed even if a two tables doesn’t contain a
common attribute. For example:Refer
https://www.tutorialspoint.com/dbms/database_joins
36. Application of primary and foreign key.
There may be several foreign keys for a primary key.
Similar to the fact that, there may be several child for a parent in tree.
37. Difference between char and varchar2
char(n) : A fixed-length character string with user specified length
Example: An attribute is declared as char(20) and its value is ‘hello’ , remaining 15 spaces
are appended to make it 20 characters long.
varchar2(n) : A variable-length character string with user specified length
Example: An attribute is declared as varchar2(20) and its value is ‘hello’ , no spaces will be
added.Hence Memory is efficiently utilized in varchar2
ii) Cursors:
It is a temporary work area used to store the data retrieved from database.
It process each and every row in it.
It Retrieves one row at a time, from a result set, unlike the SQL commands which
operate on all the rows in the result set at one time.
Cursor is used, when the user needs to update records in a row by row manner, in a
database table
sql%rowcount => Displays number of rows in resultset
sql%found => returns true, if insert, update or delete operation affects 1 or more
rows
o Example: insert into employee values(5,’dhinesh’,’male’,’cse’,50000);
=> It will insert the new row to employee table. Hence sql%found returns
true
o All Updated rows are stored in cursor and it is processed.
o After this operation, if again sql%rowcount is executed, number of rows
will get changed.