Rdbms LAB
Rdbms LAB
desc bank101;
7
3.To insert values into the table
8
5. To show the error message on abandon the constraints
9
10
2. REFERENTIAL INTEGRITY CONSTRAINTS
Aim:
To Create set of tables, add foreign key constraints and incorporate referential integrity.
A FOREIGN KEY is a field (or collection of fields) in one table that refers to the PRIMARY
KEY in another table.
The table containing the foreign key is called the child table, and the table containing the
candidate key is called the referenced or parent table.
"Persons" table:
1 Hansen Ola 30
2 Svendson Tove 23
3 Pettersen Kari 20
"Orders" table:
1 77895 3
11
2 44678 3
3 22456 2
4 24562 1
Notice that the "PersonID" column in the "Orders" table points to the
"PersonID" column in the "Persons" table.
The "PersonID" column in the "Persons" table is the PRIMARY KEY in the
"Persons" table.
The FOREIGN KEY constraint is used to prevent actions that would destroy
links between tables.
The FOREIGN KEY 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.
The following SQL creates a FOREIGN KEY on the "PersonID" column when the "Orders"
table is created:
To allow naming of a FOREIGN KEY constraint, and for defining a FOREIGN KEY
constraint on multiple columns, use the following SQL syntax:
12
OrderNumber int NOT NULL,
PersonID int,
PRIMARY KEY (OrderID),
CONSTRAINT FK_PersonOrder FOREIGN KEY (PersonID)
REFERENCES Persons(PersonID)
);
To create a FOREIGN KEY constraint on the "PersonID" column when the "Orders" table is
already created, use the following SQL:
To allow naming of a FOREIGN KEY constraint, and for defining a FOREIGN KEY
constraint on multiple columns, use the following SQL syntax:
MySQL:
13
3. GROUP BY AND ORDER BY CLAUSE AND
AGGREGATE FUNCTION
Aim:
To Query the database tables using different ‘where’ clause conditions and also implement
aggregate functions.
custname varchar2(15),
country varchar2(10));
14
4. To display the count of customers group by country and order by count of customers
15
7. Aggregate functions
16
17
.
To Query the database tables and explore sub queries and simple join operations
productname varchar2(15));
18
4. To create table order
productid number(10),
quantity number(3),
unitprice number(6,2));
7. To display the records from product table whose product id matches with product id
in order table with quantity greater than 200
19
Select productname from product101 where productid in (select productid from order101
where quantity>200);
JOIN QUERY
Desc student101;
20
4. To display the records from library table
Rollno number(10),
Bookname varchar2(20));
21
6. To insert the records into library table
8. To display the records from student and library table using equi-join
22
23
5. Joins – Natural, Equi and Outer Joins
Aim:
To Query the database tables and explore natural, equi and outer joins.
ID NAME
1 abhi
2 adam
3 alex
4 anu
ID Address
24
1 DELHI
2 MUMBAI
3 CHENNAI
ID NAME ID Address
1 abhi 1 DELHI
2 adam 2 MUMBAI
3 alex 3 CHENNAI
Natural JOIN
Natural Join is a type of Inner join which is based on column having same name and same
datatype present in both the tables to be joined.
The syntax for Natural Join is,
SELECT * FROM
25
ID NAME
1 abhi
2 adam
3 alex
4 anu
ID Address
1 DELHI
2 MUMBAI
3 CHENNAI
ID NAME Address
1 abhi DELHI
26
2 adam MUMBAI
3 alex CHENNAI
In the above example, both the tables being joined have ID column(same name and same
datatype), hence the records for which value of ID matches in both the tables will be the
result of Natural Join of these two tables.
OUTER JOIN
Outer Join is based on both matched and unmatched data. Outer Joins subdivide further into,
ON table-name1.column-name = table-name2.column-name;
27
ID NAME
1 abhi
2 adam
3 alex
4 anu
5 ashish
ID Address
1 DELHI
2 MUMBAI
3 CHENNAI
7 NOIDA
8 PANIPAT
28
SELECT * FROM class LEFT OUTER JOIN class_info ON (class.id = class_info.id);
ID NAME ID Address
1 abhi 1 DELHI
2 adam 2 MUMBAI
3 alex 3 CHENNAI
ON table-name1.column-name = table-name2.column-name;
table-name1, table-name2
ON table-name1.column-name(+) = table-name2.column-name;
29
Example of Right Outer Join
Once again the class table,
ID NAME
1 abhi
2 adam
3 alex
4 anu
5 ashish
ID Address
1 DELHI
2 MUMBAI
3 CHENNAI
7 NOIDA
30
8 PANIPAT
ID NAME ID Address
1 abhi 1 DELHI
2 adam 2 MUMBAI
3 alex 3 CHENNAI
ON table-name1.column-name = table-name2.column-name;
31
ID NAME
1 abhi
2 adam
3 alex
4 anu
5 ashish
ID Address
1 DELHI
2 MUMBAI
3 CHENNAI
7 NOIDA
8 PANIPAT
32
The resultset table will look like,
ID NAME ID Address
1 abhi 1 DELHI
2 adam 2 MUMBAI
3 alex 3 CHENNAI
33
6. FUNCTIONS AND STORED PROCEDURES
Aim:
To Write user defined functions and stored procedures in SQL.
34
e number;
BEGIN
select hiredate into hdate
from emp
where empno = p_eno;
e := months_between(sysdate, hdate) / 12;
return round(e);
end;
Procedures
35
)
is
begin
select ename, job, sal into
p_name,p_job, p_sal from emp
where empno = p_eno;
end;
36
7. DCL (Data Control Language) and TCL (Transaction
control language) Commands
Aim:
To Execute complex transactions and realize DCL and TCL commands.
Grant:
Syntax:
Revoke:
Syntax:
TCL
Transaction control language or TCL commands deal with the transaction within the
database.
Commit
Syntax:
Commit;
Rollback
37
Rollback command allows you to undo transactions that have not already been saved to the
database.
Syntax:
ROLLBACK;
SAVEPOINT
Syntax:
SAVEPOINT SAVEPOINT_NAME;
38
8. DATABASE TRIGGERS
AIM:
To Write SQL Triggers for insert, delete, and update operations in database table.
ename varchar2(20),
sal number(7,2));
PL/SQL CODING
DECLARE
sal_diff number;
BEGIN
END;
39
OUTPUT
40
41
9. VIEW AND INDEX
AIM:
To Create View and index for database tables with large number of records.
Syntax:
Create View <View_Name> As Select statement;
SQL>Create View EmpView As Select * from Employee;
Output :View created
Syntax: Select columnname,columnname from <View_Name>;
UPDATABLE VIEWS:
Syntax for creating an Updatable View:
Create View Emp_vw As
Select Empno,Ename,Deptno from Employee; View
created.
SQL>Insert into Emp_vw values(1126,’Brijesh’,20);
1 row created
SQL>Update Emp_vw set Deptno=30 where Empno=1125;
1 row updated.
SQL>Delete from Emp_vw where Empno=1122;
1 row deleted
42
SQL>Delete From EmpDept_Vw where Empno=1123;
1 row deleted
DESTROYING A VIEW:
Syntax: Drop View <View_Name>;
43
If you want to create an index on a combination of columns, you can list the column names
within the parentheses, separated by commas:
44
10. USER DEFINED EXCEPTIONS USING PL/SQL
AIM:
To execute user defined exceptions using PL/SQL
PL/SQL CODING
DECLARE
Sample_exception EXCEPTION;
PROCEDURE nested_block
IS
BEGIN
RAISE sample_exception;
EXCEPTION
END;
BEGIN
Nested_block;
EXCEPTION
45
Dbms_output.put_line ('Exception captured in main block');
END;
OUTPUT
46
REFERENCES
BOOKS
Web Links
1. https://www.appdynamics.com/topics/database-management-systems
2. https://www.javatpoint.com/dbms-tutorial
3. https://www.tutorialspoint.com/Relational-Database-Management-System-
RDMS
4. https://www.codecademy.com/article
5. https://www.ibm.com/analytics/relational-database
6. https://www.oracle.com/database/what-is-a-relational-database
47