Professional Documents
Culture Documents
DEPARTMENT OF COMPUTER
SCIENCE AND ENGINEERING
DATABASE MANAGEMENT SYSTEM
LABORATORY
(CSX-224)
INDEX:
S.
CONTENT Pages Remarks
No
Basics of DBMS and Database
1. 3-5
Languages.
2
Database Management System 16103081
TOPIC-1
Aim: Study basics of DBMS and Database Languages.
Theory:
DATA - Any row form of facts. Data is the information that has been translated
into a form that is efficient for movement or processing.
Step1 Step2
Data Information Knowledge
TYPES OF FILES:
Sequential file
Direct file
Indexed file
3
Database Management System 16103081
Time Consuming
Data Replication
Security Issue
Concurrency Access
Can’t apply Query
3. Data Control language (DCL): DCL is used for granting and revoking user
access on a database –
DATA TYPES
1. CHARCTER TYPE
CHAR - Fixed length character of length size byte . This should be used
for fixed length data.
4
Database Management System 16103081
2. NUMBER TYPE
NUMBER ( P,S) – Floating point number. P refers to Precision and S
refers to Scale.
NUMBER (P) – Floating point number with a scale zero and precision p.
NUMBER – Floating point number with precision 38.
3. DATE / TIME
DATE – Format –> YYYY-MM-DD
DATETIME – A date and time combination .Format - > YYYY-
MM_DD
HH:MI:SS
5
Database Management System 16103081
TOPIC-2
Aim: Implement DDL Commands.
Software used: Oracle 11g XE
Theory:
1. CREATE: The create table command defines each column of the table
uniquely. Each table column definition is separated from the other by a
comma.
SYNTAX:
Create table <tablename> (attributes and their datatypes)
3. ALTER : By use of alter table command we can modify our existing table
3.1 We can add new columns by using alter table command
SYNTAX:
Alter table <tablename> add (<new column name>
<datatype>(<size>),…)
3.3 We can modify a column i.e. change the datatype of the attribute
using alter command
SYNTAX:
Alter table <tablename> modify (<column name> <new data type>
(<new size>))
6
Database Management System 16103081
Queries:
7
Database Management System 16103081
8
Database Management System 16103081
9
Database Management System 16103081
10
Database Management System 16103081
TOPIC-3
Aim: Implement DML Commands.
Software used: Oracle 11g XE
Theory:
DML commands: Data Manipulation Language is used for managing data within
schema objects. Some commands are:-
1. INSERT: Using insert command we can add values into tables.
SYNTAX:
2. DELETE: Delete command can remove selected rows from the table or all
the rows.
SYNTAX:
Selected rows
Delete from <table_name> where <condition>
3. UPDATE: Update command is used to change the data values in the table
SYNTAX:
4. SELECT: Once data has been inserted into the table the next most logical
operation is to view what has been inserted SELECT verb is used to view this.
SYNTAX:
11
Database Management System 16103081
5. ELIMINATING DUPLICATES:
Select distinct * from <table_name>
Only some columns and eliminating duplicates
Select distinct <column1>, <column2>,… from <table_name>
Queries:
1. Add phone_number, city to STUDENT.
12
Database Management System 16103081
13
Database Management System 16103081
4. Truncate STUDENT1.
14
Database Management System 16103081
15
Database Management System 16103081
TOPIC-4
Aim: To Apply the below mentioned CONSTRAINTS on table using SQL
commands and queries:
NOT NULL, UNIQUE, PRIMARY KEY, FOREIGN KEY.
Also, drop these constraints when used.
Theory: CONSTRAINTS: SQL constraints are used to specify rules for the data
in a table. If there is any violation between the constraint and the data action, the
action is aborted by the constraint. These can be specified when the table is created
or after the table is created.
1. Not Null: This constraint enforces a column to NOT accept NULL values.
16
Database Management System 16103081
Queries:
1. Create a table student having columns- stu_id(number), sname(varchar2),
class(varchar2), dob(date), phone_no(number).
17
Database Management System 16103081
18
Database Management System 16103081
19
Database Management System 16103081
TOPIC-5
Aim: To implement the OPERATORS and AGGREGATE FUNCTIONS in
SQL commands.
Theory:
OPERATORS:
There are logical operators namely, AND, OR and NOT. These operators compare
two conditions at a time to determine whether a row can be selected for the output.
1. OR: For the row to be selected at least one of the conditions must be true.
2. AND: For a row to be selected all the specified conditions must be true.
3. NOT: For a row to be selected the specified condition must be false.
4. Between: This operator selects values within a range. The values can be
numbers, text or dates.
SYNTAX: SELECT column_name(s) FROM table_name where
column_name BETWEEN value1 AND value2.
5. In: This operator allows us to specify multiple values in a WHERE clause.
SYNTAX: SELECT column_name(s) FROM table_name WHERE
column_name IN(value1, value2,…).
6. Like: The LIKE operator is used to search for a specified pattern in a
column.
SYNTAX: SELECT column_name(s) FROM table_name WHERE
column_name LIKE pattern
20
Database Management System 16103081
GROUP BY: The GROUP BY statement is often used with aggregate functions
(COUNT, MAX, MIN, SUM, AVG) to group the result-set by on eor more
columns.
SYNTAX: SELECT column_name(s) FROM table_name GROUP BY
column_name(s).
HAVING: The HAVING clause was added to SQL because the WHERE clause
keyword could not be used with aggregate functions.
21
Database Management System 16103081
22
Database Management System 16103081
3. List all columns of EMP table whose dept_no is other than 10.
4. List all employee whose ENAME starts with ‘A’ and ends with ‘N’.
23
Database Management System 16103081
6. List all employee whose SAL lies b/w 1250 and 3000.
24
Database Management System 16103081
25
Database Management System 16103081
26
Database Management System 16103081
27
Database Management System 16103081
28
Database Management System 16103081
16. Find all dept number where total number of employee equal to 4.
29
Database Management System 16103081
30
Database Management System 16103081
19. List all employees who are not working in any department.
20. List the MGR and Salary of lowest paid employee for that Manager,
exclude anyone whose manager is not known.
31
Database Management System 16103081
21. Display name of those employees who get more salary than emp_id=7876.
22. Display name, salary of all employee who earn more than the average
salary.
32
Database Management System 16103081
23. Display name, salary for all employee whose salary equals one of the
salaries in dept no 20.
24. Display name, salary whose salary is greater than all employee of dept no
20.
33
Database Management System 16103081
26. Display deptno of employee of those groups that have more than 2
employees.
34
Database Management System 16103081
27. Find names & id of dept where least Salary is greater than the highest salary
in dept 10.
28. Find all employee who have their manager & dept matching with employee
having emp_id 7499 or 7788.
35
Database Management System 16103081
36
Database Management System 16103081
TOPIC-6
Aim: Perform various JOIN operations on different tables.
Software Used: Oracle 11g XE
Theory:
A JOIN clause is used to combine rows from two or more tables, based on a related
column between them.
1. INNER JOIN: The INNER JOIN keyword selects all rows from both the tables
as long as the condition satisfies. This keyword will create the result-set by
combining all rows from both the tables where the condition satisfies i.e value
of the common field will be same.
Syntax:
SELECT table1.column1,table1.column2,table2.column1,....
FROM table1
INNER JOIN table2
ON table1.matching_column = table2.matching_column;
table1: First table.
table2: Second table
matching_column: Column common to both the tables.
Note: We can also write JOIN instead of INNER JOIN. JOIN is same as INNER
JOIN.
37
Database Management System 16103081
2. LEFT JOIN: This join returns all the rows of the table on the left side of the
join and matching rows for the table on the right side of join. The rows for
which there is no matching row on right side, the result-set will contain null.
LEFT JOIN is also known as LEFT OUTER JOIN.
Syntax:
SELECT table1.column1,table1.column2,table2.column1,....
FROM table1
LEFT JOIN table2
ON table1.matching_column = table2.matching_column;
Note: We can also use LEFT OUTER JOIN instead of LEFT JOIN, both are same.
3. RIGHT JOIN: RIGHT JOIN is similar to LEFT JOIN. This join returns all the
rows of the table on the right side of the join and matching rows for the table on
the left side of join. The rows for which there is no matching row on left side,
the result-set will contain null. RIGHT JOIN is also known as RIGHT OUTER
JOIN.
38
Database Management System 16103081
Syntax:
SELECT table1.column1,table1.column2,table2.column1,....
FROM table1
RIGHT JOIN table2
ON table1.matching_column = table2.matching_column;
table1: First table.
table2: Second table
matching_column: Column common to both the tables.
Note: We can also use RIGHT OUTER JOIN instead of RIGHT JOIN, both are
same.
Syntax:
SELECT table1.column1,table1.column2,table2.column1,....
FROM table1
FULL JOIN table2
ON table1.matching_column = table2.matching_column;
table1: First table.
table2: Second table
matching_column: Column common to both the tables.
39
Database Management System 16103081
Syntax:
SELECT a.coulmn1 , b.column2
FROM table_name a, table_name b
WHERE some_condition;
40
Database Management System 16103081
Syntax:
SELECT table1.column1 , table1.column2, table2.column1...
FROM table1
CROSS JOIN table2;
table1: First table.
table2: Second table
1. STUDENT1(student_id,name,dep_name,phone_no,email)
2. DEPARTMENT(dept_id,dept_name,HOD_name)
1. STUDENT1
Desc student1
41
Database Management System 16103081
QURIES
42
Database Management System 16103081
Queries:
1. Find the inner join of table department and student1
43
Database Management System 16103081
44
Database Management System 16103081
45
Database Management System 16103081
TOPIC-7
Aim: Perform UNION, INTERSECT, MINUS operations on tables.
Software Used: Oracle 11g XE
Theory:
1. UNION OPERATOR
UNION ALL is very similar to UNION, but with one exception: UNION ALL
returns all data from all tables, no matter if it is a duplicate or not. Let’s do the
same operation as in the UNION example and see what we get:
46
Database Management System 16103081
3. MINUS OPERATOR
MINUS is a little bit different. Let’s say we want to see only book titles that are
not also movie titles. We need to “minus” everything from the BOOKS table
that is also in the MOVIES table. The MINUS operator is designed for this type
of task.
SET INTERSECT operator is used to find the common between the results of
two queries. It as follows:
47
Database Management System 16103081
Queries:
48
Database Management System 16103081
49
Database Management System 16103081
50
Database Management System 16103081
TOPIC-8
Aim: Perform GRANT, REVOKE operations on tables.
Software Used: Oracle 11g XE
Theory:
We can GRANT and REVOKE privileges on various database objects in SQL
Server. We'll look at how to grant and revoke privileges on tables in SQL Server.
We can grant users various privileges to tables. These permissions can be any
combination of SELECT, INSERT, UPDATE, DELETE, REFERENCES,
ALTER, or ALL.
Syntax
The syntax for granting privileges on a table in SQL Server is:
System: This includes permissions for creating session, table, etc and all
types of other system privileges.
Object: This includes permissions for any command or query to perform
any operation on the database tables.
GRANT: Used to provide any user access privileges or other priviliges for
the database.
REVOKE: Used to take back permissions from any user.
51
Database Management System 16103081
Queries:
1. Grant Select, Insert, Update to public
52
Database Management System 16103081
TOPIC-9
Aim: Implement Views and Indexing on tables.
Software Used: Oracle 11g XE
Theory:
VIEWS:
A view is simply any SELECT query that has been given a name and saved in the
database. For this reason, a view is sometimes called a named or a stored. To
create a view, you use the SQL
SYNTAX:
CREATE OR REPLACE VIEW <view_name> AS
SELECT <any valid select query>;
The view query itself is saved in the database, but it is not actually run until it is
called with another SELECT statement. For this reason, the view does not take
up any disk space for data storage, and it does not create any redundant copies
of data that is already stored in the tables that it references (which are
sometimes called the base tables of the view).
Although it is not required, many database developers identify views with
names such as v_Customers or Customers_view. This not only avoids name
conflicts with base tables, it helps in reading any query that uses a view.
The keywords OR REPLACE in the syntax shown above are optional.
Although you don’t need to use them the first time that you create a view,
including them will overwrite an older version of the view with your latest one,
without giving you an error message.
The syntax to remove a view from your schema is exactly what you would
expect:
DROP VIEW <view_name>;
INDEXING:
53
Database Management System 16103081
It is a data structure that the database uses to find records within a table more
quickly. Indexes are built on one or more columns of a table; each index maintains
a list of values within that field that are sorted in ascending or descending order.
Rather than sorting records on the field or fields during query execution, the
system can simply access the rows in order of the index.
SYNTAX:
Queries:
1. Create a view.
2. Drop view.
54
Database Management System 16103081
3. Create index.
4. Drop index.
55
Database Management System 16103081
TOPIC-10
EMPLOYEE MANAGEMENT SYSTEM
It maintains the information about the personal and official details of the
employees.
It enables the User to create and store the records of Employees of their
Organization.
56
Database Management System 16103081
We use here Oracle 11g Express Edition to create database which is based on SQL.
57
Database Management System 16103081
RELATIONS:
Entities:
EMP_INFO
DEPT_INFO
EMP_DEPT
EMP_JOB
Attributes:
EMP_INFO
EID
ENAME
ADDRESS
AGE
GENDER
DEPT_INFO
DID
DEPTNAME
EMP_DEPT
EID
DID
MGR
EMP_DEPT
EID
POST
DOJ
SAL
58
Database Management System 16103081
DEPTNAME
DID
DEPT_INFO
W
O
R
K
S
ADDRESS
EID
AGE EMP_INFO
ENAME
GENDER
H
HAS AS
MAN JO
AGER B
SAL
EMP_DEPT EMP_JOB
59
Database Management System 16103081
Queries:
DDL
1. Create a table named emp_info having attributes as eid, address, and name.
2. Create a table named dept_info having attributes as did and deptname
3. Create a table named emp_dept having attributes as eid, did, mgr.
4. Create a table named emp_job having attributes as eid, post, sal, doj.
60
Database Management System 16103081
61
Database Management System 16103081
62
Database Management System 16103081
DML
1. Insert sufficient values to all tables.
63
Database Management System 16103081
64
Database Management System 16103081
65
Database Management System 16103081
66
Database Management System 16103081
6. Find manager of the employees whose department has the highest salary.
67
Database Management System 16103081
7. Find the employee details of the most experienced employee in the office.
68
Database Management System 16103081
69
Database Management System 16103081
11.List all the employees whose manager earns more than 50000.
70