0% found this document useful (0 votes)
51 views

Database Normalization (AS)

A database is a repository of data designed to support efficient data storage and retrieval. A DBMS is a set of software tools that control access, organize, store, manage, retrieve and maintain data in a database. A DBMS provides features like data dictionaries, indexes, security and consistency.

Uploaded by

dabr
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
51 views

Database Normalization (AS)

A database is a repository of data designed to support efficient data storage and retrieval. A DBMS is a set of software tools that control access, organize, store, manage, retrieve and maintain data in a database. A DBMS provides features like data dictionaries, indexes, security and consistency.

Uploaded by

dabr
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 58

What is a database?

A database is a repository of data, designed to support efficient data storage,


retrieval and maintenance. A database may be specialized to store binary files,
documents, images, videos, relational data…etc.

Data can be stored in various forms,

 Tabular (data is stored in a tabular form then it is called a relational database.


 hierarchical (data is organized in a tree structure form, it is called a hierarchical
database)
 graphical forms(Data stored as graphs representing relationships between
objects is referred to as a network database.)

What is a database management system(DBMS)?

While a database is a repository of data, a database management system, or simply


DBMS, is a set of software tools that control access, organize, store, manage, retrieve
and maintain data in a database.

The functionsof a DBMS:


Database definition As most commercial databases are relational, the structure of
the database will be made up of tables and the relationships between the tables. A
specific language, data definition language (DDL) is used to specify the database
structure.

Data storage, retrieval and update. The DBMS allows users to store, retrieve and
update information as easily as possible. These users are not necessarily computer
experts and do not need to be aware of the internal structure of the database or how to
set it up. Data from the database can be retrieved by using queries.

Creation and maintenance of the data dictionary. This is a file containing the
details of the structure of the database including details of tables, fields, field types,
field lengths and any other characteristics.

data dictionary : a set of information describing the contents, format, and


structure of a database and the relationship between its elements, used to
control access to and manipulation of the database.
A data dictionary contains metadata i.e data about the database. The data dictionary is very
important as it contains information such as what is in the database, who is allowed to access it,
where is the database physically stored etc. The users of the database normally don't interact with
the data dictionary, it is only handled by the database administrators.

The data dictionary in general contains information about the following:

1. Names of all the database tables and their schemas.


2. Details about all the tables in the database, such as their owners, their security constraints,
when they were created etc.
3. Physical information about the tables such as where they are stored and how.
4. Table constraints such as primary key attributes, foreign key information etc.
5. Information about the database views that are visible.

This is a data dictionary describing a table that contains employee details.

Field Name Data Type Field Size for display Description Example

Employee
Integer 10 Unique ID of each employee 1645000001
Number

Name Text 20 Name of the employee David Heston

Date of Birth Date/Time 10 DOB of Employee 08/03/1995

Phone Number Integer 10 Phone number of employee 6583648648

The different types of data dictionary are:

Active Data Dictionary

The data dictionary is automatically updated by the database management system when any
changes are made in the database. This is known as an active data dictionary as it is self updating
Passive Data Dictionary

A passive data dictionary is maintained separately to the database whose contents are stored in
the dictionary. That means that if the database is modified the database dictionary is not
automatically updated as in the case of Active Data Dictionary. 

So, the passive data dictionary has to be manually updated to match the database. This needs
careful handling or else the database and data dictionary are out of sync.

Managing the facilities for sharing the database. Many databases need a multi-
access facility. Two or more people must be able to access the database
simultaneously and to update records without causing a problem.

Backup and recovery. Information in the database must not be lost in the event of
system failure.

security. Setting access rights for usesr. The DBMS must check user passwords and
allow appropriate privileges.

DBMS features
 Data dictionary- part of the database hidden from everyone except
DBA which contains metadata (data about tables, attributes and data
about how data is organised in the physical storage)
 Index table for improving performance: it’s a secondary table
contains the attribute values (which are unique Eg. Primary key or a
a secondary key which was a candidate key) and pointers to the
corresponding tuples in the original table. Searching the index table
quicker than searching the full table
 Security issues: access rights, implementing back up procedures

Advantages of DBMS
Data independence
The data and the programs using the data are stored separately. Any changes to the
structure of a database, for example adding a field or a table, will not affect any of
the programs that access the data. In a file-based system. a small change in a file
structure may require a considerable amount of reprogramming to all the programs
that access that file.

Data consistency

When the database is well structured, each data item stored only once, however many
applications can use it without any danger of an item, if there are many copies of
same data such as an employee’s address, being updated in one place and not in
another this happened the data would not be consistent.

No data redundancy

Redundancy occurs when data is duplicated unnecessary. In a flat file-based system,


the same information may be held on several different files, wasting space and
making updating more difficult.

More information available to users

In a database system, all information is stored together centrally. Authorised users


have access to all this information. In a file-based system data is held in separate files
in different departments, sometimes on incompatible systems.

Ease of use

The DBMS provides easy-to-use queries that enable use obtain instant answers. In a
file-based system a query would have to be specially written by a programmer.

Greater data integrity of data

It is important that the data stored in a database is maintained so that it is as correct as


possible. A range of measures can be taken to ensure the integrity of the data stored
in a database. Validation checks can be built-in to check that only sensible data can
be entered into the database.

For example, a range check could be set up for the date of birth of a new student in a
school to make sure that the student’s age is within appropriate limits. Checks, such
as parity and checksums, should be used to make sure that stored data has not
become corrupted.
Greater data security

The DBMS will ensure that only authorised users are allowed access to the data.
Different users can have different access privileges, depending on their needs. In a
file-based system using a number of files it is difficult to control access. Relational
databases provide different methods of database security.

Some terms explained


Data consistency: a data item will have the same value whatever application
program is being used. In a well-structured relational database, this is achieved by
storing every data item only once.

Data integrity: the data that is stored in the database is reasonable as it has not been
accidentally or maliciously altered.

Data redundancy: data that is duplicated unnecessarily.

Data independence: data and programs are stored separately. Changes to the
structure of the data (for example. by the addition of an extra field in a table) do not
result in all programs having to be rewritten.

Basic terminology related to RDBMS


Relation
A special type table which is used in a relational database

Tuple
A raw(record) in a relation is referred to as tuple. A tuple is a set or collection of
atomic values

Attributes

A column(fields) in a relation that contains a value.

An attribute is a characteristic of data. An attribute has to have a name, so you can


refer to that feature, and the name has to be as relevant as possible for that feature.

For example, for a person the attributes can be: Name, DateOfBirth. Informal terms
used to define an attribute are: column in a table or field in a data file.

Candidate keys

A candidate key is a unique identifier for the tuples (records) of a relation (tables).
By definition, every relation has at least one candidate key (the first property or
attribute of a relation).

In practice, most relations have multiple candidate keys. If a relation has more than
one candidate key, the one that is chosen to represent the relation is called the
primary key, and the remaining candidate keys are called alternate keys.

Primary key

A primary key is a unique attribute (or attribute combination) of the tuples of the
relation. It is a candidate key that is chosen to represent the relation in the database
and to provide a way to uniquely identify each tuple of the relation.

A database relation always has a primary key. Primary key is the most important
feature of a relational database concept

Foreign keys

An attribute in one table that refers to the primary key in another table.

A foreign key is an attribute (or attribute combination) in one relation or table whose
values are required to match those of the primary key of some relation or table to set
relation between these two tables.
Referential integrity

When a relationship is between two tables are created using primary and foreign key,
the DBMS will prevent any entry of an attribute which is not present in the main
entry table.

In a database design, a table would be given a name with the attributes names listed
in the bracket after the table name

Eg. Member( MemID, MemName, MemFamilyName,


BandName……)
Band(BandName, AgentID)
In member table, the entry in the BandName must correspond to the BandNames in
the Band Table. Otherwise it will not accept the BandName to be inserted to the
Mebmber Table.

This provides referential integrity.

Entity-relationship modelling.
The data base designing can be done using Entity- Relationship (ER)
diagram. First created and used by system analyst then passed to the
database designer.

Entity: they are real-world objects about which we collect data.

This could be a thing, a type of person (patient, student), an event


(Booking), a transaction or an organisation. An entity has a number of
instances.
An entity is something that will become a table in a relational database.
A relationship: Relationships are associations among entities. It is a form
of link created from one table to the other using primary key and foreign
key.
Entity-relationship diagram (ERD)

An entity-relationship diagram (ERD) is a data modelling technique that


graphically illustrates an information system's entities and the relationships
between those entities. An ERD is a conceptual and representational model
of data used to represent the entity framework infrastructure.
Entity Relationship Diagram.
Example of an ER diagram using the different symbols are shown below

Eg. An agency needs a database to handle booking for bands. Each band
has a number of members. Each booking is for a venue. Each booking
might be for one or more bands.
Step 1: Choose the entities

Choose Booking, Band, Member, Venue (these are the nouns in the statement.
Ignore agency because it’s only one Agency)

Step2: Identify the relationships

 Booking with venue


 Booking with Band
 Band with member

Member Band Booking Venue

Step3: Decide the cardinalities of the relationship.

 1:1(One-to-One)
 1:M(One-to-Many)
 M:1
 M:M
1. A band can have many members and a member can be in one band(M:1)

Member Band

We can make it more detailed by saying

A member must be in (belongs to) one of the bands band and a band must have

many members(M:1)

Member Band

2. A booking must have a venue associated with and venue can have many
bookings or cannot have any booking(M:1)

Booking Venue

3. A band can have many bookings or no booking (May be it’s a new band) and
booking can be for many bands or one band. (M:M)

Band Booking

The full ER diagram for agencies booking can be drawn as shown below.

Member Band Booki Ven


ng ue
A logical Entity Relationship Model

If the system is to be implemented, the ER diagram has to be converted to a logical


entity relationship model.

If a relationship is 1:M, no further refinement is needed. The entity at the many end
needs to have a foreign key referencing the primary key of the entity at the
one(relationship) end.

1:1 relationship is not considered because it’s a rare case

The problem relationship is M: M. (A foreign key cannot be used because it cannot


have many occurrences on both sides. It can have one occurrence in the table where it
is a primary key). The solution for M:M is to create link entity.

In the above example it can be a band-booking entity created to solve the M:M
problem. The logical entity model would contain the link entity as shown below.

With the link entity we can have two foreign keys. One referencing the primary key
of band and one referencing the primary key of booking.

Each entity in the ER diagram is a table in the relational database. So, the primary
keys and the foreign keys can be chosen as shown below.

Three Level Database Architecture


Data and Related Structures

Data are actually stored as bits, or numbers and strings, but it is difficult to work with
data at this level.

It is necessary to view data at different levels of abstraction.

Schema: Description of data at some level. Each level has its own schema.

We will be concerned with three forms of schemas:

 physical
 conceptual
 external

 Physical Data Level

The physical schema describes details of how data is stored: files, indices, etc. on the
random-access disk system.  It also typically describes the record layout of files and type
of files (hash, b-tree, flat).

Early applications worked at this level - explicitly dealt with details.

Problem:

 Changes to data structures are difficult to make.


 Application code becomes complex since it must deal with details.
 Rapid implementation of new features very difficult.

 Conceptual (logical) Data Level

Conceptual data level Hides details of the physical level.

 In the relational model, the conceptual schema (description of data) presents data
as a set of tables.

The DBMS maps data access between the conceptual to physical schemas automatically.

 Physical schema can be changed without changing application:

 DBMS must change mapping from conceptual to physical.


 Referred to as physical data independence.

 External Data Level


In the relational model, the external schema also presents data as a set of relations. An
external schema specifies a view of the data in terms of the conceptual level. It is
tailored to the needs of a particular category of users. Portions of stored data should not
be seen by some users and begins to implement a level of security and simplifies the
view for these users

Examples:

 Students should not see faculty salaries.


 Faculty should not see billing or payment data.

Information that can be derived from stored data might be viewed as if it were stored.

 Some fields are not stored, calculated when needed.

Applications are written in terms of an external schema. The external view is computed
when accessed.  It is not stored. Different external schemas can be provided to different
categories of users. Translation from external level to conceptual level is done
automatically by DBMS at run time. The conceptual schema can be changed without
changing application:

 Mapping from external to conceptual must be changed.


 Referred to as conceptual data independence.

Data Model

Schema: description of data at some level (e.g., tables, attributes, constraints, domains)

Model: tools and languages for describing:

 Conceptual/logical and external schema described by the data definition language


(DDL)
 Integrity constraints, domains described by DDL
 Operations on data described by the data manipulation language (DML)
 Directives that influence the physical schema (affects performance, not
semantics) are described by the storage definition language (SDL)
 

 
D
a
t
a

Independence

Logical data independence


 Immunity of external models to changes in the logical model
 Occurs at user interface level

Physical data independence

 Immunity of logical model to changes in internal model


 Occurs at logical interface level

Database Normalization
While designing a database out of an entity—relationship model, the main problem
existing in that “raw” database is redundancy. Redundancy is storing the same data
item in more than one place. A redundancy creates several problems like the
following:

1. Extra storage space: storing the same data in many places takes large amount
of disk space.
2. Entering same data more than once during data insertion.
3. Deleting data from more than one place during deletion.
4. Modifying data in more than one place.
5. Anomalies may occur in the database if insertion, deletion, modification etc
are not done properly. It creates inconsistency and unreliability in the database.

To solve this problem, the “raw” database needs to be normalized.

This is a step by step process of removing different kinds of redundancy and anomaly
at each step. At each step a specific rule is followed to remove specific kind of
impurity in order to give the database a slim and clean look.

Step 1: UNF (UnNormalised Form)


Select the data source and convert into an unnormalised table (UNF)

Process:

Create column headings (ignoring any calculated fields)

Enter sample data into table

Identify a key for table (and underline it)

Remove duplicate data

Example
ORDER(Num, CustName, City, Country, ProdID, Description)
Step 2: 1NF
Transform a table of unnormalised data into first normal form (1NF)

Rule: Remove any repeating group of attributes. A table with no repeating


groups is said to be in first normal form.
Process:

1. Identify repeating attributes

2. Remove repeating attributes to a new table together with a copy of the key
from the UNF table

3. Assign a key to the new table (and underline it).

4. The key from the unnormalised table always becomes part of the key of the
new table. A compound key is created. The value for this key must be unique
for each entity occurrence.

Example:

ORDER (Num, CustName, City, Country)

ORDER-PRODUCTS (Num, ProdID, Description)

Table 3.6.2 has repeating groups in the attributes ProdID and Description. We
remove the repeating groups by:

● moving the ProdID and Description attributes to a new table

● linking the new table to the original table ORDER with a foreign key.
2NF
Transform data in first normal form (1NF) into second normal form (2NF)

Rule: A table is in second normal form if any partial dependencies have been
removed. That is, every non-key attribute must be fully dependent on all of
the(entire) primary key. Remove any non-key attributes that only depend on part of
the table key to a new table

Ignore tables with

a) a simple primary key (Eg. Order table)

b) with no non-key attributes or only compound key attribute (these go straight to

2NF with no conversion)

Process:

Take each non-key attribute in turn and ask the question


- is this attribute dependent on one part of the key?

If yes, remove attribute to new table with a copy of the part of the key it is
dependent upon. The key it is dependent upon becomes the key in the new
table. Underline the key in this new table.

If no, check against other part of the key and repeat the above process.

If the non-key attribute is fully dependent on the compound key or not dependent on
either part of the key, keep the attribute in current table.

Example
In our ORDER-PRODUCTS table, Description depends only on ProdID and not on
Num. Hence the non-key attribute (Description) is not dependent on all of the
primary key. We say that Description is dependent on ProdID or, we can say ProdID
determines Description (ProdID →Description).

We remove the partial dependency by:

● moving the Description attribute to a new table

● linking the new table to the ORDER-PRODUCTS table with a foreign key.
Tables 3.6.5 and 3.6.6 show the data in second normal form.

3NF
To transform data in second normal form (2NF) into third normal form (3NF)

Rule: Remove to a new table, any non-key attributes that are more dependent
on other non-key attributes than the primary key, is called non-key dependency
(transitional dependency).

Third normal form (like second normal form) is concerned with the non-key
attributes. To be in 3NF, there must be no dependencies between any of the non-key
attributes.

Ignore tables with zero or only one non-key attribute (these go straight to 3NF with
no conversion).

Process:

 If a non-key attribute is more dependent on another non-key attribute than the


table key (primary key)
 Move the dependent attribute, together with a copy of the non-key attribute
upon which it is dependent, to a new table
 Make the non-key attribute, upon which it is dependent, the key in the new
table. Underline the key in this new table.
 Leave the non-key attribute, upon which it is dependent, in the original table
and mark it a foreign key(*).

Example:

A table with no or one non-key attribute must be in 3NF, so PRODUCT and


ORDER-PRODUCTS are in 3NF.

There is a problem with the original ORDER table. City determines the Country
(City → Country, that would mean, A Country can have many cities, so when
store country and city the name country can be repeated so it cannot be made a
primary key. City becomes primary key and that determines the country)

So we have two non-key attributes which are dependent. This means that ORDER is
not in 3NF. Tables 3.6.7 and 3.6.8 show the data in third normal form.
Exercise:

Write the unnormalised form and normalise table using first, second and third normal
form.
SQL
Structure Query Language(SQL) is a programming language used for storing and
managing data in RDBMS. SQL was the first commercial language introduced for
E.F Codd's Relational model. Today almost all RDBMS(MySql, Oracle, Infomix,
Sybase, MS Access) uses SQL as the standard database language. SQL is used to
perform all type of data operations in RDBMS.
SQL statements are divided into two major categories: data definition language
(DDL) and data manipulation language (DML). Both of these categories contain far
more statements than we can present here, and each of the statements is far more complex
than we show in this introduction.

Data definition language


DDL statements are used to build and modify the structure of your tables and other
objects in the database. When you execute a DDL statement, it takes effect
immediately.

Create Table

The syntax of this command is:


Table-name and Column-name: are the names used and offered by you for the table
and its columns;

Datatype stands for the kind of data to be stored in each column or field;


and size gives the maximum size of each field in terms of characters wide

NOT NULL: This is optional and can be omitted. A column can be defined as NOT
NULL. Use of this option means that the data must include a value for that column. If
a column is defined as NOT NULL, and no value is given when inserting data, then
an error will occur. The row will not be stored.

PRIMARY KEY: This part of the command specifies the column, or columns, that
make up the primary key. In a relational database all tables must have one, and only
one, primary key (PK) defined. Should a table have more than one column that
makes up the primary key then a comma separates them, eg:

PRIMARY KEY (student_number, unit_number).

FOREIGNKEY: This part of the command specifies the column, or columns, that
make up the Foreign key (FK). In a relational database a foreign key is the primary
key of the parent table that links or join tables together and enforce referential
integrity between tables

Create Table Example

CREATE TABLE employee ( empno NUMBER(3) NOT NULL,

Surname VARCHAR2(15) NOT NULL , forenames VARCHAR2(30) NOT


NULL, dobDATE , address VARCHAR2(50, telno VARCHAR2(20) , depno
NUMBER(3) NOT NULL, PRIMARY KEY (empno), FOREIGN KEY
(depno) REFERENCES department);

Rules for table and column names:

 Can include letters and numbers but must begin with a letter.
 Cannot include spaces. However, if you wish to represent a space use an
underscore (_). For example, employee_ number.
 Cannot be a reserved word used in SQL, eg TABLE.
 Cannot be the name of a SQL command, eg CREATE.
DROP TABLE
If you totally mess things up and want to start over, you can always get rid of
any object you’ve created with a drop statement. The syntax is different for
tables and constraints.

The syntax of this command is:

This command will remove the mentioned table from the database. No confirmation
message is given: the table is simply removed. Therefore use the command with care.

For example, to remove the table employee from the database, use the SQL
command:

DROP TABLE employee;

Alter TABLE
The alter table statement may be used as you have seen to specify primary and
foreign key constraints, as well as to make other modifications to the table structure.
Key constraints may also be specified in the CREATE TABLE statement.

The basic syntax of this command is:

The first command will add the specified column to the specified table. For example,
it has been decided to allocate a company car to some employees. To add the
registration number of the car to the employee table, use the SQL command:

ALTER TABLE employee


ADD car_registration CHAR(10);

The specified column will be added to the end of the table definition and the
specified column will be added as an optional column

The second command will remove the specified column from the specified table. For
example to remove the column car_registration added in the previous step use the
SQL command:

ALTER TABLE employee

DROP COLUMN car_registration;

Renaming Tables
The syntax of this command is:

For example to rename the table employee _copy to employee _Table , use the SQL
command:

RENAME employee _copy TO employee _Table;

Data Manipulation Language (DML)


Now that you have had some practice in creating, dropping and altering tables, let's
investigate some of those SQL commands to store and manipulate data in those
tables. As mentioned earlier, there are four main commands:

1. INSERT to insert rows of data into a table

2. SELECT to select rows of data from a table

3. UPDATE to change rows of data in a table


4. DELETE to remove rows of data from a table

In this section we will look at the INSERT command in detail. However, the
SELECT, UPDATE and DELETE commands will be discussed in the next section.

Insert Statement

The syntax of this command is:

To enter a value for every column in a row, for example, using the student table,
created as follows:

CREATE TABLE student

( student_number CHAR(8) NOT NULL

, surname CHAR VARYING(25) NOT NULL

, forename CHAR VARYING(20) NOT NULL

, gender CHAR(1) NOT NULL

, title CHAR VARYING(4)

, date_of_birth DATE

, dept_number CHAR(5)

, course_number CHAR(6) NOT NULL

, PRIMARY KEY (student_number)

);

Insert Example

To enter all the data for the student Miss Janet Smith into the student table, the SQL
INSERT command could be:
INSERT INTO student

VALUES ('SN000023', 'Smith', 'Janet', 'F', 'Miss', '12-Dec-1978', 'DEP45',


'C02122');

The values entered must be in the same order as the defined order of the columns in
the table.

There must be a value for every column in the table (any unknown values being
entered as the SQL reserved word NULL. See below and the discussion on the next
page).

All character strings and dates are enclosed in single quotes ('....').

When entering dates, they are specified as 'dd-mmm-yyyy' where mmm is the first
three characters of the month, ie Jan, Feb, Mar, Apr, May, Jun, Jul, Aug, Sep, Oct,
Nov, Dec. For example: '07-Sep-1949'.

If a column is optional (ie it is not specified as NOT NULL), and you do not wish to
enter a value, then use the SQL reserved word NULL when entering its value. For
example:

WHERE

The where clause sets a conditional statement for your queries in which the database
search for rows that meet your specific where condition. A typical query will have
the following syntax.

A conditional statement has 2 parts, the left side is your table column and the right
side is the condition to be met. Example: WHERE table_column(s) = condition, for
example:

SELECT employee_number, forename, surname, salary

FROM employee

WHERE surname = 'Smith';


This command works by comparing the data content of the surname column for each
row in the table. If the condition is met then a 'true' value results, else 'false'. Those
rows having a 'true' value are returned by the command. Selecting specific rows is
possible by using the WHERE clause of the SQL SELECT

Several WHERE conditions can be combined by use of the logical


operators AND or OR. The logical operator NOT inverts a result.

Select Examples

The Select command is illustrated by the following examples:


ORDER by

The order by statement allows for table column assortment. It allows for ascending or
descending lists of your table column values permitting SQL to reorder your table
rows for the purpose of viewing. A typical query will have the following syntax.

For example, list all employees in the property department. Return the results in
ascending order of forename within ascending order of surname.
SELECT *

FROM employee

WHERE depno = 22

ORDER BY surname, forenames;

List all employees in the company. Return the results in descending order of date of
birth within ascending order of department number.

SELECT *

FROM employee

ORDER BY depno, dob DESC;

BETWEEN

The BETWEEN ... AND operator selects a range of data between two values. These
values can be numbers, text, or dates. A typical query will have the following syntax.

SELECT title, forename, surname, dept_number, date_of_birth

FROM student

WHERE gender = 'F' AND date_of_birth BETWEEN 01-JAN-1978' AND '31-


DEC-1978'

ORDER BY surname, forename;


The between clause defines the limits of the selection on the specified column. Here
is another example: find all members of staff whose salary is between £15,000 and
£25,000. This can be solved by the SQL SELECT command:

SELECT * FROM staff WHERE salary BETWEEN 15000 AND 25000;

Note: The between clause returns all the rows that meet the criteria 'greater than and
equal to' the lower limit and 'less than and equal to' the higher limit. Therefore, in the
above example, staffs who have a salary of exactly £15,000 or £25,000 will be
selected as well as all the salaries in between those ranges.

LIKE

When dealing with strings, sometimes you do not want to match on exact strings like
='Blue', but instead on partial strings, substrings, or particular patterns. This could
allow you, for instance, to find all cars with a colour starting with 'B'. The LIKE
operator provides this functionality. The LIKE operator is used in place of an '=' sign.
In its basic form it is identical to '='. For instance, both of the following statements
are identical:

SELECT regno FROM car WHERE colour = 'BLUE';

SELECT regno FROM car WHERE colour LIKE 'BLUE';

The power of LIKE is that it supports two special characters, '%' and '-'. . Whenever
there is an '-' character in the string, any character will match. Whenever there is a '%'
character in the string, it means any number of characters. A typical query will have
the following syntax.
SQL supports two wildcard operators in conjunction with the LIKE operator
which are explained in detail in the following table.

Sr.No Wildcard & Description


.

The percent sign (%)

1 Matches one or more characters.

Note − MS Access uses the asterisk (*) wildcard character instead of


the percent sign (%) wildcard character.

The underscore (_)

Matches one character.


2
Note − MS Access uses a question mark (?) instead of the underscore
(_) to match any one character.

The percent sign represents zero, one or multiple characters. The underscore
represents a single number or a character. These symbols can be used in
combinations.

This search can be achieved using the wildcard character % and the LIKE clause. For


example:

SELECT forename, surname FROM student WHERE surname LIKE 'White


%';
The following table has a few examples showing the WHERE part having
different LIKE clause with '%' and '_' operators −

Sr.No. Statement & Description

WHERE SALARY LIKE '200%'


1
Finds any values that start with 200.

WHERE SALARY LIKE '%200%'


2
Finds any values that have 200 in any position.

WHERE SALARY LIKE '_00%'


3
Finds any values that have 00 in the second and third positions.

WHERE SALARY LIKE '%2'


4
Finds any values that end with 2.

WHERE SALARY LIKE '_2%3'


5
Finds any values that have a 2 in the second position and end with a 3.

WHERE SALARY LIKE '2_ _ _3'


6
Finds any values in a five-digit number that start with 2 and end with 3.

Let us take a real example, consider the CUSTOMERS table having the records
as shown below.
+----+----------+-----+-----------+----------+

|ID | NAME | AGE | ADDRESS | SALARY |


+----+----------+-----+-----------+----------+
| 1 | Ramesh | 32 | Ahmedabad | 2000.00 |
| 2 | Khilan | 25 | Delhi | 1500.00 |
| 3 | kaushik | 23 | Kota | 2000.00 |
| 4 | Chaitali | 25 | Mumbai | 6500.00 |
| 5 | Hardik | 27 | Bhopal | 8500.00 |
| 6 | Komal | 22 | MP | 4500.00 |
| 7 | Muffy | 24 | Indore | 10000.00 |
+----+----------+-----+-----------+----------+

Following is an example, which would display all the records from the
CUSTOMERS table, where the SALARY starts with 200.

SQL> SELECT * FROM CUSTOMERS


WHERE SALARY LIKE '200%';

This would produce the following result −


+----+----------+-----+-----------+----------+

|ID | NAME | AGE | ADDRESS | SALARY |


+----+----------+-----+-----------+----------+
| 1 | Ramesh | 32 | Ahmedabad | 2000.00 |
| 3 | kaushik | 23 | Kota | 2000.00 |
+----+----------+-----+-----------+----------+

IS NULL

NULL values were discussed earlier on. Just to remind you:

In SQL, the data in a column can hold a special value known as NULL. This must
not be confused with spaces or zero. It means that this column has no value assigned,
ie it is unknown.

In order to check whether a value in a column has a null value then the IS
NULL clause is used in the WHERE statement.

For example, to select all students whose date of birth has not yet been entered, a
SQL SELECT command could be:
SELECT title, forename, surname

FROM student

WHERE date_of_birth IS NULL;

Conversely, if you wish to select all students whose date of birth has been entered,
then the IS NOT NULL clause is used

SELECT forename, surname

FROM student

WHERE date_of_birth IS NOT NULL;

UPDATE

To change the values in one or more columns you use the SQL UPDATE command.
The WHERE condition is optional but, if omitted, all the rows in the table are
changed. Normally, this is not desired. It is a very common mistake, so take care
when using this command.

For example:

UPDATE employee

SET surname = 'Wilson', address = '2 High Street, Dunfermline'

WHERE empno = 22;

Will update the details of employee 22.

Example
Consider the CUSTOMERS table having the following records −

+----+----------+-----+-----------+----------+

| ID | NAME | AGE | ADDRESS | SALARY |


+----+----------+-----+-----------+----------+
| 1 | Ramesh | 32 | Ahmedabad | 2000.00 |
| 2 | Khilan | 25 | Delhi | 1500.00 |
| 3 | kaushik | 23 | Kota | 2000.00 |
| 4 | Chaitali | 25 | Mumbai | 6500.00 |
| 5 | Hardik | 27 | Bhopal | 8500.00 |
| 6 | Komal | 22 | MP | 4500.00 |
| 7 | Muffy | 24 | Indore | 10000.00 |

+----+----------+-----+-----------+----------+

The following query will update the ADDRESS for a customer whose ID
number is 6 in the table.

SQL> UPDATE CUSTOMERS


SET ADDRESS = 'Pune'
WHERE ID = 6;
Now, the CUSTOMERS table would have the following records –
Now, the CUSTOMERS table would have the following records −
+----+----------+-----+-----------+----------+
|ID | NAME | AGE | ADDRESS | SALARY |
+----+----------+-----+-----------+----------+
| 1 | Ramesh | 32 | Ahmedabad | 2000.00 |
| 2 | Khilan | 25 | Delhi | 1500.00 |
| 3 | kaushik | 23 | Kota | 2000.00 |
| 4 | Chaitali | 25 | Mumbai | 6500.00 |
| 5 | Hardik | 27 | Bhopal | 8500.00 |
| 6 | Komal | 22 | Pune | 4500.00 |
| 7 | Muffy | 24 | Indore | 10000.00 |
+----+----------+-----+-----------+----------+
If you want to modify all the ADDRESS and the SALARY column values in the
CUSTOMERS table, you do not need to use the WHERE clause as the UPDATE
query would be enough as shown in the following code block.

SQL> UPDATE CUSTOMERS


SET ADDRESS = 'Pune', SALARY = 1000.00;

Now, CUSTOMERS table would have the following records −


+----+----------+-----+---------+---------+
| ID | NAME | AGE | ADDRESS | SALARY |
+----+----------+-----+---------+---------+
| 1 | Ramesh | 32 | Pune | 1000.00 |
| 2 | Khilan | 25 | Pune | 1000.00 |
| 3 | kaushik | 23 | Pune | 1000.00 |
| 4 | Chaitali | 25 | Pune | 1000.00 |
| 5 | Hardik | 27 | Pune | 1000.00 |
| 6 | Komal | 22 | Pune | 1000.00 |
| 7 | Muffy | 24 | Pune | 1000.00 |
+----+----------+-----+---------+---------+
DELETE

To delete rows from a table you use the SQL DELETE command.

The WHERE condition is optional but, if omitted, all the rows in the table are
deleted. Normally, this is not desired. It is a very common mistake, so take care when
using this command.

If the row being deleted is referenced by a foreign key in another table then the delete
will fail (unless the on delete cascade constraint is specified in the other table). For
example:

DELETE FROM employee

WHERE surname = 'Smith' AND depno = 4;

will delete all employees called Smith from Department 4.

DELETE FROM Customers WHERE CustomerName='Alfred
s Futterkiste';

SQL INNER JOIN Keyword


The INNER JOIN keyword selects all rows from both tables as long as there is a
match between the columns in both tables.
SELECT column_name(s)
FROM table1
INNER JOIN table2
ON table1.column_name=table2.column_name;

or:

SELECT column_name(s)
FROM table1
JOIN table2
ON table1.column_name=table2.column_name;

The following SQL statement will return all customers with orders:

Example
SELECT Customers.CustomerName, Orders.OrderID
FROM Customers
INNER JOIN Orders
ON Customers.CustomerID=Orders.CustomerID
ORDER BY Customers.CustomerName;

Note: The INNER JOIN keyword selects all rows from both tables as long as there
is a match between the columns. If there are rows in the "Customers" table that
do not have matches in "Orders", these customers will NOT be listed.

You might also like