Upload
khalid-c
View
179
Download
2
Embed Size (px)
Citation preview
RDBMS Concepts – Part I [email protected]
NIC Wayanad 2
We Discuss… What is a database RDBMS Concepts Relational Model Entity – Relationship Model Relationships Constraints Keys Normalization Transaction Control
NIC Wayanad 3
What is a database ? A database is an organized collection
of information To manage databases, you need
database management systems (DBMS)
NIC Wayanad 4
DBMS A collection of programs that
enables you to store, modify, and extract information from a database.
A computer program designed for the purpose of managing database
NIC Wayanad 5
Type of databases Four main types of databases
Hierarchical Network Relational Object Relational
NIC Wayanad 6
Relational Database Concept
Dr. E.F.Codd proposed the relational model for database system in 1970.
It is the basis for the relational database management system (RDBMS)
NIC Wayanad 7
Relational Database Model Uses tables to organize data Each table corresponds to an entity Each row represents the instance of
that entity Tables can be related each other
• Relation=Table• Tuple=Row• Attribute=Column
NIC Wayanad 8
RDBMS Fundamental Features
ACID Referential Integrity Transactions Unicode
other objects are supported Data Domain Cursor Trigger Function Procedure
Database Capabilites Union Inner joins Outer joins Inner selects Merge Blobs
NIC Wayanad 9
Databases in marketRDBMS Maintainer Software license
4th Dimension 4D s.a.s ProprietaryAdabas Software AG ?Adaptive Server Enterprise Sybase ProprietaryApache Derby Apache Apache LicenseDB2 IBM ProprietaryDBISAM Elevate Software ProprietaryElevateDB Elevate Software ProprietaryFirebird Firebird project IPL and IDPLInformix IBM ProprietaryHSQLDB HSQL Development Group BSDH2 H2 Software FreewareIngres Ingres Corp. GPL and proprietaryInterBase CodeGear ProprietaryMaxDB MySQL AB, SAP AG GPL or proprietaryMicrosoft SQL Server Microsoft Proprietary
MonetDBThe MonetDB Developer Team
MonetDB Public License v1.1
MySQL MySQL AB GPL or proprietaryHP NonStop SQL Hewlett-Packard ProprietaryOracle Oracle Corporation ProprietaryOracle Rdb Oracle Corporation ProprietaryOpenEdge Progress Software Corporation ProprietaryOpenLink Virtuoso OpenLink Software GPL or proprietaryPervasive PSQL Pervasive Software Proprietary
PostgreSQLPostgreSQL Global Development Group BSD
Pyrrho DBMS University of Paisley ProprietarySmallSQL SmallSQL LGPLSQL Anywhere Sybase ProprietarySQLite D. Richard Hipp Public domainTeradata Teradata ProprietaryValentina Paradigma Software Proprietary
NIC Wayanad 10
Relational ModelThree key terms are used extensively in relational
models Relation
• relation is a table with columns and rows Attributes
• The named columns of the relation are called attributes
Domains• set of values the attributes are allowed to
take
NIC Wayanad 11
Relational ModelProducerID ProducerName Place Sex Caste DateBirth
1 BALAKRISHNAN.P.K VATTOLI BAZAR M GEN
2 VASUDEVAN NAMBIAR BALUSSERY M GEN 01/07/1948
3 HARIDASAN.M KARIYATHANKAVU M GEN 11/05/1962
4 ASIYA.N.K VATTOLI BAZAR F OBC
5 VELU VATTOLI BAZAR M OBC 02/10/1947
6 KELUKUTTI NAIR. P VATTOLI BAZAR M GEN
7 ROHINI. V.K VATTOLI BAZAR F GEN 01/07/1933
8 SURESH BABU.K.R KARIYATHANKAVU M ST
9 KUTTINARAYANAN NAIR VATTOLI BAZAR M GEN 07/10/1971 Tuple10 LAKSHMI.T.P VATTOLI BAZAR F GEN
11 ABUBAKKAR. M KAPPURAM M OBC 22/04/1959
12 RAJAN NAIR.O.K KARIYATHANKAVU M GEN 15/11/1956
13 PRABAKARAN NAIR.K.K VATTOLI BAZAR M GEN
14 BALAN.K.P VATTOLI BAZAR M SC 25/09/1967
Domain
•A relation has a unique name and represents particular entity
•Each row of a relation referred to as a tuple
•A Key is a part of tuple that uniquely distinguishes from other tuples
•relation is a set of tuples
NIC Wayanad 12
Data Modelling
Database Server
Model of system in clients’s mind
ER Model of Client’s model
Table model of ER model
Tables on disk
Analysis StageAnalysis Stage
Identifies:: entitiesentities - things of significance - things of significance relationshipsrelationships - associations between entities - associations between entities functionsfunctions - processes in the business - processes in the business businessbusiness rulesrules - restrictions or constraints - restrictions or constraints
Does NOT identify tables, views, files, screens, reports, or other computer-specific items...
NIC Wayanad 14
Entity – Relationship Model
It facilitates communication between the database designer and the end user during the requirement analysis.
To facilitate such communication the designer needs adequate communication tools
NIC Wayanad 15
Basic Elements of ER Model
Entity• An entity defines any person, place, thing
or concept • Eg : (Producer, Taluk, Course)
Attribute• Characteristics (properties) of an entity
• Eg: (ProducerName, MemberID, HouseName)
Relationship• Logical linkage between two entities
NIC Wayanad 16
ER Diagram
NIC Wayanad 17
Let us model
A Depot is a place where Producers pours their cow milk
A Producer is a person who belongs to a Depot and regularly pours Milk to Society
A Collection Agent, who procures Milk from Producers through Depot.
Procurement Register maintains by Collection Agent with the details of Milk poured by Producers
A Member is a Producer, who gets Membership through board resolution.
NIC Wayanad 18
ER Model - Entities
The highlighted words becomes entity Entities are represented by rectangular box
DepotDepotProducersProducers
MembersMembersCollection AgentCollection Agent
Procurement RegisterProcurement Register
NIC Wayanad 19
ER Model - Attributes
Attributes are the properties of entities Provides additional information about entity Possible attributes for each entity
Depot : DepotID, Name, DateStart Producers : Name, Address, DateofBirth, Sex, Caste and
MemberID etc Collection Agent : Name, Address Members: MemberID, Date of Admission, Share Value Procurement Register: ProducerID, Date, Time, MilkQty,
Milk Value
NIC Wayanad 20
Relationships A relationship is an association between
two or more entities Each entity in a relationship plays a role Examples:
if we are identifying relationships between DEPOT and PRODUCER, then we might define the “pours”, “Belongs to”
NIC Wayanad 21
ER Model - Entities
DepotDepot
DepotIDDepotIDNameNameAddressAddress
ProducersProducers
ProducerNameProducerNameAddressAddressSexSexCasteCastePhonePhoneMemberIDMemberIDDateofJoinDateofJoin
MembersMembers
MemberIDMemberIDDateofAdmissionDateofAdmissionShareNosShareNosShareValueShareValueDateOfBirthDateOfBirthIDCardNoIDCardNo
Proc_RegProc_Reg
ProducerIDProducerIDDateDateTimeTimeMilkQtyMilkQtyCLRCLRFATFATSNFSNFMilkValueMilkValue
Collection_AgentCollection_Agent
AgentIDAgentIDNameNameAddressAddress
DepotDepot
DepotID
Name
Address
NIC Wayanad 22
ER Model - Relationships Logical linkage between two entities A relationship connects two or more entity sets Relationships are represented by diamond shape
box
NIC Wayanad 23
DepotDepot
DepotIDDepotIDNameNameAddressAddress
ProducersProducers
ProducerNameProducerNameAddressAddressSexSexCasteCastePhonePhoneMemberIDMemberIDDateofJoinDateofJoin
MembersMembers
MemberIDMemberIDDateofAdmissionDateofAdmissionShareNosShareNosShareValueShareValueDateOfBirthDateOfBirthIDCardNoIDCardNo
Proc_RegProc_Reg
ProducerIDProducerIDDateDateTimeTimeMilkQtyMilkQtyCLRCLRFATFATSNFSNFMilkValueMilkValue
Collection_AgentCollection_Agent
AgentIDAgentIDNameNameAddressAddress
Contains Becomes
Pours Milk
handles
Managed by
NIC Wayanad 24
Constraints….
are rules that describe what valid data look like
ensure the logical and semantic consistency
Rules used to ensure accuracy and integrity of data
NIC Wayanad 25
Constraint Types
NIC Wayanad 26
PRIMARY KEY Constraints
primary key is a candidate key to uniquely identify each row in a table.
PCODE PANCH_NAME01 KANIAMBETTA02 KOTTATHARA03 MEPPADI04 MUTTIL05 PADINJARETHARA
NIC Wayanad 27
PRIMARY KEY Constraints
Defining PRIMARY KEYCREATE TABLE Depot (DepotID INT, DepotName VARCHAR(20), CONSTRAINT dep_pk PRIMARY
KEY(DepotID))
Add a PRIMARY KEY to an existing table
ALTER TABLE Depot ADD CONSTRAINT pk_dep PRIMARY KEY (DepotID)
NIC Wayanad 28
FOREIGN KEY Constraints Foreign key: a field in a table that is a primary
key in another table Foreign key creates a relationship between the
two tables Enforces referential integrity Maps to the PRIMARY KEY in parent table
DepotID Name1 OFFICE2 VADUVANA3 ANDOOR4 PAMBALA5 KADALMADU6 MUNNOORE
ProducerID ProducerName DepotID1 JOSEPH V.V 12 VELIYAN.V 33 JOHN.P.J 24 POULOSE. M M 25 JOSEPH.E.S 26 ACHANKUNHU N G 17 VARKY.M.M 18 MATHAI.M.M 59 APPACHAN.P.V 5
10 ISSAC.M.M 111 RAJU.P.T 2
Parent Table
(FK)
(PK)
NIC Wayanad 29
FOREIGN KEY Constraints.. Defining a FOREIGN KEY
CREATE TABLE Depot(DepotID INT PRIMARY KEY,DepotName VARCHAR(20) NOT NULL);
CREATE TABLE Producers(ProducerID INT,ProducerName VARCHAR(50) NOT NULL,DepotID INT, CONSTRAINT pk_Prod PRIMARY KEY(ProducerID),CONSTRAINT fk_prod FOREIGN KEY(DepotID) REFERENCES Depot(DepotID) );
Add a FOREIGN KEY to an existing table
ALTER TABLE Producers ADD CONSTRAINT fk_Prod FOREIGN KEY (DepotID) REFERENCES Depot(DepotID)
NIC Wayanad 30
UNIQUE KEY Constraints No duplicates allowed in referenced column NULL values are permitted
ProducerID ProducerName DepotID MemberID
1 JOSEPH V.V 1
2 VELIYAN.V 3 245
3 JOHN.P.J 2
4 POULOSE. M M 2
5 JOSEPH.E.S 2
6 ACHANKUNHU N G 1 154
7 VARKY.M.M 1
8 MATHAI.M.M 5 453
9 APPACHAN.P.V 5
10 ISSAC.M.M 1 12
11 RAJU.P.T 2
CREATE TABLE Producers(ProducerID INT PRIMARY KEY,ProducerName VARCHAR(50) NOT NULL,DepotID INT,MemberID INT UNIQUE,CONSTRAINT fk_prd1 FOREIGN KEY(DepotID) REFERENCES Depot(DepotID))
NIC Wayanad 31
CHECK Constraints
ensure valid data when adding or updating an entry in a table
ProducerID ProducerName Sex DateJoin DateBirth ShareValue
1 JOSEPH V.V M 01/04/2001 11/04/1965 102 VELIYAN.V M 15/06/2007 12/09/1954 103 JOHN.P.J M 19/04/2003 08/05/1946 104 POULOSE. M M M 24/03/1997 10/05/1964 105 ANNAMMA F 26/07/2004 17/08/1959 106 ACHANKUNHU N G M 04/09/1992 18/07/1961 107 VARKY.M.M M 08/07/1991 19/03/1942 208 MATHAI.M.M M 17/08/1987 22/05/1938 10
M or FDate Join >
Date Birth>0CHECK Constraints -->
CREATE TABLE Producers(ProducerID INT ……,CONSTRAINT check_Sex CHECK(Sex=‘M’ OR ‘F’),CONSTRAINT chk_share CHECK(ShareValue>0))
NIC Wayanad 32
NOT NULL Constraints
Ensures that a specified column cannot contain a Null Value
CREATE TABLE Depot(DepotID INT PRIMARY KEY,DepotName VARCHAR(20) NOT NULL);
DepotID Name1 OFFICE2 VADUVANA3 ANDOOR4 PAMBALA5 KADALMADU6 MUNNOORE
DepotID Name1 OFFICE2 VADUVANA3 ANDOOR4 PAMBALA56 MUNNOORE
Violation against NOT NULL
NIC Wayanad 33
Surrogate key A surrogate key in a database is a unique identifier surrogate key is not derived from application data A surrogate key may be used as the primary key some possible candidates for generating surrogates:
Globally Unique Identifiers (GUIDs) Object Identifiers (OIDs) Sybase or SQL Server identity column Oracle SEQUENCE PostgreSQL serial MySQL AUTO_INCREMENT MS Access AUTONUMBER
NIC Wayanad 34
Relationships
The following relationships can be included in an E-R Model: One-to-one One-to-many Many-to-many
NIC Wayanad 35
One-to-one Relationship Each occurrence (row) of data in one entity is related to
only one occurrence of data in the other entity Example: Each Producer has just one MemberID and each
MemberID is assigned to just one Producer
ProducerID ProducerName DepotID MemberID
1 JOSEPH V.V 1 5
2 VELIYAN.V 3 26
3 JOHN.P.J 2
4 POULOSE. M M 2 125
5 JOSEPH.E.S 2 110
6 ACHANKUNHU N G 1 12
7 VARKY.M.M 1 35
8 MATHAI.M.M 5
9 APPACHAN.P.V 5
10 ISSAC.M.M 1
11 RAJU.P.T 2 2
MemberID DateAdmit ShareValue
2 12/04/1987 10.00
5 12/04/1987 10.00
12 25/07/1988 10.00
26 25/09/1988 20.00
35 12/08/1997 10.00
110 02/12/2000 10.00
125 16/08/1998 10.00
PRODUCERSMEMBERS
NIC Wayanad 36
One-to-many Relationship Each occurrence of data in one entity can be
represented by many occurrences of the data in the other entity
Example: each Depot carries many producers
PRODUCERSDEPOT
DepotID Name1 OFFICE2 VADUVANA3 ANDOOR4 PAMBALA5 KADALMADU6 MUNNOORE
ProducerID ProducerName DepotID1 JOSEPH V.V 12 VELIYAN.V 33 JOHN.P.J 24 POULOSE. M M 25 JOSEPH.E.S 26 ACHANKUNHU N G 17 VARKY.M.M 18 MATHAI.M.M 59 APPACHAN.P.V 5
10 ISSAC.M.M 111 RAJU.P.T 2
NIC Wayanad 37
Many-to-many Relationship Data can have multiple occurrences in
both entities In a proper design, this can’t be included
in the physical database
NIC Wayanad 38
ER Diagram of
NIC Wayanad 39
DepotDepot
DepotIDDepotIDNameNameAddressAddress
ProducersProducers
ProducerNameProducerNameAddressAddressSexSexCasteCastePhonePhoneMemberIDMemberIDDateofJoinDateofJoin
MembersMembers
MemberIDMemberIDDateofAdmissionDateofAdmissionShareNosShareNosShareValueShareValueDateOfBirthDateOfBirthIDCardNoIDCardNo
Proc_RegProc_Reg
ProducerIDProducerIDDateDateTimeTimeMilkQtyMilkQtyCLRCLRFATFATSNFSNFMilkValueMilkValue
Collection_AgentCollection_Agent
AgentIDAgentIDNameNameAddressAddress
Contains Becomes
Pours Milk
handles
Managed by
NIC Wayanad 40
Normalization ..is a process you can use to split out
non-relational tables into relational tables. ..is a technique for designing relational
database tables to minimize duplication of information
.. is to safeguard the database against data anomalies
NIC Wayanad 41
Bad Design
Update anomaly
Employee 519 is shown as having different addresses on different records.
NIC Wayanad 42
Bad Design
Insertion anomaly
Until the new faculty member is assigned to teach at least one course, his details cannot be recorded.
NIC Wayanad 43
Bad Design Deletion anomaly
All information about Dr. Giddens is lost when he temporarily ceases to be assigned to any courses.
NIC Wayanad 44
First Normal Form (1NF)
Remove repeating columns by creating new table moving the columns into the new table, and linking back to the old table in a on-to-many relationship.
ProducerID ProducerName Address Day1Qty Day1Value Day2Qty Day2Value Day3Qty Day3Value
1 JOSEPH V.V KALPETTA 1.2 15.25 1.9 22.5
2 VELIYAN.V MEPPADI 2.4 36.3 2.9 39.5 3.5 49.5
3 JOHN.P.J KALPETTA 5.4 68.5
4 POULOSE. M M BATHERY 6.4 79.8 7.1 89.9 6.5 78.5
5 JOSEPH.E.S PULPALLY . 2.9 34.35
PRODUCERS_PROCUREMENT
•To Eliminate repeating groups
NIC Wayanad 45
First Normal Form (1NF)
ProducerID ProducerName Address Day1Qty Day1Value Day2Qty Day2Value Day3Qty Day3Value
1 JOSEPH V.V KALPETTA 1.2 15.25 1.9 22.5
2 VELIYAN.V MEPPADI 2.4 36.3 2.9 39.5 3.5 49.5
3 JOHN.P.J KALPETTA 5.4 68.5
4 POULOSE. M M BATHERY 6.4 79.8 7.1 89.9 6.5 78.5
5 JOSEPH.E.S PULPALLY . 2.9 34.35
Before
After
ProducerID ProducerName Address
1 JOSEPH V.V KALPETTA
2 VELIYAN.V MEPPADI
3 JOHN.P.J KALPETTA
4 POULOSE. M M BATHERY
5 JOSEPH.E.S PULPALLY
ProducerID Day Qty Value1 1 1.2 15.251 3 1.9 22.52 1 2.4 36.32 2 2.9 39.52 3 3.5 49.53 3 5.4 68.54 1 6.4 79.84 2 7.1 89.94 3 6.5 78.55 2 2.9 34.35
PRODUCERSPROCUREMENT
PRODUCERS_PROCUREMENT
NIC Wayanad 46
Second Normal Form (2NF)
Move repeating fields into a new table that contains a primary key and relate it back to the old table using a foreign key
PRODUCERS
•The table must be in 1NF
•Creates many-to-one relationship, separating static from dynamic information
ProducerID ProducerName Place
1 JOSEPH V.V KALPETTA
2 VELIYAN.V MEPPADI
3 JOHN.P.J KALPETA
4 POULOSE. M M BATHERY
5 JOSEPH.E.S PULPALLY
NIC Wayanad 47
Second Normal Form (2NF)
ProducerID Day Qty Value1 1 1.2 15.251 3 1.9 22.52 1 2.4 36.32 2 2.9 39.52 3 3.5 49.53 3 5.4 68.54 1 6.4 79.84 2 7.1 89.94 3 6.5 78.55 2 2.9 34.35
PlaceID Place
1 MEPPADI
2 VYTHIRI
3 BATHERY
4 KALPETTA
5 PULPALLY
ProducerID ProducerName Placeid
1 JOSEPH V.V 4
2 VELIYAN.V 1
3 JOHN.P.J 4
4 POULOSE. M M 3
5 JOSEPH.E.S 5
ProducerID ProducerNamePlace
1 JOSEPH V.V KALPETTA
2 VELIYAN.V MEPPADI
3 JOHN.P.J KALPETTA
4 POULOSE. M MBATHERY
5 JOSEPH.E.S PULPALLY
Before
After
ProducerID Day Qty Value1 1 1.2 15.251 3 1.9 22.52 1 2.4 36.32 2 2.9 39.52 3 3.5 49.53 3 5.4 68.54 1 6.4 79.84 2 7.1 89.94 3 6.5 78.55 2 2.9 34.35
NIC Wayanad 48
Third Normal form (3NF)
PRODUCERSDEPOT
DepotID Name1 OFFICE2 VADUVANA3 ANDOOR4 PAMBALA5 KADALMADU6 MUNNOORE
ProducerID ProducerName DepotID1 JOSEPH V.V 12 VELIYAN.V 33 JOHN.P.J 24 POULOSE. M M 25 JOSEPH.E.S 26 ACHANKUNHU N G 17 VARKY.M.M 18 MATHAI.M.M 59 APPACHAN.P.V 5
10 ISSAC.M.M 111 RAJU.P.T 2
Before
After
ProducerIDProducerName1 JOSEPH V.V2 VELIYAN.V3 JOHN.P.J4 POULOSE. M M5 JOSEPH.E.S6 ACHANKUNHU N G
DepotID Name1 OFFICE2 VADUVANA3 ANDOOR4 PAMBALA5 KADALMADU6 MUNNOORE
DEPOTPRODUCERS
ProducerID DepotID1 22 33 14 15 46 17 2
PROD_DEPOT
NIC Wayanad 49
Why Denormalization.,,, Higher degrees on normalization….
involve more tables need for a larger number of joins can reduce performance
NIC Wayanad 50
Database transaction is a unit of interaction with a DBMS
is a series of database operations which either all occur, or all do not occur (Atomic)
are also called Logical Units of Work.
a database system will guarantee the properties of ACID for each transaction.
NIC Wayanad 51
ACID Atomicity:
Either all the tasks in a transaction must be done, or none of them.
The transaction must be completed, or else it must be undone (rolled back).
Consistency: Every transaction must preserve the
integrity constraints — the declared consistency rules — of the database.
NIC Wayanad 52
ACID Isolation:
Two simultaneous transactions cannot interfere with one another.
Intermediate results within a transaction are not visible to other transactions.
Durability: Completed transactions cannot be
aborted later or their results discarded.
NIC Wayanad 53
Operating system support
RDBMSWindows
Mac OS
X Linux BSD UNIX
4th Dimension Yes Yes No No NoAdabas Yes No Yes No YesAdaptive Server Enterprise Yes Yes Yes Yes Yes
Apache Derby 2 Yes Yes Yes Yes YesDB2 Yes No Yes No YesFirebird Yes Yes Yes Yes Yes
HSQLDB 2 Yes Yes Yes Yes Yes
H2 2 Yes Yes Yes Yes YesInformix Yes Yes Yes Yes YesIngres Yes No Yes Yes Yes
InterBaseYes No Yes No
Yes (Solaris)
MaxDB Yes No Yes No YesMicrosoft SQL Server Yes No No No NoMonetDB Yes Yes Yes No YesMySQL Yes Yes Yes Yes YesOracle Yes Yes Yes No YesOpenEdge Yes No Yes No YesOpenLink Virtuoso Yes Yes Yes Yes YesPostgreSQL Yes Yes Yes Yes Yes
Pyrrho DBMSYes (.NET) No
Yes (Mono) No No
SmallSQL 2 Yes Yes Yes Yes YesSQL Anywhere Yes Yes Yes No YesSQLite Yes Yes Yes Yes YesTeradata Yes No Yes No YesValentina Yes Yes Yes No No
NIC Wayanad 54
To Be Discussed
SQL in detail… Security User Accounts Backup / replication Data warehousing Data mining
NIC Wayanad 55
END