You are on page 1of 9
MODULE 2 Relational Databases ‘The relavonal database was invented by Dr. Edgar F. Codd, @ member of the 16M Research Laboratory in San Jose in 1970, @ young programmer at IBM. The data stored ina relational database i represented by using a table composed of rows and column of dota. Here if a simple example denoting how dats in a relational databace = represented: EMPLOVEE_NO | FIRST_NAME | LAST_NAME | DATE_OF_IRTH 0001 ro Fajerdo um i974 10075 Brot Moers (08-Sep-2010 0004 Sean Shamma 4080-1977 T0096 Freve Ke 720-060 1980 10099 ‘Schon ‘washinaton | 22an-1986 vor Fenton ‘Yamaguchi 25-Mar-2005) REPRESENTATION OF DATA IN A RELATIONAL DATABASE A table is sometimes called a relation. The table consists of rows and columns. Each row of data is called records or tuples, while the columns of data are called fields or attributes. Each table can have only ONE primary key. A foreign key is a column that is added to create a relationship with another table, BASIC CONCEPTS. + Relation AA relation is sometimes used to refer to a table in which the rows of the table correspond to single records, and the table columns correspond to attributes. But it is more usually used to describe the relationships that can be created between those tables in a relational database, Attribute ‘Attributes refer to the column of a relation of a database. For example, the information on the employee is represented by the Employee relation, having columns for attributes Employee No (the employee number), First_Name, Last_Name, and Date_of Birth, Domain ‘A domain is the set of acceptable values for one or more attributes. Domains are a very powerful feature of the relational model. Every attribute in a relational database Is defined on a domain. Domains may be distinct for each attribute, or two or more: attributes may have the same domain. Here are examples of domains: BRANCH ‘BN Seer Giy__| Posteode TeLNo Mobile No 5B Salazar | Tadoban | 6500 053-323-5747 | 09553457654 Bz Burgos | Baybay | 6521. 053-530-6784 | 09368764453 BL ‘Gomez | Tacloban | 6500 (053-327-2804 | 09215679087 87 Zamora [Maasn | 6600 (053-257-8840 | 09176643212 co Dal Pilar_|Ormoc | 6541 053-325-7117 | 09155672109 Instances of the Branch relations Instances of the branch relations show the domains for some attributes of the Branch relations. Although there are six attributes in the Branch relation, there are only five ‘domains represented, as the two attributes TelNo and Mobile_No take their values from the same domain, Note that at any certain time, typically, there will be values in a ‘domain that do not currently appear as a value in the corresponding attribute. Attribute | Dornain Name Mearing Domain Definition “The set OF all possible branch | varchar: size 3, range ‘No Branch_Numbers | numbers BO1-b98 ‘Street__| Steet_Names | The set ofallstreet names varchar: si 20 city ‘Gity_Names The set of al ity names: ‘varchar: size 20 Pood | Post codes “The set of all post codes tint “The set of all telephone and Mobile_Numbers | mobile numbers varchar: size 13, The set of all telephone and Mobile_Numbers _| mobile numbers varchar: size 13, Figure 2.2 Domains for some attributes of the branch relations = Primary Key ‘The primary key helps us to uniquely identifies every row in a table, This field or attribute is commonly placed as the first attribute within a tuple. When represented in table form, the name of the field used as the primary key is underlined. The same value should not appear more than once in the table. For example, imagine we have a STUDENT table that contains a record for each student at a university. The student ID number would be the best choice for a primary key in the STUDENT table because it is unique. The student's first name and last name would not be the best choice for a primary key because there is always the chance that more than one student might have the same name. "Foreign Key A foreign key is used to cross-reference between tables because it references the primary key across tables. All relationship in the model needs to be supported by @ foreign key. To show how foreign key work, let us first define a second base relation and call it the ITEMS relation. rEB NO. TEM DESC ‘Q7Y_ON_ HAND ‘001 Microwave 10 (005 Taser Printer 20, (007 ‘Washing Machine | 15, ‘009 Flat Iron 3S 12 ‘Stand Fan 18 The ITEMS relation contains information about the products being sold by a ‘company. For this relation, the primary key is the ITEM_NO field. Let us define a third base relation and call it the SALES-SUMMARY relation. This relation contains the number of items sold by an employee. ‘EMP_NO_| ITEM NO_| QTY SOLD yooor | 001 a 10001 [007 a ‘40002 [009 2 10002 [001 2 40003 | 005 6 10003 [007 3 ‘10004 [008 Z EMP_NO is one of the two fields that make up the composite primary key of SALES_SUMMARY, Toke rote that this isthe primary key of the EMPLOYEE relation, TTEM_NO is the other field that makes up the composite primary key of SALES_SUMMARY, Toke not that this isthe primary key ofthe FTEMS relation QTY_SOLD contains the quantity sold per item. Individual, the fields EMP_NO and ITEM_NO and SALES SUMMARY relation are ‘an example of foreign Keys: EMP_NO has values that match the primary key values of EMPLOYEE relation, while ITEM_NO have values that also match the primary key ‘values of relation ITEMS. The diagram below ilistrates the relationships between the twee relations: EMPLOYEE relation TTEMS relation Emp-NO | ITEM-NO ‘SALES_SUMMARY relation = Tuple ‘Atuple sa row (eco) in a table in the database + Base Relation ‘A base relation © a relston whose tuples or records are physically stord in 2 Aotabase, Base relations ar dented by a name, likin the case of the EMPLOYEE ‘elation. [RELATIONAL INTEGRITY [As mentioned in the introduction, a relational database dais represented in rows and columns of a table, In ths regard the data used to describe the rel-world entty must be accurate and realistic. For example, no employee (or any human for that matter) can have an EMP_AGE field valve of 200 of -15. ese, oo employee can have an [EMP_NAME fil value of 3237958, The design ofthe relation must be governed by 3 set of Integrity rls to ensure that data stored in a elation is both realistic and accurate, Here are examples of possible intent rules forthe EMPLOYEE elation: 1. EMP_NO must be a unique five-digit numeric number stating fom 10001. 2. EMP_AGE must bea postive rumerc value. 3. EMP_NAME must be alphanumeric and must not equal to spaces. ‘There ae two important tntegelty rules, which re constraits or esrictions that poly to all instances ofthe database. The two main rules forthe rational model are knoe as entity integrity and referential integrity. ENTITY INTEGRITY RULE This rule states that: Entity Integrity makes sure that there are no duplicate records within the table, and each record is unique within the table and not allowed to accept null values. Defining primary key rules: + Two records cannot have the same primary key value. + There must have a primary key value for every record. +The primary key field should not be null. +The value in a primary key column can never be modified or updated if any foreign key refers to that primary key. NULL Means nothing - represents a value for an attribute that is currently unknown or is not applicable for this tuple. REFERENTIAL INTEGRITY RULE ‘This rule states that: The relations in a database must not contain unmatched foreign keys Referential integrity requires that a foreign key must have a matching primary key, or it ‘must be null. Tt means the reference from a row in one table to another table must be valid. Example from a Course/Class database: Course (CrsCode, DeptCode, Description) Class (CrsCode, Section, ClassTime) ‘The referential integrity constraint tells that CrsCode in the Class table must match a valid CrsCode in the Course table. In this situation, it is not enough that the CrsCode and Section in the Class table make up the PK; we must also enforce referential integrity.

You might also like