Unit 2
Unit 2
Unit 2
The relational model is today the primary data model for commercial
data processing applications. It attained its primary position because of
its simplicity, which eases the job of the programmer.
For example, consider the instructor table of Figure 2.1, which stores
information about instructors. The table has four column headers: ID,
name, dept name, and salary. Each row of this table records information
about an instructor, consisting of the instructor’s ID, name, dept name,
and salary.
Figure 2.3 shows a third table, prereq, which stores the prerequisite
courses for each course. The table has two columns, course id and prereq
id. Each row consists of a pair of course identifiers such that the second
course is a prerequisite for the first course.
Thus, a row in the prereq table indicates that two courses are related in
the sense that one course is a prerequisite for the other. As another
example, we consider the table instructor, a row in the table can be
thought of as representing the relationship between a specified ID
and the corresponding values for name, dept name, and salary values.
Thus, in the relational model the term relation is used to refer to a table,
while the term tuple is used to refer to a row. Similarly, the term
attribute refers to a column of a table.
The null value is a special value that signifies that the value is unknown
or does not exist. For example, suppose as before that we include the
attribute phone number in the instructor relation. It may be that an
instructor does not have a phone number at all, or that the telephone
number is unlisted.
Example:
11 Andrew Johnson
22 Tom Wood
33 Alex Hale
Here are some reasons for using sql key in the DBMS system.
Table STUDENT_COURSE
STUD_NO TEACHER_NO COURSE_NO
1 001 C001
2 056 C005
5. Foreign Key: If an attribute can only take the values which are
present as values of some other attribute, it will be a foreign key to
the attribute to which it refers. The relation which is being
referenced is called referenced relation and the corresponding
attribute is called referenced attribute the relation which refers to
the referenced relation is called referencing relation and the
corresponding attribute is called referencing attribute. The
referenced attribute of the referenced relation should be the
primary key to it.
It is a key it acts as a primary key in one table and it acts
as
secondary key in another table.
It combines two or more relations (tables) at a time.
They act as a cross-reference between the tables.
For example, DNO is a primary key in the DEPT table and
a non-key in EMP
Example:
Refer Table STUDENT shown above.
STUD_NO in STUDENT_COURSE is a
foreign key to STUD_NO in STUDENT relation.
Table STUDENT_COURSE
STUD_NO TEACHER_NO COURSE_NO
1 005 C001
2 056 C005
It may be worth noting that, unlike the Primary Key of any given
relation, Foreign Key can be NULL as well as may contain
duplicate tuples i.e. it need not follow uniqueness constraint. For
Example, STUD_NO in the STUDENT_COURSE relation is not
unique. It has been repeated for the first and third tuples.
However, the STUD_NO in STUDENT relation is a primary key and
it needs to be always unique, and it cannot be null.
In this table student_id and subject_id together will form the primary
key, hence it is a composite key.
OR
Composite key is a key which is the combination of more than one field
or column of a given table. It may be a candidate key or primary key.
Columns that make up the composite key can be of different data types.
CREATE TABLE SAMPLE_TABLE
(COL1 integer,
COL2 varchar(30),
COL3 varchar(50),
PRIMARY KEY (COL1, COL2));
7. Artificial key
The key created using arbitrarily assigned data are known as artificial
keys. These keys are created when a primary key is large and complex
and has no relationship with many other relations. The data values of
the artificial keys are usually numbered in a serial order.
referential integrity
STUDENT
ROLL_ NAME ADDRE PHONE AGE BRANCH_CODE
NO SS
4 SURE DELHI 18 IT
SH
BRANCH
BRANCH_CODE BRANCH_NAME
CS COMPUTER SCIENCE
IT INFORMATION TECHNOLOGY
CV CIVIL ENGINEERING
BRANCH_CODE of STUDENT can only take the values which are present
in BRANCH_CODE of BRANCH which is called referential integrity
constraint.
A database schema, along with primary key and foreign key dependencies,
can be depicted by schema diagrams. Figure 2.8 shows the schema
diagram for our university organization. Each relation (table) appears as
a box, with the relation name at the top in blue, and the attributes
listed inside the box. Primary key attributes are shown underlined.
Foreign key dependencies appear as arrows from the foreign key
attributes of the referencing relation to the primary key of the referenced
relation.