DP Note Sss2 Second Term 2024-25
DP Note Sss2 Second Term 2024-25
SQL is a standard database language for storing, manipulating and retrieving data
stored in relational database.
You can construct valid statements that are both easy to read and edit by using the
following simple rules and guidelines below
Creating a basic table involves naming the table and defining its columns and
column’s data type.
The CREATE TABLE statement is used to create a new table in an existing database
Syntax:
CREATE TABLE Table_Name(
Example:
Example 2
Syntax:
Column1 datatype,
Column2 datatype,
Column3 datatype,
Column4 datatype,
Column5 datatype,);
Example:
ID INT,
NAME VARCHAR(20),
AGE INT,
ADDRESS CHAR(25),
SALARY DECIMAL(18,2));
Consider the table below by writing SQL statement to create the table below
Employee_id Surname First_Name DOB State
Employee table
DROP TABLE
The SQL DROP TABLE statement is used to remove a table definition and all data,
indexes, triggers, constraints, and permission specifications for that table.
Syntax:
Example:
ALTER TABLE
Syntax:
ADD Column_Name;
Example:
ADD Phone_number;
INSERT Query
The INSERT statement is used to add new rows of data to a table in the database.
Syntax:
Where
[INTO] is an optional keyword used between INSERT and the target table.’
Examples 1:
Example 2:
UPDATE Query
Syntax:
UPDATE Table_Name
Where,
Column_Name is the name of the column in the table which record is to be updated.
<search condition> Specifies the condition to be met for the rows to be updated.
Example 1:
UPDATE EmployeeTable
Example 2:
UPDATE Customers
WHERE ID = 6;
DELETE Query
Syntax:
Example 1:
Example 2:
WHERE ID = 6;
There is number of means to achieve this but one of the commonly used and
preferred methods is to use constraints.
Constraints
Constraints are the rules enforced on data columns on table. These are used to
limit the type of data that can go into a table.
OR
Categories of constraints
Types of constraints
Defining constraints
Syntax:
Example 1:
Example 2:
ID INT,
NAME VARCHAR(20),
AGE INT,
ADDRESS CHAR(25),
SALARY DECIMAL(18,2)
PRIMARY KEY(ID));
A foreign key is a field in a relational table that matches or point to a primary key
of another table.
A foreign key is a key used to link two tables together. This is sometimes called a
referencing key.
Example:
Students table
Course table
CREATE TABLE Students(
Name VARCHAR(20),
Age INT,
Address CHAR(25));
CourseName TEXT,
CourseInstructor VARCHAR(20));
CHECK Constraint
The CHECK constraint enables a condition to check the value being entered into a
column.
Example:
ID INT,
NAME VARCHAR(20),
ADDRESS CHAR(25),
SALARY DECIMAL(18,2)
PRIMARY KEY(ID));
UNIQUE Constraint
The UNIQUE constraint is used to prevent two records from having identical values
in a particular column. It ensures that only unique values are entered in a column or
set of columns.
Example:
Surname text,
First_Name text,
Mobile_Number UNIQUE);
The NOT NULL Constraint enforces that the column will not accept null values.
Example:
ADDRESS CHAR(25),
SALARY DECIMAL(18,2),
PRIMARY KEY(ID));
DEFAULT Constraint
The DEFAULT constraint provides a default value to a column when the INSERT
INTO statement does not provide a specific value.
Example:
The following SQL creates a new table called CUSTOMERS and adds five columns.
Here SALARY column is set to 7000.00 by default, so in case INSERT INTO
statement does not provide a value for this column then by default this column
would be set to 7000.00
ADDRESS CHAR(25),
PRIMARY KEY(ID));
SELECT Statement
The SELECT statement is used to fetch or retrieve data from the database and
enables the selection of one or many rows or columns from a table.
Syntax:
FROM Table_Name;
Example:
If you want to select the entire field in a Customers table then use the following
query:
Following is an example which would fetch ID, NAME, and SALARY fields of the
customers available in Customers table:
Example:
FROM Customers;
ID NAME SALARY
1 John 2000.00
2 Deborah 1500.00
3 Taiwo 2000.00
4 Lola 6500.00
5 Sola 8500.00
6 Seun 9000.00
WHERE Clause
The SQL WHERE clause is used to specify a condition while fetching the data from
single or joining with multiple tables.
The WHERE clause can be used in SELECT, UPDATE, DELETE statement etc.
Syntax:
FROM Table_Name;
WHERE [condition]
DATABASE SECURITY
Database security concerns the use of a broad range of information security controls
to protect databases (potentially including the data, the database applications or
stored functions, the database system, the database servers and the associated
network links) against compromises of their confidentiality, integrity and
availability.
Data security
Data security is the protection and safety of data from destruction, corruption or
unauthorized access to computers, databases and websites.
Data risk assessment is the process of determining the safety of stored data
Importance of data security
Data security is important for most business and even home computer users. Client
information, payment information, personal files, bank account details. All of these
information can be hard to replace and potentially dangerous if it falls into the
wrong hands.
Data lost due to disasters such as a flood or fire is crushing, but losing it to hackers
or a malware infection can have much greater consequences.
INTEGRITY CONTROLS
Backup: A backup, or the process of backing up, refers to the copying and
archiving of computer data so it may be used to restore the original after a data loss
event. Backups have two distinct purposes. The primary purpose is to recover data
after its loss, be it by data deletion or corruption.
Application security: Application security is the use of software, hardware, and
procedural methods to protect applications from external threats.
Role of the database administrator in data security
A database administrator is the person responsible for the installation,
configuration, upgrade, administration, monitoring and maintenance of databases in
an organization.
Roles
(1) Installation and upgrading the database server and application tools
(2) Maintaining users
(3) Managing data integrity
(4) Database recovery
(5) Allocating system storage and planning future storage requirements for the
database system
(6) Modifying the database structure as necessary from information given by
application developers
(7) Managing data security and privacy
(8) Ensuring compliance with database vendor license agreement
(9) Controlling and monitoring user access to the database
(10) Monitoring and optimizing the performance of the database
(11) Planning for backup and recovery of database information
(12) Maintaining archived data
(13) Backing up and restoring databases
(14) Contacting database vendor for technical support
(15) Generating various reports/output
(16) Creating users/ID (credentials)
(17) Deleting users
CRASH RECOVERY
Crash recovery is used to recover from a failure either when a single-instance
database crashes.
The goal of crash and instance recovery is to restore the data block changes located
in the cache of the dead instance.
Crash recovery is the process by which the database is moved back to a consistent
and usable state. This is done by rolling back incomplete transactions and
completing committed transactions that were still in memory when the crash
occurred.
Introduction to ARIES
Algorithms for Recovery and Isolation Exploiting Semantics (ARIES) are designed to
work with a no-force, steal database approach; it is used by IBM DB2, Microsoft SQL
server and many other database systems.
Three main principles lie behind ARIES are:
(1) Write ahead logging
(2) Repeat history during Redo
(3) Logging changes during Undo
Write ahead logging: Any change to an object is first recorded in the log, and the
log must be written to stable storage before changes to the object are written to
disk.
Repeat history during Redo: On restart after a crash, ARIES retraces the actions
of a database before the crash and brings the system back to the exact state that it
was in before the crash
Logging changes during Undo: Changes made to the database while undoing
transactions are logged to ensure such an action is not repeated in the event of
repeated restarts.
Media recovery
Media recovery deals with failure of the storage media holding the permanent
database, in particular disk failures. The traditional database approach for media
recovery uses archive copies (dumps) of the database as well as archive logs.
Archive copies represent snapshots of the database and are periodically taken.
The archive log contains the log records for all committed changes which are not yet
reflected in the archive copy. In the event of a media failure, the current database
can be reconstructed by using the latest archive copy and redoing all changes in
chronological order from the archive log.