0% found this document useful (0 votes)
27 views

Dbms Notes

Uploaded by

lavzzz2255
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
27 views

Dbms Notes

Uploaded by

lavzzz2255
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 10

Codd’s Rules in DBMS

Rule 1: The Information Rule


All information, whether it is user information or metadata, that is stored in a
database must be entered as a value in a cell of a table . It is said that everything
within the database is organized in a table layout.
Rule 2: The Guaranteed Access Rule
Each data element is guaranteed to be accessible logically with a combination of the
table name, primary key (row value), and attribute name (column value).
Rule 3: Systematic Treatment of NULL Values
Every Null value in a database must be given a systematic and uniform treatment.
Rule 4: Active Online Catalog Rule
The database catalog, which contains metadata about the database, must be stored
and accessed using the same relational database management system.
Rule 5: The Comprehensive Data Sublanguage Rule
A crucial component of any efficient database system is its ability to offer an easily
understandable data manipulation language (DML) that facilitates defining, querying,
and modifying information within the database.
Rule 6: The View Updating Rule
All views that are theoretically updatable must also be updatable by the system.
Rule 7: High-level Insert, Update, and Delete
A successful database system must possess the feature of facilitating high-level
insertions, updates, and deletions that can grant users the ability to conduct these
operations with ease through a single query.
Rule 8: Physical Data Independence
Application programs and activities should remain unaffected when changes are made
to the physical storage structures or methods.
Rule 9: Logical Data Independence
Application programs and activities should remain unaffected when changes are made
to the logical structure of the data, such as adding or modifying tables.
Rule 10: Integrity Independence
Integrity constraints should be specified separately from application programs and
stored in the catalog. They should be automatically enforced by the database system.
Rule 11: Distribution Independence
The distribution of data across multiple locations should be invisible to users, and the
database system should handle the distribution transparently.
Rule 12: Non-Subversion Rule
If the interface of the system is providing access to low-level records, then the
interface must not be able to damage the system and bypass security and integrity
constraints.
Different Types of Database Keys
 Candidate Key
 Primary Key
 Super Key
 Alternate Key
 Foreign Key
 Composite Key
Candidate Key
The minimal set of attributes that can uniquely identify a tuple is known as a candidate
key. For Example, STUD_NO in STUDENT relation.
 It is a minimal super key.
 It is a super key with no repeated data is called a candidate key.
 The minimal set of attributes that can uniquely identify a record.
 It must contain unique values.
 It can contain NULL values.
 Every table must have at least a single candidate key.
PRIMARY KEY
 It is a unique key.
 It can identify only one tuple (a record) at a time.
 It has no duplicate values, it has unique values.
 It cannot be NULL.
 Primary keys are not necessarily to be a single column; more than one column can
also be a primary key for a table.
Super Key
 Adding zero or more attributes to the candidate key generates the super key.
 A candidate key is a super key but vice versa is not true.
 Super Key values may also be NULL.
Alternate Key
The candidate key other than the primary key is called an alternate key .
 All the keys which are not primary keys are called alternate keys.
 It is a secondary key.
 It contains two or more fields to identify two or more records.
 These values are repeated.
 Eg:- SNAME, and ADDRESS is Alternate keys
Foreign Key
 It is a key it acts as a primary key in one table and it acts as
secondary key in another table.
 It combines two or more relations (tables) at a time.
 They act as a cross-reference between the tables.
 For example, DNO is a primary key in the DEPT table and a non-key in EMP
Composite Key
 It acts as a primary key if there is no primary key in a table
 Two or more attributes are used together to make a composite key .
 Different combinations of attributes may give different accuracy in terms of
identifying the rows uniquely.
Types of Integrity Constraints:
 Domain Constraints
 Not-Null Constraints
 Entity integrity Constraints
 Key Constraints
 Primary Key Constrains
 Referential integrity constraints
Domain Constraints
These are defined as the definition of valid set of values for an attribute. The data type
of domain include string, char, time, integer, date, currency etc. The value of the
attribute must be available in comparable domains.
Not-Null Constraints
It specifies that within a tuple, attributes overs which not-null constraint is specified
must not contain any null value.
Entity Integrity Constraints
Entity integrity constraints state that primary key can never contain null value because
primary key is used to determine individual rows in a relation uniquely, if primary key
contains null value then we cannot identify those rows. A table can contain null value
in it except primary key field.
Entity Integrity Constraints
Entity integrity constraints state that primary key can never contain null value because
primary key is used to determine individual rows in a relation uniquely, if primary key
contains null value then we cannot identify those rows. A table can contain null value
in it except primary key field.
Primary Key Constraints
It states that the primary key attributes are required to be unique and not null. That is,
primary key attributes of a relation must not have null values and primary key
attributes of two tuples must never be same. This constraint is specified on database
schema to the primary key attributes to ensure that no two tuples are same.
Referential integrity constraints
It can be specified between two tables. In case of referential integrity constraints, if
a Foreign key in Table 1 refers to Primary key of Table 2 then every value of the
Foreign key in Table 1 must be null or available in Table 2.

Relational Algebra
These are the basic/fundamental operators used in Relational Algebra.
1. Selection(ς) 2.Projection(π) 3.Union(U) 4.Set Difference(-) 5.Set Intersection(∩)
6. Rename(ρ) 7.Cartesian Product(X)
1.Selection(ς): It is used to select required tuples of the relations.2.Projection(π): It is
used to project required column data from a relation. 3.Union(U): Union operation in
relational algebra is the same as union operation in set theory.4.Set Difference(-): Set
Difference in relational algebra is the same set difference operation as in set
theory.5.Set Intersection(∩): Set Intersection in relational algebra is the same set
intersection operation in set theory.6.Rename(ρ): Rename is a unary operation used
for renaming attributes of a relation.7.Cross Product(X): Cross-product between two
relations. Let’s say A and B, so the cross product between A X B will result in all the
attributes of A followed by each attribute of B. Each record of A will pair with every
record of B.
What does a Transaction mean in DBMS?
 Transaction in Database Management Systems (DBMS) can be defined as a set of logically
related operations.
 It is the result of a request made by the user to access the contents of the database and perform
operations on it.
 It consists of various operations and has various states in its completion journey.
 It also has some specific properties that must be followed to keep the database consistent.
Operations of Transaction
i) Read(X)
 A read operation is used to read the value of X from the database and store it in a buffer
in the main memory for further actions such as displaying that value.
 Such an operation is performed when a user wishes just to see any content of the
database and not make any changes to it. For example, when a user wants to
check his/her account’s balance, a read operation would be performed on user’s account
balance from the database.
ii) Write(X)
 A write operation is used to write the value to the database from the buffer in the main
memory. For a write operation to be performed, first a read operation is performed to
bring its value in buffer, and then some changes are made to it, e.g. some set of
arithmetic operations are performed on it according to the user’s request, then to store
the modified value back in the database, a write operation is performed.
iii) Commit
 This operation in transactions is used to maintain integrity in the database. Due to some
failure of power, hardware, or software, etc., a transaction might get interrupted before
all its operations are completed. This may cause ambiguity in the database, i.e. it might
get inconsistent before and after the transaction.
 To ensure that further operations of any other transaction are performed only after work
of the current transaction is done, a commit operation is performed to the changes made
by a transaction permanently to the database.
iv) Rollback
 This operation is performed to bring the database to the last saved state when any
transaction is interrupted in between due to any power, hardware, or software failure.
 In simple words, it can be said that a rollback operation does undo the operations of
transactions that were performed before its interruption to achieve a safe state of the
database and avoid any kind of ambiguity or inconsistency.
Properties of Transaction
 As transactions deal with accessing and modifying the contents of the database, they
must have some basic properties which help maintain the consistency and integrity of the
database before and after the transaction. Transactions follow 4 properties,
namely, Atomicity, Consistency, Isolation, and Durability.
i) Atomicity
 This property ensures that either all operations of a transaction are executed or it is
aborted. In any case, a transaction can never be completed partially.
 Each transaction is treated as a single unit (like an atom). Atomicity is achieved through
commit and rollback operations, i.e. changes are made to the database only if all
operations related to a transaction are completed, and if it gets interrupted, any changes
made are rolled back using rollback operation to bring the database to its last saved state.
ii) Consistency
 This property of a transaction keeps the database consistent before and after a
transaction is completed.
 Execution of any transaction must ensure that after its execution, the database is either in
its prior stable state or a new stable state.
 In other words, the result of a transaction should be the transformation of a database
from one consistent state to another consistent state.
 Consistency, here means, that the changes made in the database are a result of logical
operations only which the user desired to perform and there is not any ambiguity.
iii) Isolation
 This property states that two transactions must not interfere with each other, i.e. if some
data is used by a transaction for its execution, then any other transaction can not
concurrently access that data until the first transaction has completed.
 It ensures that the integrity of the database is maintained and we don’t get any
ambiguous values. Thus, any two transactions are isolated from each other.
 This property is enforced by the concurrency control subsystem of DBMS.
iv) Durability
 This property ensures that the changes made to the database after a transaction is
completely executed, are durable.
 It indicates that permanent changes are made by the successful execution of a
transaction.
 In the event of any system failures or crashes, the consistent state achieved after the
completion of a transaction remains intact. The recovery subsystem of DBMS is
responsible for enforcing this property.
Normalization is the process of minimizing redundancy from a relation or set of relations.
Redundancy in relation may cause insertion, deletion, and update anomalies. So, it helps to minimize
the redundancy in relations. Normal forms are used to eliminate or reduce redundancy in database
tables.
Important Points Regarding Normal Forms in DBMS
 First Normal Form (1NF): This is the most basic level of normalization. In 1NF, each table cell
should contain only a single value, and each column should have a unique name. The first normal
form helps to eliminate duplicate data and simplify queries.
 Second Normal Form (2NF): 2NF eliminates redundant data by requiring that each non-key
attribute be dependent on the primary key. This means that each column should be directly
related to the primary key, and not to other columns.
 Third Normal Form (3NF): 3NF builds on 2NF by requiring that all non-key attributes are
independent of each other. This means that each column should be directly related to the
primary key, and not to any other columns in the same table.
 Fourth Normal Form (4NF): 4NF is a further refinement of BCNF that ensures that a table does
not contain any multi-valued dependencies.
 Fifth Normal Form (5NF): 5NF is the highest level of normalization and involves decomposing a
table into smaller tables to remove data redundancy and improve data integrity.
Normal forms help to reduce data redundancy, increase data consistency, and improve database
performance. However, higher levels of normalization can lead to more complex database designs
and queries. It is important to strike a balance between normalization and practicality when
designing a database.
Advantages of Normal Form
 Reduced data redundancy
 Improved data consistency
 Simplified database design
 Improved query performance
 Easier database maintenance
Query By Example (QBE) . QBE stands for Query By Example and it was developed in 1970 by
Moshe Zloof at IBM. It is a graphical query language where we get a user interface and then
we fill some required fields to get our proper result. In SQL we will get an error if the query is
not correct but in the case of QBE if the query is wrong either we get a wrong answer or the
query will not be going to execute but we will never get any error. In QBE we don’t write
complete queries like SQL or other database languages it comes with some blank so we need
to just fill that blanks and we will get our required result. SELECT NAME FROM
COLLEGE_DEPTc WHERE BRANCH = 'MCA'" Points about QBE: • Supported by most of the
database programs. • It is a Graphical Query Language. • Created in parallel to SQL
development. Features: – 2-D relation language. – Display results. – Fill up templates of
relations for query formulation. – Example of a possible answer is entered by the user.

SUBQUERY
 You can place the Subquery in a number of SQL clauses: WHERE clause, HAVING clause,
FROM clause. Subqueries can be used with SELECT, UPDATE, INSERT, DELETE statements
along with expression operator. It could be equality operator or comparison operator
such as =, >, =, <= and Like operator.
 A subquery is a query within another query. The outer query is called as main query and
inner query is called as subquery.
 The subquery generally executes first when the subquery doesn’t have any co-
relation with the main query, when there is a co-relation the parser takes the decision on
the fly on which query to execute on precedence and uses the output of the subquery
accordingly.
 Subquery must be enclosed in parentheses.
 Subqueries are on the right side of the comparison operator.
 Use single-row operators with single row Subqueries. Use multiple-row operators with
multiple-row Subqueries.
Syntax: There is not any general syntax for Subqueries. However, Subqueries are seen to be
used most frequently with SELECT statement as shown below:
SELECT column_name
FROM table_name
WHERE column_name expression operator
(SELECT column_name FROM table_name WHERE ...);
Decision-making statements in programming languages decide the direction of the flow of
program execution. Conditional Statements available in PL/SQL are defined below:
1. IF THEN
2. IF THEN ELSE
3. NESTED-IF-THEN
4. IF THEN ELSIF-THEN-ELSE Ladder
1. IF THEN
if then the statement is the most simple decision-making statement. It is used to decide
whether a certain statement or block of statements will be executed or not i.e if a certain
condition is true then a block of statement is executed otherwise not.
Syntax:
declare
-- declare the values here
begin
if condition then
dbms_output.put_line('output');
end if;
dbms_output.put_line('output2');
end;

2. IF THEN ELSE
The if statement alone tells us that if a condition is true it will execute a block of statements
and if the condition is false it won’t. But what if we want to do something else if the
condition is false. Here comes the else statement. We can use the else statement with if
statement to execute a block of code when the condition is false.
Syntax:-
declare
num1 number:= 10;
num2 number:= 20;
begin
if num1 < num2 then
dbms_output.put_line('i am in if block');
ELSE
dbms_output.put_line('i am in else Block');
end if;
dbms_output.put_line('i am not in if or else Block');
end;
3. NESTED-IF-THEN
A nested if-then is an if statement that is the target of another if statement. Nested if-then
statements mean an if statement inside another if statement. Yes, PL/SQL allows us to nest if
statements within if-then statements. i.e, we can place an if then statement inside another if
then statement.
Syntax:-
declare
num1 number:= 10;
num2 number:= 20;
num3 number:= 20;
begin
if num1 < num2 then
dbms_output.put_line('num1 small num2');
if num1 < num3 then
dbms_output.put_line('num1 small num3 also');
end if;
end if;
dbms_output.put_line('after end if');
end;
A Cursor in PL/SQL is a pointer to a context area that stores the result set of a query.
PL/SQL Cursors
The cursor is used to retrieve data one row at a time from the results set, unlike other SQL
commands that operate on all rows at once.
Cursors update table records in a singleton or row-by-row manner.
Cursor Actions
Key actions involved in working with cursors in PL/SQL are:
1. Declare Cursor: A cursor is declared by defining the SQL statement that returns a result
set.
2. Open: A Cursor is opened and populated by executing the SQL statement defined by the
cursor.
3. Fetch: When the cursor is opened, rows can be fetched from the cursor one by one or in a
block to perform data manipulation.
4. Close: After data manipulation, close the cursor explicitly.
5. Deallocate: Finally, delete the cursor definition and release all the system resources
associated with the cursor.
Types of Cursors in PL/SQL
Cursors are classified depending on the circumstances in which they are opened.
 Implicit Cursor: If the Oracle engine opened a cursor for its internal processing it is known
as an Implicit Cursor. It is created “automatically” for the user by Oracle when a query is
executed and is simpler to code.
 Explicit Cursor: A Cursor can also be opened for processing data through a PL/SQL block,
on demand. Such a user-defined cursor is known as an Explicit Cursor.
Explicit cursor
An explicit cursor is defined in the declaration section of the PL/SQL Block. It is created on
a SELECT Statement which returns more than one row.

Syntax for creating cursorCURSOR cursor_name IS select_statement;

Where,

 cursor_name: A suitable name for the cursor.


 select_statement: A select query which returns multiple rows
How to use Explicit Cursor?
There are four steps in using an Explicit Cursor.
1. DECLARE the cursor in the Declaration section.
2. OPEN the cursor in the Execution Section.
3. FETCH the data from the cursor into PL/SQL variables or records in the Execution Section.
4. CLOSE the cursor in the Execution Section before you end the PL/SQL Block.
PL/SQL Procedure
The PL/SQL stored procedure or simply a procedure is a PL/SQL block which performs one or
more specific tasks. It is just like procedures in other programming languages.

The procedure contains a header and a body.

o Header: The header contains the name of the procedure and the parameters or variables
passed to the procedure.
o Body: The body contains a declaration section, execution section and exception section similar
to a general PL/SQL block.

How to pass parameters in procedure:


When you want to create a procedure or function, you have to define parameters .There is three
ways to pass parameters in procedure:

1. IN parameters: The IN parameter can be referenced by the procedure or function. The value of the
parameter cannot be overwritten by the procedure or the function.
2. OUT parameters: The OUT parameter cannot be referenced by the procedure or function, but the
value of the parameter can be overwritten by the procedure or function.
3. INOUT parameters: The INOUT parameter can be referenced by the procedure or function and the
value of the parameter can be overwritten by the procedure or function.

Syntax for creating procedure:

1. CREATE [OR REPLACE] PROCEDURE procedure_name


2. [ (parameter [,parameter]) ]
3. IS
4. [declaration_section]
5. BEGIN
6. executable_section
7. [EXCEPTION
8. exception_section]
9. END [procedure_name];
PL/SQL functions are reusable blocks of code that can be used to perform specific tasks. They
are similar to procedures but must always return a value.
A function in PL/SQL contains:
 Function Header: The function header includes the function name and an optional
parameter list. It is the first part of the function and specifies the name and parameters.
 Function Body: The function body contains the executable statements that implement
the specific logic. It can include declarative statements, executable statements, and
exception-handling statements.
Triggers in SQL Server
A trigger is a set of SQL statements that reside in system memory with unique names. It is a
specialized category of stored procedure that is called automatically when a database server event
occurs. Each trigger is always associated with a table.A trigger is called a special
procedure because it cannot be called directly like a stored procedure. The key distinction
between the trigger and procedure is that a trigger is called automatically when a data modification
event occurs against a table. A stored procedure, on the other hand, must be invoked directly.The
following are the main characteristics that distinguish triggers from stored procedures:

o We cannot manually execute/invoked triggers.


o Triggers have no chance of receiving parameters.
o A transaction cannot be committed or rolled back inside a trigger.
Syntax of Trigger
1. CREATE TRIGGER schema.trigger_name
2. ON table_name
3. AFTER {INSERT, UPDATE, DELETE}
4. [NOT FOR REPLICATION]
5. AS
6. {SQL_Statements}
The parameter descriptions of this syntax illustrate below:schema: It is an optional parameter that
defines which schema the new trigger belongs to.trigger_name: It is a required parameter that
defines the name for the new trigger.

You might also like