0% found this document useful (0 votes)
14 views21 pages

DBMS Manual (1) (Autosaved)

This document certifies that Patadiya Smit has successfully completed the Database Management System course at VSITR. It includes a detailed index of practical exercises covering various SQL operations such as creating and manipulating database objects, data manipulation, and SQL functions. The document also outlines specific objectives and exercises for each practical session to enhance learning outcomes.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
14 views21 pages

DBMS Manual (1) (Autosaved)

This document certifies that Patadiya Smit has successfully completed the Database Management System course at VSITR. It includes a detailed index of practical exercises covering various SQL operations such as creating and manipulating database objects, data manipulation, and SQL functions. The document also outlines specific objectives and exercises for each practical session to enhance learning outcomes.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 21

CERTIFICATE

This is to certify that PATADIYA SMIT


Enrollment .No. 22BECE54014 of Semester 3, Computer
Enginnering Department has satisfactory completed the course in
Database Management System at VSITR Vidush Somany
Institute Of Technology and Research, Gandhinagar.

Date of Submission :

Concern Faculty :

Head of Department :
ENROLLMENT NO.: 23BECE54014 NAME :- Patadiya Smit

CE – Department.

INDEX
Sr. Practical Date Sign
No. Aim

1 Creating and Manipulating Database objects and


Applying Constraints (DDL)

2 Manipulating Data with Database Objects (DML)


3 Retrieving, Restricting and Sorting Data (DRL)

4 SQL Single Row Functions

5 SQL Multiple Row Functions (Aggregate Function)

6 Displaying Data from Multiple Tables (Join)

7 Using Commit and Rollback show Transaction


ACID Property.

8 Securing data using Views and Controlling User


Access (DCL)

9 Database SET Operations


10 PL/SQL Block Syntax and DML Operation through
PL/SQL Block

11 Working with Cursor

12 Creating Procedures and Functions in PL/SQL


13 Creating Database Triggers
14 Concept of Plan Table and Audit Trails
CE – Department.

VSITR Institute of Technology and Research

Practical 1: Creating and Manipulating Database objects and Applying Constraints (DDL)

Objectives

After completing this lesson, you should be able to do the following:


1. Describe the main database objects
2. Create tables
3. Describe the data types that can be used when specifying column definition
4. Alter table definitions
5. Drop, rename, and truncate tables

Creating Tables
Querying the Data Dictionary Y
• create table employees(name varchar(20), id number, • desc employees;
salary number,
post varchar(20),
city varchar(20),
country varchar(20));

Select * from employees;

Truncating a Table
• truncate table employee_info;

ENROLLMENT NO.: 23BECE54014 NAME :- Patadiya Smit


CE – Department.

Creating a Table by Using a Subquery


Select enrollment_no,first_name from students where sem=3;

Adding a Column
• alter table employees add(mobile no number); • alter table employees add(email_id varchar(20));

Modifying a Column

• alter table employee modify post varchar(35); • alter table employee modify first_name varchar(30);

Changing the Name of an Object


Dropping a Table • alter table employees rename column id to employe_id;
• drop table employees_info; • alter table employees rename to employee_info;

ENROLLMENT NO.: 23BECE54014 NAME :-PATADIYA SMIT


CE – Department.

Exercises

1. Populate the DEPT table with data from the DEPARTMENTS table. Include only columns that you need.

o create table DEPT(dept_name varchar2(10), dept_id number);

o desc DEPT;
o insert into DEPT(dept_name,dept_id) values('DEEP',10);
o insert into DEPT(dept_name,dept_id) values('JAINAM',1);
o insert into DEPT(dept_name,dept_id) values('RUTVIK',20);

o select * from DEPT;

2. Create the EMP table based on the following table instance chart. 3. Drop the EMP table
Place the syntax in a script called lab9_3.sql, drop table EMP;
and then execute the statement in the script to create the table.
Confirm that the table is created.

o create table EMP(emp_name varchar2(20),


emp_id number,
salary number,
emp_branch varchar(20),
emp_experience number);
o desc EMP;

ENROLLMENT NO.: 23BECE54014 NAME :- Patadiya Smit


CE – Department.

4. Confirm that both the DEPT and EMP tables are stored in the data dictionary.
(Hint: USER_TABLES)

o select table_name from user_tables where table_name in('DEPT1' , 'EMP1');

5. Create the EMPLOYEES2 table based on the structure of the EMPLOYEES table. Include only the EMPLOYEE_ID,
FIRST_NAME, LAST_NAME, SALARY, and DEPARTMENT_ID columns. Name the columns in your new table ID,
FIRST_NAME, LAST_NAME, SALARY , and DEPT_ID, respectively.
⮚ create table EMPLOYEES2 as select emp_id,emp_name,emp_last_name,salary from EMP;

⮚ desc EMPLOYEES2;

⮚ alter table EMPLOYEES2 add(DEPARTMENT_ID number);


⮚ alter table EMPLOYEES2 rename column emp_id to id;
⮚ alter table EMPLOYEES2 rename column emp_name to first_name;
⮚ alter table EMPLOYEES2 rename column emp_last_name to last_name;

⮚ desc EMPLOYEES2;

ENROLLMENT NO.: 23BECE54014 NAME :- Patadiya Smit


CE – Department.

Part 2: Including Constraints


After completing this lesson, you should be able to do the following:
• Describe constraints
• Create and maintain constraints

What are Constraints?


• Constraints enforce rules at the table level.
• Constraints prevent the deletion of a table if there are dependencies.

• The following constraint types are valid:


– NOT NULL
– UNIQUE
– PRIMARY KEY
– FOREIGN KEY
– CHECK

The NOT NULL Constraint

✓ create table emp3(emp_name varchar2(10), department_name varchar2(10), emp_id number not null);

The UNIQUE Constraint

✓ create table emp5(emp_name varchar2(10) not null, department_name varchar(10), emp_id number UNIQUE);
The PRIMARY KEY Constraint

✓ create table emp6(emp_name varchar2(10) not null, department_name varchar(10), emp_id number primary key);

The FOREIGN KEY Constraint

✓ create table emp7(emp_name varchar2(10) not null, department_name varchar(10), emp_id, foreign
key(emp_id) references emp6(emp_id));

The CHECK Constraint

✓ create table emp1(emp_name varchar2(20),emp_id number,salary number,emp_branch varchar(20),check


(emp_id>5));

desc emp1;

ENROLLMENT NO.: 23BECE54014


NAME :- Patadiya Smit
CE – Department.

Exercises

1. Create a PRIMARY KEY constraint to the DEPT table using the ID column. The constraint should be named at creation.
Name the constraint my_dept_id_pk.

Hint: The constraint is enabled as soon as the ALTER TABLE command executes successfully.
alter table DEPT add constraint my_dept_id_pk primary key (dept_id);

desc DEPT;

2. Add a column DEPT_ID to the EMP table. Add a foreign key reference on the EMP table that ensures that the employee is
assigned to a nonexistent department. Name the constraint my_emp_dept_id_fk.

o alter table EMP add(DEPT_ID number);


o desc EMP;
o alter table EMP add constraint my_emp_dept_id_fk foreign key(DEPT_ID) references DEPT(dept_id); desc EMP;

ENROLLMENT NO.: 23BECE54014 NAME :- Patadiya Smit


CE – Department.

Practical 2: Manipulating Data with Database Objects (DML)

After completing this lesson, you should be able to do the following:


• Describe each DML statement
• Insert rows into a table
• Update rows in a table
• Delete rows from a table
• Merge rows in a table
• Control transactions
❖ Some major operations can be done in DML

✓ Inserting New Rows


insert into empl(emp_name,emp_id,dept_id) values('jay',113,05);

✓ Inserting Rows with Null Values

insert into empl(emp_name,emp_id,dept_id) values('Amit',null,null);

✓ Creating a Script

INSERT INTO departments (department_id , department_name , location_id) VALUES (&department_id


, „&department_name‟ , &location_id);

✓ Updating Rows in a Table

update empl set dept_id=0 where emp_id=113

✓ The DELETE Statement


delete from empl where emp_name='jay';

ENROLLMENT NO.: 23BECE54014


NAME :- Patadiya Smit
CE – Department.

Exercises

1. Describe the structure of the EMPLOYEES table to identify the column names.

create table MY_EMPLOYEE(id number(4) constraint my_employee_id_nn not null,last_name varchar2(25),first_name


varchar2(25),userid varchar2(25),salary number(9,2));

desc MY_EMPLOYEE;

2. Change the last name of employee 3 to Drexler


update MY_EMPLOYEE set last_name='DRExler' where id=1;

3. Change the salary to 1000 for all employees with a salary less than 900.
update MY_EMPLOYEE set salary=1000 where salary<900;

ENROLLMENT NO.: 23BECE54014 NAME :- Patadiya Smit


CE – Department.

Practical 3: Retrieving, Restricting and Sorting Data (DRL)

Objectives

After completing this Practical, you should be able to do the following:


• List the capabilities of SQL SELECTstatements
• Execute a basic SELECTstatement

Basic SELECTStatement

select * from empq;

Selecting Specific Columns

select emp_name,emp_id from empq;

Using Arithmetic Operators

select emp_id+dept_id from empq; select emp_id*dept_id from empq;

Using the Concatenation Operator

select emp_name || 'has dept_id is' || dept_id from empq;

Eliminating Duplicate Rows

select distinct * from empq;

Displaying Table Structure

desc EMP6;

There are coding errors in this statement. Can you identify them?

SELECT employee_id, last_name sal x 12 ANNUAL SALARY


FROM employees;

Ans: 1. The employees table does not contain a column called sal the column is called SALARY.
2 . The multiplication operator is* not x as shown
3. A comma is missing after the column last_name

ENROLLMENT NO.: 23BECE54014 NAME :- Patadiya Smit


CE – Department.

Exercise
1.Create a query to display the last name and salary of employees earning more than $12,000. Place your
SQL statement in a text file named lab2_1.sql. Run your query.

select last_name , salary from EMPLOYEES12 where salary>12000;

2.Create a query to display the employee last name and department number for employee number 176.

select last_name , first_name from EMPLOYEES12 where emp_id = 1;

4.Display the employee last name, job ID, and start date of employees hired between February 20, 1998, and
May 1, 1998. Order the query in ascending order by start date.

select last_name , job_id ,hire_date from EMPLOYEES12 where hire_date between DATE '2020-07-19' and
DATE '2020-09-10' order by hire_date;

ENROLLMENT NO.: 23BECE54014 NAME :- Patadiya Smit


CE – Department.

5 Modify lab2_3.sql to list the last name and salary of employees who earn between $5,000 and $12,000, and
are in department 20 or 50. Label the columns Employee and Monthly Salary, respectively. Resave lab2_3.sql
as lab2_6.sql. Run the statement in
lab2_6.sql.

select last_name "Employee" , salary "monthly salary" from EMPLOYEES12 where salary between 5000
AND 12000 AND job_id in(20,50);

6.Display the list of the last name and salary of employees who earn between $5,000 and $12,000, and are in department
20 or 50. Label the columns Employee and Monthly Salary, respectively.

select last_name "Employee" , salary "monthly salary" from EMPLOYEES12 where salary between 5000 AND
12000 OR job_id in(20,50);

NAME :- Patadiya Smit


ENROLLMENT NO.: 23BECE54014
7. Display the List of how many Employee are employed in each proffesion.

SELECT JOB_ID,COUNT(FIRST_NAME)
FROM EMPLOYEES12
GROUP BY JOB_ID;
CE – Department.

Practical 4: SQL Single Row Functions

Character Functions

⮚ select upper(last_name) from empl1;


⮚ select initcap(last_name) from empl1;
⮚ select lower(last_name) from empl1;

Case Manipulation Functions

⮚ select length(first_name), length(last_name) from empl1;


⮚ select concat(first_name,last_name) from empl1;
⮚ select INSTR('last_name', 't') from empl1;
⮚ select trim('e' from 'last_name') from empl1;

Character-Manipulation Functions

Number Functions

select round(45.2564, 2) from empl1;

NVL Function
select last_name, salary ,NVL(commision,0),(salary*12),(salary*12+NVL(commision,0)) as
"anl_sal" from empl1;

ENROLLMENT NO.: 23BECE54014 NAME :- Patadiya Smit


CE – Department.
Exercise

1. Write a query to display the current date. Label the column Date.

select sysdate as "date" from dual;

2. For each employee, display the employee number, last_name, salary, and salary increased by 15% and expressed as a whole
number. Label the column New Salary. Place your SQL statement in a text file named lab3_2.sql.

⮚ select emp_no,epm_sal,epm_sal+(epm_sal*15/100)"New Salary"from employee;

3. For each employee, display the employee’s last name, and calculate the number of months between today and the
date the employee was hired. Label the column ONTHS_WORKED. Order your results by the number of months
employed. Round the number of months up to the closest whole number.

⮚ select last_name, round(months_between (SYSDATE, dt)) months_worked from empl1 order by


months_between(SYSDATE, dt);

ENROLLMENT NO.: 23BECE54014


NAME :- Patadiya Smit
CE – Department.

Practical 5: SQL Multiple Row Functions (Aggregate Functions)


Using the AVG and SUM Functions

⮚ select sum(salary) from empd;


⮚ select avg(salary) from empd;

Using the MIN and MAX Functions

⮚ select MIN(salary) from empd;


SELECT MIN(salary) AS min_salary FROM empd;

⮚ select MAX(salary) from empd;


SELECT MAX(salary) AS max_salary FROM empd;

Using the COUNT Function

select count(commission) from empd;Group Functions and Null Values

Using the GROUP BY Clause on Multiple Columns

⮚ select last_name , count(*) as "Num of employees" , avg(salary) as "Avg. Dept. Salary" from empd group by
last_name order by last_name NULLS LAST;

ENROLLMENT NO.: 23BECE54014 NAME :- Patadiya Smit


CE – Department.

1. Display the highest, lowest, sum, and average salary of all employees. Label the columns Maximum, Minimum, Sum,
and Average, respectively. Round your results to the nearest whole number.

⮚ select emp_id , ROUND(max(salary),0) "Maximum" , ROUND(min(salary),0) "Minimun" , ROUND(SUM(salary),0)


"Sum" , ROUND(AVG(salary),0) "Average" from empd group by emp_id;

2. Modify the query in exe_4.sql to display the minimum, maximum, sum, and average salary for each job type.

ENROLLMENT NO.: 23BECE54014 NAME :- Patadiya Smit


CE – Department.

Practical 6: Displaying Data from Multiple Tables (Join)


Cartesian Products

⮚ select last_name , dept_name from empd , deptll;

Retrieving Records with Equijoins

⮚ select empd.last_name , empd.emp_id , empd.first_name , deptll.dept_id , deptll.dept_name from empd , deptll;

Using Table Aliases


⮚ select e.last_name , e.emp_id , e.first_name , d.dept_id , d.dept_name from empd e , deptll d;

NAME :- Patadiya Smit


ENROLLMENT NO.: 23BECE54014
CE – Department.

Exercises

Joining More than Two Tables

⮚ select e.last_name , e.emp_id , e.first_name , d.dept_id , d.dept_name , l.city , l.location_id from empd e , deptll d ,
location l where e.emp_id = d.dept_id AND d.dept_id = l.location_id;

Using Outer Joins


⮚ select w.last_name , e.emp_id from empd w , empd e;

Joining a Table to Itself (Self Join)

⮚ select e.last_name , e.emp_id , e.first_name , d.dept_id , d.dept_name from empd e , deptll d where e.emp_id (+) =
d.dept_id;

ENROLLMENT NO.: 23BECE54014 NAME :- Patadiya Smit


CE – Department. NAME :- Patadiya Smit

Practical 7: Using Commit and Rollback show Transaction ACID Property.

⮚ create table IPL(playre_name varchar2(20) , player_no number , player_team varchar(20) , player_country varchar2(20)
, player_runs number , player_bestscore number);

⮚ desc IPL;

⮚ insert into IPL values('Rohit' , 11 , 'MI' , 'INDIA' , 50 , 300);


⮚ insert into IPL values('Mahendra' , 1 , 'CSK' , 'INDIA' , 70 , 250);
⮚ insert into IPL values('Rahul' , 12 , 'KXII' , 'INDIA' , 40 , 150);
⮚ insert into IPL values('Shikhr' , 10 , 'DD' , 'INDIA' , 80 , 210);
⮚ insert into IPL values('Rishbh' , 8 , 'RR' , 'INDIA' , 60 , 200);

⮚ select * from IPL;

➢ commit;

➢ insert into IPL values('Polard' , 18 , 'MI' , 'WESTINDIS' , 66 , 260); insert into IPL values('Smith' , 23 , 'RR' ,
'AUSTRALIYA' , 55 , 220);

➢ rollback;

➢ savepoint

➢ insert into IPL values('Gayel' , 28 , 'KXII' , 'WESTINDIS' , 100 , 460); insert into IPL values('Bravo' , 5 , 'CSK' ,
'WESTINDIS' , 40 , 160);

➢ savepoint dbms_1;

➢ insert into IPL values('Jasprit' , 40 , 'MI' , 'INDIA' , 44 , 140); savepoint dbms_2;

ENROLLMENT NO.: 23BECE54014

You might also like