MySQL Exception & Database
Jamshid Hashimi
Trainer, Cresco Solution
http://www.jamshidhashimi.com
jamshid@netlinks.af
@jamshidhashimi
ajamshidhashimi
Afghanistan Workforce
Development Program
Agenda
• Error
• Exception
• About Database & Hierarchical Model
• Specialization, Generalization & Aggregation
• Database Normalization
• Relationships
Error
• Error handling is important
– First, it lets the user know, in a relatively friendly
manner, that something has gone wrong and that
they should contact the technical support
department or that someone from tech support
has been notified.
– Second it allows the programmer to put in some
niceties to aid in the debugging of issues.
Error
• mysqli_connect_errno()
– Returns the error code from the last connection error
• mysqli_connect_error()
– Returns the error description from the last connection error
• mysqli_errno()
– Returns the last error code for the most recent function call
• mysqli_error_list()
– Returns a list of errors for the most recent function call
• mysqli_error()
– Returns the last error description for the most recent function
call
Exception
• With PHP 5 came a new object oriented way
of dealing with errors.
• Exception handling is used to change the
normal flow of the code execution if a
specified error (exceptional) condition occurs.
This condition is called an exception.
Exception
$arr = array(23,44,55,77,88,99);
try{
if(in_array(4,$arr)){
echo "FOUND";
}else{
throw new exception("4 not found in the array");
}
if(is_array($arr)){
echo "TRUE";
}else{
throw new customException("It is not an array");
}
}
catch(exception $e){
echo "Error occured in the file: ".$e->getFile()." and in line
".$e->getLine()." with a message: ".$e->getMessage();
}
catch(customException $e){
echo $e->errorMessage();
}
Database
• A database is an organized collection of
information. Small businesses can use databases
in a number of different ways. A database can
help you organize information about your
customers and clients. A database can contain
information about your product inventory. A
database can track sales, expenses and other
financial information.
• In a database an entity type is the equivalent of a
table. Each individual record is represented as a
row, and each attribute as a column.
Hierarchical Database Model
• IBM introduced the first generation of
database technology, known as hierarchical,
when it installed IMS (Information
Management System ) in its mainframe
computers in the mid-1960s. This technology
was upgraded in the mid-1970s to network
database technology. Both hierarchical and
network databases are often described by the
term hierarchical database.
Hierarchical Database Model
Hierarchical Database Model
• The structure allows representing information using
parent/child relationships: each parent can have many
children, but each child has only one parent (also known as
a 1-to-many relationship). All attributes of a specific record
are listed under an entity type.
• Currently the most widely used hierarchical databases are
IMS developed by IBM and Windows Registry by Microsoft.
• Mother–child relationship: Child may only have one mother
but a mother can have multiple children. Mothers and
children are tied together by links called "pointers". A
mother will have a list of pointers to each of her children.
Hierarchical Database Model
• "Hierarchical database systems are difficult to
use. They require application developers to
program navigation through the connected
records [...]
• "They also require developers to predict all
possible access patterns in advance and
design the database accordingly. A database
access pattern that is not included in the
design becomes very difficult and inefficient."
Aggregation
• Aggregation
– The aggregation is an abstraction concept, which
is used to build higher-level object (object as a
whole) from its components. For example, a
computer is built by using various related
components.
Aggregation
• The aggregation abstraction allows the
designer to either decompose objects by
breaking them into more detailed components
or to aggregate objects, grouping them
together into higher-level objects.
Decomposition is simply the. opposite process
to aggregation. Both processes are considered
the part of the aggregation abstraction.
Generalization
• Generalization is another powerful
abstraction. Generalization allows entities of
different types to be considered as examples
of a higher level set. It means that combining
different types of entities (or objects) into a
higher-level set or in more general entity
types is called generalization.
– For example, electronic device is a more general
entity type of electronic objects like computer, TV
set etc.
Specialization
• Categorizing the entities (or objects) in the set
according to their roles in a relationship, is
called specialization. Specialization is the
process of defining a set of subclasses of an
entity. The set of subclasses that form a
specialization are defined on the basis of
some distinguishing characteristics of the
entities in the super-class.
Specialization
• Clerk, manager, and programmer
– super-class 'Employee’
• An entity may have several specializations.
– EMPLOYEE
• 'Permanent-Employee'
• 'Temporary-Employee'
Database Normalization
• Normalization is the process of efficiently
organizing data in a database.
• There are two goals of the normalization
process:
– Eliminating redundant data (for example, storing
the same data in more than one table)
– Ensuring data dependencies make sense (only
storing related data in a table).
Database Normalization
• First Normal Form (1NF): First normal form
(1NF) sets the very basic rules for an organized
database:
– Eliminate duplicative columns from the same
table.
– Create separate tables for each group of related
data and identify each row with a unique column
or set of columns (the primary key).
Database Normalization
Database Normalization
Database Normalization
• Second Normal Form (2NF): Second normal
form (2NF) further addresses the concept of
removing duplicative data:
– Meet all the requirements of the first normal
form.
– Remove subsets of data that apply to multiple
rows of a table and place them in separate tables.
– Create relationships between these new tables
and their predecessors through the use of foreign
keys.
Database Normalization
Database Normalization
Database Normalization
• Third normal form (3NF) goes one large step
further:
– Meet all the requirements of the second normal
form.
– There is no transitive functional dependency
Database Normalization
Database Normalization
Database Normalization
• The Boyce-Codd Normal Form, also referred to
as the "third and half (3.5) normal form", adds
one more requirement:
– Meet all the requirements of the third normal
form.
– Every determinant must be a candidate key.
Database Normalization
• In order to convert a table to BCNF, we must:
1. Find and remove the overlapping candidate
keys. Place the part of the candidate key and
the attribute it is functionally dependent on,
in a different table.
2. Group the remaining items into a table.
• Suppose we have five columns in a table.
Primary Key
Database Normalization
• but column C can determine the value of column
B.
• In other words, the value of Column B is
determined by the Column C then this table can
not be in the Boyce - Codd Normal Form.
Database Normalization
• To convert this table into BCNF, we need to
divide into two tables.
• The table1 contains the following columns.
• where primary key is the combination of the
column A and B
Database Normalization
• And table2 will contain the two columns.
• where C will be the primary key.
• Thus every table satisfying the 3NF condition
cannot be said to be in BCNF normal form but a
table in BCNF will always be in 3NF form.
Database Normalization
• Consider the following non-BCNF table:
The candidate keys of the table are:
• {Person, Shop Type}
• {Person, Nearest Shop}
Database Normalization
Why it is not BCNF?
• The violation of BCNF means that the table is subject to
anomalies. For example, Eagle Eye might have its Shop
Type changed to "Optometrist" on its "Fuller" record
while retaining the Shop Type "Optician" on its
"Davidson" record.
Database Normalization
How to convert itn
into BCNF?
Database Normalization
• After normalization
Candidate keys are {person, shop}
and{shop},respectively.
Database Normalization
Database Normalization
• Fourth Normal Form (4NF): Finally, fourth
normal form (4NF) has one additional
requirement:
– Meet all the requirements of the third normal
form.
– A relation is in 4NF if it has no multi-valued
dependencies.
Database Normalization
Database Normalization
Relationship
• One-to-one relationships: Occur when each entry in the first table has one, and
only one, counterpart in the second table.
– One-to-one relationships are rarely used because it is often more efficient to simply put all of
the information in a single table.
• One-to-many relationships: Are the most common type of database relationship.
They occur when each record in the first table corresponds to one or more records
in the second table but each record in the second table corresponds to only one
record in the first table.
– For example, the relationship between a Teachers table and a Students table in an elementary
school database would likely be a one-to-many relationship, because each student has only
one teacher, but each teacher may have multiple students.
• Many-to-many relationships: Occur when each record in the first table
corresponds to one or more records in the second table and each record in the
second table corresponds to one or more records in the first table.
– For example, the relationship between a Teachers and a Courses table would likely be many-
to-many because each teacher may instruct more than one course and each course may have
more than one instructor.
ASSIGNMENT
QUESTIONS?

Exception & Database

  • 1.
    MySQL Exception &Database Jamshid Hashimi Trainer, Cresco Solution http://www.jamshidhashimi.com jamshid@netlinks.af @jamshidhashimi ajamshidhashimi Afghanistan Workforce Development Program
  • 2.
    Agenda • Error • Exception •About Database & Hierarchical Model • Specialization, Generalization & Aggregation • Database Normalization • Relationships
  • 3.
    Error • Error handlingis important – First, it lets the user know, in a relatively friendly manner, that something has gone wrong and that they should contact the technical support department or that someone from tech support has been notified. – Second it allows the programmer to put in some niceties to aid in the debugging of issues.
  • 4.
    Error • mysqli_connect_errno() – Returnsthe error code from the last connection error • mysqli_connect_error() – Returns the error description from the last connection error • mysqli_errno() – Returns the last error code for the most recent function call • mysqli_error_list() – Returns a list of errors for the most recent function call • mysqli_error() – Returns the last error description for the most recent function call
  • 5.
    Exception • With PHP5 came a new object oriented way of dealing with errors. • Exception handling is used to change the normal flow of the code execution if a specified error (exceptional) condition occurs. This condition is called an exception.
  • 6.
    Exception $arr = array(23,44,55,77,88,99); try{ if(in_array(4,$arr)){ echo"FOUND"; }else{ throw new exception("4 not found in the array"); } if(is_array($arr)){ echo "TRUE"; }else{ throw new customException("It is not an array"); } } catch(exception $e){ echo "Error occured in the file: ".$e->getFile()." and in line ".$e->getLine()." with a message: ".$e->getMessage(); } catch(customException $e){ echo $e->errorMessage(); }
  • 7.
    Database • A databaseis an organized collection of information. Small businesses can use databases in a number of different ways. A database can help you organize information about your customers and clients. A database can contain information about your product inventory. A database can track sales, expenses and other financial information. • In a database an entity type is the equivalent of a table. Each individual record is represented as a row, and each attribute as a column.
  • 8.
    Hierarchical Database Model •IBM introduced the first generation of database technology, known as hierarchical, when it installed IMS (Information Management System ) in its mainframe computers in the mid-1960s. This technology was upgraded in the mid-1970s to network database technology. Both hierarchical and network databases are often described by the term hierarchical database.
  • 9.
  • 10.
    Hierarchical Database Model •The structure allows representing information using parent/child relationships: each parent can have many children, but each child has only one parent (also known as a 1-to-many relationship). All attributes of a specific record are listed under an entity type. • Currently the most widely used hierarchical databases are IMS developed by IBM and Windows Registry by Microsoft. • Mother–child relationship: Child may only have one mother but a mother can have multiple children. Mothers and children are tied together by links called "pointers". A mother will have a list of pointers to each of her children.
  • 11.
    Hierarchical Database Model •"Hierarchical database systems are difficult to use. They require application developers to program navigation through the connected records [...] • "They also require developers to predict all possible access patterns in advance and design the database accordingly. A database access pattern that is not included in the design becomes very difficult and inefficient."
  • 12.
    Aggregation • Aggregation – Theaggregation is an abstraction concept, which is used to build higher-level object (object as a whole) from its components. For example, a computer is built by using various related components.
  • 13.
    Aggregation • The aggregationabstraction allows the designer to either decompose objects by breaking them into more detailed components or to aggregate objects, grouping them together into higher-level objects. Decomposition is simply the. opposite process to aggregation. Both processes are considered the part of the aggregation abstraction.
  • 14.
    Generalization • Generalization isanother powerful abstraction. Generalization allows entities of different types to be considered as examples of a higher level set. It means that combining different types of entities (or objects) into a higher-level set or in more general entity types is called generalization. – For example, electronic device is a more general entity type of electronic objects like computer, TV set etc.
  • 15.
    Specialization • Categorizing theentities (or objects) in the set according to their roles in a relationship, is called specialization. Specialization is the process of defining a set of subclasses of an entity. The set of subclasses that form a specialization are defined on the basis of some distinguishing characteristics of the entities in the super-class.
  • 16.
    Specialization • Clerk, manager,and programmer – super-class 'Employee’ • An entity may have several specializations. – EMPLOYEE • 'Permanent-Employee' • 'Temporary-Employee'
  • 17.
    Database Normalization • Normalizationis the process of efficiently organizing data in a database. • There are two goals of the normalization process: – Eliminating redundant data (for example, storing the same data in more than one table) – Ensuring data dependencies make sense (only storing related data in a table).
  • 18.
    Database Normalization • FirstNormal Form (1NF): First normal form (1NF) sets the very basic rules for an organized database: – Eliminate duplicative columns from the same table. – Create separate tables for each group of related data and identify each row with a unique column or set of columns (the primary key).
  • 19.
  • 20.
  • 21.
    Database Normalization • SecondNormal Form (2NF): Second normal form (2NF) further addresses the concept of removing duplicative data: – Meet all the requirements of the first normal form. – Remove subsets of data that apply to multiple rows of a table and place them in separate tables. – Create relationships between these new tables and their predecessors through the use of foreign keys.
  • 22.
  • 23.
  • 24.
    Database Normalization • Thirdnormal form (3NF) goes one large step further: – Meet all the requirements of the second normal form. – There is no transitive functional dependency
  • 25.
  • 26.
  • 27.
    Database Normalization • TheBoyce-Codd Normal Form, also referred to as the "third and half (3.5) normal form", adds one more requirement: – Meet all the requirements of the third normal form. – Every determinant must be a candidate key.
  • 28.
    Database Normalization • Inorder to convert a table to BCNF, we must: 1. Find and remove the overlapping candidate keys. Place the part of the candidate key and the attribute it is functionally dependent on, in a different table. 2. Group the remaining items into a table.
  • 29.
    • Suppose wehave five columns in a table. Primary Key Database Normalization
  • 30.
    • but columnC can determine the value of column B. • In other words, the value of Column B is determined by the Column C then this table can not be in the Boyce - Codd Normal Form. Database Normalization
  • 31.
    • To convertthis table into BCNF, we need to divide into two tables. • The table1 contains the following columns. • where primary key is the combination of the column A and B Database Normalization
  • 32.
    • And table2will contain the two columns. • where C will be the primary key. • Thus every table satisfying the 3NF condition cannot be said to be in BCNF normal form but a table in BCNF will always be in 3NF form. Database Normalization
  • 33.
    • Consider thefollowing non-BCNF table: The candidate keys of the table are: • {Person, Shop Type} • {Person, Nearest Shop} Database Normalization
  • 34.
    Why it isnot BCNF? • The violation of BCNF means that the table is subject to anomalies. For example, Eagle Eye might have its Shop Type changed to "Optometrist" on its "Fuller" record while retaining the Shop Type "Optician" on its "Davidson" record. Database Normalization
  • 35.
    How to convertitn into BCNF? Database Normalization
  • 36.
    • After normalization Candidatekeys are {person, shop} and{shop},respectively. Database Normalization
  • 37.
    Database Normalization • FourthNormal Form (4NF): Finally, fourth normal form (4NF) has one additional requirement: – Meet all the requirements of the third normal form. – A relation is in 4NF if it has no multi-valued dependencies.
  • 38.
  • 39.
  • 40.
    Relationship • One-to-one relationships:Occur when each entry in the first table has one, and only one, counterpart in the second table. – One-to-one relationships are rarely used because it is often more efficient to simply put all of the information in a single table. • One-to-many relationships: Are the most common type of database relationship. They occur when each record in the first table corresponds to one or more records in the second table but each record in the second table corresponds to only one record in the first table. – For example, the relationship between a Teachers table and a Students table in an elementary school database would likely be a one-to-many relationship, because each student has only one teacher, but each teacher may have multiple students. • Many-to-many relationships: Occur when each record in the first table corresponds to one or more records in the second table and each record in the second table corresponds to one or more records in the first table. – For example, the relationship between a Teachers and a Courses table would likely be many- to-many because each teacher may instruct more than one course and each course may have more than one instructor.
  • 41.
  • 42.