3_Basics of Relational Model

Embed Size (px)

DESCRIPTION

3_Basics of Relational Model

Citation preview

Relational Database Management System

Basics of Relational ModelChapter 5-2Relational Model ConceptsThe relational Model of Data is based on the concept of a Relation.

A Relation is a mathematical concept based on the ideas of sets.

The strength of the relational approach to data management comes from the formal foundation provided by the theory of relations.

Chapter 5-3INFORMAL DEFINITIONSRELATION: A table of values

A relation may be thought of as a set of rows.A relation may alternately be though of as a set of columns.Each row represents a fact that corresponds to a real-world entity or relationship.Each row has a value of an item or set of items that uniquely identifies that row in the table.Each column typically is called by its column name or column header or attribute name.Chapter 5-4FORMAL DEFINITIONSA Relation may be defined in multiple ways.The Schema of a Relation: R (A1, A2, .....An)Relation schema R is defined over attributes A1, A2, .....An For Example -CUSTOMER (Cust-id, Cust-name, Address, Phone#)

Here, CUSTOMER is a relation defined over the four attributes Cust-id, Cust-name, Address, Phone#, each of which has a domain or a set of valid values. For example, the domain of Cust-id is 6 digit numbers.Example of a Relation

A relational Model is concerned with 3 components:Data StructureData IntegrityData ManipulationAttribute:An attribute is a named column of a relation.A relation is represented as a two dimensional table in which the rows of the table correspond to individual records and the table columns correspond to attributes.Attributes can appear in any order and the relation will be the same relation and therefore covey the same meaning.Chapter 5-8TupleA tuple is an ordered set of valuesEach value is derived from an appropriate domain.Each row in the CUSTOMER table may be referred to as a tuple in the table and would consist of four values. is a tuple belonging to the CUSTOMER relation.A relation may be regarded as a set of tuples (rows).Chapter 5-9Example - Figure 5.1

DomainIs the set of allowed values for one or more attributes.Defines the kind of data represented by the attribute.Example: If a college has 5000 students then the domain of Roll number may be from 1001 to 6000.DomainEach attribute in the model should be assigned domain information that includes:Data typeLengthDate FormatRangeConstraintsNull SupportDefault valueChapter 5-12DomainA domain has a logical definition: e.g.,USA_phone_numbers are the set of 10 digit phone numbers valid in the U.S.

A domain may have a data-type or a format defined for it. The USA_phone_numbers may have a format: (ddd)-ddd-dddd where each d is a decimal digit. E.g., Dates have various formats such as monthname, date, year or yyyy-mm-dd, or dd mm,yyyy etc.

An attribute designates the role played by the domain. E.g., the domain Date may be used to define attributes Invoice-date and Payment-date.TupleExtensionIntensionDegreeCardinalitySlide 2-14Schemas versus InstancesSchema Diagram: A diagrammatic display of (some aspects of) a database schema.Schema Construct: A component of the schema or an object within the schema, e.g., STUDENT, COURSE.Chapter 5-15DEFINITION SUMMARYInformal Terms Formal Terms Table Relation ColumnAttribute/DomainRowTupleValues in a columnDomainTable DefinitionSchema of a RelationPopulated TableExtensionRelational KeysThere should not be any duplicate tuple within a relation.Therefore we should identify one or more attributes(called relational keys) that uniquely identify each tuple in a relation.Types of KeysSuper KeyCandidate KeyPrimary KeyAlternate KeyArtificial KeyThere is one more key, which is very imp. But it does not hold the property of uniqueness and it is known as Foreign Key.Super KeyAre those attributes of a relation, which have the properties of uniqueness.Let us consider a relation R, by definition ,the set of all attributes of R has the uniqueness property , meaning that, at any given time, no two tuples of relation R are duplicates of one another.Let K be a set of attributes of relation R.Then K is a super key for R if and only if it posses uniqueness.

ExampleR=Student(Roll no., Name, Class)

Roll No.Roll No.,NameRoll No.,ClassRoll No.,Class,NameCandidate KeyAre those attributes of a relation, which have the properties of uniqueness and irreducibility.Let K be a set of attributes of Relation R. Then K is a candidate key for R if and only if it possesses both properties:UniquenessIrreducibility: No proper subset of K has the uniqueness property.

Every relation has atleast one candidate key.Example (Name,Class) is unique then it can be identified as the candidate key if and only if Name and class individually are nor unique.

IS (Roll No, Class) is a candidate key?There can be any number of candidate keys in a relation and no component of candidate key is allowed to be null.Primary KeyIt has three properties:UniquenessIrreducibilityNot NullA relation has only one primary key. It may be a single attribute or a combination of attributes.ExampleStudent (Roll NO.,Name,Class,Mobile No.) Identify candidate keys and a primary key.PropertiesStable- The value of Primary Key must not change or should not be null throughout the life of an entity. Example: AgeMinimal: Should be composed of the minimum number of fields that ensures the occurrences are unique.Definitive: A value must exist for every record at creation time.Accessible: anyone who wants to create , read and delete a record must be able to see the primary key value.Identify the Primary KeyEmp IDProject IDHours_worked01012000102120020150020312003031000304200Alternate KeysCandidate keys which are not chosen or promoted as the primary key by the database designer, are known as Alternate Keys.Student(Roll No., Name, Class, Mobile No.).Identify candidate , primary and alternate keys.

Artificial KeysAn artificial key is the one that has no meaning to the business or organization and performs the function of primary key in a relation.Artificial Keys are permitted whenNo attributes has the primary key properties or the primary key is large and complex.

Foreign KeysAre the attributes of a table, which refers to the primary key of some other table.Foreign Keys permit only those values, which appear in the primary key of the table to which it refers and may be null.The foreign key is a reference to the tuple of a table from which it is taken ,this tuple being called the Referenced table .Emp No.EnamejOBSalDept NO1AClerk4000102AClerk

4000203BMgr8000204C Peon2000405DClerk

4000106EMgr800050Dept No.DnameLoc10Aasr20BJal30CQadian40DTarget Attribute or Primary KeyForeign KeyEMPDept OrTarget TableReferential IntegrityForeign key values should always be matched by corresponding primary keys.Referential IntegrityEntity IntegrityEnterprise Constraints