Dbms Handbk
Dbms Handbk
Dbms Handbk
What is Database?
i. Integrated: Data is stored in a uniform way, typically all in one place (a single
physical computer for example)
iii. Related: Data has some relationship to other data. In a University we have
learners who take courses taught by instructors
1. User or Conceptual Models: How users perceive the world and/or the
business.
a. The Database;
Advantages of a DBMS
c. Data Sharing - by controlling access to data items, many users can access
data concurrently
Contents of a Database
Database consists of:
i. Users work with database directly by entering, updating and viewing data.
ii. For our purposes, data will be generally stored in tables with some
relationships between tables.
iii. Each table has one or more columns. A set of columns forms a database
record.
Metadata
Recall that a database is self describing, therefore, Metadata can be described
as:
ii. Data that describe how user data are stored in terms of table name, column
name, data type, length, primary keys, etc.
iii. Metadata are typically stored in System tables or System Catalog and are
typically only directly accessible by the DBMS or by the system administrator.
Indexes
In keeping with our desire to provide users with several different views of data,
indexes provide an alternate means of accessing, sorting and searching data.
An index for our new banking example might include the account numbers in a
sorted order. Indexes allow the database to access a record without having to
search through the entire table.
Updating data requires an extra step: The index must also be updated.
Applications Metadata
Many DBMS have storage facilities for forms, reports, queries and other
application components.
c. Data Model:
The following are brief outline describing the database development process.
d. Testing: The system is tested using real data. e. Deployment: The system is
deployed to users. Maintenance of the system begins.
DATA MODELING
There are two major methodologies used to create a data model: the Entity-
Relationship (ER) approach and the Object Model.
Database design is defined as: “designing the logical and physical structure of
one or more databases to accommodate the information needs of the users in
an organization for a defined set of applications”. The design process roughly
follows five steps:
1. Planning and analysis
2. Conceptual design
3. Logical design
4. Physical design
5. Implementation
The data model gets its inputs from the planning and analysis stage. Here the
modeler, along with system analysts, collects information about the
requirements of the database by reviewing the existing documentation and
interviewing end-users.
The data model has two outputs. The first is an entity-relationship diagram
which represents the data structure in a pictorial form. Because the diagram is
easily learned, it is valuable tool to communicate the model to the end-user.
The second component is a data document. This is a document that describes
in detail the data objects, relationships, and rules required by the database.
The data model is also detailed enough to be used by the database developers
as a “blueprint” for building the physical database.
The information contained in a data model will be used to define the relational
tables, the primary and the foreign keys, stored procedures, and triggers.
b. Non redundancy: Does the model specify a database in which the same fact
could be recorded more than once?
c. Enforcement of Business Rules: How accurately does the model reflect and
enforce the rules that apply to the business data?
d. Data Reusability: Will the data stored in the database be reusable for the
purposes beyond those anticipated in the process model?
e. Stability and Flexibility: How well will the model cope with possible changes
to the business requirements?
f. Elegance: Does the data model provide a reasonable neat and simple
classification of the data?
h. Integration: How will the proposed database fit with the organization’s
existing and future database?
a. It maps well to the relational model. The constructs used in the ER model
can easily be transformed into relational tables.
The ER model views the real world as a construct of entities and association
between entities. E-R Modeling Constructs are: Entity, Relationship, Attributes,
and Identifiers
MODULE 5
To eliminate redundant data from your database, you must take special care to
organize the data in your data tables. Normalization is a method of organizing
your data to prevent redundancy. Normalization involves establishing and
maintaining the integrity of your data tables as well as eliminating inconsistent
data dependencies.
The goal of normalization is to create a set of relational tables that are free of
redundant data and that can be consistently and correctly modified. This
means that all tables in a relational database should be in the third normal
form (3NF). A relational table is in 3NF if and only if all non-key columns are:
• mutually independent and • fully dependent upon the primary key.
Functional Dependencies
The concept of functional dependencies is the basis for the first three normal
forms. A column, Y, of the relational table R is said to be functionally
dependent upon column X of R if and only if each value of X in R is associated
with precisely one value of Y at any given time. X and Y may be composite.
Saying that column Y is functionally dependent upon X is the same as saying
the values of column X identify the values of column Y. If column X is a primary
key, then all columns in the relational table R must be functionally dependent
upon X.
A short-hand notation for describing a functional dependency is:
MODULE 5
a. Data Definition Language (DDL) Used to create (define) data structures such
as tables, indexes, clusters
Character Strings
Data Definition Language (DDL) The Data Definition Language (DDL) is used to
create and destroy databases and database objects. Let us take a look at the
structure and usage of basic DDL commands:
The data type specifies what type of data the column can hold.
SQL Constraints
Constraints are used to limit the type of data that can go into a table.
Constraints can be specified when a table is created (with the CREATE TABLE
statement) or after the table is created (with the ALTER TABLE statement).
In this section, we will focus on the following constraints:
a. NOT NULL
b. UNIQUE
c. PRIMARY KEY
d. FOREIGN KEY
The NOT NULL constraint enforces a column to NOT accept NULL values.
The NOT NULL constraint enforces a field to always contain a value. This means
that you cannot insert a new record, or update a record without adding a value
to this field.
The UNIQUE and PRIMARY KEY constraints both provide a guarantee for
uniqueness for a column or set of columns.
Note that you can have many UNIQUE constraints per table, but only one
PRIMARY KEY constraint per table.
Each table should have a primary key, and each table can have only one
primary key.
The USE command allows you to specify the database you wish to work with
within your DBMS,
ALTER
Once you have created a table within a database, you may wish to modify the
definition of it. The ALTER command allows you to make changes to the
structure of a table without deleting and recreating it.
DROP
The SELECT statement is used to select data from a database. The result is
stored in a result table, called the result-set. The SQL SELECT syntax:
The DISTINCT keyword can be used to return only distinct (different) values.
The syntax is SELECT DISTINCT column_name(s) FROM table_name.
The WHERE clause is used to extract only those records that fulfill a specified
criterion. The syntax is:
SELECT column_name(s)
FROM table_name
WHERE column_name operator value
The INSERT INTO statement is used to insert a new row in a table. The syntax
is:
The UPDATE statement is used to update existing records in a table. The SQL
UPDATE syntax is:
UPDATE table_name SET column1=value, column2=value2,... WHERE
some_column=some_value
The DELETE statement is used to delete rows in a table. The SQL DELETE Syntax
is:
DELETE FROM table_name
WHERE some_column=some_value
The "%" sign can be used to define wildcards (missing letters in the pattern)
both before and after the pattern.
The IN Operator
The IN operator allows you to specify multiple values in a WHERE clause. The
syntax is:
SELECT column_name(s)
FROM table_name
WHERE column_name IN (value1,value2,...)
The BETWEEN operator selects a range of data between two values. The values
can be numbers, text, or dates. The SQL BETWEEN Syntax is
SELECT column_name(s)
FROM table_name
WHERE column_name
BETWEEN value1 AND value2
SQL JOIN
The JOIN keyword is used in an SQL statement to query data from two or more
tables, based on a relationship between certain columns in these tables.
• JOIN: Return rows when there is at least one match in both tables
• LEFT JOIN: Return all rows from the left table, even if there are no matches
in the right table
• RIGHT JOIN: Return all rows from the right table, even if there are no
matches in the left table
The INNER JOIN keyword return rows when there is at least one match in both
tables. The SQL INNER JOIN Syntax is:
SELECT column_name(s)
FROM table_name1
INNER JOIN table_name2
ON table_name1.column_name=table_name2.column_name
The LEFT JOIN keyword returns all rows from the left table (table_name1),
even if there are no matches in the right table (table_name2). The SQL LEFT
JOIN Syntax is:
SELECT column_name(s)
FROM table_name1
LEFT JOIN table_name2
ON table_name1.column_name=table_name2.column_name
The UNION operator is used to combine the result-set of two or more SELECT
statements.
Notice that each SELECT statement within the UNION must have the same
number of columns. The columns must also have similar data types. Also, the
columns in each SELECT statement must be in the same order. SQL UNION
Syntax:
SELECT column_name(s)
FROM table_name1
UNION SELECT column_name(s)
FROM table_name2
SQL Functions
The AVG function returns the average value of a numeric column. AVG Syntax
is:
The COUNT function returns the number of rows that matches specified
criteria. Note that null values will not be counted. In the section, we shall
consider the following:
The FIRST function returns the first value of the selected column. The SQL
Syntax is:
The LAST function returns the last value of the selected column. The syntax is:
The MAX function returns the largest value of the selected column. The SQL
MAX Syntax is:
The MIN function returns the smallest value of the selected column. The SQL
MIN Syntax is as follows:
The SUM function is used to calculate the total for a column. The syntax is,
The HAVING clause was added to SQL because the WHERE keyword could not
be used with aggregate functions. The syntax is: