RDBMS and SQL
Saras M SrivastvaPGT (Comp. Sc)
Kendriya Vidyalaya Tenga Valley, Dist. Kameng (W)Arunachal Pradesh – 790 116
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
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
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
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
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
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
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
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
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
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
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
Saras M Srivastava, PGT(CS), KV, IISc, Bangalore -12 16
Common DBMS Components
Databases Tables User-Defined Data Types Indexes Relations Views Stored Procedures
Terminology
Relation Domain Tuple Attribute Degree Cardinality
Saras M Srivastava, PGT(CS), KV, IISc, Bangalore -12 17
Keys
Primary Key Candidate Key Alternate Key Foreign Key
Saras M Srivastava, PGT(CS), KV, IISc, Bangalore -12 18
Relational Algebra
The Select Operation The Project Operation Cartesian Product Union Intersect Set Difference
Saras M Srivastava, PGT(CS), KV, IISc, Bangalore -12 19
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
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
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
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
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
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!)
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
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
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
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
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
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
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
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
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
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
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
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
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
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
Saras M Srivastava, PGT(CS), KV, IISc, Bangalore -12 40
SQL’s CRUD Commands
INSERT SELECT UPDATE DELETE
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
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
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;
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;
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;
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
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)
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;
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
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
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;
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
Saras M Srivastava, PGT(CS), KV, IISc, Bangalore -12 53
ACID Rules
Atomicity Consistency Isolation Durability
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
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
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
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)
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.