Top Banner
RDBMS and SQL Saras M Srivastva PGT (Comp. Sc) Kendriya Vidyalaya Tenga Valley, Dist. Kameng (W) Arunachal Pradesh – 790 116
60

RDBMS and SQL

Jan 02, 2016

Download

Documents

kelsey-lamb

RDBMS and SQL. Saras M Srivastva PGT (Comp. Sc) Kendriya Vidyalaya Tenga Valley, Dist. Kameng (W) Arunachal Pradesh – 790 116. What is a Database?. A collection of information generally stored on and accessed using a computer Databases generally are made up of various objects - PowerPoint PPT Presentation
Welcome message from author
This document is posted to help you gain knowledge. Please leave a comment to let me know what you think about it! Share it to your friends and learn new things together.
Transcript
Page 1: RDBMS and SQL

RDBMS and SQL

Saras M SrivastvaPGT (Comp. Sc)

Kendriya Vidyalaya Tenga Valley, Dist. Kameng (W)Arunachal Pradesh – 790 116

Page 2: RDBMS and SQL

Saras M Srivastava, PGT(CS), KV, IISc, Bangalore -12 2

What is a Database?

A collection of information generally stored on and accessed using a computer

Databases generally are made up of various objects These objects provides structured access to the

information stored In addition, databases usually contain meta-data—

data about the data storedin the database

Page 3: RDBMS and SQL

Saras M Srivastava, PGT(CS), KV, IISc, Bangalore -12 3

Purpose of Database

To Reduce Redundancy To Control Data Inconsistency To Facilitate Data Sharing To Enforce Standards To Ensure Data Security To Maintain Data Integrity

Page 4: RDBMS and SQL

Data Base Abstraction Internal Level Conceptual Level External Level / View Lavel

• Internal schema at the internal level to describe physical storage structures and access paths. Typically uses a physical data model.

• Conceptual schema at the conceptual level to describe the structure and constraints for the whole database for a community of users. Uses a conceptual or an implementation data model.

• External schemas at the external level to describe the various user views. Usually uses the same data model as the conceptual level.

Saras M Srivastava, PGT(CS), KV, IISc, Bangalore -12 4

Page 5: RDBMS and SQL

View of DataAn architecture for a database system

Page 6: RDBMS and SQL

Data Independance

• Physical Data Independence: The capacity to change the internal schema without having to change the conceptual or view schema.

• Logical Data Independence: The capacity to change the conceptual schema without having to change the view schema.

Saras M Srivastava, PGT(CS), KV, IISc, Bangalore -12 6

Page 7: RDBMS and SQL

Data Models

A collection of tools for describing data data relationships data semantics data constraints

Entity-Relationship model Relational model Other models:

object-oriented model semi-structured data models Older models: network model and hierarchical

model

Page 8: RDBMS and SQL

Entity-Relationship Model

Example of schema in the entity-relationship model

Page 9: RDBMS and SQL

Entity Relationship Model (Cont.) E-R model of real world

Entities (objects) E.g. customers, accounts, bank branch

Relationships between entities E.g. Account A-101 is held by customer Johnson Relationship set depositor associates customers with

accounts

Widely used for database design Database design in E-R model usually converted to

design in the relational model (coming up next) which is used for storage and processing

Page 10: RDBMS and SQL

Relational Model Example of tabular data in the relational model

customer-name

Customer-idcustomer-street

customer-city

account-number

Johnson

Smith

Johnson

Jones

Smith

192-83-7465

019-28-3746

192-83-7465

321-12-3123

019-28-3746

Alma

North

Alma

Main

North

Palo Alto

Rye

Palo Alto

Harrison

Rye

A-101

A-215

A-201

A-217

A-201

Attributes

Page 11: RDBMS and SQL

A Sample Relational Database

Page 12: RDBMS and SQL

Saras M Srivastava, PGT(CS), KV, IISc, Bangalore -12 12

Relational Model

Introduced by E.F. Ted Codd Based upon “relational algebra” Relations actually refer to what are more

commonly referred to as tables Tuples are rows Columns are fields

Page 13: RDBMS and SQL

Network Model

The Data is represented by collection of records and the relationship among the data are represented by links.

In a n/w database the collection of records are connected to one another by means of links. A record is a collection of fields, each of which contain only one data value.

Saras M Srivastava, PGT(CS), KV, IISc, Bangalore -12 13

Page 14: RDBMS and SQL

Hierarchical Model

The Data is represented by collection on records and the relationship among the data are represented by links.

This is true of Hierarchical model. Records are organized as tree rather than

arbitrary graph. It represents the relationship among the

records through parent child relationship represented by tree.

Saras M Srivastava, PGT(CS), KV, IISc, Bangalore -12 14

Page 15: RDBMS and SQL

Saras M Srivastava, PGT(CS), KV, IISc, Bangalore -12 15

What Is A DBMS?

A DBMS is a database management system Software that allows for the creation and

management of databases and their related components

SQL Server 2000, Oracle

Page 16: RDBMS and SQL

Saras M Srivastava, PGT(CS), KV, IISc, Bangalore -12 16

Common DBMS Components

Databases Tables User-Defined Data Types Indexes Relations Views Stored Procedures

Page 17: RDBMS and SQL

Terminology

Relation Domain Tuple Attribute Degree Cardinality

Saras M Srivastava, PGT(CS), KV, IISc, Bangalore -12 17

Page 18: RDBMS and SQL

Keys

Primary Key Candidate Key Alternate Key Foreign Key

Saras M Srivastava, PGT(CS), KV, IISc, Bangalore -12 18

Page 19: RDBMS and SQL

Relational Algebra

The Select Operation The Project Operation Cartesian Product Union Intersect Set Difference

Saras M Srivastava, PGT(CS), KV, IISc, Bangalore -12 19

Page 20: RDBMS and SQL

Saras M Srivastava, PGT(CS), KV, IISc, Bangalore -12 20

SQL

A standardized language used to retrieve and manipulate information in a database

Formally meant Structured Query Language

Page 21: RDBMS and SQL

Saras M Srivastava, PGT(CS), KV, IISc, Bangalore -12 21

SQL as a DDL

DDL = Data Definition Language SQL supports commands that allow the

creation, alteration, or deletion of database objects CREATE ALTER DROP

Page 22: RDBMS and SQL

Saras M Srivastava, PGT(CS), KV, IISc, Bangalore -12 22

Data Modeling

Data modeling is the process of planning the logical design of your database

Use Data modeling to define the relationships between your data and processes

Page 23: RDBMS and SQL

Saras M Srivastava, PGT(CS), KV, IISc, Bangalore -12 23

Normalization

Normalization is the process of using formal methods to separate data to be stored in a database into multiple, related tables

Normalization helps maintain the consistency and integrity of your data

Done primarily for on-line transaction processing (OLTP) systems In contrast, on-line analytical processing (OLAP)

systems are often heavily denormalized

Page 24: RDBMS and SQL

Saras M Srivastava, PGT(CS), KV, IISc, Bangalore -12 24

Entity Integrity

Entities are single objects or concepts from the real world

Entity integrity means that you must be able to uniquely identify each entity you store in a database

Each entity in a table should have a primary key This can be a single column or multiple columns Multiple columns would be composite key

Page 25: RDBMS and SQL

Saras M Srivastava, PGT(CS), KV, IISc, Bangalore -12 25

Domain Integrity

Domain integrity restricts what can be stored in the database

Domain integrity represents your business rules You can enforce them in your application

Either at the presentation layer or business logic layer (or both!)

Page 26: RDBMS and SQL

Saras M Srivastava, PGT(CS), KV, IISc, Bangalore -12 26

Referential Integrity

Referential integrity preserves the defined relationships between tables when records are entered or deleted

Tables can have relationships based upon keys Foreign keys and primary keys Foreign keys and unique keys

Page 27: RDBMS and SQL

Saras M Srivastava, PGT(CS), KV, IISc, Bangalore -12 27

User-Defined Integrity

Applies when a business rule does not fit into one of the three other forms

Implement using constraints, triggers, or stored procedures

Page 28: RDBMS and SQL

Saras M Srivastava, PGT(CS), KV, IISc, Bangalore -12 28

Normal Forms

Codd defined various “normal forms”, or levels of normalization

Generally you want to get to at least third normal form

Page 29: RDBMS and SQL

Saras M Srivastava, PGT(CS), KV, IISc, Bangalore -12 29

Denormalization

The deliberate process of introducing redundant data into your database

Generally done to increase application performance

Often occurs when one database is both a Transaction Processing and Decision Support System source

Page 30: RDBMS and SQL

Saras M Srivastava, PGT(CS), KV, IISc, Bangalore -12 30

Databases

A database is logically a collection of components and data

Physically represented by one or more operating system files

Use the CREATE DATABASE command to define new databases

Page 31: RDBMS and SQL

Saras M Srivastava, PGT(CS), KV, IISc, Bangalore -12 31

Tables

Tables are database objects that contain all of the data in a database

Tables have one or more columns Each column has a specific data type

Tables have zero or more rows Define using CREATE TABLE command

Page 32: RDBMS and SQL

Saras M Srivastava, PGT(CS), KV, IISc, Bangalore -12 32

Indexes

Indexes provide a way to access data in a table quickly

Indexes can be applied to one or more columns in a table or view

Create using the CREATE INDEX command Can also be created as part of table creation

Page 33: RDBMS and SQL

Saras M Srivastava, PGT(CS), KV, IISc, Bangalore -12 33

Index Characteristics

Clustered versus nonclustered Unique versus non-unique Single-column versus multi-column Ascending or descending order on the

columns in the index Covering or non-covering

Page 34: RDBMS and SQL

Saras M Srivastava, PGT(CS), KV, IISc, Bangalore -12 34

Views

Views are virtual tables whose contents are defined by a query

Views are defined using SQL SELECT statements JOINS are used to bring data from more than one

table together SQL Server supports indexed views

Page 35: RDBMS and SQL

Saras M Srivastava, PGT(CS), KV, IISc, Bangalore -12 35

Stored Procedures

Stored procedures are SQL programs stored in your database

Stored procedures can increase the data access and manipulation performance of your application

Stored procedures are more secure than dynamic SQL execution

Define using the CREATE PROCEDURE command

Page 36: RDBMS and SQL

Saras M Srivastava, PGT(CS), KV, IISc, Bangalore -12 36

Constraints

Rules that enforce entity integrity Can be applied to columns or tables

SQL Server supports five classes: NOT null CHECK UNIQUE PRIMARY key FOREIGN key

Page 37: RDBMS and SQL

Saras M Srivastava, PGT(CS), KV, IISc, Bangalore -12 37

Defaults

Specify values placed in a column if you do not specify a value for the column when inserting a row

Can be anything that evaluates to a constant value Static value Built-in function Mathematical expression

Page 38: RDBMS and SQL

Saras M Srivastava, PGT(CS), KV, IISc, Bangalore -12 38

Triggers

Special class of stored procedure Execute automatically when an UPDATE,

INSERT, or DELETE statement is issues against a table or view

Tables can have multiple triggers Define using CREATE TRIGGER

Page 39: RDBMS and SQL

Saras M Srivastava, PGT(CS), KV, IISc, Bangalore -12 39

SQL As A DML

DML = Data Manipulation Language DML commands allow you to perform

standard CRUD operations Create data Read data Update data Delete data

Page 40: RDBMS and SQL

Saras M Srivastava, PGT(CS), KV, IISc, Bangalore -12 40

SQL’s CRUD Commands

INSERT SELECT UPDATE DELETE

Page 41: RDBMS and SQL

Saras M Srivastava, PGT(CS), KV, IISc, Bangalore -12 41

SELECT

The SELECT command allows you to retrieve one or more rows from a table or view

Can use JOIN statements to access data in multiple tables/views

Page 42: RDBMS and SQL

Saras M Srivastava, PGT(CS), KV, IISc, Bangalore -12 42

You read data from the database by using a select

The simple form of the syntax is

This looks at the tables, finds the rows that satisfy the where predicates, and returns the columns from those rows

Select (simple form)

select columnsfrom tables where predicates

Page 43: RDBMS and SQL

Saras M Srivastava, PGT(CS), KV, IISc, Bangalore -12 43

Selectselect owner from accounts;

select * from accounts;

select owner from accounts where balance > 1000;

select time, amount from transactions where account = 'Bill' and deposit = 1;

select owner from accounts where balance between 1000 and 50000;

select * from accounts where owner in (‘Bill’, ‘Bob’, ‘Tom’)order by balance;

select * from accounts where owner in (‘Bill’, ‘Bob’, ‘Tom’)order by balance, owner;

Page 44: RDBMS and SQL

Saras M Srivastava, PGT(CS), KV, IISc, Bangalore -12 44

select avg(balance) from accounts;

You can compute an aggregate function on the return value of a select

You can use ordinary functions in where clauses, but not aggregate functions

Set Functions

select count(*) from accounts;

select sum(amount) from transactions where account = ‘Bob’

and deposit = 1;

Page 45: RDBMS and SQL

Saras M Srivastava, PGT(CS), KV, IISc, Bangalore -12 45

Grouping controls how the set functions are computed no “group by” clause ? Compute one value for

entire result set Otherwise, use columns in a group by clause to

control aggregations

group by can take a sequence of column names (like order by)

Grouping for Set Functions

select sum(amount) from transactions where deposit = 1group by account;

Page 46: RDBMS and SQL

Saras M Srivastava, PGT(CS), KV, IISc, Bangalore -12 46

INSERT

The SQL INSERT command adds a new row to a table or view

Can be used with views, however, only one table will be updated

Can perform multiple-row INSERT statements

Page 47: RDBMS and SQL

Saras M Srivastava, PGT(CS), KV, IISc, Bangalore -12 47

Occasionally, you also have to put data into the database. This is done using insert

The syntax is

This can fail, if any of the database constraints are violated by the insert

No where, order by, ... clauses in inserts

Insert

insert into table(column-1, column-2, ..., column-k) values (value-1, value-2, ..., value-k)

Page 48: RDBMS and SQL

Saras M Srivastava, PGT(CS), KV, IISc, Bangalore -12 48

Update

Update is a way to change existing rows

The where clause functions exactly as in the select statement No where clause ? Change every row

Still no need for order by or group by

update tableset column-1 = value-1,

column-2 = value-2, ...where predicates;

Page 49: RDBMS and SQL

Saras M Srivastava, PGT(CS), KV, IISc, Bangalore -12 49

UPDATE

Update allows you to change data in a table or view

Changes can affect a single row or multiple rows

Page 50: RDBMS and SQL

Saras M Srivastava, PGT(CS), KV, IISc, Bangalore -12 50

DELETE

The SQL DELETE statement allows you to remove one or more rows from a table or view

Page 51: RDBMS and SQL

Saras M Srivastava, PGT(CS), KV, IISc, Bangalore -12 51

Delete

Delete is a way to delete existing rows

The where clause functions exactly as in the select statement No where clause ? Delete everything

delete from tablewhere predicates;

Page 52: RDBMS and SQL

Saras M Srivastava, PGT(CS), KV, IISc, Bangalore -12 52

Transactions

Often you need to perform multiple commands as a single unit of work

A transaction is a sequence of individual command grouped together into one logical command

Page 53: RDBMS and SQL

Saras M Srivastava, PGT(CS), KV, IISc, Bangalore -12 53

ACID Rules

Atomicity Consistency Isolation Durability

Page 54: RDBMS and SQL

Saras M Srivastava, PGT(CS), KV, IISc, Bangalore -12 54

Atomicity

Atomicity specifies that a transaction is an “all or nothing” operation

All individual commands either succeed or fail as a single unit

Page 55: RDBMS and SQL

Saras M Srivastava, PGT(CS), KV, IISc, Bangalore -12 55

Consistency

Consistency is really more of a side effect of the other three parts

Consistency can be enforced by using constraints, defaults, relations, and triggers

Consistency is all about the integrity of your data

Page 56: RDBMS and SQL

Saras M Srivastava, PGT(CS), KV, IISc, Bangalore -12 56

Isolation

Isolation affects how transactions “see” one another Modifications can be hidden until completion

Isolation is set in levels Lower levels increase concurrency while reducing

consistency Higher levels increase consistency while reducing

concurrency Isolation is set on a connection-by-connection

basis

Page 57: RDBMS and SQL

Saras M Srivastava, PGT(CS), KV, IISc, Bangalore -12 57

Locks

Locks are used to enforce isolation Locks are applied to data items

Tables, sets of rows, and rows Two core types of locks

Read locks (shared) Write locks (exclusive)

Page 58: RDBMS and SQL

Saras M Srivastava, PGT(CS), KV, IISc, Bangalore -12 58

Durability

Once a transaction signals completion, the system guarantees that the effects of the transaction are permanent

Assuming there isn’t a catastrophic failure On commit, your changes are successful On rollback, your data is returned to it’s previously

known consistent state.

Page 59: RDBMS and SQL

Saras M Srivastava, PGT(CS), KV, IISc, Bangalore -12 59

Questions?

Page 60: RDBMS and SQL

Saras M Srivastava, PGT(CS), KV, IISc, Bangalore -12 60

Thank You