You are on page 1of 16

Relational model was first introduced by Ted Codd of IBM Research in 1970.

The model uses the concept of a mathematical relation as its basic building block and has its theoretical basis in set theory and first order predicate logic. Examples of relational model includes: Oracle( from Oracle), SQL Server, Ms Access( from Microsoft) Data models that preceded the relational model include hierarchical and network models.

Relational Model Concepts: The relational model represents the database as a collection of relations. A relation is a named, two-dimensional table of data. Each row in the table represents a collection of related data values. Terminology

Domain: a domain D is a set of atomic values. Atomic means that each value in the domain is indivisible . In other words domain refers to the description of an attribute's allowed values. A common method of specifying a domain is to specify a data type from which the data values forming the domain are drawn. E.g. the data type for the domain Phone Numbers can be declared as of character strings, Data type for employee age is an integer number between 15 and 80.

Relation Schema: A relation schema R denoted by R(A1,A2,A3,..,AN) is made up of a relation name R and a list of attributes A1, A2, ,AN. Each attribute Ai is the name of a role played by some domain D in the relation schema R. D is called the domain of Ai and is denoted by dom(Ai). A relation schema is used to describe a relation, R is called the name of the relation.

Meta Data: A metadata / data dictionary is defined as the data about the data. It is also known as system catalog. The system catalog integrates the metadata. Metadata is the data that describes the objects in the database. So it is easier to access those objects. It describes the database structure, constraints, applications and size of data types etc. Degree ( Arity) of a relation: Degree of a relation is the number of attributes n of its relation schema. E.g. Student ( RollNo, Name, HomePhone. Address, Age, Percentage) Relation Name: Student Attributes: RollNo,Name,Homephone,Address,Age,percentage Degree of the relation: 6

Relation : A relation r of the relation schema R(A1,A2,A3,..,AN) also denoted by r( R) , is a set of n-tuples r={t1, t2, t3, tm}. Each n-tuple is an ordered list of n values t=<v1,v2,v3 ,vn>, where each value vi, 1<=i<=n, is an element of dom(Ai)or is a special null value. The ith value in tuple t, which corresponds to the attribute Ai , is referred to as t[Ai]. In other words ,a relation r(R) is a ,mathematical relation of degree n on the domain dom(A1), dom(A2), ,dom(An) which is a subset of the Cartesian product of the domains that define R:

r (R)

(dom(A1) X dom (A2) X Xdom(An))

Characteristics of Relations 1) Ordering of tuples in a Relation: Tuples in a relation do not have any particular order. 2) Ordering of values within a tuple: Since a tuple is an ordered list of n values, so the ordering of values in the tuple and hence the attributes in a relation schema is important. 3) Values and Nulls in the tuple: Each value in a tuple is an atomic value that is not divisible into components within the framework of the basic relational model. A special value , called Null value is used to represent values of attributes that may be unknown or may not apply to the tuples. 4) Cardinality of a Relation: No: of tuples in the relation.

Types of keys A key is an attribute (also known as column or field) or a combination of attribute that is used to identify records. The various types of keys are mentioned below, (For examples let suppose we have an Employee Table with attributes ID , Name , Address , Department_ID , Salary )

Super Key: An attribute or a combination of attribute that is used to identify the records uniquely is known as Super Key. A table can have many Super Keys. E.g. of Super Key 1 ID 2 ID, Name 3 ID, Address

4 ID, Department_ID 5 ID, Salary 6 Name, Address 7 Name, Address, Department_ID uniquely will be a Super Key. So on as any combination which can identify the records

Candidate key: It can be defined as minimal Super Key or irreducible Super Key. In other words an attribute or a combination of attribute that identifies the record uniquely but none of its proper subsets can identify the records uniquely. E.g. of Candidate Key 1 ID 2 Name, Address For above table we have only two Candidate Keys (i.e. Irreducible Super Key) used to identify the records from the table uniquely. ID Key can identify the record uniquely and similarly combination of Name and Address can identify the record uniquely, but neither Name nor Address can be used to identify the records uniquely as it might be possible that we have two employees with similar name or two employees from the same house. Thus the Key constraints are:  Two distinct tuples in any state of the relation cannot have identical values for all the attributes in the key.  It is a minimal superkey- that is, a superkey from which we cannot remove any attributes and still have the uniqueness constraint in condition 1.

Primary Key: A Candidate Key that is used by the database designer for unique identification of each row in a table is known as Primary Key. A Primary Key can consist of one or more attributes of a table. E.g. of Primary Key - Database designer can use one of the Candidate Key as a Primary Key. In this case we have ID and Name, Address as Candidate Key, we can consider ID Key as a Primary Key.

Alternate Key: Alternate Key can be any of the Candidate Keys except for the Primary Key. E.g. of Alternate Key is Name, Address as it is the only other Candidate Key which is not a Primary Key.

Secondary Key : The attributes that are not the Super Key but can be still used for identification of records (not unique) are known as Secondary Key. E.g. of Secondary Key can be Name, Address, Salary, Department_ID etc. as they can identify the records on the basis of Name, address, Salary and Department. Composite Key : If we use multiple attributes to create a Primary Key then that Primary Key is called Composite Key (also called a Compound Key or Concatenated Key). E.g. of Composite Key, if we have used Name, Address as a Primary Key then it will be our Composite Key. Foreign Key A foreign key is an attribute or combination of attribute in one base table that points to the candidate key (generally it is the primary key) of another table. Foreign key can be used to cross-reference tables. The purpose of the foreign key is to ensure referential integrity of the data i.e. only values that are supposed to appear in the database are permitted. A set of attributes FK in a relation schema R1 is a foreign key of R1 that references relationR2 if it satisfies the following two rules: 1) The attributes in FK have the same domain as the primary key attributes PK of R2 ; the attributes FK are said to reference or refer to the relation R2. 2) A value of FK in a tuple t1 of the current state r1(R1) either occurs as a value of PK for some tuple t2 in the current state r2(R2) or is null. E.g. of Foreign Key Let consider we have another table i.e. Department Table with Attributes Department_ID , Department_Name , Manager_ID , Location_ID with Department_ID as an Primary Key. Now the Department_ID attribute of Employee Table can be defined as the Foreign Key as it can reference to the Department_ID attribute of the Departments table (the referenced or parent table), a Foreign Key value must match an existing value in the parent table or be NULL.

Relational Database Schema: A relational database schema S is a set of relation schemas S= {R1, R2, Rm} and a set of integrity constraints IC. A database state that does not obey all the integrity constraints is called an invalid state, and the state that satisfies all the constraints in IC is called a valid state.

Creating relations using SQL The CREATE TABLE statement is used to specify a new base relation by giving it a name and specifying its attributes and initial constraints. The attributes are specified first, and each attribute is given a name, a data type to specify its domain of values, and any attribute constraints such as NOT NULL. The key, entity integrity and referential integrity constraints can be specified within the CREATE TABLE statement after the attributes are declared. Because SQL permits NULL as attribute values, a constraint NOT NULL may be specified if NULL is not permitted for a particular attribute. It is also possible to define a default value for an attribute by appending the clause DEFAULT <value> to an attribute definition. The default value is included in any new tuple if an explicit value is not provided for that attribute. Syntax :create table table name ( attr name type, attr name type ) E.g. Create table Students( Roll No Char(20) DEFAULT 1 , name char(30) NOT NULL, percentage Real) Integrity Constraints: An integrity constraint (IC) is a condition specified on a database schema and restricts the data that can be stored in an instance of the database. A database state that does not obey all the integrity constraints is called an invalid state, and the state that satisfies all the constraints in IC is called a valid state. All integrity constraints should be specified on the relational database schema, hence DDL includes provisions for specifying the various types of constraints so that the DBMS can automatically enforce them. Many kinds of integrity constraints can be specified in the Relational model: y y y y Domain Constraints Key Constraints Entity Integrity constraint Referential Integrity constraint ( specified between 2 attributes) Foreifn key

Domain Constraint: specify that within each tuple, the value of each attribute A must be atomic value from the domain(A). A domain constraint specified by the data type of each attribute in the Create table command. E.g. Create table Students( Roll No Char(20), name char(30), percentage Real)

Domain

Alternatively a domain can be declared, and the domain name can be used with the attribute specifications. E.g. Create Domain ageval as Integer Default 18 Check (ageval>=18 and ageval <=21) After defining a domain ageval we can use it as a data type as shown below. Create table Student ( Roll NO Char(10), Name Char(20), Age ageval)

Key Constraint: A key of a database must satisfy the following two conditions:  Two distinct tuples in any state of the relation cannot have identical values for all the attributes in the key.  It is a minimal superkey- that is, a superkey from which we cannot remove any attributes and still have the uniqueness constraint in condition 1. Primary key clause is used to specify one or more attributes that make the primary key of the relation. If the primary key has a single attribute, the clause can follow the attribute directly. E.g. Create table Students( Roll No Char(20), name char(30), percentage Real) CONSTRAINT STUKEY PRIMARY KEY (Roll No))

OR Create table Students( Roll No Char(20) PRIMARY KEY, name char(30), percentage Real))

Entity Integrity constraint: The Integrity constraint states that no primary key value can be null. This is because the primary key value is used to identify individual tuples in a relation. Having Null values for the primary key implies that we cannot identify some tuples. Referential Integrity Constraint ( Foreign Key Constarint): The referential integrity constraint is specified between two relations and is used to maintain the consistency among tuples in the two relations. Referential integrity involves the concept of Foreign key. The FOREIGN KEY constraint is used to prevent actions that would destroy links between tables. The FOREIGN KEY constraint also prevents that invalid data from being inserted into the foreign key column, because it has to be one of the values contained in the table it points to. Referential integrity constraint is specified via the Foreign Key clause. Create table Enrolled( Roll No Char(20), CourseID char(30), Grade Char(10), PRIMARY KEY (Roll No), CourseID) FOREIGN KEY (Roll No) REFERENCES Students)

Enforcing Integrity Constraints:


IC s are specified when a relation is created and enforced when a relation is modified. If an insert, update and delete command causes a violation, it is rejected. For example consider the following two relations : Create table Students( Roll No Char(20), name char(30), percentage Real) UNIQUE ( name); CONSTRAINT STUKEY PRIMARY KEY (Roll No)) Create table Enrolled( Roll No Char(20), CourseID char(30),

Grade Char(10), CONSTRAINT STUKEY PRIMARY KEY (Roll No), CourseID) FOREIGN KEY (Roll No) REFERENCES Students(Roll No))

Some examples of Violations of ICs Insertions: into student two rows with same Roll no / a row with null value for Roll no ( Violation of Primary key constraint) into enrolled a row with Roll no not in student (Violation of foreign key constraint)

Deletions : a row from student which is referred to in Enrolled relation. (Referential Integrity violation)

Update: Roll No of a student , referred to in the Enrolled relation (Referential Integrity violation) In case of integrity violation, several actions can be taken: y y y Cancel the operation that causes the violation (REJECT option) Perform the operation but inform the user of the violation Trigger additional updates so the violation is corrected (CASCADE option, SET NULL option)

These actions are specified as a part of Foreign key declaration: 1) Restrict : means that the action( DELETE or UPDATE) is to be rejected. 2) Cascade : cascade the deletion by deleting tuples that reference the tuple that is being deleted 3) Set Default: modify the referencing attribute values that cause the violation; each such value is either set to Null or changed to reference another valid tuple.

SQL The name SQL is derived from Structured Query language and was designed and implemented at IBM research. SQL is the standard language for commercial relational DBMS. SQL is a comprehensive database language. It has statements for data definition, query and update. Hence it is both a DDL and a DML.

SQL DDL DDL 1) Create table 2) Drop table 3) Alter table

Create Table: The CREATE TABLE statement is used to specify a new base relation by giving it a name
and specifying its attributes and initial constraints. The attributes are specified first, and each attribute is given a name, a data type to specify its domain of values, and any attribute constraints such as NOT NULL. The key, entity integrity and referential integrity constraints can be specified within the CREATE TABLE statement after the attributes are declared. Because SQL permits NULL as attribute values, a constraint NOT NULL may be specified if NULL is not permitted for a particular attribute. It is also possible to define a default value for an attribute by appending the clause DEFAULT <value> to an attribute definition. The default value is included in any new tuple if an explicit value is not provided for that attribute. Syntax :create table table name ( attr name type, attr name type ) E.g. Create table Students( Roll No Char(20) DEFAULT 1 , name char(30) NOT NULL, percentage Real) DROP TABLE: If a base table within a schema is not needed any longer, the relation and its definition can be deleted by using DROP TABLE command. The relation can no longer be used in queries, updates, or any other commands since its description no longer exists Syntax: DROP TABLE TABLE NAME There are two drop behavior option: y Cascade : all foreign key constraints and views that reference the table are dropped automatically from the schema along with the table itself E.g. DROP TABLE EMP CASCADE Restrict: a table is dropped only if it is not referenced in any constraints. E.g. DROP TABLE EMP RESTRICT

Alter table: Alter table modifies the structure of an existing table. It is used to: a) Add new columns b) Drop existing columns c) Modify existing column d) Add integrity constraints e) Drop existing constraints E.g. Alter table Students Add Address(30) Alter table Students Modify Roll No Char(15) Alter table Students Modify Roll No Set Default 1001 Alter table Students Add Constraint chkage check (Age>=18 and Age<=21) Alter table Drop Percentage To drop a column we must use either CASCADE or RESTRICT for drop behavior. If Cascade is used all constraints and views that reference the column are dropped automatically from the schema. If Restrict is used the column is dropped only if it is not referenced in any constraints or views. DML: There are three SQL commands to modify the database: 1) Insert 2) Delete 3) Update Insert : In its simplest form, it is used to add one or more tuples to a relation. Attribute values should be listed in the same order as the attributes were specified in the CREATE TABLE command. Example: Suppose a table Student is created using the Create table command as follows: Create table Students( Roll No Char(20), name char(30), percentage Real)

UNIQUE ( name); CONSTRAINT STUKEY PRIMARY KEY (Roll No)) Tuples can be inserted into Student table using the following Insert statements: Insert into Student Values ( 11 , Rita ,83) An alternate form of INSERT specifies explicitly the attribute names that correspond to the values in the new tuple Attributes with NULL values can be left out Example: Insert a tuple for a new Student for whom we only know the Roll No and Name attributes. Insert into Student( Roll No, Name) Values ( 23 , Sita )

Delete: Removes tuples from a relation. It includes a WHERE-clause to select the tuples to be deleted. Tuples are deleted from only one table at a time (unless CASCADE is specified on a referential integrity constraint) A missing WHERE-clause specifies that all tuples in the relation are to be deleted; the table then becomes an empty table. The number of tuples deleted depends on the number of tuples in the relation that satisfy the WHERE-clause. Examples: Delete From Student where Roll No= 11 Delete from student where Name= Sita Delete from student

Update: Used to modify attribute values of one or more selected tuples. A WHERE-clause selects the tuples to be modified. An additional SET-clause specifies the attributes to be modified and their new values. Each command modifies tuples in the same relation Update Student Set Name= Riya Where Roll No= 11

SQL queries from book which will include: 1) Syntax 2) To learn how to write SQL queries (practice as many questions as possible, including previous years)

3) 4) 5) 6) 7) 8) 9)

Aggregate functions //IMPORTANT String comparision //IMPORTANT Order By clause //IMPROTANT Group by clause (including having) //IMPORTANT Use of *, Distinct,Exists, Not exists, //IMPORTANT Set operations ( UNION , INTERSECT, MINUS) //IMPORTANT What is meant by union compatibility between relations. //IMPORTANT

Views: A view in SQL is a single table that is derived from other tables. These other tables could be base tables or previously defined views. A view is considered as a virtual table, in contrast to base table whose tuples are actually stored in the database. This limits the possible update operations that can be applied to views., but allows full query operations. Uses of Views: 1. 2. 3. 4. It can be used to hide sensitive columns. It can be used to hide complex queries involving multiple tables. Views that are created with a check option, prevents updating of other rows and columns. It can simplify complex operations on the base relations.

The command to specify a view is CREATE VIEW. The view is given a view name, a list of attribute names and a query to specify the contents of the view. E.g. CREATE VIEW WORKS_ON_NEW AS SELECT FNAME,LNAME, PNAME, HOURS FROM EMPLOYEE, PROJECT, WORKS_ON WHERE ENO=EENO AND PNO=PNUMBER We can specify SQL queries on a newly create view SELECT FNAME, LNAME FROM WORKS_ON_NEW WHERE PNAME= Seena When no longer needed, a view can be dropped: DROP WORKS_ON_NEW Update of views is complicated and can be ambiguous. In general,

i)

ii) iii)

A view with a single table is updatable if the view attributes contain the primary key of the base table, as well as all the attributes with the NOT NULL constraint that do not have default values specified. Views defined on multiple tables using joins are generally not updateable. Views defined using grouping and aggregate functions are not updatable.

Microsoft Access Data Types


Data type Text Memo Byte Integer Long Single Double Currency AutoNumber Date/Time Yes/No Ole Object Hyperlink Lookup Wizard Description Use for text or combinations of text and numbers. 255 characters maximum Memo is used for larger amounts of text. Stores up to 65,536 characters. Allows whole numbers from 0 to 255 Allows whole numbers between -32,768 and 32,767 Allows whole numbers between -2,147,483,648 and 2,147,483,647 Single precision floating-point. Will handle most decimals Double precision floating-point. Will handle most decimals Use for currency. Holds up to 15 digits of whole dollars, plus 4 decimal places. Storage Upto characters Upto characters 1 byte 2 bytes 4 bytes 4 bytes 8 bytes 8 bytes 255 63999

AutoNumber fields automatically give each record its own number, usually 4 bytes starting at 1 Use for dates and times A logical field can be displayed as Yes/No, True/False, or On/Off. Can store pictures, audio, video Contain links to other files, including web pages Let you type a list of options, which can then be chosen from a drop-down list 4 bytes 8 bytes 1 bit up to 1GB

Codd s Rules: Codd's twelve rules are a set of thirteen rules (numbered zero to twelve) proposed by Edgar F. Codd, designed to define what is required from a database management system in order for it to be considered relational, i.e., a relational database management system (RDBMS). The rules Rule 0: The system must qualify as relational, as a database, and as a management system. For a system to qualify as a relational database management system (RDBMS), it system must use its relational facilities (exclusively) to manage the database. Rule 1: The information rule: All information in the database is to be represented in one and only one way, namely by values in column positions within rows of tables.

Rule 2: The guaranteed access rule: All data must be accessible. This rule is essentially a restatement of the fundamental requirement for primary keys. It says that every individual scalar value in the database must be logically addressable by specifying the name of the containing table, the name of the containing column and the primary key value of the containing row. Rule 3: Systematic treatment of null values: The DBMS must allow each field to remain null (or empty). Specifically, it must support a representation of "missing information and inapplicable information" that is systematic, distinct from all regular values and independent of data type. It is also implied that such representations must be manipulated by the DBMS in a systematic way. Rule 4: Active online catalog based on the relational model: The system must support an online, inline, relational catalog that is accessible to authorized users by means of their regular query language. That is, users must be able to access the database's structure (catalog) using the same query language that they use to access the database's data. Rule 5: The comprehensive data sublanguage rule: The system must support at least one relational language that 1. Has a linear syntax 2. Can be used both interactively and within application programs, 3. Supports data definition operations (including view definitions), data manipulation operations (update as well as retrieval), security and integrity constraints, and transaction management operations (begin, commit, and rollback). Rule 6: The view updating rule: All views that are theoretically updatable must be updatable by the system. Rule 7: High-level insert, update, and delete: The system must support set-at-a-time insert, update, and delete operators. This means that data can be retrieved from a relational database in sets constructed of data from multiple rows and/or multiple tables. This rule states that insert, update, and delete operations should be supported for any retrievable set rather than just for a single row in a single table. Rule 8: Physical data independence: Changes to the physical level must not require a change to an application based on the structure. Rule 9: Logical data independence:

Changes to the logical level (tables, columns, rows, and so on) must not require a change to an application based on the structure. Logical data independence is more difficult to achieve than physical data independence. Rule 10: Integrity independence: Integrity constraints must be specified separately from application programs and stored in the catalog. It must be possible to change such constraints as and when appropriate without unnecessarily affecting existing applications. Rule 11: Distribution independence: The distribution of portions of the database to various locations should be invisible to users of the database. Existing applications should continue to operate successfully: 1. when a distributed version of the DBMS is first introduced; and 2. when existing distributed data are redistributed around the system. Rule 12: The nonsubversion rule: If the system provides a low-level (record-at-a-time) interface, then that interface cannot be used to subvert the system, for example, bypassing a relational security or integrity constraint.

Database Objects: Database objects are the components that save and query information. A database contains several objects that are used to provide storage and display large, relational data. The main objects of a database are tables, stored procedures, views, users and functions. Tables: All data is stored in tables. When we create a new table, MS Access asks us to define fields (column headings), giving each a unique name, and telling Access the data type. Once we have defined a table's structure, we can enter data. Each new row that we add to the table is called a record. To define relationships between tables: in Access 2007 or later: Database Tools | Relationships, in Access 95 2003: Tools | Relationships,

Queries: Queries retrieve the data based on specific criteria. This is the most important element of SQL, which is performed with the declarative SELECT statement. SELECT retrieves data from one or more tables, or expressions. With a query, we can display the records that match certain criteria (e.g. all the members called "Barry"), sort the data (e.g. by Surname), and even combine data from different tables. Queries allow the user to describe desired data, leaving the database management system (DBMS) responsible for planning, optimizing, and performing the physical operations necessary to produce that result as it chooses.

Forms: A form is a database object that us can use to enter, edit, or display data from a table or a query. We can use forms to control access to data, such as which fields or rows of data are displayed. An effective form speeds the use of the database, because people don't have to search for what they need. A visually attractive form makes working with the database more pleasant and more efficient, and it can also help prevent incorrect data from being entered. Create a form by using the Form tool: ( Ms Access 2007) We can use the Form tool to quickly create a single item form. This type of form displays information about one record at a time. 1. In the Navigation Pane, click the table or query that contains the data that you want to see on the form. 2. On the Create tab, in the Forms group, click Form. Access creates the form and displays it in Layout view. In Layout view, you can make design changes to the form while it is displaying data. For example, you can adjust the size of the text boxes to fit the data. 3. To begin working with the form, switch to Form view: On the Home tab, in the Views group, click View, and then click Form View Reports:
manner.

A database report presents information retrieved from a table or query in a preformatted, attractive

Create a report by using the Report tool The Report tool provides the fastest way to create a report, because it generates a report immediately without prompting us for information. The report displays all the fields from the underlying table or query. The Report tool may not create the final, polished product that we ultimately want, but it is quite useful as a means to quickly look at the underlying data. We can then save the report and modify it in Layout view or Design view so that it better serves your purposes. 1. In the Navigation Pane, click the table or query on which you want to base the report. 2. On the Create tab, in the Reports group, click Report. Access builds the report and displays it in Layout view. After viewing the report, we can save it and then close both the report and the underlying table or query that we used as a record source. The next time that we open the report, Access will display the most recent data from your record source

You might also like