Dbms Notes
Dbms Notes
Dbms Notes
Let's assume 'variable a' and 'variable b'. Here, 'a' contains 20 and
'b' contains 10.
- It is used to subtract the right-hand operand from the a-b will give
left-hand operand. 10
% It is used to divide the left-hand operand by the right- a%b will give
hand operand and returns reminder. 0
Let's assume 'variable a' and 'variable b'. Here, 'a' contains 20 and
'b' contains 10.
> It checks if the left operand value is greater than right (a>b) is not
operand value, if yes then condition becomes true. true
< It checks if the left operand value is less than right (a<b) is true
operand value, if yes then condition becomes true.
>= It checks if the left operand value is greater than or (a>=b) is not
equal to the right operand value, if yes then condition true
becomes true.
<= It checks if the left operand value is less than or equal (a<=b) is
to the right operand value, if yes then condition true
becomes true.
!< It checks if the left operand value is not less than the (a!=b) is not
right operand value, if yes then condition becomes true
true.
!> It checks if the left operand value is not greater than (a!>b) is
the right operand value, if yes then condition true
becomes true.
Operator Description
BETWEEN It is used to search for values that are within a set of values.
Operation on Table
1. Create table
2. Drop table
3. Delete table
4. Rename table
Syntax
Example
If you create the table successfully, you can verify the table by
looking at the message by the SQL server. Else you can use DESC
command as follows:
Now you have an EMPLOYEE table in the database, and you can
use the stored information related to the employees.
Drop table
A SQL drop table is used to delete a table definition and all the
data from a table. When this command is executed, all the
information available in the table is lost forever, so you have to
very careful while using this command.
Syntax
Firstly, you need to verify the EMPLOYEE table using the following
command:
Now, we can check whether the table exists or not using the
following command:
Syntax
Example
If you don't specify the WHERE condition, it will remove all the
rows from the table.
Views in SQL
o Views in SQL are considered as a virtual table. A view also
contains rows and columns.
o To create the view, we can select the fields from one or more
tables present in the database.
o A view can either have specific rows based on certain
condition or all the rows of a table.
Sample table:
Student_Detail
1 Stephan Delhi
2 Kathrin Noida
3 David Ghaziabad
4 Alina Gurugram
Student_Marks
1 Stephan 97 19
2 Kathrin 86 21
3 David 74 18
4 Alina 90 20
5 John 96 18
1. Creating view
Syntax:
1. CREATE VIEW view_name AS
2. SELECT column1, column2.....
3. FROM table_name
4. WHERE condition;
2. Creating View from a single table
Query:
Just like table query, we can query the view to view the data.
Output:
NAME ADDRESS
Stephan Delhi
Kathrin Noida
David Ghaziabad
Query:
1. CREATE VIEW MarksView AS
2. SELECT Student_Detail.NAME, Student_Detail.ADDRESS, Student
_Marks.MARKS
3. FROM Student_Detail, Student_Mark
4. WHERE Student_Detail.NAME = Student_Marks.NAME;
Stephan Delhi 97
Kathrin Noida 86
David Ghaziabad 74
Alina Gurugram 90
4. Deleting View
Syntax
Example:
Syntax
Example
Syntax
Example
Example
Example 1:
All the records present in the customers table are displayed in the
ascending order of the customer's name.
Example 2:
Write a query to sort the records in the ascending order of the
addresses stored in the customers table.
Query:
Example 3:
Query:
All the records present in the customers table are displayed in the
descending order of the customer's salary.
Example 4:
Query:
All the records present in the customers table are displayed in the
descending order of the customer's age.
Example 1:
Query:
All the records present in the agents table are displayed in the
ascending order of the agent's name.
Example 2:
Query:
All the records present in the agents table are displayed in the
descending order of the customer's work area.
Example 3:
Query:
All the records present in the agents table are displayed in the
ascending order of the customer's salary.
Example 4:
Important Rule:
Syntax
1. SELECT column_name
2. FROM table_name
3. WHERE column_name expression operator
4. ( SELECT column_name from table_name WHERE ... );
Example
1 John 20 US 2000.00
4 Alina 29 UK 6500.00
1. SELECT *
2. FROM EMPLOYEE
3. WHERE ID IN (SELECT ID
4. FROM EMPLOYEE
5. WHERE SALARY > 4500);
4 Alina 29 UK 6500.00
Syntax:
Example
Syntax
1. UPDATE table
2. SET column_name = new_value
3. WHERE VALUE OPERATOR
4. (SELECT COLUMN_NAME
5. FROM TABLE_NAME
6. WHERE condition);
Example
1. UPDATE EMPLOYEE
2. SET SALARY = SALARY * 0.25
1 John 20 US 2000.00
4 Alina 29 UK 1625.00
This would impact three rows, and finally, the EMPLOYEE table
would have the following records.
Syntax
1. DELETE FROM TABLE_NAME
2. WHERE VALUE OPERATOR
3. (SELECT COLUMN_NAME
4. FROM TABLE_NAME
5. WHERE condition);
Example
1 John 20 US 2000.00
o System-defined Exceptions
o User-defined Exceptions
1. DECLARE
2. <declarations section>
3. BEGIN
4. <executable command(s)>
5. EXCEPTION
6. <exception handling goes here >
7. WHEN exception1 THEN
8. exception1-handling-statements
9. WHEN exception2 THEN
10. exception2-handling-statements
11. WHEN exception3 THEN
12. exception3-handling-statements
13. ........
14. WHEN others THEN
15. exception3-handling-statements
16. END;
Example of exception handling
1. DECLARE
2. c_id customers.id%type := 8;
3. c_name customers.name%type;
4. c_addr customers.address%type;
5. BEGIN
6. SELECT name, address INTO c_name, c_addr
7. FROM customers
8. WHERE id = c_id;
9. DBMS_OUTPUT.PUT_LINE ('Name: '|| c_name);
10. DBMS_OUTPUT.PUT_LINE ('Address: ' || c_addr);
11. EXCEPTION
12. WHEN no_data_found THEN
13. dbms_output.put_line('No such customer!');
14. WHEN others THEN
15. dbms_output.put_line('Error!');
16. END;
17. /
No such customer!
PL/SQL procedure successfully completed.
Note: You get the result "No such customer" because the
customer_id used in the above example is 8 and there is no
cutomer having id value 8 in that table.
If you use the id defined in the above table (i.e. 1 to 6), you will get
a certain result. For a demo example: here, we are using the id 5.
1. DECLARE
2. c_id customers.id%type := 5;
3. c_name customers.name%type;
4. c_addr customers.address%type;
5. BEGIN
6. SELECT name, address INTO c_name, c_addr
7. FROM customers
8. WHERE id = c_id;
9. DBMS_OUTPUT.PUT_LINE ('Name: '|| c_name);
10. DBMS_OUTPUT.PUT_LINE ('Address: ' || c_addr);
11. EXCEPTION
12. WHEN no_data_found THEN
13. dbms_output.put_line('No such customer!');
14. WHEN others THEN
15. dbms_output.put_line('Error!');
16. END;
17. /
After the execution of above code at SQL prompt, you will get the
following result:
Name: alex
Address: paris
PL/SQL procedure successfully completed.
Raising Exceptions
1. DECLARE
2. exception_name EXCEPTION;
3. BEGIN
4. IF condition THEN
5. RAISE exception_name;
6. END IF;
7. EXCEPTION
8. WHEN exception_name THEN
9. statement;
10. END;
PL/SQL User-defined Exceptions
1. DECLARE
2. my-exception EXCEPTION;
PL/SQL Pre-defined Exceptions
PL/SQL Procedure
Table creation:
Procedure Code:
Output:
Procedure created.
PL/SQL program to call procedure
1. BEGIN
2. insertuser(101,'Rahul');
3. dbms_output.put_line('record inserted successfully');
4. END;
5. /
Now, see the "USER" table, you will see one record is inserted.
ID Name
101 Rahul
PL/SQL - Packages
In this chapter, we will discuss the Packages in PL/SQL. Packages
are schema objects that groups logically related PL/SQL types,
variables, and subprograms.
A package will have two mandatory parts −
Package specification
Package body or definition
Package Specification
The specification is the interface to the package. It
just DECLARES the types, variables, constants, exceptions,
cursors, and subprograms that can be referenced from outside
the package. In other words, it contains all information about the
content of the package, but excludes the code for the
subprograms.
All objects placed in the specification are called public objects.
Any subprogram not in the package specification but coded in the
package body is called a private object.
The following code snippet shows a package specification having
a single procedure. You can have many global variables defined
and multiple procedures or functions inside a package.
CREATE PACKAGE cust_sal AS
PROCEDURE find_sal(c_id customers.id%type);
END cust_sal;
/
When the above code is executed at the SQL prompt, it produces
the following result −
Package created.
Package Body
The package body has the codes for various methods declared in
the package specification and other private declarations, which
are hidden from the code outside the package.
The CREATE PACKAGE BODY Statement is used for creating the
package body. The following code snippet shows the package
body declaration for the cust_sal package created above. I
assumed that we already have CUSTOMERS table created in our
database as mentioned in the PL/SQL - Variables chapter.
CREATE OR REPLACE PACKAGE BODY cust_sal AS
+----+----------+-----+-----------+----------+
| ID | NAME | AGE | ADDRESS | SALARY |
+----+----------+-----+-----------+----------+
| 1 | Ramesh | 32 | Ahmedabad | 3000.00 |
| 2 | Khilan | 25 | Delhi | 3000.00 |
| 3 | kaushik | 23 | Kota | 3000.00 |
| 4 | Chaitali | 25 | Mumbai | 7500.00 |
| 5 | Hardik | 27 | Bhopal | 9500.00 |
| 6 | Komal | 22 | MP | 5500.00 |
+----+----------+-----+-----------+----------+
The Package Specification
CREATE OR REPLACE PACKAGE c_package AS
-- Adds a customer
PROCEDURE addCustomer(c_id customers.id%type,
c_name customers.Name%type,
c_age customers.age%type,
c_addr customers.address%type,
c_sal customers.salary%type);
-- Removes a customer
PROCEDURE delCustomer(c_id customers.id%TYPE);
--Lists all customers
PROCEDURE listCustomer;
END c_package;
/
When the above code is executed at the SQL prompt, it creates
the above package and displays the following result −
Package created.
Creating the Package Body
CREATE OR REPLACE PACKAGE BODY c_package AS
PROCEDURE addCustomer(c_id customers.id%type,
c_name customers.Name%type,
c_age customers.age%type,
c_addr customers.address%type,
c_sal customers.salary%type)
IS
BEGIN
INSERT INTO customers (id,name,age,address,salary)
VALUES(c_id, c_name, c_age, c_addr, c_sal);
END addCustomer;
PROCEDURE listCustomer IS
CURSOR c_customers is
SELECT name FROM customers;
TYPE c_list is TABLE OF customers.Name%type;
name_list c_list := c_list();
counter integer :=0;
BEGIN
FOR n IN c_customers LOOP
counter := counter +1;
name_list.extend;
name_list(counter) := n.name;
dbms_output.put_line('Customer(' ||counter||
')'||name_list(counter));
END LOOP;
END listCustomer;
END c_package;
/
The above example makes use of the nested table. We will discuss
the concept of nested table in the next chapter.
When the above code is executed at the SQL prompt, it produces
the following result −
Package body created.
Using The Package
The following program uses the methods declared and defined in
the package c_package.
DECLARE
code customers.id%type:= 8;
BEGIN
c_package.addcustomer(7, 'Rajnish', 25, 'Chennai', 3500);
c_package.addcustomer(8, 'Subham', 32, 'Delhi', 7500);
c_package.listcustomer;
c_package.delcustomer(code);
c_package.listcustomer;
END;
/
When the above code is executed at the SQL prompt, it produces
the following result −
Customer(1): Ramesh
Customer(2): Khilan
Customer(3): kaushik
Customer(4): Chaitali
Customer(5): Hardik
Customer(6): Komal
Customer(7): Rajnish
Customer(8): Subham
Customer(1): Ramesh
Customer(2): Khilan
Customer(3): kaushik
Customer(4): Chaitali
Customer(5): Hardik
Customer(6): Komal
Customer(7): Rajnish
1-Tier Architecture
o In this architecture, the database is directly available to the
user. It means the user can directly sit on the DBMS and uses
it.
o Any changes done here will directly be done on the database
itself. It doesn't provide a handy tool for end users.
o The 1-Tier architecture is used for development of the local
application, where programmers can directly communicate
with the database for the quick response.
2-Tier Architecture
3-Tier Architecture
Data Models
2) Entity-Relationship
Relationship Data Model
Model: An ER model is the logical
representation of data as objects and relationships among them.
These objects are known as entities, and relationship is an
association among these entities. This model was designed by
Peter Chen and published in 1976 papers. IItt was widely used in
database designing. A set of attributes describe the entities. For
example, student_name, student_id describes the 'student' entity.
A set of the same type of entities is known as an 'Entity set', and
the set of the same type of relati
relationships
onships is known as 'relationship
set'.
3) Object-based
based Data Model: An extension of the ER model with
notions of functions, encapsulation, and object identity, as well.
This model supports a rich type system that includes structured
and collection types. Thus,
Thus, in 1980s, various database systems
following the object-oriented
oriented approach were developed. Here, the
objects are nothing but the data carrying its properties.
4) Semistructured Data Model: This type of data model is different
from the other three data models (explained above). The
semistructured data model allows the data specifications at
places where the individual data items of the same type may have
different attributes sets. The Extensible Markup Language, also
known as XML, is widely used for representing the semistructured
data. Although XML was initially designed for including the markup
information to the text document, it gains importance because of
its application in the exchange of data.
1. Entity:
2. Attribute
For example, id, age, contact number, name, etc. can be attributes
of a student.
a. Key Attribute
c. Multivalued Attribute
An attribute can have more than one value. These attributes are
known as a multivalued attribute. The double oval is used to
represent multivalued attribute.
For example, a student can have more than one phone number.
d. Derived Attribute
3. Relationship
a. One-to-One Relationship
For example, A female can marry to one male, and a male can
marry to one female.
b. One-to-many relationship
When only one instance of the entity on the left, and more than one
instance of an entity on the right associates with the relationship
then this is known as a one-to-many relationship.
c. Many-to-one relationship
When more than one instance of the entity on the left, and only one
instance of an entity on the right associates with the relationship
then it is known as a many-to-one relationship.
For example, Student enrolls for only one course, but a course can
have many students.
d. Many-to-many relationship
When more than one instance of the entity on the left, and more
than one instance of an entity on the right associates with the
relationship then it is known as a many-to-many relationship.
Mapping Constraints
o A mapping constraint is a data constraint that expresses the
number of entities to which another entity can be related via
a relationship set.
o It is most useful in describing the relationship sets that
involve more than two entity sets.
o For binary relationship set R on an entity set A and B, there
are four possible mapping cardinalities. These are as
follows:
1. One to one (1:1)
2. One to many (1:M)
3. Many to one (M:1)
4. Many to many (M:M)
One-to-one
One-to-many
Many-to-one
Generalization
o Generalization is like a bottom-up approach in which two or
more entities of lower level combine to form a higher level
entity if they have some attributes in common.
o In generalization, an entity of a higher level can also combine
with the entities of the lower level to form a further higher
level entity.
o Generalization is more like subclass and superclass system,
but the only difference is the approach. Generalization uses
the bottom-up approach.
o In generalization, entities are combined to form a more
generalized entity, i.e., subclasses are combined to make a
superclass.
In addition to this, the system may also store some statistical and
descriptive data about the relations, such as:
Inclusion Dependency
o Multivalued dependency and join dependency can be used to
guide database design although they both are less common
than functional dependencies.
o Inclusion dependencies are quite common. They typically
show little influence on designing of the database.
o The inclusion dependency is a statement in which some
columns of a relation are contained in other columns.
o The example of inclusion dependency is a foreign key. In one
relation, the referring relation is contained in the primary key
column(s) of the referenced relation.
o Suppose we have two relations R and S which was obtained
by translating two entity sets such that every R entity is also
an S entity.
o Inclusion dependency would be happen if projecting R on its
key attributes yields a relation that is contained in the
relation obtained by projecting S on its key attributes.
o In inclusion dependency, we should not split groups of
attributes that participate in an inclusion dependency.
o In practice, most inclusion dependencies are key-based that
is involved only keys.