Database Management
Database Management
Database Management
Q.1
Q.2
1. Describe the use of primary key and unique key constraints with
example?
4M
Ans There are two Entity constraints:
1.Primary Key constraint
2. Unique Constraint
1. Primary Key constraint: It is use to avoid redundant/duplicate value entry within the row of
specified column in table. It restricts null values too.
Syntax: CREATE TABLE TABLE_NAME (COLUMN_NAME
DATA_TYPE, COLUMN_NAME DATA_TYPE CONSTRAINT
CONSTRAINT_NAME PRIMARY KEY);
Example: SQL> CREATE TABLE EMP (ID NUMBER
(5)CONSTRAINT ID_PK PRIMARY KEY, NAME VARCHAR2
(10), SAL NUMBER (10));
2. Unique Constraint: The UNIQUE constraint uniquely identifies each record in a database
table. The UNIQUE and PRIMARY KEY constraints both provide a guarantee for uniqueness
for a column or set of columns.
syntax: CREATE TABLE TABLE_NAME (COLUMN_NAME
DATA_TYPE, COLUMN_NAME DATA_TYPE CONSTRAINT
CONSTRAINT_NAME UNIQUE);
Example: CREATE TABLE PERSONS (P_ID NUM CONSTRAINT
P_UK UNIQUE , FIRSTNAME VARCHAR(20), CITY
VARCHAR(20) );
FULL JOIN: Full Join or the Full Outer Join returns all those records
which either have a match in the left (Table1) or the right (Table2) table.
For e.g.,
SELECT Employee.EmpFname, Employee.EmpLname, Projects.ProjectID
FROM Employee
FULL JOIN Projects
ON Employee.EmpID = Projects.EmpID;
LEFT JOIN: The LEFT JOIN or the LEFT OUTER JOIN returns all
the records from the left table and also those records which satisfy a
condition from the right table. Also, for the records having no matching
values in the right table, the output or the result-set will contain the
NULL values.
For e.g.
SELECT Employee.EmpFname, Employee.EmpLname,
Projects.ProjectID, Projects.ProjectName
FROM Employee
LEFT JOIN
ON Employee.EmpID = Projects.EmpID ;
RIGHT JOIN: The RIGHT JOIN or the RIGHT OUTER JOIN returns
all the records from the right table and also those records which satisfy
a condition from the left table. Also, for the records having no matching
values in the left table, the output or the result-set will contain the NULL
values.
For e.g.,
SELECT Employee.EmpFname, Employee.EmpLname,
Projects.ProjectID, Projects.ProjectName
FROM Employee
RIGHT JOIN: ON Employee.EmpID = Projects.EmpID;
3. Create Sequence seq-1 with starting value 1 and maximum value 20 with an increment of
1. Consider schema Customer (custno, custname, telephone) and use seq-1 for inserting a
row in customer table.Sequence:
It is database object that generate/produce integer values in sequential order.
It automatically generates primary key and unique key values.
It may be ascending or descending order
It can be used for multiple tables.
Sequence numbers are stored and generated independently of tables
Q.3
1. Write a PL/SQL code to find sum of numbers from 1 to 20. 4M
Ans declare
i number(10);
ans number(10);
begin
ans:=0;
i:=1;
while i<=20 loop
ans:=ans+i;
i:=i+1;
end loop;
dbms_output.put_line('Sum of 1 to 20 numbers is:'||ans);
end;
Syntax:
SELECT column_name1… column_name n
FROM <table_name>
WHERE column1 operator (SELECT column from <table_name>
where condition);
Example
Find the employees who earn the same salary as minimum salary for
departments.
Select empno,ename,job,salary,deptno
From emp
Where salary IN (select min(salary) from emp group by deptno);
Multiple column subqueries
Queries that return the values from more than one column are called
multiple column subqueries.
Syntax:
SELECT column_name1, ….. column_name n
FROM <table_name>
WHERE (column_name, column_name) IN
(SELECT column_name, column_name….from
<table_name> Where <condition> );
Example: Display the name, department number, salary and
commission of any employee whose salary and commission matches
both the commission and salary of any employee in department 10
Query: Select empno,deptno,salary,comm
From emp
Where (salary,comm) IN (select salary,comm from emp where
deptno=10);
Q.4
1. Write steps to create execute and delete stored procedure. 4M
Ans Step 1:
Stored Procedure creation: A stored procedure has header, a declaration section, an executable
section and optional exception handling section.
Syntax:-
CREATE OR REPLACE PROCEDURE
<procedure_name>(<Argument> {IN | OUT | IN OUT}<Data
type>…){IS | AS}
Variable declarations;
Constant declarations;
BEGIN
<PROCEDURE_BODY>
EXCEPTION
Exception pl/sql block;
END ;
Step 2:
Executing Stored Procedure:
Use EXCE command with help of any application program
Ex:
EXEC use_test
Invoke this procedure from PL/SQL code block
DECLARE
BEGIN
use_test
END;
Step 3 : delete stored procedure
Syntax : drop procedure <procedure_name>; Ex: drop procedure use_test;
2. Describe simple and composite index.
1) Simple index (Single column): An index created on single column of a table is called a Simple
Index.
Syntax: Create index index_name on <tablename><column name>;
E.g.: Create index on employee (empno);
Composite (concatenated): Indexes that contain two or more columns from the same table
which are useful for enforcing uniquely identify a row.
Syntax: Create index index_name on <tablename><Column_name1,
Column_name2>;
E.g.: Create index on employee (ename, empno);
7. Create sequence for department table and also altered the created sequence.
Create sequence deptid
Start with 1
Increment by 1
Maxvalue 100;
Alter the created sequence
Alter sequence deptid maxvalue 1500;
8. List the types of trigger. Write the steps to create trigger with
example.
Types of Triggers:
1. Row-level trigger
2. Statement-level trigger
3. Before-trigger
4. After-trigger
Steps to create trigger:
1) Trigger can be created with the following syntax in database environment :
CREATE [OR REPLACE ] TRIGGER trigger_name {BEFORE |
AFTER | INSTEAD OF } {INSERT [OR] | UPDATE [OR] |
DELETE} [OF col_name] ON table_name [REFERENCING OLD
AS o NEW AS n] [FOR EACH ROW] WHEN (condition)
2) User does not have to fire the trigger, but it gets automatically fired
according to definition of the trigger.
Example :
CREATE OR REPLACE TRIGGER display_salary_changes
BEFORE DELETE OR INSERT OR UPDATE ON customers
FOR EACH ROW
WHEN (NEW.ID > 0)
DECLARE
sal_diff number; BEGIN
sal_diff := :NEW.salary - :OLD.salary;
dbms_output.put_line('Old salary: ' || :OLD.salary);
dbms_output.put_line('New salary: ' || :NEW.salary);
dbms_output.put_line('Salary difference: ' || sal_diff); END; /
Q.5
1. Write SQL statements for following
i) Create table student with rollno, name, d-o-b, percentage, assign rollno as primary key.
ii) Add new column email in student table.
iii) Delete table ‘student’ with its structure and data.
Ans i) Create table student with rollno, name, d-o-b, percentage, assign
rollno as primary key.
Ans : Create table student( rollno number(5) primary key, name
varchar2(20), d-o-b date, percentage number(6,2)) ;
ii) Add new column email in student table.
Ans : Alter table student add email varchar2(30);
iii) Delete table ‘student’ with its structure and data.
Ans : Drop table student;
4.
4.