Vous êtes sur la page 1sur 22

Reference and referring relationship:

A relation, say r1, may include among its attributes the primary key of
another relation, say r2. This attribute is called a foreign key from r1,
referencing r2. The relation r1 is also called the referencing relation of the
foreign key dependency, and r2 is called the referenced relation of the
foreign key

Participation constraint:
The participation of an entity set E in a relationship set R is said to be total if
every entity in E participates in at least one relationship in R. If only some
entities in E participate in relationships in R, the participation of entity set E
in relationship R is said to be partial. In Figure 7.5a, the participation of B in
the relationship set is total while the participation of A in the relationship set
is partial. In Figure 7.5b, the participation of both A and B in the relationship
set are total.

Normalization:

How to enable or disable triggers:


There are three ways to enable/disable a trigger in SQL Server by using a
graphical option or by using a query editor
In the example below, we will assume that we have a TRG_Em_History
trigger already created on the Employee table.
1. ALTER TABLE option
o Enable a Trigger
ALTER TABLE EMPLOYEE
ENABLE TRIGGER TRE_Em_History

o Disable a Trigger
ALTER TABLE EMPLOYEE
DISABLE TRIGGER TRE_Em_History

2. Specifying the trigger and the table name


o Enable a Trigger
ENABLE TRIGGER TRG_Em_History ON EMPLOYEE

o Disable a Trigger

DISABLE TRIGGER TRG_Em_History ON EMPLOYEE

3. Using the Graphical Option


[+] Object Explorer
[-] Database Name
[-] Table Name
[-] expand Triggers
[-] Right-click on the trigger name to
enable/disable it.

Why Used: The trigger is mostly used for maintaining the integrity of the
information on the database. For example, when a new record (representing
a new worker) is added to the employees table, new records should also be
created in the tables of the taxes, vacations and salaries.

Required system privileges:


To create triggers in another users schema you must have the CREATE ANY
TRIGGER system privilege.
o The CREATE TRIGGER system privilege is part of the RESOURCE
role provided with Oracle.
o A role is a set or group of privileges that can be granted to users or
another role.
To alter a trigger, you must either own the trigger or have the ALTER ANY
TRIGGER system privilege.

o To enable or disable triggers by altering the tables you have either


the ALTER privilege for that table or the ALTER ANY TABLE system
privilege.
To create a trigger on a database-level event, you must have the
ADMINISTER DATABASE TRIGGER system privilege

Required table privileges:


Triggers may reference tables

For example, if you use triggers to audit changes to data in the


BOOKSHELF table, then you may insert a record from BOOKSELF table
into a different table every time a record is changed in BOOKSHELF.

To do this, you need to have privileges to insert into


BOOKSHELF_AUDIT

The privileges needed for triggered transactions cannot come from


roles. They must be granted directly to the creator of the trigger.

Data Modeling Concepts: Attributes


An attribute is a descriptive property or characteristic of an entity. Synonyms
include element, property, and field. For example, student Rahim has
properties of his own Student Identification number, name, and grade.

Simple and composite attributes:


Attributes that cant be divided into subparts are called Simple or Atomic
attributes. For example, Employee Number is a simple attribute. Age of a
person is a simple attribute.
Composite attributes can be divided into smaller subparts. These subparts
represent the basic attributes with independent meanings of their own. For
example, take Name attributes. We can divide it into sub-parts like First
name, Middle name, and Last-named.

Single valued and multi valued attributes


Attributes that can have single value at a particular instance of time are
called single valued. A person cant have more than one age value.
Therefore, age of a person is a single-values attribute.
A multi-valued attribute can have more than one value at one time. For
example, degree of a person is a multi-valued attribute since a person can
have more than one degree.

Derived attributes:
Derived attributes are those whose values are created from other attributes.
These values are generated with the help of algorithms, calculations and
other relevant procedures.
Example: In the entity Student, student Age would be considered a derived
attribute since it could be calculated using the student's date of birth with
the current date to find their age.

Required or Optional Attributes:


A required attribute is an attribute that must have a value in it, while an
optional attribute may not have a value in it and can be left blank.
Example: Consider the entity Student. st_LastName and st_FirstName would
be required attributes as we assume all students have a first and last name.
Optional
attributes
could
be stu_MiddleName, stu_Email,
and stu_Phone since some students may not have a middle name, a phone
number, or an email address.

Keys and non-keys Attributes:

In every entity an attribute or grouped attributes uniquely identify that


entity. These attributes are the key attributes and range from Primary key to
a Composite Key. The rest of the attributes after the identifier are considered
the non-key attributes or descriptors, which just describe the entity.
Example: suppose in Student table there is only one unique
identifier, stu_LastName, which is the primary key of the table. The rest of
the attributes are descriptors.

2. B: Difference between ERD and Schema diagram.


ERD

Schema

ERD diagram is a graphical In a schema diagram, all


representation of entities
database tables are designated
with unique columns and special
features
It shows the relationship
Schema
diagram
shows
between entities
primary/foreign keys or not null,
etc features.
Entities, Relationships, Attributes Each relation appears as a box
can be represented by Boxes,
Diamonds, ovals respectively

3A

create or replace trigger BOOKSHELF_BEF_UPD_ROW


before update on BOOKSHELF
for each row
when (new.Rating < old.Rating)
begin
insert into BOOKSHELF_AUDIT
(Title, Publisher, CategoryName,
Old_Rating, New_Rating, Audit_Date)
values
(:old.Title, :old.Publisher, :old.CategoryName,
:old.Rating, :new.Rating, Sysdate);
end;
/

First, the trigger is named:


create or replace trigger BOOKSHELF_BEF_UPD_ROW
The name of the trigger contains the name of the table it acts upon
and the type of trigger it is.

o This trigger applies to the BOOKSHELF table;


o it will be executed before update transactions have been committed to
the database:
before update on BOOKSHELF

o Because of the for each row clause the trigger will apply to each
row affected by the update statement.
for each row

The when clause adds further criteria to the triggering condition.

The triggering statement will not only update the BOOKSHELF table,
but it reflects a lowering of the Rating value:
when (new.Rating < old.Rating)

The PL/SQL code shown in the following listing----- >is the trigger
body.
The commands shown here are to be executed for every update of
the BOOKSHELF table that passes through the when condition.
Therefor the BOOKSHELF_AUDIT table must exist, and the owner of
the trigger must have been granted privileges.

This example inserts the old values from the BOOKSHELF into the
BOOKSHELF_AUDIT table before the BOOKSHELF is updated.
/* begin
insert into BOOKSHELF_AUDIT
(Title, Publisher, CategoryName,
Old_Rating, New_Rating, Audit_Date)
values
(:old.Title, :old.Publisher, :old.CategoryName,
:old.Rating, :new.Rating, Sysdate);
end; */

4.B:
5. First, create a table that will store the audit records:
drop table BOOKSHELF_AUDIT;
create table BOOKSHELF_AUDIT
(Title VARCHAR2(100),
Publisher VARCHAR2(20),
CategoryName VARCHAR2(20),
Old_Rating VARCHAR2(2),
New_Rating VARCHAR2(2),

Audit_Date DATE);

The following example shows a trigger that is executed whenever an insert


or an update occurs. The update portion of the trigger occurs only when the
Rating columns value is updated. An if clause is used to determine which
of the two commands invoked the trigger.
drop trigger BOOKSHELF_BEF_UPD_ROW;
create or replace trigger BOOKSHELF_BEF_UPD_INS_ROW
before insert or update of Rating on BOOKSHELF
for each row
begin
if INSERTING then
insert into BOOKSHELF_AUDIT
(Title, Publisher, CategoryName,
New_Rating, Audit_Date)
values
(:new.Title, :new.Publisher, :new.CategoryName,
:new.Rating, Sysdate);
else -- if not inserting then we are updating the Rating
insert into BOOKSHELF_AUDIT
(Title, Publisher, CategoryName,
Old_Rating, New_Rating, Audit_Date)
values
(:old.Title, :old.Publisher, :old.CategoryName,
:old.Rating, :new.Rating, Sysdate);
end if;
end;
/
o

First, the trigger is named:


create or replace trigger BOOKSHELF_BEF_UPD_ROW
Then the trigger body starts.

In the first part of the trigger body the type of transaction is checked via an
if clause. Valid transaction types are INSERTING, and UPDATING.

The trigger checks whether to insert records into the BOOKSHELF table or
not. If it is, The INSERTING portion inserts the new values of the record into
the BOOKSHELF_AUDIT table. Otherwise update portion will execute

Placement of Relationship Attributes:


The cardinality ratio of a relationship can affect the placement of relationship
attributes. Thus, attributes of one-to-one or one-to-many relationship sets
can be associated with one of the participating entity sets, rather than with
the relationship set.
For instance, let us consider the one-to-many relationship. One customer
may have several accounts, but each account is held by only one customer.

In this case, the attribute access_date which specifies when the customer
last accessed that account, could be associated with the account entity set.
Since each account entity participates in a relationship with at most one
instance of customer, which meaning the same if access_date is placed with
the depositor relationship set.
Attributes of a one-to-many relationship set can be repositioned only in the
many side of the relationship. For one-to-one relationship sets any one of
the participating entities can be repositioned.

4B.i

If there are five consecutive failed connection attempts to the JANE account,
the account will be automatically locked by Oracle. When you then use the
correct password for the JANE account, you will receive an error. Connect
jane/eyre
ERROR:
ORA-28000: the account is locked
To unlock the account, use the account unlock clause of the alter user
command (from a DBA account), as shown in the following listing:
alter user JANE account unlock;
Following the unlocking of the account, connections to the JANE account will
once again be allowed. You can manually lock an account via the account
lock clause of the alter user command.
alter user JANE account lock;

4b.ii
To prevent a password from being reused, you can use one of two profile
parameters: PASSWORD_REUSE_MAX or PASSWORD_REUSE_TIME. These
two parameters are mutually exclusive; if you set a value for one of them,
the other must be set to UNLIMITED.
The PASSWORD_REUSE_TIME parameter specifies the number of days that
must pass before a password can be reused. For example, if you set
PASSWORD_REUSE_TIME to 60, you cannot reuse the same password within
60 days.
The PASSWORD_REUSE_MAX parameter specifies the number of password
changes that must occur before a password can be reused. If you attempt to
reuse the password before the limit is reached, Oracle will reject your
password change.

Weak Entity set:


An entity set that does not have sufficient attributes to form a primary key is
called a weak entity set. Represented by putting double rectangle around
entity and a double diamond around each supporting relationship.

The existence of a weak entity set depends on the existence of a strong


entity set; it must be related to the strong entity set via a one-to-many
relationship set.
A distriminator of a weak entity set is the set of attributes that distinguishes
among all the entities of a weak entity set.
The primary key of a weak entity set is formed by the primary key of the
strong entity set on which the weak entity sets existence depend upon, plus
the weak entity sets distriminator.

There are two types of weak entities: associative and subtype entities.

Example of a weak entity set


o entity set [[Crews]] is weak (a weak entity set)
o relationship <<works for>> is a supporting relationship
o entity set [[Crews]] gets part of its key from the key of entity set
[Studios]
o key of entity set [[Crews]] consists of its own attribute (number) and
the key attribute (name) from entity set [Studios]

Difference between table and view:


Table
Tables hold data which are used in
applications and reports
Tables are the actual database
entities that hold rows.

View
View is a query () used as a
table which can be linked to another
table.
Views are "imaginary" tables that
are constructed based on the actual
tables.

We can have
a table without having a views.
Table does not provide security
Table takes more space than view
Table is little complex than view as
it is a list of all records.

we cant have a views without


having a table
View provides security for the data
and limits the display of data
View takes very little space
A view hides the complexity of the
database tables from end users.

Trigger:

In DBMS, trigger is a SQL procedure that initiates an action (i.e.,


fires any action) when an event (INSERT, DELETE or UPDATE) occurs. A
trigger cannot be called or executed; the DBMS automatically fires the
trigger as a result of a data modification to the associated table
These are also used
o
o
o
o

to preserve data integrity,


to control server operations,
to audit a server
And to implement business logic or business rule.

Types of Triggers:
Triggers can be categorized according to their means of invocation and the
type of actions they perform. Oracle Database supports the following types
of triggers:

1. Row triggers
A row trigger fires each time when the table is affected by the triggering
statement. For example, if a statement updates multiple rows, then a row
trigger fires once for each row affected by the UPDATE. If a triggering
statement affects no rows, then a row trigger is not run.
Row triggers are useful if the code in the trigger action depends on the data
provided by the triggering statement or the rows affected.

2. Statement triggers
A statement trigger is fired once on behalf of the triggering statement,
regardless of the number of rows affected by the triggering statement. For

example, if a statement deletes 100 rows from a table, a statementlevel DELETE trigger is fired only once.
Statement triggers are useful if the code in the trigger action does not
depend on the data provided by the triggering statement or the rows
affected.

3. Instead OF triggers
An INSTEAD OF trigger is fired by Database instead of executing the
triggering statement key.
These triggers are useful for transparently (clearly) modifying views that
cannot be modified directly through DML statements.

4. Event triggers
Event triggers can be used to publish information about database events to
subscribers. Event triggers are divided into the following categories:
o A system event trigger can be caused by database instance startup
and shutdown or error messages etc.
o A user event trigger is fired because of events related to user logon
and logoff, DDL statements, and DML statements.

Data Base: a database is a collection of information that is organized so


that it can easily be accessed, managed, and updated.

Traditional databases are organized by fields, records, and files. A field is a


single piece of information; a record is one complete set of fields; and a file
is a collection of records

A database management system is the software that allows a computer


to perform database functions of storing, retrieving, adding, deleting and
modifying data. Relational database management systems implement the
relational model of tables and relationships.
Examples: Microsoft Access, MySQL, Microsoft SQL Server, Oracle and
FileMaker Pro are all examples of database management systems.

Advantages:
1.
2.
3.
4.
5.
6.
7.

All of the information is together.


The information can be portable if on a laptop.
The information is easy to access at any time.
It's more easily retrievable.
Many people can access the same database at the same time.
Improved data security.
Reduced data entry, storage, and retrieval costs.

Data Modeling Concepts: Entity


An entity is a class of persons, places, objects, events, or concepts about which we
need to capture and store data.
An entity set is a set of entities of the same type (e.g., all persons having an
account at a bank).
An entity instance is a single occurrence of an entity.

Data Modeling Concepts: Identification


o A key is an attribute, or a group of attributes, that assumes a unique
value for each entity instance.
o A group of attributes that uniquely identifies an instance of an entity is
called a concatenated (---= ) key.

o A candidate key is a candidate to become the primary key of


instances of an entity.
o A primary key is that candidate key that will most commonly be used
to identify a single entity instance.
o Any candidate key that is not selected to become the primary key is
called an alternate key.
o A sub-setting criteria is an attribute (or concatenated attribute) whose
finite values divide all entity instances into useful subsets.

Super key
A super key is a combination of columns that uniquely identifies any row
within a relational database management system table.
For example, imagine a table used to store customer master details that
contains columns such as:

Customer name
Customer ID
Social security number (SSN)
Address
Date of birth

A certain set of columns may be unique to each customer. Examples of super


keys are as follows:

Name + SSN + Birthdate


ID + Name + SSN

E-R Diagram:
Definition: a graphical representation of entities and their relationships to
each other
The elements of an ERD are:

Entities

Relationships
Attributes

ER diagrams often use symbols to represent three different types of


information. Boxes are commonly used to represent entities. Diamonds are
normally used to represent relationships and ovals are used to represent
attributes.

Data mining
Data Mining is the process of extracting knowledge hidden from large
volumes of raw data. The knowledge must be new, not obvious, and one
must be able to use it.
There are two main kinds of models in data mining. One is predictive
models, which use data with known results to develop a model that can be
used to explicitly predict values. Another is descriptive models, which
describe patterns in existing data

Reasons to use data mining

Too much data and too little information


There is a need to extract useful information from the data

Relationship: is a situation that exists between two relational database


tables when one table has a foreign key that references the primary key of
the other table. When this exists between many database tables then it is
known as relationship set.

User, Roles, and Privilesges:


Every Oracle user has a name and password and owns any tables, views,
and other resources that he or she creates.

An Oracle role is a set of privileges (or the type of access that each user
needs, depending on his or her status and responsibilities).
Database object privileges give you the ability to perform some operation on
various objects.The DELETE privilege, for example, lets you delete rows from
tables and views.

Rollback:
We can perform a large number of insert, update, and delete operations and
still undo the work (return the tables to the way they used to be) by issuing
this command
rollback;
rollback complete
The Oracle can rolled back any work that hasnt been committed.
The process of committing or rolling back work is controlled by two SQL*Plus
commands: commit and rollback.
Delete
Removing a row or rows from a table requires the delete command. The
following command deletes the Walpole entries from the COMFORT
table:
delete from COMFORT where City = 'WALPOLE';
o recovery is possible, so long as a commit hasnt occurred using rollback
o An additional command for deleting records, truncate, does not behave
the same as delete
o Whereas delete allows you to commit or roll back the deletion

Vous aimerez peut-être aussi