You are on page 1of 70

Dr.

B R AMBEDKAR NATIONAL INSTITUTE OF TECHNOLOGY


JALANDHAR – 144011, PUNJAB (INDIA)
JAN-MAY, 2018

DEPARTMENT OF COMPUTER
SCIENCE AND ENGINEERING
DATABASE MANAGEMENT SYSTEM
LABORATORY
(CSX-224)

Submitted To: Submitted By:


Dr. Harsh Kumar Verma Shyam lal saini
H.O.D. and Associate Professor 16103081
Dept. of CSE G-4 Group (2nd Year)
4th sem.
Database Management System 16103081

INDEX:

S.
CONTENT Pages Remarks
No
Basics of DBMS and Database
1. 3-5
Languages.

2. Implement DDL Commands. 6-10

3. Implement DML Commands. 11-15


Create Tables using Constraints like
4. Primary Key, Foreign Key, Not Null, 16-19
etc.

Perform various Operators and


5. 20-36
Aggregate Functions on Tables.

Perform various JOIN operations on


6. 37-45
Tables.

Use Union, Intersect, Minus Operations


7. 46-49
on Tables.

Perform Grant and Revoke Operations


8. 50-51
on Tables.

Implementing Views and Indexing on


9. 52-54
Tables.

2
Database Management System 16103081

Employee Management System


10. 55-68
(PROJECT).

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

Step1- Applied for purpose.


Step2 - Build and process.

 DBMS – (DATA BASE MANAGEMENT SYSTEM)


A database management system (DBMS) is a software package designed to
define, manipulate, retrieve and manage data in a database. A DBMS
generally manipulates the data itself, the data format, field names, record
structure and file structure. It also defines rules to validate and manipulate
this data.
 FILE SYSTEM-
A file is a collection of related information that is recorded on secondary
storage.

TYPES OF FILES:

 Sequential file
 Direct file
 Indexed file

 DISADVANTAGE OF FILE SYSTEM

3
Database Management System 16103081

 Time Consuming
 Data Replication
 Security Issue
 Concurrency Access
 Can’t apply Query

 Types of DBMS languages:


1. Data Definition Language (DDL): DDL is used for specifying the database
schema. Let’s take SQL for instance to categorize the statements that comes
under DDL.

 To create the database instance –CREATE


 To alter the structure of database – ALTER
 To drop database instances – DROP
 To delete tables in a database instance – TRUNCATE
 To rename database instances – RENAME

2. Data Manipulation Language (DML): DML is used for accessing and


manipulating data in a database.

 To read records from table(s) – SELECT


 To insert record(s) into the table(s) – INSERT
 Update the data in table(s) – UPDATE
 Delete all the records from the table –DELETE

3. Data Control language (DCL): DCL is used for granting and revoking user
access on a database –

 To grant access to user – GRANT


 To revoke access from user – REVOKE

 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

 VARCHAR2 – Variable length character string having maximum length


size byte. We must have to specify size.
 LONG – Store large amount of character string of variable length.
Maximum length is up to 2GB.

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

TIME – Format-> HH:MI:SS

5
Database Management System 16103081

TOPIC-2
Aim: Implement DDL Commands.
Software used: Oracle 11g XE

Theory:

DDL: Data Definition Language (a.k.a Data Description Language) is a computer


language used to create and modify the structure of database objects in a database,
these includes views, schemas and tables. Some of the commands are:-

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)

2. RENAME: Rename command is used to change name of a table


SYNTAX:
RENAME <old table name> to <new table name>

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.2 We can drop a column using alter command


SYNTAX:
Alter table <tablename> drop column <column name>

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:

1. Create table STUDENT–> STUDENT_ID(NUMBER(5)), NAME


(VARCHAR2(200)), ADDRESS(VARCHAR2(200))
2. Insert 5 rows

3. Create table STUDENT1 with same structure as STUDENT without


explicitly mentioning column names (includes data also).

7
Database Management System 16103081

4. Same as 3 but does not contain data of STUDENT.

5. Show structure of STUDENT.

8
Database Management System 16103081

6. Rename NAME to STUDENT_NAME.

7. Change data type from NUMBER(5) to NUMBER(9) for STUDENT_ID.

9
Database Management System 16103081

8. Change data type from NUMBER(9) to NUMBER(5).

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:

insert into <table_name> values (<value1>,<value2>,…)

 Insertion into selected columns

insert into <table_name> (<column1>,<column2>,…) values


(<value1>,<value2>,…)

2. DELETE: Delete command can remove selected rows from the table or all
the rows.
SYNTAX:

 All the rows


Delete from <tablename>

 Selected rows
Delete from <table_name> where <condition>

3. UPDATE: Update command is used to change the data values in the table
SYNTAX:

Update <table_name> set <attribute_name>=<value> where <condition>

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:

 All the rows and columns


Select * from <tablename>

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

2. Drop column city.

3. Put comment on STUDENT_ID.

13
Database Management System 16103081

4. Truncate STUDENT1.

5. Update all phone values to 1234.

14
Database Management System 16103081

6. Update phone value of STUDENT_ID 1 to 987.

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.

Software Used: Oracle 11g XE

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.

2. Unique: This constraint uniquely identifies each record in a database table.


A table can have many UNIQUE constraints.

3. Primary Key: The PRIMARY KEY constraint uniquely identifies each


record in a database table. These must contain UNIQUE values. A primary
key column cannot contain NULL values. Most tables should have a primary
key, and each table can have only ONE primary key.

4. Foreign Key: A FOREIGN key in one table points to a PRIMARY KEY in


another table. This constraint is used to prevent actions that would destroy
links between tables. This constraint also prevents invalid data from being
inserted into the foreign key column, because it has to be one of the values
contained in the table it points to.

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).

2. Set the stu_id as primary key.

17
Database Management System 16103081

3. Insert 5 records to student.

4. Delete one record from it.

18
Database Management System 16103081

5. Update the phone_no of one student.

6. Set the not null constraint for dob column.

19
Database Management System 16103081

TOPIC-5
Aim: To implement the OPERATORS and AGGREGATE FUNCTIONS in
SQL commands.

Software Used: Oracle 11g XE

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

AGGREGATE FUNCTIONS: SQL aggregate functions return a single value,


calculated from values in a column.
Useful aggregate functions:
1. AVG() – Returns the average value .
2. COUNT() – Returns the number of rows
3. MAX() – Returns the largest value
4. MIN() – Returns the smallest value
5. SUM() – Returns the sum.

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.

SYNTAX: SELECT column_name(s) FROM table_name GROUP BY


column_name(s) HAVING condition.

Queries: The employee table is as:

21
Database Management System 16103081

1. List ENAME of employee whose salary is greater than 2000.

2. List ENAME, JOB, SAL of employee whose dept_no=10 & sal>2000.

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

5. List all employees whose JOB post is ‘clerk’ or ‘manager’.

6. List all employee whose SAL lies b/w 1250 and 3000.

24
Database Management System 16103081

7. List all employees who is getting a commission.

8. List all employees according to date of joining.

25
Database Management System 16103081

9. List the last three character of ENAME of each employee.

10. List all names and EMPNO’s as <EMPNO>, name as <ENAME>.

26
Database Management System 16103081

11. List hiredates of each employee in format ‘DD/MM/YYYY’.

12. List maximum, minimum, average of salary.

27
Database Management System 16103081

13. List sum of SAL and COMM of each employee.

14. Count number of JOB titles in emp table.

28
Database Management System 16103081

15. Calculate total number of employee in each dept except DEPTNO=30.

16. Find all dept number where total number of employee equal to 4.

29
Database Management System 16103081

17. List employees who are not clerks.

18. List all employees who do not have a manager.

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

25. No. of employee in each department.

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

29. Find 3rd maximum salary from employee table.

30. Select * from employee where salary<ALL(Select salary from employee


where dept=100).
Find equivalent query by using function inside 1st query.

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;

table1: First table.


table2: Second table
matching_column: Column common to both the tables.

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.

4. FULL JOIN: FULL JOIN creates the result-set by combining result of both


LEFT JOIN and RIGHT JOIN. The result-set will contain all the rows from
both the tables. The rows for which there is no matching, the result-set will
contain NULL values.

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

5. SELF JOIN: As the name signifies, in SELF JOIN a


table is joined to itself. That is, each row of the table is joined with itself and all
other rows depending on some conditions. In other words we can say that it is a
join between two copies of the same table.

Syntax:
SELECT a.coulmn1 , b.column2
FROM table_name a, table_name b
WHERE some_condition;

table_name: Name of the table.


some_condition: Condition for selecting the rows.

6. CARTESIAN JOIN: The CARTESIAN JOIN is also known as CROSS JOIN.


In a CARTESIAN JOIN there is a join for each row of one table to every row of
another table. This usually happens when the matching column or WHERE
condition is not specified.

 In the absence of a WHERE conditions the CARTESIAN JOIN will behave


like a CARTESIAN PRODUCT. i.e., the number of rows in the result-set is
the product of the number of rows of the two tables.
 In the presence of WHERE condition this JOIN will function like a INNER
JOIN.
 Generally speaking, Cross join is similar to an inner join where the join-
condition will always evaluate to True

40
Database Management System 16103081

Syntax:
SELECT table1.column1 , table1.column2, table2.column1...
FROM table1
CROSS JOIN table2;
table1: First table.
table2: Second table

TABLE ON WHICH WE WILL PWEFORM DIFFERENT JOIN OPERATION.

1. STUDENT1(student_id,name,dep_name,phone_no,email)
2. DEPARTMENT(dept_id,dept_name,HOD_name)

DESCRIPTION OF BOTH THE TABLE

1. STUDENT1

Desc student1

All data of table student1

41
Database Management System 16103081

 Student id is primary key in table student1.


 Dep_name is foreign key in table student1.

2. Description of department table

All data of department table

QURIES

42
Database Management System 16103081

Queries:
1. Find the inner join of table department and student1

2. Find left outer


join of table department and student1.

43
Database Management System 16103081

3. Find right outer join of table department and student1

4. Find full outer join of table student1 and department.

44
Database Management System 16103081

5. Find self join of table department and student1.

6. Find cross join of table department and student1.

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 merges the results of two SELECT statements. UNION statements only


return UNIQUE values. Below, you’ll see a Venn diagram representing this
operation and the code that will make it happen: 

2. UNION ALL 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.

4. SET INTERSECT OPERATOR

SET INTERSECT operator is used to find the common between the results of
two queries. It as follows:

47
Database Management System 16103081

Queries:

1. Use UNION operator between table department and student1.

2. Use UNION operator between table department and student1.

48
Database Management System 16103081

3. Use MINUS operator between table department and student1.

4. Use INTERSECT operator between table department and student1.

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:

GRANT privileges ON object TO user;

Data Control Language(DCL) is used to control privileges in Database. To perform


any operation in the database, such as for creating tables, sequences or views, a
user needs privileges. Privileges are of two types,

 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.

In DCL we have two commands,

 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

2. Grant all to public

3. Revoke all from 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:

CREATE INDEX <indexname> ON <tablename> (<column>,


<column>...);

 To enforce unique values, add the UNIQUE keyword:


CREATE UNIQUE INDEX <indexname> ON <tablename> (<column>,
<column>...);

 To remove an index, simply enter:


DROP INDEX <indexname>;

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

 Employee Management System is a distributed application, developed to


maintain the details of employees working in any organization.

 The Employee Management System has been developed to override the


problems prevailing in the practicing manual 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.

 It is useful in handling the large number of details of the employees, thus


reducing the task of handling the paperwork and handwritten records.

A successful system must: -

1. Satisfy the user requirements


2. Be easy to understand by user and operator
3. Be easy to operate
4. Have a good user interface
5. Be easy to modify
6. Be expandable
7. Have adequate security control against the misuse of data
8. Handle the errors and exceptions satisfactorily
Thus, SQL is used to create the database of Employee Management System.

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

EID DID MGR EID POST DOJ

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.

5. Add columns age, gender to table emp_info.

60
Database Management System 16103081

6. Rename column ‘name’ to ‘ename’ in table emp_info.

7. Add primary key to all tables.

61
Database Management System 16103081

8. Add ‘check in’ constraint for gender attribute in emp_info.

62
Database Management System 16103081

DML
1. Insert sufficient values to all tables.

63
Database Management System 16103081

64
Database Management System 16103081

2. Find eid, ename having highest salary.

65
Database Management System 16103081

3. Find eid, ename whose post is ‘clerk’.

4. Count all the employees whose address is ‘Jalandhar’.

66
Database Management System 16103081

5. Find maximum salary and employee details of each department.

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.

8. Calculate the number of male and female employees in the office.

68
Database Management System 16103081

9. List the date of joining in the format ‘dd-month-year’.

10.List all the employees who joined after 2016.

69
Database Management System 16103081

11.List all the employees whose manager earns more than 50000.

70

You might also like