d b m s animate

Post on 24-Jan-2015

1.699 Views

Category:

Technology

2 Downloads

Preview:

Click to see full reader

DESCRIPTION

 

TRANSCRIPT

Data

Data - raw facts/details

Entity: a thing of significance about which

information needs to be known.

Attributes :The characteristics that describe

or qualify an entity are called its attributes

Data Continued:

In case of a student , the basic entity is the student.

Entity attributes (information recorded about that entity) may include:

First and last nameHome addressCurrent addressDate of birthCourse optedGrade point average etc…

Data Continued:

Field: A field consists of a group of characters.

Record : All the details related to an entity is combined to form a record.

File : A collection of related records Example: For a student, Admission file, Fee

detail file, exam detail file etc…Database: Collection of logically related

records & files.

Traditional Approach

Applications developed in an ad-hoc and

opportunistic manner

Data files developed for individual applications

Files Dedicated to Application Programs

Database

What is a database?

A database is an organized collection of related files and records designed to meet the needs of an organization.

Database System ComponentsData

Hardware

Software

Users

Interacting with a Database Application

Data Sharing in a Database Environment

DBMSThe Database Management System

(DBMS) software that enables users to

define, create and maintain the database

and provides flexible management of the

data.

Why We Need DBMS

There is an information explosion in today’s society

Need to have right information at the right time to make accurate decisions

Using DBMS

Centralization of information management

Data shared by different groups of users and

application programs

Provision of multiple interfaces

Advanced facilities for backup and recovery

DBMS AdvantagesControlled redundancyData Consistency/IntegritySharing of dataEnforcement of security Enforcement of development and

maintenance standardsData independence

DBMS Disadvantages

Centralized Database

More Disk Space

Operationality of the system

Security Risk

DBMS-Users There are a number of users who can access or retrieve data on

demand using the applications and interfaces provided by the DBMS.

Each type of user needs different software capabilities:

DDL-The application programmers interact with the database by accessing the data from programs written in high-level languages

DML– The end users are the people who sit at workstations and interact directly with the system.

DCL– The database administrator (DBA) is the person or group in charge of implementing the database system within the organization.

The Entity Relationship Model

The ER model is a graphic representation to concisely present the data requirements of an application in a way that is easy to understand

Today ER model is a standard for the design methodologies of software systems

Data Models

Models generally allow people to conceptualize

an abstract idea more easilyModel airplanes Model homesA data model is a way of explaining the

logical layout of the data and the relationship of

various parts to each other and the whole.

Data ModelsHierarchical Model

Data ModelsNetwork Model

Data ModelsRelational Model

Data ModelsThe Relational Database Model:

Relational database model, where all data are kept in tables or relations.

More flexible & easy to use.Almost any item of data can be accessed more

quickly than the other models.Retrieval time is reduced so that interactive

access becomes more feasible.This is what is referred to as Relational

Database Management System(RDBMS)

Relational DBMS Edgar F. Codd at IBM invented the relational database in

1970. Called Father of RDBMS.

RDBMS allows operations in a human logical environment.

The main elements of RDBMS are based on Codd’s 13 rules for a relational system.

The relational database is perceived as a collection of tables.

Each table consists of a series of row/column intersections.

Tables (or relations) are related to each other by sharing common characteristics

H

How do I design a database?„ Logical design™Determine and define fields, tables, keys, and data integrity„ Physical implementation™Creating tables, establishing key fields, and table relationships„ Application development™Determine end-user tasks

Data AbstractionThe major purpose of a database system is to

provide users with an abstract view of the system. The system hides certain details of how data is stored and created and maintained

Complexity should be hidden from database users.

1.Physical level:- The lowest level of abstraction that describes how the data is actually stored.

Data Abstraction

2.Logical Level:-The next level of abstraction that describes the relationships among data.

3.View level:-Highest level of abstraction that describes part of the database for a particular group of users

Can be many different views of a database.

Data Abstraction

Instances & SchemasInstance:- The information that is currently being

stored in the database is called an instance of the database

Schema :- Overall design of the database is called schema.

At the lowest level is the physical schema, followed by Logical schema & the highest level is the subschema.

Database Systems will support only one Physical & Logical Schema but can have several subschemas.

RDBMSA database management system that stores

data in the form of related tables is called Relational Database Management System.

The goal of RDBMS is to make data easy to store & retrieve

Relational databases help solve problems as they are designed to create tables & then combine the information in interesting ways to create valid information.

RDBMSTypical RDBMS include Microsoft Access Microsoft SQL Server Sybase (The forerunner of Microsoft SQL Server) IBM DB2 Oracle Ingres MySQL Postgresql etc

RDBMSConstraints are conditions that must hold on

all valid relation instances. There are different types of constraints:

Entity integrity constraints, and referential integrity constraints

A candidate key is one that can identify each row of a table uniquely.

Generally a candidate key becomes the primary key of the table.

RDBMS

If the table has more than one candidate key, one of them will become the primary key, and the rest are called alternate keys.

A key formed by combining at least two or

more columns is called composite key.

Integrity Rules

Entity integrity rule states that Primary attribute in the table should be unique & not null

Referential integrity rule states that a given non-null foreign key value must have a matching primary key value somewhere in the referenced relation

Cascading deletions through related tables & preventing deletion when related records exist are the methods used to enforce referential integrity

Integrity RulesDomain Integrity rule ensures that the value of

the columns of a relation are legal according to the domain definitions.

Domain definitions can be physical & Logical.

NormalizationNormalization is the the process of reducing

duplication in a database, with the ultimate goal of eliminating duplicate data entirely. A basic goal of normalization is to create a set of relational tables that are free of redundant data and the data should be consistent.

Duplication takes more disk space, the bigger issue is consistency. Duplication creates the risk of data corruption when information is inserted, updated, or deleted, by having a particular piece of information in more than one place.

Relations that have redundant data may have problems called update anomalies, which are classified as ,

Insertion anomaliesDeletion anomaliesModification anomalies

Unnormalized form (UNF)

A table that contains one or more repeating groups.ClientNo cName propertyNo pAddress rentStart rentFinish rent ownerNo oName

CR76John

kay

PG4

PG16

6 lawrence

St,Glasgow

5 Novar Dr,

Glasgow

1-Jul-00

1-Sep-02

31-Aug-01

1-Sep-02

350

450

CO40

CO93

Tina Murphy

Tony Shaw

CR56Aline

Stewart

PG4

PG36

PG16

6 lawrence

St,Glasgow

2 Manor Rd,

Glasgow

5 Novar Dr,

Glasgow

1-Sep-99

10-Oct-00

1-Nov-02

10-Jun-00

1-Dec-01

1-Aug-03

350

370

450

CO40

CO93

CO93

Tina Murphy

Tony Shaw

Tony Shaw

Figure 3 ClientRental unnormalized table

Repeating group = (propertyNo, pAddress, rentStart, rentFinish, rent, ownerNo, oName)

First Normal Form (1NF)First Normal Form is a relation in which

the intersection of each row and column contains one and only one value.

There are two approaches to removing repeating groups from unnormalized tables:

1. Removes the repeating groups by entering appropriate data in the empty columns of rows containing the repeating data.

2. Removes the repeating group by placing the repeating data, along with a copy of the original key attribute(s), in a separate relation. A primary key is identified for the new relation.

Client (clientNo, cName)PropertyRentalOwner (clientNo, propertyNo, pAddress, rentStart,

rentFinish, rent, ownerNo, oName)

ClientNo cName

CR76 John Kay

CR56 Aline Stewart

ClientNo propertyNo pAddress rentStart rentFinish rent ownerNo oName

CR76 PG46 lawrence

St,Glasgow1-Jul-00 31-Aug-01 350 CO40

Tina Murphy

CR76 PG165 Novar Dr,

Glasgow1-Sep-02 1-Sep-02 450 CO93

Tony Shaw

CR56 PG46 lawrence

St,Glasgow1-Sep-99 10-Jun-00 350 CO40

Tina Murphy

CR56 PG362 Manor Rd,

Glasgow10-Oct-00 1-Dec-01 370 CO93

Tony Shaw

CR56 PG165 Novar Dr,

Glasgow1-Nov-02 1-Aug-03 450 CO93

Tony Shaw

Second Normal Form (2NF)Second normal form (2NF) is a relation

that is in first normal form and every non-primary-key attribute is fully functionally dependent on the primary key.

The normalization of 1NF relations to 2NF involves the removal of partial dependencies. If a partial dependency exists, we remove the function dependent attributes from the relation by placing them in a new relation along with a copy of their determinant.

Second Normal Form (2NF)After removing the partial dependencies, the creation of

the three new relations called Client, Rental, and PropertyOwner

ClientNo cNameCR76 John Kay

CR56 Aline Stewart

Client

ClientNo propertyNo rentStart rentFinishCR76 PG4 1-Jul-00 31-Aug-01

CR76 PG16 1-Sep-02 1-Sep-02

CR56 PG4 1-Sep-99 10-Jun-00

CR56 PG36 10-Oct-00 1-Dec-01

CR56 PG16 1-Nov-02 1-Aug-03

Rental

propertyNo pAddress rent ownerNo oName

PG4 6 lawrence St,Glasgow 350 CO40 Tina Murphy

PG16 5 Novar Dr, Glasgow 450 CO93 Tony Shaw

PG36 2 Manor Rd, Glasgow 370 CO93 Tony Shaw

PropertyOwner

Third Normal FormTransitive dependency A condition where A, B, and C are attributes

of a relation such thatif A B and B C, then C is transitively

dependent on A via B (provided that A is not functionally dependent on B or C).

The resulting 3NF relations have the forms:

Client (clientNo, cName)Rental (clientNo, propertyNo, rentStart, rentFinish)PropertyOwner (propertyNo, pAddress, rent, ownerNo)Owner (ownerNo, oName)

ClientNo cNameCR76 John Kay

CR56 Aline Stewart

ClientClientNo propertyNo rentStart rentFinishCR76 PG4 1-Jul-00 31-Aug-01

CR76 PG16 1-Sep-02 1-Sep-02

CR56 PG4 1-Sep-99 10-Jun-00

CR56 PG36 10-Oct-00 1-Dec-01

CR56 PG16 1-Nov-02 1-Aug-03

Rental

propertyNo pAddress rent ownerNo

PG4 6 lawrence St,Glasgow 350 CO40

PG16 5 Novar Dr, Glasgow 450 CO93

PG36 2 Manor Rd, Glasgow 370 CO93

PropertyOwner

ownerNo oName

CO40 Tina Murphy

CO93 Tony Shaw

Owner

INTRODUCTIONSQL (Structured Query Language) is a

computer language aimed to store, manipulate, and retrieve data stored in relational databases.

The first incarnation of SQL appeared in 1974, when a group in IBM developed the first prototype of a relational database.

The first commercial relational database was released by Relational Software later becoming Oracle.

DDLDDL - Data Definition Language:

Statements used to define the database structure or schema. Some examples:

CREATE - to create objects in the databaseALTER - alters the structure of the

databaseDROP - delete objects from the databaseRENAME - rename an object

Data Manipulation LanguageDML- Data Manipulation Language:

Statements used for managing data within schema objects.

SELECT - retrieve data from the a database INSERT - insert data into a tableUPDATE - updates existing data within a

tableDELETE - deletes all records from a table,

the space for the records remain CALL - call a PL/SQL or Java subprogram

CREATE TABLEThe SQL syntax for CREATE TABLE isCREATE TABLE "table_name"

("column "data_type_for_column_1","column "data_type_for_column_2",... );

CREATE TABLESo, if we are to create the customer table

specified as above, we would type inCREATE TABLE customer

(First_Name char(50),Last_Name char(50),Address char(50),City char(50),Country char(25),Birth_Date date) ;

CREATE TABLE customer(Cust_ID int Primary key,

First_Name char(50),Last_Name char(50),Address char(50),City char(50),Country char(25),Birth_Date date);

ALTER TABLE Using the "customer" table created in the CREATE TABLE section:

If we want to add a column called "Gender" to this table. To do this, we key in:

ALTER table customer add Gender char(1) ;

DROP TABLE Sometimes we may decide that we need to get rid

of a table in the database for some reason.The syntax for DROP TABLE is

DROP TABLE "table_name"

So, if we wanted to drop the table called customer that we created in the CREATE TABLE section, we simply type

DROP TABLE customer.

TRUNCATE TABLE Sometimes we wish to get rid of all the data in

a table. One way of doing this is with DROP TABLE. But here we get rid of the data but not the table itself ? For this, we can use the TRUNCATE TABLE command.

The syntax for TRUNCATE TABLE isTRUNCATE TABLE "table_name"

So, if we wanted to truncate the table called customer that we created ,we simply type,

TRUNCATE TABLE customer

RENAME TABLE If we want the change the name of the table

we have created ,use the rename commandThe syntax for RENAME TABLE is RENAME TABLE customer TO new table name So if we want to change the name to

persons ,type in

RENAME TABLE customer TO Person

Data manipulation language are used for managing data within schema objects

INSERTINSERT INTO table_name

VALUES (value1, value2, value3,...) The second form specifies both the column

names and the values to be inserted:INSERT INTO table_name (column1,

column2, column3,...)VALUES (value1, value2, value3,...)

UPDATEThe UPDATE statement is used to update

existing records in a table.SQL UPDATE SyntaxUPDATE table_name

SET column1=value, column2=value2,...WHERE some_column=some_value

DML CommandsSELECT: retrieve data from the databaseINSERT : insert data into the table UPDATE : updates existing data within a

table DELETE: delete all records from a

table ;only the space for the records remain

CALL: call a PL/SQL OR Java subprogram

DELETEThe DELETE statement is used to delete rows

in a table.SQL DELETE SyntaxDELETE FROM table_name

WHERE some_column=some_value

SELECTTo illustrate this , assume that we have the

following table:

Table : Store Information

store_name Sales DateLos Angeles $1,500 J an- 05- 1999San Diego $250 J an- 07- 1999Los Angeles $300 J an- 08- 1999Boston $700 J an- 08- 1999

SELECTTo select all the stores in this table, we key in, SELECT store_name FROM Store_Information

Result:store_nameLos AngelesSan DiegoLos AngelesBoston

SELECT DISTINCTIf we only want to select each distinct element? We have to

do, is to add DISTINCT after SELECT. The syntax is as follows:

SELECT DISTINCT "column_name"FROM "table_name"For example, to select all distinct stores in Table Store_Information, we key in,

SELECT DISTINCT store_name FROM Store_InformationResult:

store_name Los Angeles San Diego Boston

WHERENext, we might want to conditionally select the data from a table.. To do

this, we use the WHERE keyword. The syntax is as follows:

SELECT "column_name“ FROM "table_name“ WHERE "condition“

For example, we may want to only retrieve stores with sales above $1,000

we key in,

SELECT store_name FROM Store_Information WHERE Sales > 1000

Result:store_nameLos Angeles

ORDER BY We want to list the output in a particular order.

This could be in ascending order, in descending order, or could be based on either numerical value or text value. In such cases, we can use the ORDER BY keyword.The syntax for an ORDER BY statement is as follows:SELECT "column_name“ FROM "table_name"

[WHERE "condition"] ORDER BY "column_name" [ASC, DESC]

ORDER BY we may wish to list the contents of Table Store_Information by dollar amount, in descending order:

we key in,SELECT store_name, Sales, Date FROM

Store_InformationORDER BY Sales DESCResult:

  store_name Sales DateLos Angeles $1,500 J an- 05- 1999Boston $700 J an- 08- 1999Los Angeles $300 J an- 08- 1999San Diego $250 J an- 07- 1999

INDEXWith the proper index in place, the database system can first go

through the index to find out where to retrieve the data, and then go to these locations directly to get the needed data.

The general syntax for creating an index is: CREATE INDEX "INDEX_NAME" ON "TABLE_NAME"

(COLUMN_NAME) If we want to create an index on the column Last_Name, we

would type in, CREATE INDEX IDX_CUSTOMER_LAST_NAME

on CUSTOMER (Last_Name) If we want to create an index on both City and Country, we

would type in, CREATE INDEX IDX_CUSTOMER_LOCATION

on CUSTOMER (City, Country)

Primary key Primary keys can be specified either when the table is

created using CREATE TABLE or by changing the existing table structure using ALTER TABLE

Specifying a primary key when creating a table:

CREATE TABLE Customer (SID integer, Last_Name varchar(30), First_Name varchar(30), PRIMARY KEY (SID));

Contd…For specifying a primary key by altering a table:

ALTER TABLE Customer ADD PRIMARY KEY (SID);

ALIASEThere are two types of aliases that are used most frequently: column alias and table alias.

The syntax for both the column and table aliases: SELECT "table_alias"."column_name1" "column_alias“

FROM "table_name" "table_alias"

Using the same example

SELECT A1.store_name Store, SUM(A1.Sales) "Total Sales"FROM Store_Information A1GROUP BY A1.store_name

Result:

Store  Total SalesLos Angeles  $1800San Diego  $250Boston  $700

JOINIf we want to find out sales by region. We see that table Geography includes information on regions and stores, and table Store_Information contains sales information for each store. To get the sales information by region, we have to combine the information from the two tables.

SELECT A1.region_name REGION, SUM(A2.Sales) SALESFROM Geography A1, Store_Information A2WHERE A1.store_name = A2.store_nameGROUP BY A1.region_name

JOINResult:REGION  SALESEast  $70West  $2050

JOINResult:REGION  SALESEast  $70West  $2050

Thank You

top related