Introduction to the
Relational Model
UNIT - II
 How is data represented in the relational model?
 What integrity constraints can be expressed?
 How can data be created and modified?
 How can data be manipulated and queried?
 How do we obtain a relational database design from an ER diagram?
 What are views and why are they used?
SQL
 Structured Query Language
 Standard language for storing, manipulating and retrieving data in databases.
 Standard language for Relational Database system.
 All the Relational Database Management Systems (RDBMS) like Mysql, Ms Access,
Oracle, Sybase, Informix, Postgres and SQL Server use SQL as their standard
database language.
 Allows users to access data in the RDBMS.
 Allows users to describe data.
 Allows users to define the data in a database and manipulate that data.
Relational Model – What Exactly it is?
 Relational model is very simple & elegant: a db is a collection of one or more
relations, where each relation is a table with rows and columns.
 Simple Tabular representation
 Enables novice users to understand the db.
 Permits the use of simple, high-level languages to query the data.
 Major advantage of R-model is its simple data representation and the ease with
which even complex queries can be expressed.
The main construct for representing data in the relational model is a relation.
A relation consists of a relation schema and a relation instance.
The relation instance is a table, and the relation schema describes the column heads for the table.
We first describe the relation schema and then the relation instance.
The schema specifies the relation’s name, the name of each field (or column, or attribute)
and the domain of each field.
A domain is referred to in a relation schema by the domain name and has a set of associated values.
Students(sid: string, name: string, login: string, age: integer, gpa: real)
This says, for instance, that the field named sid has a domain named string.
The set of values associated with domain string is the set of all character strings.
An instance of a relation is a set of tuples, also called records, in which each tuple has the
Same number of fields as the relation schema.
A relation instance can be thought of as a table in which each tuple is a row, and all rows have the same
Number of fields.
Note that no two rows are identical. This is a requirement of the relational model – each relation
Is defined to be a set of unique tuples or rows.
A relation schema specifies the domain of each field or column in the relation instance.
These domain constraints in the schema specify an important condition that we want
each instance of the relation to satisfy:
The values that appear in a column must be drawn from the domain associated with that
column.
Thus, the domain of a field is essentially the type of that field, in programming language
terms, and restricts the values that can appear in the field.
An instance of the relations students can be expressed:
{sid: 53666, name: Jones, login: Jones@cs, age: 18, gpa: 7.4}
More specifically, a relation instance means a relation instance that satisfies the domain
constraints of the schema
More Terminology
 The degree or arity of a relation is its number of fields
 The cardinality of a relation is the number of tuples in it.
 A relational database is a collection of relations with distinct relation names.
 The relational database schema is the collection of schemas for the relations in
the database.
Creating and Modifying Relations using
SQL
 The subset of SQL that supports the creation, deletion and modification of tables
is called the DDL (Data definition language).
 CREATE TABLE Students (sid CHAR(20), name CHAR(30), login CHAR(20), age
INTEGER, gpa REAL)
 Tuples are inserted using the INSERT command. We can insert a single tuple into
the Students table as follows:
 INSERT
INTO Students (sid, name, login, age, gpa)
VALUES (53688,’Smith’,’smith@ee’,18,3.2)
DELETE
FROM Students S
WHERE S.name = ‘Smith’
UPDATE Students S
SET S.age = S.age + 1, S.gpa = S.gpa – 1
WHERE S.sid = 53688
Key Constraints
 Consider the Students relation and the constraint that no two students have the
same student id. This IC is an example of a key constraint.
 A key constraint is a statement that a certain minimal subset of the fields of a
relation is a unique identifier for a tuple.
 A set of fields that uniquely identifies a tuple according to a key constraint is
called a candidate key for the relation. We often abbreviate this to just key.
 In case of Students relation, the sid field is a candidate key.
 There are two parts to the definition
 Two distinct tuples in a legal instance cannot have identical values in all the fields of a
key
 No subset of the set of fields in a key is a unique identifier for a tuple.
The first part of the definition means that, in any legal instance, the values in the key
fields uniquely identify a tuple in the instance. When specifying a key constraint, the
DBA or user must be sure that this constraint will not prevent them from storing a
‘correct’ set of tuples.
The second part of the definition means, for e.g., that the set of fields {sid, name} is
not a key for Students, because this set properly contains the key {sid}. The set {sid,
name} is an example of a superkey, which is a set of fields that contains a key.
Observe that two different rows always have different sid values.
Sid is a key and uniquely identifies a tuple. However, this does not hold for nonkey
fields. For e.g. the relation contains two rows with Smith in the name field.
A relation may have several candidate keys. For e.g., the login and age fields of the Students
relation may, taken together, also identify students uniquely. That is {login, age} is also a key.
It may seem that login is a key, since no two rows in the example instance have the same login
value. However, the key must identify tuples uniquely in all possible legal instances of the relation.
By stating that {login, age} is a key, the user is declaring that two students may have the same login
or age, but not both.
What are keys in DBMS?
 Databases are used to store massive amounts of information which is stored
across multiple tables. Each table might be running into thousands of rows.
Needless to say, there will be many duplicate rows with redundant information.
 How do we deal with that?
 How do we manage records so that we are storing only unique data?
 And, how do we relate the multiple tables that are present in the database?
SQL keys are the answer to all these queries.
AN SQL key is either a single column (or attribute) or a group of columns that can
uniquely identify rows (or tuples) in a table.
SQL keys ensure that there are no rows with duplicate information.
They also help in establishing a relationship between multiple tables in the database.
What is a Super Key in SQL?
Super key is a single key or a group of multiple keys that can uniquely
tuples in a table.
Consider that Id attribute is unique to every employee. In that case, we can say
that the Id attribute can uniquely identify the tuples of this table. So, Id is a Super
key of this table.
For instance – (Id, Name), (Id, Email), (Id, Name, Email) etc. can all be Super keys
as they can all uniquely identify the tuples of the table. This is so because of the
presence of the Id attribute which is able to uniquely identify the tuples.
What is a Candidate Key ?
Candidate key is a single key or a group of multiple keys that uniquely identify rows in a table.
The value for the Candidate key is unique and non-null for all tuples. And every table
has to have at least one Candidate Key. But there can be more than one Candidate Key too.
For e.g., in the example that we took earlier, both Id and Email can act as Candidate key for
The table as they contain unique and non-null values.
On the other hand, we cannot use the attributes like City or Gender to retrieve tuples
from the table as they have no unique values.
Whereas on querying the table on the Id attribute will help us to retrieve unique tuples.
Primary Key in SQL
Primary key is the candidate key selected by the database administrator to uniquely
identify tuples in a table.
Out of all the Candidate Keys that can be possible for a table, there can be only one key
that will be used to retrieve unique tuples from the table. This Candidate key is called the
Primary Key.
There can be only one Primary key for a table. Depending on how the Candidate key is
constructed the primary key can be a single attribute or a group of attributes. But the
important point to remember is that the Primary key should be a unique and non-null
attribute(s).
There can be two ways to create a Primary key for the table. The first way is to alter an
already created to add the PK constraint on an attribute.
Now if I try to add a new row with duplicate Id value, it will give me an error message.
The second way of adding a Primary key is during the creation of the table itself. All you have to
do is add the Primary Key constraint at the end after defining all the attributes in the table.
To define a Primary Key constraint on multiple attributes, you can list all the attributes in the
parenthesis as shown below.
Alternate or Secondary keys in SQL
Alternate keys are those candidate keys which are not the Primary Key
There can be only one Primary key for a table. Therefore all the remaining Candidate
keys are known as Alternate or Secondary keys. They can also uniquely identify tuples
in a table, but the database administrator chose a different key as the Primary key.
Foreign key in SQL
Foreign key is an attribute which is a Primary key in its parent table, but is included as an
attribute in another host table.
A Foreign key generates a relationship between the parent table and the host table.
For eg. In addition to the Employee table containing the personal details of the
employees, we might have another table Department containing information related to
the department of the employee.
The Primary key in this table is the Department Id. We can add this attribute to the
Employee by making it the Foreign key in the table.
We can either do this when we are creating the table or we can alter the table later to add
the Foreign Key constraint. Here I have altered the table, but creating Foreign Key during
table creation is similar to that for Primary Key.
Here, Dep_Id is now the Foreign Key in table Employee while it is a Primary Key in the
Department table.
The Foreign key allows you to create a relationship between two tables in the
database. Each of these tables describes data related to a particular field (employee
and department here). Using the Foreign key we can easily retrieve data from both the
tables.

DBMS-Unit-2.pptx

  • 1.
  • 2.
     How isdata represented in the relational model?  What integrity constraints can be expressed?  How can data be created and modified?  How can data be manipulated and queried?  How do we obtain a relational database design from an ER diagram?  What are views and why are they used?
  • 3.
    SQL  Structured QueryLanguage  Standard language for storing, manipulating and retrieving data in databases.  Standard language for Relational Database system.  All the Relational Database Management Systems (RDBMS) like Mysql, Ms Access, Oracle, Sybase, Informix, Postgres and SQL Server use SQL as their standard database language.  Allows users to access data in the RDBMS.  Allows users to describe data.  Allows users to define the data in a database and manipulate that data.
  • 4.
    Relational Model –What Exactly it is?  Relational model is very simple & elegant: a db is a collection of one or more relations, where each relation is a table with rows and columns.  Simple Tabular representation  Enables novice users to understand the db.  Permits the use of simple, high-level languages to query the data.  Major advantage of R-model is its simple data representation and the ease with which even complex queries can be expressed.
  • 5.
    The main constructfor representing data in the relational model is a relation. A relation consists of a relation schema and a relation instance. The relation instance is a table, and the relation schema describes the column heads for the table. We first describe the relation schema and then the relation instance. The schema specifies the relation’s name, the name of each field (or column, or attribute) and the domain of each field. A domain is referred to in a relation schema by the domain name and has a set of associated values. Students(sid: string, name: string, login: string, age: integer, gpa: real) This says, for instance, that the field named sid has a domain named string. The set of values associated with domain string is the set of all character strings.
  • 6.
    An instance ofa relation is a set of tuples, also called records, in which each tuple has the Same number of fields as the relation schema. A relation instance can be thought of as a table in which each tuple is a row, and all rows have the same Number of fields. Note that no two rows are identical. This is a requirement of the relational model – each relation Is defined to be a set of unique tuples or rows.
  • 7.
    A relation schemaspecifies the domain of each field or column in the relation instance. These domain constraints in the schema specify an important condition that we want each instance of the relation to satisfy: The values that appear in a column must be drawn from the domain associated with that column. Thus, the domain of a field is essentially the type of that field, in programming language terms, and restricts the values that can appear in the field. An instance of the relations students can be expressed: {sid: 53666, name: Jones, login: Jones@cs, age: 18, gpa: 7.4} More specifically, a relation instance means a relation instance that satisfies the domain constraints of the schema
  • 8.
    More Terminology  Thedegree or arity of a relation is its number of fields  The cardinality of a relation is the number of tuples in it.  A relational database is a collection of relations with distinct relation names.  The relational database schema is the collection of schemas for the relations in the database.
  • 9.
    Creating and ModifyingRelations using SQL  The subset of SQL that supports the creation, deletion and modification of tables is called the DDL (Data definition language).  CREATE TABLE Students (sid CHAR(20), name CHAR(30), login CHAR(20), age INTEGER, gpa REAL)  Tuples are inserted using the INSERT command. We can insert a single tuple into the Students table as follows:  INSERT INTO Students (sid, name, login, age, gpa) VALUES (53688,’Smith’,’smith@ee’,18,3.2)
  • 10.
    DELETE FROM Students S WHERES.name = ‘Smith’ UPDATE Students S SET S.age = S.age + 1, S.gpa = S.gpa – 1 WHERE S.sid = 53688
  • 11.
    Key Constraints  Considerthe Students relation and the constraint that no two students have the same student id. This IC is an example of a key constraint.  A key constraint is a statement that a certain minimal subset of the fields of a relation is a unique identifier for a tuple.  A set of fields that uniquely identifies a tuple according to a key constraint is called a candidate key for the relation. We often abbreviate this to just key.  In case of Students relation, the sid field is a candidate key.  There are two parts to the definition  Two distinct tuples in a legal instance cannot have identical values in all the fields of a key  No subset of the set of fields in a key is a unique identifier for a tuple.
  • 12.
    The first partof the definition means that, in any legal instance, the values in the key fields uniquely identify a tuple in the instance. When specifying a key constraint, the DBA or user must be sure that this constraint will not prevent them from storing a ‘correct’ set of tuples. The second part of the definition means, for e.g., that the set of fields {sid, name} is not a key for Students, because this set properly contains the key {sid}. The set {sid, name} is an example of a superkey, which is a set of fields that contains a key. Observe that two different rows always have different sid values. Sid is a key and uniquely identifies a tuple. However, this does not hold for nonkey fields. For e.g. the relation contains two rows with Smith in the name field.
  • 13.
    A relation mayhave several candidate keys. For e.g., the login and age fields of the Students relation may, taken together, also identify students uniquely. That is {login, age} is also a key. It may seem that login is a key, since no two rows in the example instance have the same login value. However, the key must identify tuples uniquely in all possible legal instances of the relation. By stating that {login, age} is a key, the user is declaring that two students may have the same login or age, but not both.
  • 14.
    What are keysin DBMS?  Databases are used to store massive amounts of information which is stored across multiple tables. Each table might be running into thousands of rows. Needless to say, there will be many duplicate rows with redundant information.  How do we deal with that?  How do we manage records so that we are storing only unique data?  And, how do we relate the multiple tables that are present in the database?
  • 15.
    SQL keys arethe answer to all these queries. AN SQL key is either a single column (or attribute) or a group of columns that can uniquely identify rows (or tuples) in a table. SQL keys ensure that there are no rows with duplicate information. They also help in establishing a relationship between multiple tables in the database.
  • 16.
    What is aSuper Key in SQL? Super key is a single key or a group of multiple keys that can uniquely tuples in a table. Consider that Id attribute is unique to every employee. In that case, we can say that the Id attribute can uniquely identify the tuples of this table. So, Id is a Super key of this table. For instance – (Id, Name), (Id, Email), (Id, Name, Email) etc. can all be Super keys as they can all uniquely identify the tuples of the table. This is so because of the presence of the Id attribute which is able to uniquely identify the tuples.
  • 17.
    What is aCandidate Key ? Candidate key is a single key or a group of multiple keys that uniquely identify rows in a table. The value for the Candidate key is unique and non-null for all tuples. And every table has to have at least one Candidate Key. But there can be more than one Candidate Key too. For e.g., in the example that we took earlier, both Id and Email can act as Candidate key for The table as they contain unique and non-null values.
  • 18.
    On the otherhand, we cannot use the attributes like City or Gender to retrieve tuples from the table as they have no unique values. Whereas on querying the table on the Id attribute will help us to retrieve unique tuples.
  • 19.
    Primary Key inSQL Primary key is the candidate key selected by the database administrator to uniquely identify tuples in a table. Out of all the Candidate Keys that can be possible for a table, there can be only one key that will be used to retrieve unique tuples from the table. This Candidate key is called the Primary Key. There can be only one Primary key for a table. Depending on how the Candidate key is constructed the primary key can be a single attribute or a group of attributes. But the important point to remember is that the Primary key should be a unique and non-null attribute(s). There can be two ways to create a Primary key for the table. The first way is to alter an already created to add the PK constraint on an attribute.
  • 20.
    Now if Itry to add a new row with duplicate Id value, it will give me an error message. The second way of adding a Primary key is during the creation of the table itself. All you have to do is add the Primary Key constraint at the end after defining all the attributes in the table. To define a Primary Key constraint on multiple attributes, you can list all the attributes in the parenthesis as shown below.
  • 21.
    Alternate or Secondarykeys in SQL Alternate keys are those candidate keys which are not the Primary Key There can be only one Primary key for a table. Therefore all the remaining Candidate keys are known as Alternate or Secondary keys. They can also uniquely identify tuples in a table, but the database administrator chose a different key as the Primary key.
  • 22.
    Foreign key inSQL Foreign key is an attribute which is a Primary key in its parent table, but is included as an attribute in another host table. A Foreign key generates a relationship between the parent table and the host table. For eg. In addition to the Employee table containing the personal details of the employees, we might have another table Department containing information related to the department of the employee. The Primary key in this table is the Department Id. We can add this attribute to the Employee by making it the Foreign key in the table.
  • 23.
    We can eitherdo this when we are creating the table or we can alter the table later to add the Foreign Key constraint. Here I have altered the table, but creating Foreign Key during table creation is similar to that for Primary Key. Here, Dep_Id is now the Foreign Key in table Employee while it is a Primary Key in the Department table. The Foreign key allows you to create a relationship between two tables in the database. Each of these tables describes data related to a particular field (employee and department here). Using the Foreign key we can easily retrieve data from both the tables.