Dbms Complete Notes - 152
Dbms Complete Notes - 152
1.Database System
What is Database?
The database is a collection of inter-related data which is used to retrieve, insert and
delete the data efficiently. It is also used to organize the data in the form of a table,
schema, views, and reports, etc.
For example: The college Database organizes the data about the admin, staff, students
and faculty etc.
Using the database, you can easily retrieve, insert, and delete the information.
Advantages of DBMS
Disadvantages of DBMS
o Cost of Hardware and Software: It requires a high speed of data processor and
large memory size to run DBMS software.
o Size: It occupies a large space of disks and large memory to run them
efficiently.
o Complexity: Database system creates additional complexity and requirements.
o Higher impact of failure: Failure is highly impacted the database because in
most of the organization, all the data stored in a single database and if the
database is damaged due to electric failure or database corruption then the data
may be lost forever.
Example:
These commands are used to update the database schema that's why they come under
Data definition language.
DML stands for Data Manipulation Language. It is used for accessing and
manipulating data in a database. It handles user requests.
For example: INSERT INTO javatpoint (Author, Subject) VALUES ("Sonoo", "DBMS");
Syntax:
UPDATE table_name SET [column_name1= value1,...column_nameN = valueN] [WHERE CONDITI
ON]
WHERE Author="Sonoo";
(But in Oracle database, the execution of data control language does not have
the feature of rolling back.)
Example:
GRANT SELECT, UPDATE ON MY_TABLE TO SOME_USER, ANOTHER_USER;
There are the following operations which have the authorization of Revoke:
TCL is used to run the changes made by the DML statement. TCL can be grouped into
a logical transaction.
Syntax: commit;
2.Rollback: It is used to restore the database to original since the last Commit.
Syntax: rollback;
1. Domain constraints
o Domain constraints can be defined as the definition of a valid set of values for an
attribute.
o The data type of domain includes string, character, integer, time, date, currency, etc.
The value of the attribute must be available in the corresponding domain.
Example:
Example:
3. Referential Integrity Constraints
o A referential integrity constraint is specified between two tables.
o In the Referential integrity constraints, if a foreign key in Table 1 refers to the Primary
Key of Table 2, then every value of the Foreign Key in Table 1 must be null or be
available in Table 2.
Example:
4. Key constraints
o Keys are the entity set that is used to identify an entity within its entity set uniquely.
o An entity set can have multiple keys, but out of which one key will be the primary key.
A primary key can contain a unique and null value in the relational table.
Example:
SQL JOIN
As the name shows, JOIN means to combine something. In case of SQL, JOIN means "to
combine two or more tables".
In SQL, JOIN clause is used to combine the records from two or more tables in a database.
1. INNER JOIN
2. LEFT JOIN
3. RIGHT JOIN
4. FULL JOIN
1. INNER JOIN
In SQL, INNER JOIN selects records that have matching values in both tables as long as the
condition is satisfied. It returns the combination of all rows from both the tables where the
condition satisfies.
FROM table1
Query
FROM EMPLOYEE
INNER JOIN PROJECT
ON PROJECT.EMP_ID = EMPLOYEE.EMP_ID;
Output:
EMP_NAME DEPARTMENT
Angelina Testing
Robert Development
Christian Designing
Kristen Development
2. LEFT JOIN
The SQL left join returns all the values from left table and the matching values from
the right table. If there is no matching join value, it will return NULL.
Syntax
FROM table1
LEFT JOIN table2
ON table1.matching_column = table2.matching_column;
Query
Angelina Testing
Robert Development
Christian Designing
Kristen Development
Russell NULL
Marry NULL
3. RIGHT JOIN
In SQL, RIGHT JOIN returns all the values from the values from the rows of right table and
the matched values from the left table. If there is no matching in both tables, it will return
NULL.
Syntax
Query
Output
EMP_NAME DEPARTMENT
Angelina Testing
Robert Development
Christian Designing
Kristen Development
4. FULL JOIN
In SQL, FULL JOIN is the result of a combination of both left and right outer join. Join
tables have all the records from both tables. It puts NULL on the place of matches not
found.
Syntax
Query
Output
EMP_NAME DEPARTMENT
Angelina Testing
Robert Development
Christian Designing
Kristen Development
Russell NULL
Marry NULL
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 .
1. Creating view
A view can be created using the CREATE VIEW statement. We can create a view from a single
table or multiple tables.
Syntax:
In this example, we create a View named DetailsView from the table Student_Detail.
Query:
Just like table query, we can query the view to view the data.
Output:
NAME ADDRESS
Stephan Delhi
Kathrin Noida
David Ghaziabad
View from multiple tables can be created by simply include multiple tables in the
SELECT statement.
In the given example, a view is created named MarksView from two tables
Student_Detail and Student_Marks.
Query
Stephan Delhi 97
Kathrin Noida 86
David Ghaziabad 74
Alina Gurugram 90
4. Deleting View
Syntax
Example:
DBMS Architecture
o The DBMS design depends upon its architecture. The basic client/server architecture
is used to deal with a large number of PCs, web servers, database servers and other
components that are connected with networks.
o The client/server architecture consists of many PCs and a workstation which are
connected via the network.
o DBMS architecture depends upon how users are connected to the database to get their
request done.
Database architecture can be seen as a single tier or multi-tier. But logically, database
architecture is of two types like: 2-tier architecture and 3-tier architecture.
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
o The 2-Tier architecture is same as basic client-server. In the two-tier architecture,
applications on the client end can directly communicate with the database at the server
side. For this interaction, API's like: ODBC, JDBC are used.
o The user interfaces and application programs are run on the client-side.
o The server side is responsible to provide the functionalities like: query processing and
transaction management.
o To communicate with the DBMS, client-side application establishes a connection with
the server side.
2-tier Architecture
3-Tier Architecture
o The 3-Tier architecture contains another layer between the client and server. In this
architecture, client can't directly communicate with the server.
o The application on the client-end interacts with an application server which further
communicates with the database system.
o End user has no idea about the existence of the database beyond the application server.
The database also has no idea about any other user beyond the application.
o The 3-Tier architecture is used in case of large web application.
Relational model can represent as a table with columns and rows. Each row is known as a
tuple. Each table of the column has a name or attribute.
Attribute: It contains the name of a column in a particular table. Each attribute Ai must have
a domain, dom(Ai)
Relational instance: In the relational database system, the relational instance is represented
by a finite set of tuples. Relation instances do not have duplicate tuples.
Relational schema: A relational schema contains the name of the relation and name of all
columns or attributes.
Relational key: In the relational key, each row has one or more attributes. It can identify the
row in the relation uniquely.
o In the given table, NAME, ROLL_NO, PHONE_NO, ADDRESS, and AGE are the
attributes.
o The instance of schema STUDENT has 5 tuples.
o t3 = <Laxman, 33289, 8583287182, Gurugram, 20>
Relational Algebra
1. Select Operation:
o The select operation selects tuples that satisfy a given predicate.
o It is denoted by sigma (σ).
o Notation: σ p(r)
Where:
σ isusedforselectionprediction
r isusedforrelation
p is used as a propositional logic formula which may use connectors like: AND OR
and NOT. These relational can use as relational operators like =, ≠, ≥, <, >, ≤.
Input:
1. σ BRANCH_NAME="perryride" (LOAN)
Output:
Where
Input:
Output:
NAME CITY
Jones Harrison
Smith Rye
Hays Harrison
Curry Rye
Johnson Brooklyn
Brooks Brooklyn
3. Union Operation:
o Suppose there are two tuples R and S. The union operation contains all the tuples that
are either in R or S or both in R & S.
o It eliminates the duplicate tuples. It is denoted by ∪.
1. Notation: R ∪ S
DEPOSITOR RELATION
CUSTOMER_NAME ACCOUNT_NO
Johnson A-101
Smith A-121
Mayes A-321
Turner A-176
Johnson A-273
Jones A-472
Lindsay A-284
Example:
BORROW RELATION
CUSTOMER_NAME LOAN_NO
Jones L-17
Smith L-23
Hayes L-15
Jackson L-14
Curry L-93
Smith L-11
Williams L-17
DEPOSITOR RELATION
CUSTOMER_NAME ACCOUNT_NO
Johnson A-101
Smith A-121
Mayes A-321
Turner A-176
Johnson A-273
Jones A-472
Lindsay A-284
CUSTOMER_NAME
Johnson
Smith
Hayes
Turner
Jones
Lindsay
Jackson
Curry
Williams
1. Notation: R ∩ S
Output:
CUSTOMER_NAME
Smith
Jones
5. Set Difference:
o Suppose there are two tuples R and S. The set intersection operation contains all tuples
that are in R but not in S.
o It is denoted by intersection minus (-).
1. Notation: R - S
Example: Using the above DEPOSITOR table and BORROW table
Input:
Output:
CUSTOMER_NAME
Jackson
Hayes
Willians
Curry
6. Cartesian product
o The Cartesian product is used to combine each row in one table with each row in the
other table. It is also known as a cross product.
o It is denoted by X.
1. Notation: E X D
Example:
EMPLOYEE
1 Smith A
2 Harry C
3 John B
DEPARTMENT
DEPT_NO DEPT_NAME
A Marketing
B Sales
C Legal
Input:
1. EMPLOYEE X DEPARTMENT
Output:
1 Smith A A Marketing
1 Smith A B Sales
1 Smith A C Legal
2 Harry C A Marketing
2 Harry C B Sales
2 Harry C C Legal
3 John B A Marketing
3 John B B Sales
3 John B C Legal
7. Rename Operation:
The rename operation is used to rename the output relation. It is denoted by rho (ρ).
Example: We can use the rename operator to rename STUDENT relation to STUDENT1.
1. ρ(STUDENT1, STUDENT)
SQL INTRODUCTION:
o SQL stands for Structured Query Language. It is used for storing and managing data
in relational database management system (RDMS).
o It is a standard language for Relational Database System. It enables a user to create,
read, update and delete relational databases and tables.
o SQL allows users to query the database in a number of ways, using English-like
statements.
Triggers:
Triggers are stored programs, which are automatically executed or fired when some events occur. A
trigger in SQL is a procedural code that is automatically executed in response to certain events on a
specified table. It is important to understand how these small codes make such a huge difference in
database performance.
• Create Trigger
These two keywords are used to specify that a trigger block is going to be declared.
• Trigger_Name
It specifies the name of the trigger. Trigger name has to be unique and shouldn’t repeat.
• ( Before | After )
This specifies when the trigger will be executed. It tells us the time at which the trigger is
initiated, i.e, either before the ongoing event or after.
• Before Triggers are used to update or validate record values before they’re saved to the
database.
• After Triggers are used to access field values that are set by the system and to effect changes in
other records. The records that activate the after trigger are read-only. We cannot use After
trigger if we want to update a record because it will lead to read-only error.
• [ Insert | Update | Delete ]
These are the DML operations and we can use either of them in a given trigger.
• on [ Table_Name ]
We need to mention the table name on which the trigger is being applied. Don’t forget to
use on keyword and also make sure the selected table is present in the database.
• [ for each row | for each column ]
• [ trigger_body]
It consists of queries that need to be executed when the trigger is called.
So this was all about a simple trigger. But we can also create a nested trigger that can do multi-process.
Also handling it and terminating it at the right time is very important. If we don’t end the trigger
properly it may lead to an infinite loop.
Example for Trigger:
In the below trigger, we are trying to calculate the percentage of the student as soon as his details are
updated to the database.
Operations in Triggers
We can perform many operations using triggers. Some may be simple and some may be a little complex,
but once if we go through the query its easy to understand.
• DROP A Trigger
• Display A Trigger
The below code will display all the triggers that are present.
1 SHOW TRIGGERS;
The below code will display all the triggers that are present in a particular database.
1 SHOW TRIGGERS
2 IN database_name;
Example:
We also look at some major variants of the triggers that is Before insert and After insert. We have
already seen a trigger in the example. But with the help of the table lets see how exactly this works.
As we have already understood how to create a trigger, now let’s understand the two variants of the
trigger those are Before insert and After insert. in order to implement them, let’s create a student table
with various columns as shown below:
1
2 CREATE TABLE Student(
studentID INT NOT NULL AUTO_INCREMENT,
3 FName VARCHAR(20),
4 LName VARCHAR(20),
5 Address VARCHAR(30),
6 City VARCHAR(15),
Marks INT,
7 PRIMARY KEY(studentID)
8 );
9
Now if we execute this query we get the following table.
Student ID Fname Lname Address City Marks
int Varchar(20) Varchar(20) Varchar(30) Varchar(15) int
Let’s try to use the first variant i.e, Before Insert
To use this variant we need one more table i.e, Percentage where the trigger will store the results. Use
the below code to create the Percentage Table.
2.Database Design
Database design is the organization of data according to a database model. The designer determines
what data must be stored and how the data elements interrelate. With this information, they can begin
to fit the data to the database model. Database management system manages the data accordingly.
1.Conceptual design
3.Logical design
4.Physical design
ER model
o ER model stands for an Entity-Relationship model. It is a high-level data model.
This model is used to define the data elements and relationship for a specified
system.
o It develops a conceptual design for the database. It also develops a very simple
and easy to design view of data.
o In ER modeling, the database structure is portrayed as a diagram called an
entity-relationship diagram.
Component of ER Diagram
1. Entity:
An entity may be any object, class, person or place. In the ER diagram, an entity can
be represented as rectangles.
Employee
a. Weak entity:
o An entity that depends on another entity called a weak entity. The weak entity
doesn't contain any key attribute of its own. The weak entity is represented by
a double rectangle.
2. Attribute:
The attribute is used to describe the property of an entity. Eclipse is used to represent
an attribute.
For example, id, age, contact number, name, etc. can be attributes of a student.
a. Key Attribute
The key attribute is used to represent the main characteristics of an entity. It represents
a primary key. The key attribute is represented by an ellipse with the text underlined.
b. Composite 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
An attribute that can be derived from other attribute is known as a derived attribute.
It can be represented by a dashed ellipse.
For example, A person's age changes over time and can be derived from another
attribute like Date of birth.
3. Relationship
a. One-to-One Relationship
When only one instance of an entity is associated with the relationship, then it is
known as 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.
For example, Scientist can invent many inventions, but the invention is done by the
only specific scientist.
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.
For example, Employee can assign by many projects and project can have many
employees.
Notation of ER diagram
Database can be represented using the notations. In ER diagram, many notations are
used to express the cardinality. These notations are as follows:
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
In one-to-one mapping, an entity in E1 is associated with at most one entity in E2, and
an entity in E2 is associated with at most one entity in E1.
One-to-many
Many-to-many
Keys
o Keys play an important role in the relational database.
o It is used to uniquely identify any record or row of data from the table. It is also
used to establish and identify relationships between tables.
For example: In Student table, ID is used as a key because it is unique for each student.
In PERSON table, passport_number, license_number, SSN are keys since they are
unique for each person.
Types of key:
1. Primary key
o It is the first key which is used to identify one and only one instance of an entity
uniquely. An entity can contain multiple keys as we saw in PERSON table. The key
which is most suitable from those lists become a primary key.
o In the EMPLOYEE table, ID can be primary key since it is unique for each employee.
In the EMPLOYEE table, we can even select License_Number and Passport_Number
as primary key since they are also unique.
o For each entity, selection of the primary key is based on requirement and developers.
2. Candidate key
o A candidate key is an attribute or set of an attribute which can uniquely identify
a tuple.
o The remaining attributes except for primary key are considered as a candidate
key. The candidate keys are as strong as the primary key.
For example: In the EMPLOYEE table, id is best suited for the primary key. Rest of the
attributes like SSN, Passport_Number, and License_Number, etc. are considered as a
candidate key.
3. Super Key
Super key is a set of an attribute which can uniquely identify a tuple. Super key is a
superset of a candidate key.
4. Foreign key
o Foreign keys are the column of the table which is used to point to the primary key of
another table.
o In a company, every employee works in a specific department, and employee and
department are two different entities. So we can't store the information of the
department in the employee table. That's why we link these two tables through the
primary key of one table.
o We add the primary key of the DEPARTMENT table, Department_Id as a new
attribute in the EMPLOYEE table.
o Now in the EMPLOYEE table, Department_Id is the foreign key, and both the tables
are related.
Three schema Architecture
o The three schema architecture is also called ANSI/SPARC architecture or three-level
architecture.
o This framework is used to describe the structure of a specific database system.
o The three schema architecture is also used to separate the user applications and
physical database.
o The three schema architecture contains three-levels. It breaks the database down into
three different categories.
1. Internal Level
o The internal level has an internal schema which describes the physical storage
structure of the database.
o The internal schema is also known as a physical schema.
o It uses the physical data model. It is used to define that how the data will be stored in
a block.
o The physical level is used to describe complex low-level data structures in detail.
2. Conceptual Level
o The conceptual schema describes the design of a database at the conceptual level.
Conceptual level is also known as logical level.
o The conceptual schema describes the structure of the whole database.
o The conceptual level describes what data are to be stored in the database and also
describes what relationship exists among those data.
o In the conceptual level, internal details such as an implementation of the data structure
are hidden.
o Programmers and database administrators work at this level.
3. External Level
o At the external level, a database contains several schemas that sometimes called as
subschema. The subschema is used to describe the different view of the database.
o An external schema is also known as view schema.
o Each view schema describes the database part that a particular user group is interested
and hides the remaining database from that user group.
o The view schema describes the end user interaction with database systems.
Functional Dependency
The functional dependency is a relationship that exists between two attributes. It typically exists
between the primary key and non-key attribute within a table.
1.X→Y
The left side of FD is known as a determinant, the right side of the production is known as a dependent.
For example:Assume we have an employee table with attributes: Emp_Id, Emp_Name, Emp_Address .
Here Emp_Id attribute can uniquely identify the Emp_Name attribute of employee table because if we
know the Emp_Id, we can tell that employee name associated with it.
Emp_Id → Emp_Name
Example:
ID→Name,
Name → DOB
Transitive Dependency
When an indirect relationship causes functional dependency it is called Transitive Dependency.
Multivalued Dependency
When existence of one or more rows in a table implies one or more other rows in the same table, then
the Multi-valued dependencies occur.
->->
For our example:
P->->Q
Q->->R
In the above case, Multivalued Dependency exists only if Q and R are independent attributes.
Partial Dependency
Partial Dependency occurs when a nonprime attribute is functionally dependent on part of a candidate
key.
The 2nd Normal Form (2NF) eliminates the Partial Dependency. Let us see an example −
<StudentProject>
As stated, the non-prime attributes i.e. StudentName and ProjectName should be functionally
dependent on part of a candidate key, to be Partial Dependent.
The StudentName can be determined by StudentID that makes the relation Partial Dependent.
The ProjectName can be determined by ProjectID, which that the relation Partial Dependent.
Normalization
o Normalization is the process of organizing the data in the database.
o Normalization is used to minimize the redundancy from a relation or set of relations. It is also
used to eliminate the undesirable characteristics like Insertion, Update and Deletion
Anomalies.
o Normalization divides the larger table into the smaller table and links them using relationship.
o The normal form is used to reduce redundancy from the database table.
o It states that an attribute of a table cannot hold multiple values. It must hold only single-valued
attribute.
o First normal form disallows the multi-valued attribute, composite attribute, and their
combinations.
EMPLOYEE table:
14 John 7272826385, UP
9064738238
The decomposition of the EMPLOYEE table into 1NF has been shown below:
14 John 7272826385 UP
14 John 9064738238 UP
o In the second normal form, all non-key attributes are fully functional dependent on the primary
key
Example: Let's assume, a school can store the data of teachers and the subjects they teach. In a school,
a teacher can teach more than one subject.
TEACHER table
25 Chemistry 30
25 Biology 30
47 English 35
83 Math 38
83 Computer 38
To convert the given table into 2NF, we decompose it into two tables:
TEACHER_DETAIL table:
TEACHER_ID TEACHER_AGE
25 30
47 35
83 38
TEACHER_SUBJECT table:
TEACHER_ID SUBJECT
25 Chemistry
25 Biology
47 English
83 Math
83 Computer
o A relation will be in 3NF if it is in 2NF and not contain any transitive partial dependency.
o 3NF is used to reduce the data duplication. It is also used to achieve the data integrity.
o If there is no transitive dependency for non-prime attributes, then the relation must be in third
normal form.
A relation is in third normal form if it holds atleast one of the following conditions for every non-trivial
function dependency X → Y.
1. X is a super key.
Example:
EMPLOYEE_DETAIL table:
Non-prime attributes: In the given table, all attributes except EMP_ID are non-prime.
That's why we need to move the EMP_CITY and EMP_STATE to the new <EMPLOYEE_ZIP>
table, with EMP_ZIP as a Primary key.
EMPLOYEE table:
EMPLOYEE_ZIP table:
201010 UP Noida
02228 US Boston
60007 US Chicago
06389 UK Norwich
462007 MP Bhopal
o A table is in BCNF if every functional dependency X → Y, X is the super key of the table.
o For BCNF, the table should be in 3NF, and for every FD, LHS is super key.
Example: Let's assume there is a company where employees work in more than one department.
EMPLOYEE table:
In the above table Functional dependencies are as follows:
1. EMP_ID → EMP_COUNTRY
2. EMP_DEPT → {DEPT_TYPE, EMP_DEPT_NO}
The table is not in BCNF because neither EMP_DEPT nor EMP_ID alone are keys.
To convert the given table into BCNF, we decompose it into three tables:
EMP_COUNTRY table:
EMP_ID EMP_COUNTRY
264 India
264 India
EMP_DEPT table:
EMP_DEPT_MAPPING table:
EMP_ID EMP_DEPT
D394 283
D394 300
D283 232
D283 549
Functional dependencies:
1. EMP_ID → EMP_COUNTRY
2. EMP_DEPT → {DEPT_TYPE, EMP_DEPT_NO}
Candidate keys:
Forthefirsttable: EMP_ID
Forthesecondtable: EMP_DEPT
For the third table: {EMP_ID, EMP_DEPT}
o A relation will be in 4NF if it is in Boyce Codd normal form and has no multi-valued
dependency.
o For a dependency A → B, if for a single value of A, multiple values of B exists, then the relation
will be a multi-valued dependency.
Example
STUDENT
21 Computer Dancing
21 Math Singing
34 Chemistry Dancing
74 Biology Cricket
59 Physics Hockey
The given STUDENT table is in 3NF, but the COURSE and HOBBY are two independent entity. Hence,
there is no relationship between COURSE and HOBBY.
In the STUDENT relation, a student with STU_ID, 21 contains two courses, Computer and Math and
two hobbies, Dancing and Singing. So there is a Multi-valued dependency on STU_ID, which leads to
unnecessary repetition of data.
So to make the above table into 4NF, we can decompose it into two tables:
STUDENT_COURSE
STU_ID COURSE
21 Computer
21 Math
34 Chemistry
74 Biology
59 Physics
STUDENT_HOBBY
Fifth
STU_ID HOBBY fifth
fifth
21 Dancing
21 Singing
34 Dancing
74 Cricket
59 Hockey
o 5NF is satisfied when all the tables are broken into as many tables as possible in order to avoid
redundancy.
Example
In the above table, John takes both Computer and Math class for Semester 1 but he doesn't take Math
class for Semester 2. In this case, combination of all these fields required to identify a valid data.
Suppose we add a new Semester as Semester 3 but do not know about the subject and who will be
taking that subject so we leave Lecturer and Subject as NULL. But all three columns together acts as a
primary key, so we can't leave other two columns blank.
So to make the above table into 5NF, we can decompose it into three relations P1, P2 & P3:
P1:
SEMESTER SUBJECT
Semester 1 Computer
Semester 1 Math
Semester 1 Chemistry
Semester 2 Math
P2
SUBJECT LECTURER
Computer Anshika
Computer John
Math John
Math Akash
Chemistry Praveen
P3
SEMSTER LECTURER
Semester 1 Anshika
Semester 1 John
Semester 1 John
Semester 2 Akash
Semester 1 Praveen
3.Database application design and development
Embedded Sql:
SQL is known as the Structured Query Language. It is the language that we use to perform operations
and transactions on the databases.
When we talk about industry-level applications we need properly connected systems which could
draw data from the database and present to the user. In such cases, the embedded SQL comes to our
rescue.
We embed SQL queries into high-level languages such that they can easily perform the logic part of
our analysis.
Some of the prominent examples of languages with which we embed SQL are as follows:
•
C++
•
Java
•
Python etc.
Dynamic sql:
Dynamic SQL is the process that we follow for programming SQL queries in such a way that the
queries are built dynamically with the application operations.
It helps us to manage big industrial applications and manage the transactions without any added
overhead.
With dynamic SQL we are free to create flexible SQL queries and the names of the variables or any
other parameters are passed when the application runs.
1. When we need to run dynamic queries on our database, mainly DML queries.
2. When we need to access an object which is not in existence during the compile time.
3. Whenever we need to optimize the run time of our queries.
4. When we need to instantiate the created logic blocks.
5. When we need to perform operations on application fed data using invoker rights.
In SQL procedures, a cursor make it possible to define a result set (a set of data rows) and perform
complex logic on a row by row basis.
By using the same mechanics, an SQL procedure can also define a result set and return it directly to
the caller of the SQL procedure or to a client application.
A cursor can be viewed as a pointer to one row in a set of rows. The cursor can only reference one
row at a time, but can move to other rows of the result set as needed.
To work with cursors you must use the following SQL statements:
• DECLARE CURSOR
• OPEN
• FETCH
• CLOSE
The following example demonstrates the basic use of a read-only cursor within an SQL procedure:
•
• CREATE PROCEDURE sum_salaries(OUT sum INTEGER)
• LANGUAGE SQL
• BEGIN
• DECLARE p_sum INTEGER;
• DECLARE p_sal INTEGER;
• DECLARE SQLSTATE CHAR(5) DEFAULT '00000';
• DECLARE c CURSOR FOR SELECT SALARY FROM EMPLOYEE;
•
• SET p_sum = 0;
•
• OPEN c;
•
• FETCH FROM c INTO p_sal;
•
• WHILE(SQLSTATE = '00000') DO
• SET p_sum = p_sum + p_sal;
• FETCH FROM c INTO p_sal;
• END WHILE;
•
• CLOSE c;
•
• SET sum = p_sum;
•
• END
JDBC:
JDBC stands for Java Database Connectivity. JDBC is a Java API to connect and execute the query with
the database. It is a part of JavaSE (Java Standard Edition). JDBC API uses JDBC drivers to connect with
the database. There are four types of JDBC drivers:
o Native Driver,
o Thin Driver
We can use JDBC API to access tabular data stored in any relational database. By the help of JDBC API,
we can save, update, delete and fetch data from the database. It is like Open Database Connectivity
(ODBC) provided by Microsoft.
Why Should We Use JDBC
Before JDBC, ODBC API was the database API to connect and execute the query with the database. But,
ODBC API uses ODBC driver which is written in C language (i.e. platform dependent and unsecured).
That is why Java has defined its own API (JDBC API) that uses JDBC drivers (written in Java language).
We can use JDBC API to handle database using Java program and can perform the following activities:
Authorization in sql
Authorization is a privilege provided by the Database Administer. Users of the database can only view
the contents they are authorized to view. The rest of the database is out of bounds to them.
• System Administrator - This is the highest administrative authorization for a user. Users
with this authorization can also execute some database administrator commands such as
restore or upgrade a database.
• System Control - This is the highest control authorization for a user. This allows
maintenance operations on the database but not direct access to data.
• System Maintenance - This is the lower level of system control authority. It also allows users
to maintain the database but within a database manager instance.
• System Monitor - Using this authority, the user can monitor the database and take snapshots
of it.
Applications of Internet
4.Query Evaluation
The query evaluation plan is also referred to as the query execution plan. A query execution
engine is responsible for generating the output of the given query. It takes the query
execution plan, executes it, and finally makes the output for the user query.
Query Processing in DBMS
Query Processing is the activity performed in extracting data from the database. In query processing,
it takes various steps for fetching the data from the database. The steps involved are:
2. Optimization
3. Evaluation
After translating the given query, we can execute each relational algebra operation by
using different algorithms. So, in this way, a query processing begins its working.
Evaluation
For this, with addition to the relational algebra translation, it is required to annotate the translated
relational algebra expression with the instructions used for specifying and evaluating each operation.
Thus, after translating the user query, the system executes a query evaluation plan.
o The annotations in the evaluation plan may refer to the algorithms to be used for the particular
index or the specific operations.
o Such relational algebra with annotations is referred to as Evaluation Primitives. The evaluation
primitives carry the instructions needed for the evaluation of the operation.
o Thus, a query evaluation plan defines a sequence of primitive operations used for evaluating a
query. The query evaluation plan is also referred to as the query execution plan.
o A query execution engine is responsible for generating the output of the given query. It takes
the query execution plan, executes it, and finally makes the output for the user query.
Optimization
o The cost of the query evaluation can vary for different types of queries.
Although the system is responsible for constructing the evaluation plan, the
user does need not to write their query efficiently.
o Usually, a database system generates an efficient query evaluation plan, which
minimizes its cost. This type of task performed by the database system and is
known as Query Optimization.
o For optimizing a query, the query optimizer should have an estimated cost
analysis of each operation. It is because the overall operation cost depends on
the memory allocations to several operations, execution costs, and so on.
Performance tuning
it takes time to become a Database Expert or an expert Database Administrator. This
all comes with lot of experience in various database designs and good trainings.
But the following list may be helpful for the beginners to have a nice database
performance −
• Use 3BNF database design explained in this tutorial in RDBMS Concepts
chapter.
• Avoid number-to-character conversions because numbers and characters
compare differently and lead to performance downgrade.
• While using SELECT statement, only fetch whatever information is required
and avoid using * in your SELECT queries because it would load the system
unnecessarily.
• Create your indexes carefully on all the tables where you have frequent search
operations. Avoid index on the tables where you have less number of search
operations and more number of insert and update operations.
• A full-table scan occurs when the columns in the WHERE clause do not have
an index associated with them. You can avoid a full-table scan by creating an
index on columns that are used as conditions in the WHERE clause of an SQL
statement.
• Be very careful of equality operators with real numbers and date/time values.
Both of these can have small differences that are not obvious to the eye but
that make an exact match impossible, thus preventing your queries from ever
returning rows.
• Use pattern matching judiciously. LIKE COL% is a valid WHERE condition,
reducing the returned set to only those records with data starting with the
string COL. However, COL%Y does not further reduce the returned results
set since %Y cannot be effectively evaluated. The effort to do the evaluation is
too large to be considered. In this case, the COL% is used, but the %Y is thrown
away. For the same reason, a leading wildcard %COL effectively prevents the
entire filter from being used.
• Fine tune your SQL queries examining the structure of the queries (and
subqueries), the SQL syntax, to discover whether you have designed your
tables to support fast data manipulation and written the query in an optimum
manner, allowing your DBMS to manipulate the data efficiently.
• For queries that are executed on a regular basis, try to use procedures. A
procedure is a potentially large group of SQL statements. Procedures are
compiled by the database engine and then executed. Unlike an SQL statement,
the database engine need not optimize the procedure before it is executed.
• Avoid using the logical operator OR in a query if possible. OR inevitably slows
down nearly any query against a table of substantial size.
• You can optimize bulk data loads by dropping indexes. Imagine the history
table with many thousands of rows. That history table is also likely to have
one or more indexes. When you think of an index, you normally think of faster
table access, but in the case of batch loads, you can benefit by dropping the
index(es).
• When performing batch transactions, perform COMMIT at after a fair number
of records creation in stead of creating them after every record creation.
• Plan to defragment the database on a regular basis, even if doing so means
developing a weekly routine.
• Explain plan − tool identifies the access path that will be taken when the SQL statement is
executed.
• tkprof − measures the performance by time elapsed during each phase of SQL statement
processing.
5.Database System Architectures
Architectures for DBMSs have followed trends similar to those for general computer system architectures . The
reason was that most users accessed such systems via computer terminals that did not have processing power
and only provided display capabilities .all processing was performed remotely on the computer system,
and only display information and controls were sent from the computer to the display terminals, which
were connected to the central computer via various types of communications networks.
At first, database systems used these computers similarly to how they had used display terminals, so
that the DBMS itself was still a centralized DBMS in which all the DBMS functionality, application
program execution, and user inter-face processing were carried out on one machine.
Other machines would be dedicated servers, and others would have both client and server
functionality.
The user interface programs and application programs can run on the client side. When DBMS access
is required, the program establishes a connection to the DBMS (which is on the server side); once the
connection is created, the client program can communicate with the DBMS. A standard called Open
Database Connectivity
The architectures described here are called two-tier architectures because the soft-ware components
are distributed over two systems: client and server. The advan-tages of this architecture are its
simplicity and seamless compatibility with existing systems. The emergence of the Web changed the
roles of clients and servers, leading to the three-tier architecture.
Parallel Database
In parallel database system data processing performance is improved by using multiple resources in
parallel. In this CPU, the disk is used parallel to enhance the processing performance.
Operations like data loading and query processing are performed parallel. The centralized and client
server database systems are not powerful enough to handle applications which need fast processing.
Parallel database systems have great advantages for online transaction processing and decision support
applications. Parallel processing divides a large task into multiple tasks and each task is performed
concurrently on several nodes. This gives a larger task to complete more quickly.
Architectural Models
There are several architectural models for parallel machines. The most important one are as follows −
• Shared-memory multiple CPU − Here, the computer has several simultaneously active CPUs
that are attached to an interconnection network and share a single main memory and a common
array of disk storage.
• Shared disk architecture − Here, each node has its own main memory but all nodes share mass
storage. In practice, each node also has multiple processors.
• Shared nothing architecture − Here, each node has its own mass storage as well as main
memory.
Distributed Database
It is a collection of multiple interconnected databases that are spread physically across various locations
that communicate via a computer network.
Distributed Database gives us the following −
• Modular Development − The term modular development includes that when we want to
expand the using system on different locations then we just need to add nodes to the current
network and these nodes do not interrupt the current network functionalities.
• Increases Reliability − The term increases reliability refers to, if one node on a network fails
then its work can be distributed between other nodes on the network, failing of one node
does not stop the system.
• Improves Performance − We all know that a small database is easy to handle compared to a
large database, so in a distributed database a large database is distributed into a small
database across various locations which are easy to handle with better performance.
• Increase Availability − The failure of one node will not affect data availability because data
can be obtained from various other nodes on the network.
• Faster Response − Data retrieval becomes more efficient as data is available locally.
An object-oriented database is organized around objects rather than actions, and data rather than
logic. For example, a multimedia record in a relational database can be a definable data object, as
opposed to an alphanumeric value.
XML Database:
XML Database is used to store huge amount of information in the XML format. As the use of XML is
increasing in every field, it is required to have a secured place to store the XML documents. The data
stored in the database can be queried using XQuery, serialized, and exported into a desired format.
• XML- enabled
• Native XML (NXD)
XML - Enabled Database
XML enabled database is nothing but the extension provided for the conversion of XML document.
This is a relational database, where data is stored in tables consisting of rows and columns. The tables
contain set of records, which in turn consist of fields.
<contact2>
<name>Manisha Patil</name>
<company>TutorialsPoint</company>
<phone>(011) 789-4567</phone>
</contact2>
</contact-info>
Here, a table of contacts is created that holds the records of contacts (contact1 and contact2), which in
turn consists of three entities − name, company and phone.
Datatypes in database
SQL Data Type is an attribute that specifies the type of data of any object. Each column, variable and
expression has a related data type in SQL. You can use these data types while creating your tables.
You can choose a data type for a table column based on your requirement.
SQL Server offers six categories of data types for your use which are listed below −
bit 0 1
Note − Here, datetime has 3.33 milliseconds accuracy where as smalldatetime has 1 minute accuracy.
Character Strings Data Types
1 char
Maximum length of 8,000 characters.( Fixed length non-Unicode characters)
varchar
2
Maximum of 8,000 characters.(Variable-length non-Unicode data).
varchar(max)
3
Maximum length of 2E + 31 characters, Variable-length non-Unicode data (SQL Server 2005
only).
text
4
Variable-length non-Unicode data with a maximum length of 2,147,483,647 characters.
1 nchar
Maximum length of 4,000 characters.( Fixed length Unicode)
nvarchar
2
Maximum length of 4,000 characters.(Variable length Unicode)
nvarchar(max)
3
Maximum length of 2E + 31 characters (SQL Server 2005 only).( Variable length Unicode)
ntext
4
Maximum length of 1,073,741,823 characters. ( Variable length Unicode )
1 binary
Maximum length of 8,000 bytes(Fixed-length binary data )
varbinary
2
Maximum length of 8,000 bytes.(Variable length binary data)
varbinary(max)
3
Maximum length of 2E + 31 bytes (SQL Server 2005 only). ( Variable length Binary data)
image
4
Maximum length of 2,147,483,647 bytes. ( Variable length Binary Data)
sql_variant
1
Stores values of various SQL Server-supported data types, except text, ntext, and
timestamp.
timestamp
2
Stores a database-wide unique number that gets updated every time a row gets
updated
uniqueidentifier
3
Stores a globally unique identifier (GUID)
xml
4
Stores XML data. You can store xml instances in a column or a variable (SQL Server
2005 only).
cursor
5
Reference to a cursor object
table
6
Stores a result set for later processing
Storage Devices
The cloud storage system stores multiple copies of data on multiple servers, at multiple locations. If
one system fails, then it is required only to change the pointer to the location, where the object is stored.
To aggregate the storage assets into cloud storage systems, the cloud provider can use storage
virtualization software known as StorageGRID. It creates a virtualization layer that fetches storage
from different storage devices into a single management system. It can also manage data
from CIFS and NFS file systems over the Internet. The following diagram shows how StorageGRID
virtualizes the storage into storage clouds:
Virtual Storage Containers
The virtual storage containers offer high performance cloud storage systems. Logical Unit Number
(LUN) of device, files and other objects are created in virtual storage containers. Following diagram
shows a virtual storage container, defining a cloud storage domain:
00:31/06:36
10 Sec
10.1M
236