SlideShare a Scribd company logo
1 of 23
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.

More Related Content

Similar to DBMS-Unit-2.pptx

Physical elements of data
Physical elements of dataPhysical elements of data
Physical elements of data
Dimara Hakim
 
Eer >r.model
Eer >r.modelEer >r.model
Eer >r.model
lavya3
 
RELATIONSHIP IN DBMS.pptx
RELATIONSHIP IN DBMS.pptxRELATIONSHIP IN DBMS.pptx
RELATIONSHIP IN DBMS.pptx
KAnurag2
 

Similar to DBMS-Unit-2.pptx (20)

DATABASE CONCEPTS AND PRACTICAL EXAMPLES
DATABASE CONCEPTS AND PRACTICAL EXAMPLESDATABASE CONCEPTS AND PRACTICAL EXAMPLES
DATABASE CONCEPTS AND PRACTICAL EXAMPLES
 
COMPUTERS Database
COMPUTERS Database COMPUTERS Database
COMPUTERS Database
 
DATABASE-1.pptx
DATABASE-1.pptxDATABASE-1.pptx
DATABASE-1.pptx
 
Relational Model
Relational ModelRelational Model
Relational Model
 
The Relational Model
The Relational ModelThe Relational Model
The Relational Model
 
Dbms ii mca-ch4-relational model-2013
Dbms ii mca-ch4-relational model-2013Dbms ii mca-ch4-relational model-2013
Dbms ii mca-ch4-relational model-2013
 
Fundamentals of database system - Relational data model and relational datab...
Fundamentals of database system  - Relational data model and relational datab...Fundamentals of database system  - Relational data model and relational datab...
Fundamentals of database system - Relational data model and relational datab...
 
2. Relational_Data_Model_Keys_10b.pptx
2. Relational_Data_Model_Keys_10b.pptx2. Relational_Data_Model_Keys_10b.pptx
2. Relational_Data_Model_Keys_10b.pptx
 
Fg d
Fg dFg d
Fg d
 
Physical elements of data
Physical elements of dataPhysical elements of data
Physical elements of data
 
Eer >r.model
Eer >r.modelEer >r.model
Eer >r.model
 
Unit03 dbms
Unit03 dbmsUnit03 dbms
Unit03 dbms
 
RELATIONSHIP IN DBMS.pptx
RELATIONSHIP IN DBMS.pptxRELATIONSHIP IN DBMS.pptx
RELATIONSHIP IN DBMS.pptx
 
Codds rules & keys
Codds rules & keysCodds rules & keys
Codds rules & keys
 
Unit03 dbms
Unit03 dbmsUnit03 dbms
Unit03 dbms
 
Database and Math Relations
Database and Math RelationsDatabase and Math Relations
Database and Math Relations
 
Key,ID Field and Tables Relationship
Key,ID Field and Tables Relationship Key,ID Field and Tables Relationship
Key,ID Field and Tables Relationship
 
MIS201 SQL database .pdf
MIS201  SQL database .pdfMIS201  SQL database .pdf
MIS201 SQL database .pdf
 
Steps towards of sql server developer
Steps towards of sql server developerSteps towards of sql server developer
Steps towards of sql server developer
 
Relational model
Relational modelRelational model
Relational model
 

Recently uploaded

怎样办理圣地亚哥州立大学毕业证(SDSU毕业证书)成绩单学校原版复制
怎样办理圣地亚哥州立大学毕业证(SDSU毕业证书)成绩单学校原版复制怎样办理圣地亚哥州立大学毕业证(SDSU毕业证书)成绩单学校原版复制
怎样办理圣地亚哥州立大学毕业证(SDSU毕业证书)成绩单学校原版复制
vexqp
 
In Riyadh ((+919101817206)) Cytotec kit @ Abortion Pills Saudi Arabia
In Riyadh ((+919101817206)) Cytotec kit @ Abortion Pills Saudi ArabiaIn Riyadh ((+919101817206)) Cytotec kit @ Abortion Pills Saudi Arabia
In Riyadh ((+919101817206)) Cytotec kit @ Abortion Pills Saudi Arabia
ahmedjiabur940
 
Sonagachi * best call girls in Kolkata | ₹,9500 Pay Cash 8005736733 Free Home...
Sonagachi * best call girls in Kolkata | ₹,9500 Pay Cash 8005736733 Free Home...Sonagachi * best call girls in Kolkata | ₹,9500 Pay Cash 8005736733 Free Home...
Sonagachi * best call girls in Kolkata | ₹,9500 Pay Cash 8005736733 Free Home...
HyderabadDolls
 
Top profile Call Girls In Purnia [ 7014168258 ] Call Me For Genuine Models We...
Top profile Call Girls In Purnia [ 7014168258 ] Call Me For Genuine Models We...Top profile Call Girls In Purnia [ 7014168258 ] Call Me For Genuine Models We...
Top profile Call Girls In Purnia [ 7014168258 ] Call Me For Genuine Models We...
nirzagarg
 
Top profile Call Girls In Rohtak [ 7014168258 ] Call Me For Genuine Models We...
Top profile Call Girls In Rohtak [ 7014168258 ] Call Me For Genuine Models We...Top profile Call Girls In Rohtak [ 7014168258 ] Call Me For Genuine Models We...
Top profile Call Girls In Rohtak [ 7014168258 ] Call Me For Genuine Models We...
nirzagarg
 
Top profile Call Girls In dimapur [ 7014168258 ] Call Me For Genuine Models W...
Top profile Call Girls In dimapur [ 7014168258 ] Call Me For Genuine Models W...Top profile Call Girls In dimapur [ 7014168258 ] Call Me For Genuine Models W...
Top profile Call Girls In dimapur [ 7014168258 ] Call Me For Genuine Models W...
gajnagarg
 
Jual obat aborsi Bandung ( 085657271886 ) Cytote pil telat bulan penggugur ka...
Jual obat aborsi Bandung ( 085657271886 ) Cytote pil telat bulan penggugur ka...Jual obat aborsi Bandung ( 085657271886 ) Cytote pil telat bulan penggugur ka...
Jual obat aborsi Bandung ( 085657271886 ) Cytote pil telat bulan penggugur ka...
Klinik kandungan
 
Sealdah % High Class Call Girls Kolkata - 450+ Call Girl Cash Payment 8005736...
Sealdah % High Class Call Girls Kolkata - 450+ Call Girl Cash Payment 8005736...Sealdah % High Class Call Girls Kolkata - 450+ Call Girl Cash Payment 8005736...
Sealdah % High Class Call Girls Kolkata - 450+ Call Girl Cash Payment 8005736...
HyderabadDolls
 
Jual Obat Aborsi Surabaya ( Asli No.1 ) 085657271886 Obat Penggugur Kandungan...
Jual Obat Aborsi Surabaya ( Asli No.1 ) 085657271886 Obat Penggugur Kandungan...Jual Obat Aborsi Surabaya ( Asli No.1 ) 085657271886 Obat Penggugur Kandungan...
Jual Obat Aborsi Surabaya ( Asli No.1 ) 085657271886 Obat Penggugur Kandungan...
ZurliaSoop
 

Recently uploaded (20)

怎样办理圣地亚哥州立大学毕业证(SDSU毕业证书)成绩单学校原版复制
怎样办理圣地亚哥州立大学毕业证(SDSU毕业证书)成绩单学校原版复制怎样办理圣地亚哥州立大学毕业证(SDSU毕业证书)成绩单学校原版复制
怎样办理圣地亚哥州立大学毕业证(SDSU毕业证书)成绩单学校原版复制
 
In Riyadh ((+919101817206)) Cytotec kit @ Abortion Pills Saudi Arabia
In Riyadh ((+919101817206)) Cytotec kit @ Abortion Pills Saudi ArabiaIn Riyadh ((+919101817206)) Cytotec kit @ Abortion Pills Saudi Arabia
In Riyadh ((+919101817206)) Cytotec kit @ Abortion Pills Saudi Arabia
 
Sonagachi * best call girls in Kolkata | ₹,9500 Pay Cash 8005736733 Free Home...
Sonagachi * best call girls in Kolkata | ₹,9500 Pay Cash 8005736733 Free Home...Sonagachi * best call girls in Kolkata | ₹,9500 Pay Cash 8005736733 Free Home...
Sonagachi * best call girls in Kolkata | ₹,9500 Pay Cash 8005736733 Free Home...
 
Top profile Call Girls In Purnia [ 7014168258 ] Call Me For Genuine Models We...
Top profile Call Girls In Purnia [ 7014168258 ] Call Me For Genuine Models We...Top profile Call Girls In Purnia [ 7014168258 ] Call Me For Genuine Models We...
Top profile Call Girls In Purnia [ 7014168258 ] Call Me For Genuine Models We...
 
5CL-ADBA,5cladba, Chinese supplier, safety is guaranteed
5CL-ADBA,5cladba, Chinese supplier, safety is guaranteed5CL-ADBA,5cladba, Chinese supplier, safety is guaranteed
5CL-ADBA,5cladba, Chinese supplier, safety is guaranteed
 
Top profile Call Girls In Rohtak [ 7014168258 ] Call Me For Genuine Models We...
Top profile Call Girls In Rohtak [ 7014168258 ] Call Me For Genuine Models We...Top profile Call Girls In Rohtak [ 7014168258 ] Call Me For Genuine Models We...
Top profile Call Girls In Rohtak [ 7014168258 ] Call Me For Genuine Models We...
 
Top profile Call Girls In dimapur [ 7014168258 ] Call Me For Genuine Models W...
Top profile Call Girls In dimapur [ 7014168258 ] Call Me For Genuine Models W...Top profile Call Girls In dimapur [ 7014168258 ] Call Me For Genuine Models W...
Top profile Call Girls In dimapur [ 7014168258 ] Call Me For Genuine Models W...
 
SAC 25 Final National, Regional & Local Angel Group Investing Insights 2024 0...
SAC 25 Final National, Regional & Local Angel Group Investing Insights 2024 0...SAC 25 Final National, Regional & Local Angel Group Investing Insights 2024 0...
SAC 25 Final National, Regional & Local Angel Group Investing Insights 2024 0...
 
Fun all Day Call Girls in Jaipur 9332606886 High Profile Call Girls You Ca...
Fun all Day Call Girls in Jaipur   9332606886  High Profile Call Girls You Ca...Fun all Day Call Girls in Jaipur   9332606886  High Profile Call Girls You Ca...
Fun all Day Call Girls in Jaipur 9332606886 High Profile Call Girls You Ca...
 
Ranking and Scoring Exercises for Research
Ranking and Scoring Exercises for ResearchRanking and Scoring Exercises for Research
Ranking and Scoring Exercises for Research
 
Charbagh + Female Escorts Service in Lucknow | Starting ₹,5K To @25k with A/C...
Charbagh + Female Escorts Service in Lucknow | Starting ₹,5K To @25k with A/C...Charbagh + Female Escorts Service in Lucknow | Starting ₹,5K To @25k with A/C...
Charbagh + Female Escorts Service in Lucknow | Starting ₹,5K To @25k with A/C...
 
Case Study 4 Where the cry of rebellion happen?
Case Study 4 Where the cry of rebellion happen?Case Study 4 Where the cry of rebellion happen?
Case Study 4 Where the cry of rebellion happen?
 
Jual obat aborsi Bandung ( 085657271886 ) Cytote pil telat bulan penggugur ka...
Jual obat aborsi Bandung ( 085657271886 ) Cytote pil telat bulan penggugur ka...Jual obat aborsi Bandung ( 085657271886 ) Cytote pil telat bulan penggugur ka...
Jual obat aborsi Bandung ( 085657271886 ) Cytote pil telat bulan penggugur ka...
 
Top Call Girls in Balaghat 9332606886Call Girls Advance Cash On Delivery Ser...
Top Call Girls in Balaghat  9332606886Call Girls Advance Cash On Delivery Ser...Top Call Girls in Balaghat  9332606886Call Girls Advance Cash On Delivery Ser...
Top Call Girls in Balaghat 9332606886Call Girls Advance Cash On Delivery Ser...
 
💞 Safe And Secure Call Girls Agra Call Girls Service Just Call 🍑👄6378878445 🍑...
💞 Safe And Secure Call Girls Agra Call Girls Service Just Call 🍑👄6378878445 🍑...💞 Safe And Secure Call Girls Agra Call Girls Service Just Call 🍑👄6378878445 🍑...
💞 Safe And Secure Call Girls Agra Call Girls Service Just Call 🍑👄6378878445 🍑...
 
Sealdah % High Class Call Girls Kolkata - 450+ Call Girl Cash Payment 8005736...
Sealdah % High Class Call Girls Kolkata - 450+ Call Girl Cash Payment 8005736...Sealdah % High Class Call Girls Kolkata - 450+ Call Girl Cash Payment 8005736...
Sealdah % High Class Call Girls Kolkata - 450+ Call Girl Cash Payment 8005736...
 
Nirala Nagar / Cheap Call Girls In Lucknow Phone No 9548273370 Elite Escort S...
Nirala Nagar / Cheap Call Girls In Lucknow Phone No 9548273370 Elite Escort S...Nirala Nagar / Cheap Call Girls In Lucknow Phone No 9548273370 Elite Escort S...
Nirala Nagar / Cheap Call Girls In Lucknow Phone No 9548273370 Elite Escort S...
 
20240412-SmartCityIndex-2024-Full-Report.pdf
20240412-SmartCityIndex-2024-Full-Report.pdf20240412-SmartCityIndex-2024-Full-Report.pdf
20240412-SmartCityIndex-2024-Full-Report.pdf
 
RESEARCH-FINAL-DEFENSE-PPT-TEMPLATE.pptx
RESEARCH-FINAL-DEFENSE-PPT-TEMPLATE.pptxRESEARCH-FINAL-DEFENSE-PPT-TEMPLATE.pptx
RESEARCH-FINAL-DEFENSE-PPT-TEMPLATE.pptx
 
Jual Obat Aborsi Surabaya ( Asli No.1 ) 085657271886 Obat Penggugur Kandungan...
Jual Obat Aborsi Surabaya ( Asli No.1 ) 085657271886 Obat Penggugur Kandungan...Jual Obat Aborsi Surabaya ( Asli No.1 ) 085657271886 Obat Penggugur Kandungan...
Jual Obat Aborsi Surabaya ( Asli No.1 ) 085657271886 Obat Penggugur Kandungan...
 

DBMS-Unit-2.pptx

  • 2.  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?
  • 3. 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.
  • 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 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.
  • 6. 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.
  • 7. 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
  • 8. 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.
  • 9. 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)
  • 10. 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
  • 11. 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.
  • 12. 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.
  • 13. 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.
  • 14. 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?
  • 15. 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.
  • 16. 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.
  • 17. 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.
  • 18. 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.
  • 19. 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.
  • 20. 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.
  • 21. 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.
  • 22. 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.
  • 23. 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.