Week10 CM MDL CC225
Week10 CM MDL CC225
Learning Outcomes:
After completing this course you are expected to demonstrate the following:
1. Explains the data integrity and constraints in SQL and its standard
standards. Elaborate the
triggers and active datab
databases
ases and further data definition commands.
A. Engage
Word Search
Instruction: Find the word in the puzzle.Words can go in any direction and can share letters
as they cross over each other. Write your answer on the space provided below.
Answers:
1.______________________
________________ 4.______________________ 7.______________________
2.______________________ 5.______________________ 8.______________________
3.______________________ 6.______________________ 9.______________________
B. Explore
Video Title: Data Integrity
Integrity– Week 10
YouTube Link: https://www.youtube.com/watch?v=1D_h
https://www.youtube.com/watch?v=1D_h-yFtQVo
yFtQVo
Module Video Filename: Module10 video – Data Integrity
C. Explain
In this era of big data, when more pieces of information are pro processed and stored
than ever, implementing measures that preserve the integrity of the data that’s collected is
increasingly important. Understanding the fundamentals of data integrity and how it works is
the first step in keeping data safe. Read on to lear
learnn what data integrity is, why it’s essential,
and what you can do to keep your data intact.
Data integrity is the overall accuracy, completeness, and consistency of data. Data
integrity also refers to the safety of data in regards to regulatory compliance and security. It
Prepared and Validated By: Ms. Almira Sacriz | Dean Manuel Luis C. Delos Santos,
Santos MSCS Page 1 of 34
ASIAN INSTITUTE OF COMPUTER STUDIES
Bachelor of Science in Computer Science
Course Modules
CC225 - Information Management (Database System)
2nd Year – 2nd Semester
D. Elaborate
It is important that data adhere to a predefined set of rules, as determined by the
database administrator or application developer. As an example of data integrity, consider the
tables employees and departments and the business rules for the information in each of the
tables, as illustrated in Figure 10.1.
Note that some columns in each table have specific rules that constrain the data
contained within them.
Types of Data Integrity
This section describes the rules that can be applied to table columns to enforce different
types of data integrity.
Null Rule - A null rule is a rule defined on a single column that allows or disallows inserts
or updates of rows containing a null (the absence of a value) in that column.
Prepared and Validated By: Ms. Almira Sacriz | Dean Manuel Luis C. Delos Santos, MSCS Page 2 of 34
ASIAN INSTITUTE OF COMPUTER STUDIES
Bachelor of Science in Computer Science
Course Modules
CC225 - Information Management (Database System)
2nd Year – 2nd Semester
Unique Column Values - A unique value rule defined on a column (or set of columns)
allows the insert or update of a row only if it contains a unique value in that column (or
set of columns).
Primary Key Values - A primary key value rule defined on a key (a column or set of
columns) specifies that each row in the table can be uniquely identified by the values in
the key.
Referential Integrity Rules - A referential integrity rule is a rule defined on a key (a
column or set of columns) in one table that guarantees that the values in that key match
the values in a key in a related table (the referenced value).
Referential integrity also includes the rules that dictate what types of data manipulation
are allowed on referenced values and how these actions affect dependent values. The
rules associated with referential integrity are:
Integrity Constraints
An integrity constraint is a declarative method of defining a rule for a column of a table.
1. NOT NULL Integrity Constraints
By default, all columns in a table allow nulls. Null means the absence of a value.
A NOT NULL constraint requires a column of a table contain no null values. For example, you
can define a NOT NULL constraint to require that a value be input in the last_name column for
every row of the employees table.Figure 10.2 illustrates a NOT NULL integrity constraint.
Prepared and Validated By: Ms. Almira Sacriz | Dean Manuel Luis C. Delos Santos, MSCS Page 3 of 34
ASIAN INSTITUTE OF COMPUTER STUDIES
Bachelor of Science in Computer Science
Course Modules
CC225 - Information Management (Database System)
2nd Year – 2nd Semester
Prepared and Validated By: Ms. Almira Sacriz | Dean Manuel Luis C. Delos Santos, MSCS Page 4 of 34
ASIAN INSTITUTE OF COMPUTER STUDIES
Bachelor of Science in Computer Science
Course Modules
CC225 - Information Management (Database System)
2nd Year – 2nd Semester
Prepared and Validated By: Ms. Almira Sacriz | Dean Manuel Luis C. Delos Santos, MSCS Page 5 of 34
ASIAN INSTITUTE OF COMPUTER STUDIES
Bachelor of Science in Computer Science
Course Modules
CC225 - Information Management (Database System)
2nd Year – 2nd Semester
Primary Keys
The columns included in the definition of a table's PRIMARY KEY integrity constraint
are called the primary key. Although it is not required, every table should have a primary key so
that:
Each row in the table can be uniquely identified
No duplicate rows exist in the table
Figure 10.5 illustrates a PRIMARY KEY constraint in the dept table and examples of
rows that violate the constraint.
Prepared and Validated By: Ms. Almira Sacriz | Dean Manuel Luis C. Delos Santos, MSCS Page 6 of 34
ASIAN INSTITUTE OF COMPUTER STUDIES
Bachelor of Science in Computer Science
Course Modules
CC225 - Information Management (Database System)
2nd Year – 2nd Semester
A referential integrity constraint requires that for each row of a table, the value in the
foreign key matches a value in a parent key.
Figure 10.6 shows a foreign key defined on the deptno column of the emp table. It
guarantees that every value in this column must match a value in the primary key of
the dept table (also the deptno column). Therefore, no erroneous department numbers can
exist in the deptno column of the emp table.
Foreign keys can be defined as multiple columns. However, a composite foreign key
must reference a composite primary or unique key with the same number of columns and the
same datatypes. Because composite primary and unique keys are limited to 32 columns, a
composite foreign key is also limited to 32 columns.
Prepared and Validated By: Ms. Almira Sacriz | Dean Manuel Luis C. Delos Santos, MSCS Page 7 of 34
ASIAN INSTITUTE OF COMPUTER STUDIES
Bachelor of Science in Computer Science
Course Modules
CC225 - Information Management (Database System)
2nd Year – 2nd Semester
Prepared and Validated By: Ms. Almira Sacriz | Dean Manuel Luis C. Delos Santos, MSCS Page 8 of 34
ASIAN INSTITUTE OF COMPUTER STUDIES
Bachelor of Science in Computer Science
Course Modules
CC225 - Information Management (Database System)
2nd Year – 2nd Semester
Consider the insertion of the first row into the emp table. No rows currently exist, so
how can a row be entered if the value in the mgr column cannot reference any existing value in
the empno column? Three possibilities for doing this are:
A null can be entered for the mgr column of the first row, assuming that the mgr column
does not have a NOT NULL constraint defined on it. Because nulls are allowed in
foreign keys, this row is inserted successfully into the table.
The same value can be entered in both the empno and mgr columns. This case reveals
that Oracle performs its constraint checking after the statement has been completely
run. To allow a row to be entered with the same values in the parent key and the
foreign key, Oracle must first run the statement (that is, insert the new row) and then
check to see if any row in the table has an empno that corresponds to the new
row's mgr.
A multiple row INSERT statement, such as an INSERT statement with
nested SELECT statement, can insert rows that reference one another. For example,
the first row might have empno as 200 and mgr as 300, while the second row might
have empno as 300 and mgr as 200.
This case also shows that constraint checking is deferred until the complete execution of
the statement. All rows are inserted first, then all rows are checked for constraint
violations. You can also defer the checking of constraints until the end of
the transaction.
Consider the same self-referential integrity constraint in this scenario. The company has
been sold. Because of this sale, all employee numbers must be updated to be the current value
plus 5000 to coordinate with the new company's employee numbers. Because manager
numbers are really employee numbers, these values must also increase by 5000.
Prepared and Validated By: Ms. Almira Sacriz | Dean Manuel Luis C. Delos Santos, MSCS Page 9 of 34
ASIAN INSTITUTE OF COMPUTER STUDIES
Bachelor of Science in Computer Science
Course Modules
CC225 - Information Management (Database System)
2nd Year – 2nd Semester
Even though a constraint is defined to verify that each mgr value matches
an empno value, this statement is legal because Oracle effectively performs its constraint
checking after the statement completes. Figure 10.11 shows that Oracle performs the actions
of the entire SQL statement before any constraints are checked.
Trigger execution:
There are mainly two types of triggers supported by the SQL standard the before and after
triggers although some vendors also support the instead of trigger.
The after trigger is executed automatically after the statement that fires the trigger
completes but before the transaction is committed or rolled back.
The before trigger is executed automatically first and then allows the requested action
on the database object to occur.
The instead of trigger is executed automatically in place of the triggering action.
In SQL Server 2005 we can find the after and instead of triggers; the after trigger is
associated only to tables.
Prepared and Validated By: Ms. Almira Sacriz | Dean Manuel Luis C. Delos Santos, MSCS Page 10 of 34
ASIAN INSTITUTE OF COMPUTER STUDIES
Bachelor of Science in Computer Science
Course Modules
CC225 - Information Management (Database System)
2nd Year – 2nd Semester
In Oracle Database we can find before and after triggers and instead of
triggers associated only to views.
Trigger granularity:
There are two types of trigger granularity:
1. Row-level and
2. Statement-level granularity.
The row-level granularity assumes that a change to single row is an event, and changes
to several rows are view as separate events, thus, this kind of trigger is execute multiple times
and might know the old and new value of the affected row.
On the other hand, the statement-level granularity fires once for the whole statement such as
insert, delete and update and not for individual rows.
Listing 1: The SQL statement syntax for the trigger creation in Microsoft SQL Server:
It is remarkable to say that Microsoft SQL Server creates two virtual table
named deleted and inserted to store the changes. These virtual tables not always have values
and depend on the requested operation. When the operation is update the deleted table has
the old values and the inserted table has the new ones. When the operation is insert, then
the inserted table has the inserted or new values and the deleted table is empty. And finally, if
the operation is delete, then the inserted table is empty and the deleted table has the deleted
or old values.
When a trigger is fired, you can determine which columns have been modified by using
the update function which returns true if an insert or update operation has occurred against
the columns, otherwise it returns false.
As part of the new features of Microsoft SQL Server 2005, now you can define triggers
which respond to server events for example for auditing when a table is created or dropped.
Let's illustrate the concepts using the proposed audit application. We're going to use the
AdventureWorks database shipped with the installation of Microsoft SQL Server 2005.
First of all, we're going to illustrate the after trigger.
Let's create a table to store the audits for the modifications to the
Purchasing.ShipMethod table using the entity identifier (ShipMethodID) and ship name (Name)
from this base table as well as three new fields for storing the modification date, kind of
operation (insert, update, delete) and who does the changes. Finally create the after
Prepared and Validated By: Ms. Almira Sacriz | Dean Manuel Luis C. Delos Santos, MSCS Page 11 of 34
ASIAN INSTITUTE OF COMPUTER STUDIES
Bachelor of Science in Computer Science
Course Modules
CC225 - Information Management (Database System)
2nd Year – 2nd Semester
trigger, which is fired when one modification operation occurs. The underlying SQL code is
illustrated in Listing 2.
Listing 2: Create the audit table and trigger associated to Purchasing,ShipMethod table.
Now let's execute some database operations on the table Purchasing.ShipMethod and
finally look at the audits.
Now let's demonstrate the use of instead of trigger which is allowed for both tables and
views, although the main use is to handle data modifications to views which do not allow data
modifications or the modification is unambiguous.
Assume that you want to select all the ship methods and the associated purchase
orders. In order to achieve this purpose, we create a view as shown in Listing 4.
Prepared and Validated By: Ms. Almira Sacriz | Dean Manuel Luis C. Delos Santos, MSCS Page 12 of 34
ASIAN INSTITUTE OF COMPUTER STUDIES
Bachelor of Science in Computer Science
Course Modules
CC225 - Information Management (Database System)
2nd Year – 2nd Semester
Now when you try to insert a row into the view v_ShipMethodByOrder and audit error is
inserted into the table Purchasing.ShipMethodAudit as shown in Listing 6.
And finally, we're going to illustrate how to enforce rules such as the cancelation of forbidden
transactions. Let's suppose that we have a business rules that dictates not to delete any error
audit as shown in Listing 7.
Listing 7:
Now, let's try to delete the error audits as shown in Listing 8.
Prepared and Validated By: Ms. Almira Sacriz | Dean Manuel Luis C. Delos Santos, MSCS Page 13 of 34
ASIAN INSTITUTE OF COMPUTER STUDIES
Bachelor of Science in Computer Science
Course Modules
CC225 - Information Management (Database System)
2nd Year – 2nd Semester
Listing 8:
When you try to execute the transaction, you receive you receive a message from the database
system as shown in Listing 9.
The SQL statement for the creation of trigger is shown in Listing 10:
Listing 10: The SQL statement syntax for the trigger creation in Oracle Database.
Prepared and Validated By: Ms. Almira Sacriz | Dean Manuel Luis C. Delos Santos, MSCS Page 14 of 34
ASIAN INSTITUTE OF COMPUTER STUDIES
Bachelor of Science in Computer Science
Course Modules
CC225 - Information Management (Database System)
2nd Year – 2nd Semester
Now, we're going to see how to use and create a before trigger. Because the table emp_audit
has a surrogate key as primary key, we need to create a sequence and associate the sequence
values to new rows. We want to do it automatically the same as Microsoft SQL Server 2005's
identity approach.
To implement this logic, we need to create a sequence object and then create a before
trigger as shown in Listing 12.
Now, let's test the code adding an insert employee audit as shown in Listing 13.
Listing 14:
Now we're going to create the after trigger which is fired when any modification operation
occurs (insert, delete, update) and logs the underlying operation, the date and the responsible
as shown in Listing 15.
Prepared and Validated By: Ms. Almira Sacriz | Dean Manuel Luis C. Delos Santos, MSCS Page 15 of 34
ASIAN INSTITUTE OF COMPUTER STUDIES
Bachelor of Science in Computer Science
Course Modules
CC225 - Information Management (Database System)
2nd Year – 2nd Semester
Listing 15: The after trigger logging any modification to the emp table.
Now, we test the code with the following case as shown in Listing 16.
Listing 16:
Listing 17:
And finally, let's demonstrate how to enforce integrity constraints using customs triggers as
shown in Listing 18.
Prepared and Validated By: Ms. Almira Sacriz | Dean Manuel Luis C. Delos Santos, MSCS Page 16 of 34
ASIAN INSTITUTE OF COMPUTER STUDIES
Bachelor of Science in Computer Science
Course Modules
CC225 - Information Management (Database System)
2nd Year – 2nd Semester
Listing 18:
Now, let's attempt to insert a row with a negative salary as shown in Listing 19.
Listing 19:
The following are the main control measures are used to provide security of data in databases:
1. Authentication:
Authentication is the process of confirmation that whether the user log in only according to
the rights provided to him to perform the activities of data base. A particular user can login
only up to his privilege, but he can’t access the other sensitive data. The privilege of
accessing sensitive data is restricted by using Authentication.
By using these authentication tools for biometrics such as retina and figure prints can
prevent the data base from unauthorized/malicious users.
2. Access Control:
The security mechanism of DBMS must include some provisions for restricting access to the
data base by unauthorized users. Access control is done by creating user accounts and to
control login process by the DBMS. So, that database access of sensitive data is possible
only to those people (database users) who are allowed to access such data and to restrict
access to unauthorized persons.
The database system must also keep the track of all operations performed by certain user
throughout the entire login time.
3. Inference Control:
This method is known as the countermeasures to statistical database security problem. It is
used to prevent the user from completing any inference channel. This method protects the
sensitive information from indirect disclosure.
Prepared and Validated By: Ms. Almira Sacriz | Dean Manuel Luis C. Delos Santos, MSCS Page 18 of 34
ASIAN INSTITUTE OF COMPUTER STUDIES
Bachelor of Science in Computer Science
Course Modules
CC225 - Information Management (Database System)
2nd Year – 2nd Semester
4. Flow Control:
This prevents information from flowing in a way that it reaches unauthorized users.
Channels are the pathways for information to flow implicitly in ways that violate the privacy
policy of a company are called covert channels.
This allows to access the database to get statistical information about the number of
employees in the company but not to access the detailed confi
confidential/personal information
about specific individual employee.
6. Encryption:
This method is mainly used to protect sensitive data (such as credit card numbers, OTP
numbers) and other sensitive numbers. The data is encoded using some encoding
algorithms.
An unauthorized user who tries to access this encoded data will face difficulty in decoding
it, but authorized users are given decoding keys to decode data.
Table 10.0 Data Definition Commands
Data definition commands are used to create, modify and remove database objects such as
schemas, tables, views, indexes etc.Common Data Definition commands −
Prepared and Validated By: Ms. Almira Sacriz | Dean Manuel Luis C. Delos Santos,
Santos MSCS Page 19 of 34
ASIAN INSTITUTE OF COMPUTER STUDIES
Bachelor of Science in Computer Science
Course Modules
CC225 - Information Management (Database System)
2nd Year – 2nd Semester
Create
The main use of create command is to create a new table in database. It has a predefined
syntax in which we specify the columns and their respective data types.
CREATE TABLE <TABLE NAME>
( <COLUMN NAME> <DATA TYPE>,
<COLUMN NAME> <DATA TYPE>,
<COLUMN NAME> <DATA TYPE>,
<COLUMN NAME> <DATA TYPE>
);
Example :Create a student table with columns student name and roll number.
CREATE TABLE STUDENT
(STUDENT_NAME VARCHAR(30),
ROLL_NUMBER INT
);
Alter
An existing database object can be modified using the alter command. Alter command can do
following changes to any table-
Drop
This command can delete an index, table or view. Basically, any component from a relational
database management system can be removed using the Drop command. Once the object is
dropped, it cannot be reused.The general syntax of drop command is as follows −
DROP TABLE <table_name>;
DROP DATABASE <database_name>;
Prepared and Validated By: Ms. Almira Sacriz | Dean Manuel Luis C. Delos Santos, MSCS Page 20 of 34
ASIAN INSTITUTE OF COMPUTER STUDIES
Bachelor of Science in Computer Science
Course Modules
CC225 - Information Management (Database System)
2nd Year – 2nd Semester
Truncate
Using the truncate command, all the records in a database are deleted, but the database
structure is maintained.
TRUNCATE TABLE <table name>
Comment
This command is used to add comments to the data dictionary.
Rename
The rename command renames an object
Rename <old name> to <new name>
Around 1978, the Committee on Data Systems and Language (CODASYL) commissioned
the development of a network data model as a prototype for any future database
implementations. This continued work started in the early 1970s with the Data Definition
Language Committee (DDLC). By 1982, these efforts culminated in the data definition language
(DDL) and data manipulation language (DML) standards proposal. They became standards four
years later — endorsed by an organization with an improbably long name, the American
National Standards Institute National Committee on Information Technology Standards H2
Technical Committee on Database (ANSI NCITS H2 TCD).
NCITS H2 was given a mandate to standardize relational data model in 1982. The project
initially was based on IBM SQL/DS specifications, and for some time followed closely IBM DB2
developments. In 1984, the standard was redesigned to be more generic, to allow for more
diversity among database products vendors. After passing through all the bureaucratic loops it
was endorsed as an American National Standards Institute in 1986. The International Standard
Organization (ISO) adopted the standard in 1987. The revised standard, commonly known as
SQL89, was published two years later.
Prepared and Validated By: Ms. Almira Sacriz | Dean Manuel Luis C. Delos Santos, MSCS Page 21 of 34
ASIAN INSTITUTE OF COMPUTER STUDIES
Bachelor of Science in Computer Science
Course Modules
CC225 - Information Management (Database System)
2nd Year – 2nd Semester
SQL89 (SQL1)
SQL89 (or SQL1) is a rather worthless standard that was established by encircling all
RDBMS in existence in 1989. The major commercial vendors could not (and still to certain
degree cannot) agree upon implementation details, so much of the SQL89 standard is
intentionally left incomplete, and numerous features are marked as implementer-defined.
SQL92 (SQL2)
Because of the aforesaid, the previous standard had been revised, and in 1992 the first
solid SQL standard, SQL92 or SQL2, was published. ANSI took SQL89 as a basis, but corrected
several weaknesses in it, filled many gaps in the old standard, and presented conceptual SQL
features, which at that time exceeded the capabilities of any existing RDBMS
implementation.Also, the SQL92 standard is over five times longer than its predecessor (about
600 pages more), and has three levels of conformance.
In 1996, NIST dismantled the conformance testing program (citing "high costs" as the
reason behind the decision). Since then, the only verification of SQL standards compliance
comes from the RDBMS vendors themselves; this understandably increased the number of
vendor-specific features as well as nonstandard implementation of the standard ones. By 2001,
the original number of RDBMS vendors belonging to the ANSI NCIT had shrunk from 18 (at the
beginning of the 1990s) to just 7, though some new companies came aboard.
SQL99 (SQL3)
SQL3 represents the next step in SQL standards development. The efforts to define this
standard began virtually at the same time when its predecessor — SQL92 (SQL2) — was
adopted. The new standard was developed under guidance of both ANSI and ISO committees,
and the change introduced into the database world by SQL3 was as dramatic a shift from
nonrelational to relational database model; its sheer complexity is reflected in the number of
Prepared and Validated By: Ms. Almira Sacriz | Dean Manuel Luis C. Delos Santos, MSCS Page 22 of 34
ASIAN INSTITUTE OF COMPUTER STUDIES
Bachelor of Science in Computer Science
Course Modules
CC225 - Information Management (Database System)
2nd Year – 2nd Semester
pages describing the standard — over 1,500 — comparing to 120 or so pages for SQL89 and
about 600 pages for SQL92. Some of the defined standards (for example, stored procedures)
existed as vendor-specific extensions, some of them (like OOP) are completely new to SQL
proper. SQL3 was released as an ANSI/ISO draft standard in 1999; later the same year its status
was changed to a standard level.
SQL3 extends traditional relational data models to incorporate objects and complex
data types within the relational tables, along with all supporting mechanisms. It brings into SQL
all the major OOP principles, namely inheritance, encapsulation, and polymorphism, all of
which are beyond the scope of this book, in addition to "standard" SQL features defined in
SQL92. It provides seamless integration with the data consumer applications designed and
implemented in OO languages (SmallTalk, Eiffel, etc.).
While it is impossible to predict what model will emerge as a winner in the future, it
seems reasonable to assume that relational databases are here in for a long haul and have not
yet reached their potential; SQL as the language of the RDBMS will keep its importance in the
database world.
Summary:
Data integrity is the overall accuracy, completeness, and consistency of data. It also
refers to the safety of data in regards to regulatory compliance and security. It is
maintained by a collection of processes, rules, and standards implemented during the
design phase.
Data security is but one of the many facets of data integrity. Data security is not broad
enough to include the many processes necessary for keeping data unchanged over time.
Integrity constraint is a declarative method of defining a rule for a column of a table.
Not Null, Unique Key, Primary Key Referential, Self-Referential and Check are the types
of integrity constraints.
After passing through all the bureaucratic loops SQL was endorsed as an American
National Standards Institute in 1986. The International Standard Organization (ISO)
adopted the standard in 1987.
Prepared and Validated By: Ms. Almira Sacriz | Dean Manuel Luis C. Delos Santos, MSCS Page 23 of 34
ASIAN INSTITUTE OF COMPUTER STUDIES
Bachelor of Science in Computer Science
Course Modules
CC225 - Information Management (Database System)
2nd Year – 2nd Semester
E. Evaluate
ASSESSMENT:
Instruction: Answer the questions below. Write your answer in the Answer Sheet (AS)
provided for 2 points each.
Identification.
Review Question/S:
1. Explain the 3 standards of SQL.
Prepared and Validated By: Ms. Almira Sacriz | Dean Manuel Luis C. Delos Santos, MSCS Page 24 of 34
ASIAN INSTITUTE OF COMPUTER STUDIES
Bachelor of Science in Computer Science
Course Modules
CC225 - Information Management (Database System)
2nd Year – 2nd Semester
}
// Check for a last name
if (empty($_POST['lname'])) {
$errors[] = 'You forgot to enter your last name.';
} else {
$ln = mysqli_real_escape_string($dbcon, trim($_POST['lname']));
}
// Check for an email address
Prepared and Validated By: Ms. Almira Sacriz | Dean Manuel Luis C. Delos Santos, MSCS Page 25 of 34
ASIAN INSTITUTE OF COMPUTER STUDIES
Bachelor of Science in Computer Science
Course Modules
CC225 - Information Management (Database System)
2nd Year – 2nd Semester
if (empty($_POST['email'])) {
$errors[] = 'You forgot to enter your email address.';
} else {
$e = mysqli_real_escape_string($dbcon, trim($_POST['email']));
}
// Check for a password and match it against the confirmed password
if (!empty($_POST['psword1'])) {
if ($_POST['psword1'] != $_POST['psword2']) {
$errors[] = 'Your two passwords did not match.';
} else {
$p = mysqli_real_escape_string($dbcon, trim($_POST['psword1']));
}
} else {
$errors[] = 'You forgot to enter your password.';
}
if (empty($errors)) { // If it runs
// Register the user in the database...
// Make the query:
$q = "INSERT INTO users (user_id, fname, lname, email, psword,
registration_date)
}
echo '</p><h3>Please try again.</h3><p><br></p>';
}// End of if (empty($errors))
} // End of the main Submit conditional.
?>
<h2>Register</h2>
<form action="register-page.php" method="post">
<p><label class="label" for="fname">First Name:</label>
<input id="fname" type="text" name="fname" size="30"
maxlength="30"
value="<?php if (isset($_POST['fname'])) echo $_POST['fname']; ?>"></p>
To maintain the security of private pages, we use a device called sessions. A session is a
server-side store of information about a user. It is deleted when a user exits a site, or it times-
out after a period (typically 20 minutes) that is set by the server administrator. A session checks
the credentials of users before allowing them to access a page.
The solution will be to restrict access to the view_table.php page and all other
administrator pages so that only the membership secretary is allowed to view them. This will be
achieved by using sessions and a different user_level number for the administrator. The
Prepared and Validated By: Ms. Almira Sacriz | Dean Manuel Luis C. Delos Santos, MSCS Page 27 of 34
ASIAN INSTITUTE OF COMPUTER STUDIES
Bachelor of Science in Computer Science
Course Modules
CC225 - Information Management (Database System)
2nd Year – 2nd Semester
administrator will be provided with a user-friendly interface so that he can search and amend
membership records.
To sum up, our rules for differentiating between types of membership will be as follows:
• Non-members will not be able to view private pages because users can’t log in until they
are registered.
• Registered members will be able to access members’ pages because they can log in.
Doing so, initiates a session that allows them to open members’ pages.
• The administrator is the only person able to access administration pages. When he logs
in, the act of logging in starts a session that checks his user_level before he can open an
administrator’s page. His user_level is different from ordinary members’ user levels.
Before designing a login page, we must also create a means of differentiating between
an ordinary registered member and a member who is also the administrator. The administrator
will have extra privileges. In the next tutorial, you will learn how to add a new column with the
title user_level to an existing database table. This new column will enable us to differentiate
between types of membership.
To limit access to the view table page, we will add a column to the users table called
user_level. In this column, we will give the administrator a user level number 1. That number
relates to the membership secretary’s login details and to no other person.
Access phpMyAdmin, and click the database logindb. Then click the users table. Click the
Structure tab. Look below the records to find the item Add one column. The next steps are
illustrated in Figure 3-6.
Figure 10.2. The Add symbol (circled) can be seen at the bottom of this screen shot
Below the list of fields, you will see where you can add another column (shown at the
bottom of Figure 10.2). The details are as follows.
Find the item labeled Add 1, select the radio button labeled After, and use the drop-
down menu to select registration_date. Alternatively, select the radio button labeled At end of
table then click the Go button.
Prepared and Validated By: Ms. Almira Sacriz | Dean Manuel Luis C. Delos Santos, MSCS Page 28 of 34
ASIAN INSTITUTE OF COMPUTER STUDIES
Bachelor of Science in Computer Science
Course Modules
CC225 - Information Management (Database System)
2nd Year – 2nd Semester
Figure 10.3. Creating the title and attributes for the new user_level column
The next step is to launch XAMPP and access the page by entering
http://localhost/login/index.php into the address field of a browser. When the index page
appears, click the Register button on the header menu and register this user as an ordinary
member:
We will now appoint James Smith to be the membership secretary, with the right to
administer the membership list. For security, he needs a second name and a pseudo e-mail
address and password to access the administration section; therefore, he needs an additional
registration identity. The second e-mail address is important because his office colleagues
probably know his personal e-mail address. Every effort must be made to keep the
administrator’s login details secret. The e-mail address should be fictitious, but it must conform
to the accepted format for e-mails. Now register the membership secretary a second time using
his pseudonym (“Jack”), the new e-mail address, and the new password as follows:
Prepared and Validated By: Ms. Almira Sacriz | Dean Manuel Luis C. Delos Santos, MSCS Page 29 of 34
ASIAN INSTITUTE OF COMPUTER STUDIES
Bachelor of Science in Computer Science
Course Modules
CC225 - Information Management (Database System)
2nd Year – 2nd Semester
In a real-world situation, you would not use a password that could be easily guessed.
The one I used in this tutorial would not be secure, but I chose it because it is memorable and
therefore helpful for exploring this tutorial.
Now use phpMyAdmin to access the database logindb and the users table. Click the
Browse tab, and find the administrator Jack Smith’s record, as shown in Figure 10.4. If you click
the Edit link, you will be able to change his user_level field from 0 to 1. Click the Go button to
save the change.
Figure 10.4. Find Jack Smith’s record so that you can edit his user_level
Listing 10.2. Creating the Header for the Login Page (login-header.php)
<div id="header">
<h1>This is the header</h1>
<div id="reg-navigation">
<ul>
<li><a href="login.php">Erase Entries</a></li>
<li><a href="register-page.php">Register</a></li>
<li><a href="index.php">Cancel</a></li>
</ul>
</div>
Prepared and Validated By: Ms. Almira Sacriz | Dean Manuel Luis C. Delos Santos, MSCS Page 30 of 34
ASIAN INSTITUTE OF COMPUTER STUDIES
Bachelor of Science in Computer Science
Course Modules
CC225 - Information Management (Database System)
2nd Year – 2nd Semester
</div>
Now we need to look at a procedure for limiting access to the table of members. We will
prevent general users and registered members from viewing the table, but we will allow the
administrator to view the table and amend records.
The form fields for the e-mail address and password could have been located in the code in the
login page, but for increased security, an external included file is used. A malevolent person
would then have the difficulty of assembling the components before he could interfere with the
form, especially when the components are PHP files and they are located in different folders.
The listing for the login page uses the include() function to pull the form’s fields into the page
from the file named login-page.inc.php. Note that the redundant buttons have been removed
from the heading on this page.
Prepared and Validated By: Ms. Almira Sacriz | Dean Manuel Luis C. Delos Santos, MSCS Page 31 of 34
ASIAN INSTITUTE OF COMPUTER STUDIES
Bachelor of Science in Computer Science
Course Modules
CC225 - Information Management (Database System)
2nd Year – 2nd Semester
Prepared and Validated By: Ms. Almira Sacriz | Dean Manuel Luis C. Delos Santos, MSCS Page 32 of 34
ASIAN INSTITUTE OF COMPUTER STUDIES
Bachelor of Science in Computer Science
Course Modules
CC225 - Information Management (Database System)
2nd Year – 2nd Semester
Prepared and Validated By: Ms. Almira Sacriz | Dean Manuel Luis C. Delos Santos, MSCS Page 33 of 34
ASIAN INSTITUTE OF COMPUTER STUDIES
Bachelor of Science in Computer Science
Course Modules
CC225 - Information Management (Database System)
2nd Year – 2nd Semester
References:
3. West, A. W. (2013). Practical PHP and MySQL Web Site Databases: A Simplified Approach, ISBN-13
(electronic): 978-1-4302-6077-6
Facilitated By:
Name :
Prepared and Validated By: Ms. Almira Sacriz | Dean Manuel Luis C. Delos Santos, MSCS Page 34 of 34