DBMS Manual (1) (Autosaved)
DBMS Manual (1) (Autosaved)
Date of Submission :
Concern Faculty :
Head of Department :
ENROLLMENT NO.: 23BECE54014 NAME :- Patadiya Smit
CE – Department.
INDEX
Sr. Practical Date Sign
No. Aim
Practical 1: Creating and Manipulating Database objects and Applying Constraints (DDL)
Objectives
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));
Truncating a Table
• truncate table employee_info;
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);
Exercises
1. Populate the DEPT table with data from the DEPARTMENTS table. Include only columns that you need.
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);
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.
4. Confirm that both the DEPT and EMP tables are stored in the data dictionary.
(Hint: USER_TABLES)
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;
⮚ desc EMPLOYEES2;
✓ create table emp3(emp_name varchar2(10), department_name varchar2(10), emp_id number not null);
✓ 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);
✓ create table emp7(emp_name varchar2(10) not null, department_name varchar(10), emp_id, foreign
key(emp_id) references emp6(emp_id));
desc emp1;
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.
✓ Creating a Script
Exercises
1. Describe the structure of the EMPLOYEES table to identify the column names.
desc MY_EMPLOYEE;
3. Change the salary to 1000 for all employees with a salary less than 900.
update MY_EMPLOYEE set salary=1000 where salary<900;
Objectives
Basic SELECTStatement
desc EMP6;
There are coding errors in this statement. Can you identify them?
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
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.
2.Create a query to display the employee last name and department number for employee number 176.
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;
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);
SELECT JOB_ID,COUNT(FIRST_NAME)
FROM EMPLOYEES12
GROUP BY JOB_ID;
CE – Department.
Character Functions
Character-Manipulation Functions
Number Functions
NVL Function
select last_name, salary ,NVL(commision,0),(salary*12),(salary*12+NVL(commision,0)) as
"anl_sal" from empl1;
1. Write a query to display the current date. Label the column Date.
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.
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 , count(*) as "Num of employees" , avg(salary) as "Avg. Dept. Salary" from empd group by
last_name order by last_name NULLS LAST;
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.
2. Modify the query in exe_4.sql to display the minimum, maximum, sum, and average salary for each job type.
Exercises
⮚ 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;
⮚ 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;
⮚ 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;
➢ 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;