Download as pptx, pdf, or txt
Download as pptx, pdf, or txt
You are on page 1of 21

ANY and ALL

• The keywords ANY and ALL may be used with subqueries that produce a
single column of numbers.
• If the subquery is preceded by the keyword ALL, the condition will be true
only if it is satisfied by all values produced by the subquery.
• If the subquery is preceded by the keyword ANY, the condition will be true
if it is satisfied by any (one or more) values produced by the subquery.
• If the subquery is empty, the ALL condition returns true, the ANY condition
returns false.
• The ISO standard also allows the qualifier SOME to be used in place of
ANY.
SELECT staffNo, fName, IName, position, salary FROM
Staff WHERE salary > SOME (SELECT salary FROM Staff
WHERE branchNo = ‘B003’);
Result
SELECT staffNo, fName, IName, position, salary FROM
Staff WHERE salary > ALL (SELECT salary FROM Staff
WHERE branchNo = ‘B003’);
Result
Database Updates
SQL is a complete data manipulation language that can be
used for modifying the data in the database as well as
querying the database. The three SQL statements that are
available to modify the contents of the tables in the
database:
• INSERT – adds new rows of data to a table
• UPDATE – modifies existing data in a table
• DELETE – removes rows of data from a table
• Adding data to the database (INSERT)
Allows a single row to be inserted into a named table and
has the following format:
INSERT INTO TableName [(columnList)] VALUES
(dataValueList)

• Modifying data in the database (UPDATE)


The UPDATE statement allows the contents of existing rows
in a named table to be changed. The format of the command
is:
UPDATE TableName SET columnName1 = dataValue1
[, columnName2 = dataValue2 . . .] [WHERE search
Condition]
UPDATE all rows
Give all staff a 3% pay increase.
UPDATE Staff SET salary = salary*1.03;
UPDATE specific rows
Give all Managers a 5% pay increase.
UPDATE Staff SET salary = salary*1.05 WHERE position
= ‘Manager’;
UPDATE multiple columns
UPDATE Staff SET position = ‘Manager’, salary = 18000
WHERE staffNo = ‘SG14’;
Deleting data from the database (DELETE)
The DELETE statement allows rows to be deleted from a
named table.
The format of the command is:
DELETE FROM TableName [WHERE searchCondition]
SQL Data Types
The following restrictions are imposed on an identifier:
an identifier can be no longer than 128 characters (most
dialects have a much lower limit than this);
• an identifier must start with a letter;
• an identifier cannot contain spaces.
ISO Data types
SQL Scalar Data Types
1. Boolean data
2. Character data
3. Bit data
4. Exact Numeric Data
5. Datetime data
6. Interval data
Integrity control
Integrity control consists of constraints that wish to impose
in order to protect the database from becoming inconsistent.

These constraints can be defined in the CREATE and


ALTER TABLE statements
• The term data integrity refers to the accuracy and
consistency of data.
• When creating databases, attention needs to be given to
data integrity and how to maintain it. A good database will
enforce data integrity whenever possible.
• For example, a user could accidentally try to enter a phone
number into a date field. If the system enforces data
integrity, it will prevent the user from making
these mistakes.
Types of integrity constraints

• Entity integrity
• Referential integrity
• Domain integrity
• User-defined integrity
Entity Integrity

• Entity integrity defines each row to be unique within its table. No two rows
can be the same.
• To achieve this, a primary key can be defined. The primary key field
contains a unique identifier – no two rows can contain the same unique
identifier.
Referential Integrity is concerned with relationships. When
two or more tables have a relationship, we have to ensure
that the foreign key value matches the primary key value at
all times. So referential integrity will prevent users from:
•Adding records to a related table if there is no associated
record in the primary table.
•Changing values in a primary table that result in orphaned
records in a related table.
•Deleting records from a primary table if there are matching
related records.
Domain Integrity
Domain integrity concerns the validity of entries for a
given column. Selecting the appropriate data type for a
column is the first step in maintaining domain
integrity. Other steps could include, setting up
appropriate constraints and rules to define the data
format and/or restricting the range of possible values.
User-Defined Integrity
User-defined integrity allows the user to
apply business rules to the database that aren’t covered
by any of the other three data integrity types.
• Maintaining data integrity means making sure the data remains intact
and unchanged throughout its entire life cycle. This includes the
capture of the data, storage, updates, transfers, backups, etc.
Every time data is processed there’s a risk that it could get corrupted
(whether accidentally or maliciously)
Risks to Data Integrity

• A user tries to enter a date outside an acceptable range.


• A user tries to enter a phone number in the wrong format.
• A bug in an application attempts to delete the wrong
record.
• While transferring data between two databases, the
developer accidentally tries to insert the data into the
wrong table
Creating a Table (CREATE TABLE)
Having created the database structure, you may now create the table
structures for the base relations to be stored in the database.
Basic syntax:
CREATE TABLE TableName
{(columName dataType [NOT NULL] [UNIQUE]
[DEFAULT defaultOption] [CHECK (searchCondition)] [, . . .]}
[PRIMARY KEY (listOfColumns),]
{[UNIQUE (listOfColumns)] [, . . .]}
{[FOREIGN KEY (listOfForeignKeyColumns)
REFERENCES ParentTableName (listOfCandidateKeyColumns)]
[MATCH {PARTIAL | FULL}
[ON UPDATE referentialAction]
[ON DELETE referentialAction]] [, . . .]}
{[CHECK (searchCondition)] [, . . .]})
Changing a Table Definition (ALTER TABLE)
The ISO standard provides an ALTER TABLE statement for
changing the structure of a table once it has been created.
The definition of the ALTER TABLE statement in the ISO
standard consists of six options to:
• add a new column to a table;
• drop a column from a table;
• add a new table constraint;
• drop a table constraint;
• set a default for a column;
• drop a default for a column.
• The basic format of the statement is:
ALTER TABLE TableName
[ADD [COLUMN] columnName dataType [NOT NULL] [UNIQUE]
[DEFAULT defaultOption] [CHECK (searchCondition)]]
[DROP [COLUMN] columnName [RESTRICT | CASCADE]]
[ADD [CONSTRAINT [ConstraintName]]
tableConstraintDefinition]
[DROP CONSTRAINT ConstraintName [RESTRICT | CASCADE]]
[ALTER [COLUMN] SET DEFAULT defaultOption]
[ALTER [COLUMN] DROP DEFAULT]
Removing a Table (DROP TABLE)
The structure of a database will change; new tables will be
created and some tables will no longer be needed. To
remove a redundant table from the database using the DROP
TABLE statement, which has the format:
DROP TABLE TableName [RESTRICT | CASCADE]
• RESTRICT: The DROP operation is rejected if there are
any other objects that depend for their existence upon the
continued existence of the table to be dropped.
• CASCADE: The DROP operation proceeds and SQL
automatically drops all dependent objects (and objects
dependent on these objects).

You might also like