Database Systems

You might also like

Download as pdf or txt
Download as pdf or txt
You are on page 1of 46

EXPERIMENT-1

• Create a table STUDENT with appropriate data types and


perform the follow- ing queries. Attributes are Roll number,
student name, date of birth, branch and year of study
AIM: To Create a table STUDENT with appropriate data types and
perform thefollowing queries.
DESCRIPTION:
CREATE:
CREATE TABLE table_name(
column1 datatype,column2 datatype,. ... );

INSERT:
INSERT INTO table_name
VALUES (value1, value2, value3, ... );

SELECT:
SELECT column1, column2, ...
FROM table_name;

UPDATE:
UPDATE table_name
SET column1 = value1, column2 = value2, ...
WHERE condition;

DELETE:
DELETE
FROM
table_nam
eWHERE
condition

; LIKE:
The LIKE operator is used in a WHERE clause to search for a specified
pattern in a
column.
SELECT column1, column2, ...
FROM table_name
WHERE columnN LIKE pattern;

REVERSE:
The REVERSE() function reverses a string and returns the result.
SELECT REVERSE('SQL Tutorial');

SOURE CODE:
• Insert 5 to 10 rows in a table?

• List all the students of all branches

• List student names whose name starts with ‘s ‘


• List student names whose name contains ‘s as third literal

• List student names whose contains two ‘s ‘anywhere in the name

• List students whose branch is NULL

• List students of AIDS & ECE who born after 1980

• List all students in reverse order of their names


• Delete students of any branch whose name starts with ‘s ‘

• Update the branch of ECE students to AIDS

RESULT: EXECUTED SUCESSFULLY


• Display student names padded with * after the name of all students and
before the name of all students
EXPERIMENT-2
• Create the following tables based on the above Schema Diagram
with appropriatedata types and constraints and perform the
following queries.
SAILORS (Sail id,
Sailname, Rating, Age)
RESERVES (Sailid, boatid,
Day)
BOATS (Boatid, Boat-name, Color)

AIM: To Create the following tables based on the above Schema Diagram
with appro-priate data types and constraints and perform the following
queries.
DESCRIPTION:
AND:
The AND operator displays a record if all the conditions separated by AND
are TRUE.
SELECT column1, column2, ...
FROM table_name
WHERE condition1 AND condition2 AND condition3 ...;
OR:
The OR operator displays a record if any of the conditions separated by OR
is TRUE.
SELECT column1, column2, ...
FROM table_name
WHERE condition1 OR condition2 OR condition3 ...;
UNION:
The UNION operator is used to combine the result-set of two or more
SELECT state-ments.
SELECT column_name(s) FROM table1
UNION
SELECT column_name(s) FROM table2;
SELECT DISTINCT:
The SELECT DISTINCT statement is used to return only distinct (different)
values.
SELECT DISTINCT column1, column2, ...
FROM table_name;

SOURCE CODE:

• Insert 5 to 10 rows in all tables


• Find the name of sailors who reserved boat number 108.

• Find the name of sailors who reserved green boat.


• Find the colors of boats reserved by Krish.

• Find the names of sailors who have reserved atleast one boat

• Find the all sailed ofs ailors who have a rating of 10 or have
reserved boated104

• Find the Sailid ‘s of sailors with age over 20 who have not
registered a redboat.
• Find the names of sailors who have reserved a red or green boat.

• Find sailors whose rating is better than some sailor called Krish.

• Find the names of sailors who are older than the oldest sailor
with a rating of 9.5.

RESULT: EXECUTED SUCESSFULLY

EXPERIMENT-3
• Schema Diagram for the rest of the SQL and PLSQL Programs.
Create the fol- lowing tables based on the above Schema Diagram
with appropriate data types and constraints.
EMPLOYEE (Fname, Mname, Lname, SSN, Bdate, Address,
Gender, Salary, SuperSSN,DnO,)
DEPARTMENT (Dnumber, Dname, MgrSSN,
Mgrstartdate) DEPENDENT (ESSN, Dependent_Name,
Gender, Bdate, Relationship)

AIM: To Create the following tables based on the above Schema Diagram
with appropri-ate data types and constraints.
DESCRIPTION:
IN:
The IN command allows you to specify multiple values in a WHERE clause.
SELECT column_name(s)
FROM table_name
WHERE column_name IN (value1, value2, ...);

NOT IN:

The NOT operator displays a record if the condition(s) is NOT TRUE.

SELECT column1, column2, ...


FROM table_name
WHERE NOT condition;
• Insert 5 to 10 rows into all the tables
• Display all employees’ names along with their department names.

• Display all employees’ names along with their dependent details.

• Display name and address of all employees who work for


Research depart-ment.

• List the names of all employees with two or more dependents.


• List the names of employee who have no dependents

• List the names of employees who have at least one dependent.

• List the names of the employees along with names of their


supervisors usingaliases.

• Display name of the department and name of manager for all the
departments.
• Display the name of each employee who has a dependent with
the same firstname and gender as the employee.

RESULT: EXECUTED SUCESSFULLY

EXPERIMENT-4
• Create the following tables based on the above Schema Diagram
with appropri-ate data types and constraints in addition to the
tables in Experiment 2. DEPT_LOCATIONS (Dnumber,
Dloaction)
PROJECT (Pname, Pnumber,
Plocation, Dnum)WORKS_ON
(ESSN, Pno, Hours)

AIM: To Create the following tables based on the above Schema Diagram
with appro-priate data types and constraints and perform the following
queries.
DESCRIPTION:
GROUP BY:
The GROUP BY statement is often used with aggregate
function ( COUNT(), MAX(), MIN(), SUM(), AVG()) to group the result-
set by one or more col- umns.
SELECT
column_n
ame(s)
FROM
table_na
me WHERE
conditio
n
GROUP BY
column_name
(s) ORDER
BY
column_name
(s);
SOURCE
CODE:

• Insert 5 to 10 rows into all the tables.


• Find the names of the employees who work on all the projects
controlled by thedepartment Research.

• List the project number, name and no. Of employees who work
on that projectfor all the projects.

• List the names of all the projects controlled by the


departments department wise.
• Retrieve the names of employees who work on all projects that
prapull workson.

• List the project numbers for projects that involve an employee


either as workeror as a manager of the department that controls the
project.

• List the names of all employees in one department who work


more than 10 hours on one specific project.

• For each project, list the project name and total hours (by all
employees) spent on that project.

• Retrieve the names of all employees who work on every project


• Retrieve the names of all employees who do not work on any project.

RESULT: EXECUTED SUCESSFULLY

EXPERIMENT-5
• Create a view that has project name, controlling department name,
number of employees and total hours worked on the project for
each project with more than one employee working on it.
AIM: To create a view that has project name, controlling department name,
number ofemployees and total hours worked on the project for each project
with more than one employee working on it.
DESCRIPTION:
VIEW:
A view contains rows and columns, just like a real table. The fields
in a view arefields from one or more real tables in the database.
CREATE VIEW
view_name AS
SELECT column1,
column2, ...
FROM table_name
WHERE condition;
SOURCE CODE:

• List the projects that are controlled by one department from this
view.

• List the managers of the controlling departments for all the projects.

• Demonstrate one update operation on this view.


• UPDATION OF VIEW CAN’T BE DONE IF THAT VIEW
CONTAINS ANYPRIMARY OR FOREIGN KEYS.

• List the Location of the controlling departments for all the projects.

• Retrieve the data from the view.


RESULT: EXECUTED SUCESSFULLY

EXPERIMENT-6
• Create a view emp from employee such that it contains only
emp_no, emp_name and department.
AIM: To create a view emp from employee such that
it contains onlyemp_noemp_name and department.
SOURCE CODE:

RESULT: EXECUTED SUCESSFULLY

EXPERIMENT-7
• Create a view dept from department with only dept_no
and location. AIM: To create a view dept from department
with only dept_no and location. SOURCE CODE:
RESULT: EXECUTED SUCESSFULLY

EXPERIMENT-8
• Create a view that contains the details of employees who are
managers only AIM: To create a view that contains the details of
employees who are managers only SOURCE CODE:

RESULT: EXECUTED SUCESSFULLY

EXPERIMENT-9
• Write a procedure to check whether the given number is
Armstrong or not. AIM: To write a procedure to check whether the
given number is Armstrong or not. DESCRIPTION:
DELIMITER:
Delimiters can be used when you need to define the stored procedures, function as well as
to create triggers.The default delimiter is semicolon. You can change the delimiters to
create procedures and so on. However, but if you are considering multiple statements, then
you need to use different delimiters like $$ or //.

SOURCE CODE:

RESULT: EXECUTED SUCESSFULLY

EXPERIMENT-10
• Write a procedure which accept the account number of a
customer and re-trieve the balance
AIM: To write a procedure which accept the account number of a
customer and re-trieve the balance.
SOURCE CODE:
RESULT: EXECUTED SUCESSFULLY

EXPERIMENT-11
• Write a procedure which accepts the student number and
displays the depart-ment in which he belongs to.
AIM: To write a procedure which accepts the student number and
displays the depart-ment in which he belongs to.
SOURCE CODE:
RESULT: EXECUTED SUCESSFULLY

EXPERIMENT-12
• Create a cursor to modify the salary of all employees belonging to
'Research' de-partment by 150%.
AIM: To create a cursor to modify the salary of all employees belonging to
'Research'department by 150%.
DESCRIPTION:
CURSOR:
Cursor is a Temporary Memory or Temporary Work Station. It is
Allocated by Database Server at the Time of Performing DML (Data
Manipulation Language) operations on Table by User. Cursors are used
to store Database Tables. There are 2 types of Cursors: Implicit Cursors,
and Explicit Cursors. These are explained as following below.
Implicit Cursors:
Implicit Cursors are also known as Default Cursors of SQL SERVER.
These Cursorsare allocated by SQL SERVER when the user performs
DML operations.
Explicit Cursors:
Explicit Cursors are Created by Users whenever the user requires them.
Explicit Cur-sors are used for Fetching data from Table in Row-By-
Row Manner.
Declare Cursor Object.

Syntax: DECLARE cursor _ name CURSOR FOR SELECT * FROM table


_ name
SOURCE CODE:

RESULT: EXECUTED SUCESSFULLY

EXPERIMENT-13
• Consider the college database. Retrieve all students who have
registered for aspecific course and store their details into another
table using Cursors.
AIM: To retrieve all students who have registered for a specific course
and store theirdetails into another table using Cursors.
SOUREC CODE:

RESULT: EXECUTED SUCESSFULLY

EXPERIMENT-14
• Write an update trigger on Account table. The system should
keep track of therecords that are being updated.
AIM: To update trigger on Account table. The system should keep track
of the records that are being updated.
DESCRIPTION:
TRIGGER:
Trigger: A trigger is a stored procedure in database which automatically
invokes when- ever a special event in the database occurs. For example, a
trigger can be invoked when a row is inserted into a specified table or
when certain table columns are being updated. Syntax:
create trigger
[trigger
_name][before
| after]
{insert |
update |
delete}on
[table
_name]

[for
each
row]
[trig
ger
_bo
dy]
SO
UR
CE
CO
DE:

RESULT: EXECUTED SUCESSFULLY

• Create NOSQL database for a sample application and perform


CRUD opera-tions.
To install MongoDB:

sudo apt install mongodb


To see databases:
su show
dbs sudo mongo
To create:
use database_name
To insert:
db.std.insert({sno:14,name:”Rishi”, Rishi
branch:”AI&DS”})db.std.find()
({(“id”:
objected)”
sno”:”14”})
db.sd.find().prett
y()
{
id =”14”
name
=
“Rishi

branch
=
“AI&
DS”
}

To insert many collections:


db.std.insertmany({sno:”04”, sname:”Sankar”, branch:”AI&DS”},
{id=”104”, sno:”300”,sname:”Rahul”})
To update:
db.std.update({sno;”500”},{$set;{snam
e:”some”}})To delete:
db.std.deleteOne({sno:”04”})

To Drop database:
{dropped: “database_name”, ”ok”,:1}
RESULT: EXECUTED SUCESSFULLY

V
I
V
A

V
O
I
C
E
• Define Database.
A prearranged collection of figures known as data is called database.
• What is DBMS?
Database Management Systems (DBMS) are applications designed
especially whichenable user interaction with other applications.
• What are the various kinds of interactions catered by DBMS?

The various kind of interactions


catered by DBMS are:Data definition
U
p
d
a
t
e

R
e
t
r
i
e
v
a
l
A
d
m
i
n
i
s
t
r
a
t
i
o
n
• Segregate database technology’s development.

The development of database


technology is divided into:Structure or
data model

Navigat
ional
model
SQL/
relation
al
model
• Who proposed the relational model?
Edgar F. Codd proposed the relational model in 1970.
• What are the features of Database language?
A database language may also incorporate features like:
DBMS-specific Configuration and management of storage engine
Computations to modification of query results by computations, like
summing, count- ing, averaging, grouping, sorting and cross-referencing
Constraint enforcement Appli-cation programming Interface
• What do database languages do?

As special-purpose
languages, they have:
Data definition
language

Data
manipulation
language
Query
language
• Define database model.

A data model determining fundamentally how data can be stored,


manipulated andOrganised and the structure of the database logically is
called database model.
• What is SQL?
Structured Query Language (SQL) being ANSI standard language
updates databaseand commands for accessing.
• Enlist the various relationships of database.
The various of database are:

One-to-one: Single table having drawn relationship with another


table having simi-lar kind

of columns.

One-to-many: Two tables having primary and foreign key relation.

Many-to-many: Junction table having many tables related to many


tables.
• Define Normalization.
Organized data void of inconsistent dependency and redundancy within
a database iscalled normalization.
• Enlist the advantages of normalizing database.

Advantages of
normalizing database
are:No duplicate entries

Saves storage space

Boasts the query performances.


• Define Denormalization.
Boosting up database performance, adding of redundant data which in turn
helps rid ofComplex data is called denormalization.
• Define DDL and DML.
Managing properties and attributes of database is called Data
Definition Language(DDL).
Manipulating data in a database such as inserting, updating, deleting is
defined as DataManipulation Language. (DML)
• Enlist some commands of DDL.

They are:
CREATE:

Create is used in the CREATE TABLE statement. Syntax is:


CREATE TABLE [column name] ( [column definitions] ) [ table
parameters]
ALTER:
It helps in modification of an existing object of database. Its
syntax is:ALTER object type object name parameters.
DROP:
It destroys an existing database, index, table or
view. Its syntax is:DROP object type object name.
• Define Union All operator and Union.
Full recordings of two tables is
Union All operator. A distinct
recording of two tables is Union.
• Define cursor.

A database object which helps in manipulating data row by row


representing a resultset is called cursor.
• Enlist the cursor types.
They are:

Dynamic: it reflects changes while scrolling.

Static: doesn’t reflect changes while scrolling and works


on recording of snapshot. Keyset: data modification without
reflection of new data is seen.
• Enlist the types of cursors.
The types of cursor are:

Implicit cursor: Declared automatically as soon as the execution of


SQL takes placewithout the awareness of the user.

Explicit cursor: Defined by PL/ SQL which handles query in more


than one row.
• Define sub-query.
A query contained by a query is called Sub-query.
• Why is group-clause used?
Group-clause uses aggregate values to be derived by collecting similar data.
• Compare Non-clustered and clustered index
Both having B-tree structure, non-clustered index has data pointers
enabling one table many non-clustered indexes while clustered index is
distinct for every table.

• Define Aggregate functions.


Functions which operate against a collection of values and returning
single value iscalled aggregate functions
• Define Scalar functions.
Scalar function is depended on the argument given and returns sole value.
• What restrictions can you apply when you are creating views?
Restrictions that are applied are:

Only the current database can have views.

You are not liable to change any computed


value in any particular view. Integrity constants decide
the functionality of INSERT and DELETE. Full-text
index definitions cannot be applied.

Temporary views
cannot be created.
Temporary tables cannot
contain views.

No association with DEFAULT definitions.

Triggers such as INSTEAD OF is associated with views.


• Define “correlated subqueries”.
A „correlated subquery‟ is a sort of sub query but correlated subquery
is reliant on an- other query for a value that is returned. In case of
execution, the sub query is executed first and then the correlated query.
• Define Data Warehousing.
Storage and access of data from the central location in order to take
some strategic de-cision is called Data Warehousing. Enterprise
management is used for managing the information whose framework is
known as Data Warehousing.
• Define Join and enlist its types.
Joins help in explaining the relation between different tables. They also
enable you toselect data with relation to data in another table.
The various types are:

INNER JOINs: Blank rows are left in the middle while more
than equal to two ta-bles are joined.

OUTER JOINs: Divided into Left Outer Join and Right Outer
Join. Blank rows are left at the specified side by joining tables in other
side.

Other joins are CROSS JOINs, NATURAL JOINs, EQUI JOIN


and NON-EQUIJOIN.
• What is the role of view in DBMS?
Ans. A database view is a subset of a database and is based on a query
that runs on one or more database tables. Database views are saved in
the database as named que-ries and can be used to save frequently
used, complex queries.
• What are types of Views in DBMS?
Ans. There are two types of database views: dynamic views and static
views. Dynamic views can contain data from one or two tables and
automatically include all of the col- umns from the specified table or
tables.
• What are the advantages of Views?
•Views can represent a subset of the data contained in a table. ...
•Views can join and simplify multiple tables into a single virtual table.
•Views can act as aggregated tables, where the database engine aggregates
data (sum,average, etc.) ...
•Views can hide the complexity of data.

• Why update is not possible on Complex views?


Ans. Complex View Contains Multiple Tables Data hence it is not
possible to per-form DML command on this view.
• Can we create a view based on other views?
Ans. A view can refer to tables or views in other databases by
qualifying the table orview name with the appropriate database name.
• What is meant by Delimiter?
Ans. Delimiters are used when we need to define the stored procedures
as well as tocreate triggers.
• What is a Procedure?
Ans. A procedure is a subroutine (like a subprogram) in a regular
scripting language, stored in a database.
• What is the process to create a procedure?

• The name of the procedure must be specified after the Create Procedure
keyword
• After the name of the procedure, the list of parameters must be
specified in the pa-renthesis. The parameter list must be comma-
separated
• The SQL Queries and code must be written between BEGIN and END
keywords

• What is the syntax for while loop?


[begin _label:] WHILE
condition DO
Statement _list
END WHILE [end _label]
• What is meant by Armstrong number?
Ans. An Armstrong number is a number such that the sum! of its
digits raised to thethird power is equal to the number! itself.
• How the statements written while using procedures in MYSQL?
Ans. The MYSQL Queries and code must be written between BEGIN
and END key-words while using Procedures.
• What is the syntax to call procedure?
Ans. “call procedure _name(value)”
• How do we retrieve the data using procedure?
Ans. A stored procedure does not have a return value so we use “call”
function.
• How many modes are available to pass parameters?
Ans. IN, OUT, INOUT
• What is the task of IN mode?
Ans. If you want to pass the value to the value to the subprogram then
like a constant you can assign a value by using IN mode.
• What do you about stored functions?
Ans. You can call it also User Function or User Defined Function.
Simply it is a set of PL/SQL statements that you can call by name. They
are stored and compiled in a data- base. This function will return a value
to the environment in which it is called.
• What is the use of OUT mode?
Ans. Once we expect return values to the called subprograms, you can use
the OUTmethod to initialize the assigned variable.
• Tell me the use of INOUT mode?
Ans. It will help pass the values to the subprogram called and return the
caller value.

• What is the common feature between Function and Procedure?


Ans. A Database can be a procedure within a function.
• Tell me how you can recompile stored procedures at runtime?
Ans: At the time of creating stored procedures with the recompile
option, it will be ex-ecuted newly every time. This is very good for high
performances. Every time perfect variables are passed.
• What is the syntax to declare cursors?
Ans.
DECL
ARE
CURS
OR
OPEN
F
E
T
C
H

C
L
O
S
E
• What is the syntax to create cursor?
Ans.
Declare
cursor _name
cursorSelect
statement;
• How to Fetch the cursor?
Ans. FETCH [ NEXT [ FROM]] cursor _name INTO variable _list;
• Why cursor is used in MYSQL?
Ans. In MySQL, a cursor allows row-by-row processing of the result
sets. A cursor is used for the result set and returned from a query. By
using a cursor, you can iterate, or step through the results of a query and
perform certain operations on each row.
• How many types of cursors in MYSQL?
Ans. There are 2 types of Cursors: Implicit Cursors, and Explicit Cursors.
• What is meant by Implicit cursor?
Ans. Implicit Cursors are also known as Default Cursors of SQL
SERVER. TheseCursors are allocated by SQL SERVER when the
user performs DML operations.
• What is meant by Explicit cursor?
Ans. Explicit Cursors are Created by Users whenever the user requires
them. ExplicitCursors are used for Fetching data from Table in Row-By-
Row Manner.
• What are the advantages of cursors?

Ans.
•They are helpful in performing the row-by-row processing and also
row wise valida-tion on each row.
•Better concurrency control can be achieved by using cursors.

•Cursors are faster than while loops.


• How many rows can a cursor hold?
Ans. A cursor can be viewed as a pointer to one row in a set of rows.
The cursor canonly reference one row at a time, but can move to
other rows of the result set as needed.
• What is a Trigger?
Ans. A trigger is a stored procedure in database which automatically
invokes when- ever a special event in the database occurs. For
example, a trigger can be invoked when a row is inserted into a
specified table or when certain table columns are beingupdated.
• What is the syntax for Trigger?
Ans.
create trigger
[trigger
_name][before
| after]
{insert |
update |
delete}on
[table
_name]
[
f
o
r
e
a
c
h

r
o
w
]
[
t
r
i
g
g
e
r
_
b
o
d
y
]
• How many types of Triggers we have?
Ans. There are two types of triggers
• After Triggers (For Triggers)
• Instead Of Triggers
• Explain what is after Trigger?
Ans. Insert, Update, Delete.
• What is the maximum depth level for nested triggers?
Ans. 32 levels.

• What is NoSQL?
NoSQL databases are non-tabular databases which store data
differently than rela- tional databases. The hiring manager may start
your interview question to check yourbasic understanding of data
management. In your answer, you may briefly explain what NoSQL is
and consider sharing some of its unique traits or features
• What are the different NoSQL databases?
There are four different types of NoSQL databases. List all four of
them in your an- swer to show your understanding of the data
management system to the hiring man-ager.
• What is the key value offering in NoSQL?
NoSQL has a few value offerings that differ from other database
management sys- tems. Based on your experience, tell the interviewer
what you feel provides the most value. Try to keep the language as
simple as possible since the interviewer may not be from a technical
background.
• How to increase scalability in NoSQL?
As the company grows, scalability is often a priority. As a team
member, you may search for ways to scale the database and offer
solutions effectively. You can discuss one such solution in your answer
and prove your problem-solving ability to the hiring manager.
• What is a graph database?
As a technical professional, you may work with members outside of
your team. The hiring manager may want to assess your ability to
explain complex terms simply. In your answer, define what a graph
database is and consider talking about its usage.
69: Can NoSQL replace SQL?
A: No, NoSQL cannot replace SQL as certain projects require SQL only.
70: Where is NoSQL used?
A: NoSQL is used by companies that need a distributed data system for
storing and maintaining a large amount of unstructured and structured types
of data.
71: Where is SQL used?

A: SQL is used by administrators who want to retrieve, store and


manipulate data fordifferent purposes. SQL supports only relation-
based databases only.

You might also like