0% found this document useful (0 votes)
21 views13 pages

DBMS Unit Ii

The document provides an overview of the relational model in database management systems, detailing key concepts such as domains, attributes, tuples, and relations, as well as the importance of null values and integrity constraints in SQL. It also introduces relational algebra, including fundamental operators like selection, projection, union, and set operations, and discusses relational calculus as a non-procedural query language. Additionally, the document emphasizes the significance of null values and constraints in maintaining data integrity within relational databases.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
21 views13 pages

DBMS Unit Ii

The document provides an overview of the relational model in database management systems, detailing key concepts such as domains, attributes, tuples, and relations, as well as the importance of null values and integrity constraints in SQL. It also introduces relational algebra, including fundamental operators like selection, projection, union, and set operations, and discusses relational calculus as a non-procedural query language. Additionally, the document emphasizes the significance of null values and constraints in maintaining data integrity within relational databases.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 13

DBMS UNIT II

Relational Model: Introduction to relational model, concepts of domain, attribute, tuple, relation,
importance of null values - Comparisons Using Null Values, Logical Connectives AND, OR, and NOT,
Impact on SQL Constructs, Disallowing Null Values; Integrity constraints in SQL - Domain constraints,
Entity constraints, Referential integrity constraints, Assertions.

NEC CSE 1
DBMS UNIT II

Relational Model:

 Relational data model is the primary data model, which is used widely around the world for data
storage and processing. This model is simple and have all the properties and capabilities required
to process data with storage efficiency.
 The relational model represents the database as a collection of relations which is used widely
around the world for data storage and processing. This model is simple and have all the properties
and capabilities required to process data with storage efficiency.

Concepts:

 Tables: In relation data model, relations are saved in the format of Tables. This format stores the
relation among entities. A table has rows and columns, where rows represent records and columns
represents the attributes.
 Tuple: A single row of a table, which contains a single record for that relation, is called a tuple.
 Relation instance: A finite set of tuples in the relational database system represents relation
instance. Relation instances do not have duplicate tuples.
 Relation schema: This describes the relation name (table name), attributes and their names. A
relation schema denoted by R is a list of attributes (A1, A2, …, An) The degree of the relation is
the number of attributes of its relation schema.
 The cardinality of the relation is the number of tuples in the relation.
 Relation key: Each row has one or more attributes which can identify the row in the relation
(table) uniquely, is called the relation key.
 Attribute: A column of a relation designated by name. The name associated should be
meaningful. Each attributes associates with a domain.
 Attribute domain: Every attribute has some pre-defined value scope, known as attribute
 Domain: A domain D is the original sets of atomic values used to model data.
 Each row in the table represents a collection of related data values.
 Each Column in the table represents how to interpret the data values in each row.
 All values in a column are of the same data type.

Definitions:

Domain:
The data type describing the types of
values that can appear in each column
is called a domain
Attributes:

NEC CSE 2
DBMS UNIT II
A column header is called an attribute
Tuple:
A row is called a tuple
Relation:
The table is called a relation

Characteristics of relation:

 Ordering of Tuples in a relation: A tuple is a set of values. A relation is a set of tuples. Since a
relation is a set, there is no ordering on rows.
 Ordering of Values within a tuple: The order of attributes and their values within a relation is not
important as long as the correspondence between attributes and values is maintained.
 Values and NULL values in the tuple: Each value in a tuple is atomic. That means each value
cannot be divided into smaller components. Hence, the composite and multivalued attributes are
not allowed in a relation.

Concept of Domain:

 A domain D is a set of atomic values. By atomic we mean that each value in the domain is
indivisible.
 A common method of specifying a domain is to specify a data type.
Examples:

 Names: The set of names of persons


 Indian_phone_numbers : The set of 10-digit phone numbers
 Local_phone_numbers : The set of 7-digit phone numbers valid within a particular area code
 Employee_ages: Possible ages of employees of a company; each must be a value between 15 and
80 years old.
 The preceding are called logical definitions of domains. A data type or format is also specified
for each domain.
 The data type of Names is a varchar
 The data type for Employee_ages is an integer number between 15 and 80.
 A domain is thus given a name, data type, and format. Additional information for interpreting the
values of a domain can also be given
Relational Algebra?
Relational Algebra is a procedural query language. The main purpose of using Relational Algebra is to define
operators that transform one or more input relations into an output relation.
Fundamental Operators
Relational algebra consists of various operators that help us fetch and manipulate data from relational tables in
the database to perform certain operations on relational data.

These are the basic/fundamental operators used in Relational Algebra


1. Selection(σ)
2. Projection(π)
3. Union(U)
4. Set Difference(-)
5. Set Intersection(∩)
6. Rename(ρ)
7. Cartesian Product(X)

1. Selection(σ) : Selection Operation is basically used to filter out rows from a given table based on certain
given condition. It basically allows you to retrieve only those rows that match the condition as per condition
passed during SQL Query. It is used to select required tuples of the relations.
NEC CSE 3
DBMS UNIT II
Illustration : If we want to get the details of all the workers in the Department “IT”, we would use the
selection operator to filter out these based on the given condition.
Example:
A B C

1 2 4

2 2 3

3 2 3

4 3 4

Output Table :
For the above relation, σ(c>3)R will select the tuples which have c more than 3.
A B C

1 2 4

4 3 4

2. Projection(π) : While Selection operation works on rows , similarly projection operation of relational
algebra works on columns. It basically allows you to pick specific columns from a given relational table based
on the given condition and ignoring all the other remaining columns.
Illustration : If we are interested in the specific columns from the relational tables, we would prefer to use the
Projection Operator. It is used to project required column data from a relation.
Example: Consider following Table. Suppose we want columns B and C from Relation R.
A B C

1 2 4

2 2 3

3 2 3

4 3 4

π(B,C)R will show following columns.


B C

2 4

2 3

3 4

3. Union(U) : Union Operator is basically used to combine the results of two queries into a single result. The
NEC CSE 4
DBMS UNIT II
only condition is that both queries must return same number of columns with same data types.
Illustration : If in case we want a list of all the employee from two different department . Then in that case we
should use union operation to merge both the list from two different department. Union operation in relational
algebra is the same as union operation in set theory.
Example: FRENCH
Student_Name Roll_Number

Ram 01

Mohan 02

Vivek 13

Geeta 17

GERMAN
Student_Name Roll_Number

Vivek 13

Geeta 17

Shyam 21

Rohan 25

Consider the following table of Students having different optional subjects in their course.
π(Student_Name)FRENCH U π(Student_Name)GERMAN
Student_Name

Ram

Mohan

Vivek

Geeta

Shyam

Rohan

Note: The only constraint in the union of two relations is that both relations must have the same set of
Attributes.
4. Set Difference(-) : Set difference basically provides the rows that are present in one table , but not in
another tables.
Illustration : If you have two lists of employees, one from the IT Department and one from the Marketing
department & you want to know which employees are in IT but not in Marketing , the set difference operator
would used.
NEC CSE 5
DBMS UNIT II
Set Difference in relational algebra is the same set difference operation as in set theory.
Example: From the above table of FRENCH and GERMAN, Set Difference is used as follows
π(Student_Name)FRENCH - π(Student_Name)GERMAN
Student_Name

Ram

Mohan

Note: The only constraint in the Set Difference between two relations is that both relations must have the same
set of Attributes.

5. Set Intersection(∩) : Set Intersection basically allows to fetches only those rows of data that are common
between two sets of relational tables.
Illustration : If we want to find the number of employees who work in both IT and Marketing Department ,
then in that case we use Intersection Operator.
Set Intersection in relational algebra is the same set intersection operation in set theory.
Example: From the above table of FRENCH and GERMAN, the Set Intersection is used as follows:
π(Student_Name)FRENCH ∩ π(Student_Name)GERMAN
Student_Name

Vivek

Geeta

Note: The only constraint in the Set Difference between two relations is that both relations must have the same
set of Attributes.

6. Rename(ρ) : Rename operator basically allows you to give a temporary name to a specific relational table
or to its columns. It is very useful when we want to avoid ambiguity, especially in complex Queries.
Rename is a unary operation used for renaming attributes of a relation.
ρ(a/b)R will rename the attribute 'b' of the relation by 'a'.

7. Cross Product(X) : Cartesian product Operator combines every row of one table with every row of another
table , producing all the possible combination. It’s mostly used as a precursor to more complex operation like
joins.
Cross-product between two relations. Let’s say A and B, so the cross product between A X B will result in all
the attributes of A followed by each attribute of B. Each record of A will pair with every record of B.
Example:
A
Name Age Sex

Ram 14 M

Sona 15 F

Kim 20 M

B
ID Course

1 DS
NEC CSE 6
DBMS UNIT II
ID Course

2 DBMS

AXB
Name Age Sex ID Course

Ram 14 M 1 DS

Ram 14 M 2 DBMS

Sona 15 F 1 DS

Sona 15 F 2 DBMS

Kim 20 M 1 DS

Kim 20 M 2 DBMS

Note: If A has ‘n’ tuples and B has ‘m’ tuples then A X B will have ‘ n*m ‘ tuples.

Relational Calculus
There is an alternate way of formulating queries known as Relational Calculus. Relational calculus is a non-
procedural query language. In the non-procedural query language, the user is concerned with the details of how
to obtain the end results. The relational calculus tells what to do but never explains how to do.

o Universal Quantifiers: The universal quantifier denoted by ∀ is read as for all which means that in a
Many of the calculus expressions involves the use of Quantifiers. There are two types of quantifiers:

given set of tuples exactly all tuples satisfy a given condition.

o Existential Quantifiers: The existential quantifier denoted by ∃ is read as for all which means that in a
given set of tuples there is at least one occurrences whose value satisfy a given condition.

Types of Relational calculus:

1. Tuple Relational Calculus (TRC)


It is a non-procedural query language which is based on finding a number of tuple variables also known as range
variable for which predicate holds true. It describes the desired information without giving a specific procedure
for obtaining that information. The tuple relational calculus is specified to select the tuples in a relation. In TRC,
filtering variable uses the tuples of a relation. The result of the relation can have one or more tuples.
Notation:
A Query in the tuple relational calculus is expressed as following notation
1. {T | P (T)} or {T | Condition (T)}

NEC CSE 7
DBMS UNIT II
Where
T is the resulting tuples
P(T) is the condition used to fetch T.
For example:
1. { T.name | Author(T) AND T.article = 'database' }
Output: This query selects the tuples from the AUTHOR relation. It returns a tuple with 'name' from Author
who has written an article on 'database'.

2. Domain Relational Calculus (DRC)


The second form of relation is known as Domain relational calculus. In domain relational calculus, filtering

uses logical connectives ∧ (and), ∨ (or) and ┓ (not). It uses Existential (∃) and Universal Quantifiers (∀) to bind
variable uses the domain of attributes. Domain relational calculus uses the same operators as tuple calculus. It

the variable. The QBE or Query by example is a query language related to domain relational calculus.
Notation:
1. { a1, a2, a3, ..., an | P (a1, a2, a3, ... ,an)}
Where
a1, a2 are attributes
P stands for formula built by inner attributes

1. {< article, page, subject > | ∈ FacultyTable ∧ subject = 'database'}


For example:

Output: This query will yield the article, page, and subject from the relational FacultyTable, where the subject is
a database.

Importance of Null values:

 IN sql, NULL is used to represent a missing value, but that it usually has one of three different
interpretations—value unknown (exists but is not known), value not available (exists but is
purposely withheld), or value not applicable (the attribute is undefined for this tuple).

Consider the following examples to illustrate each of the meanings of NULL.

1. Unknown value. A person’s date of birth is not known, so it is represented by NULL in the
database.
2. Unavailable or withheld value. A person has a home phone but does not want it to be listed, so
it is withheld and represented as NULL in the database.
3. Not applicable attribute. An attribute LastCollegeDegree would be NULL for a person who has
no college degrees because it does not apply to that person.

NEC CSE 8
DBMS UNIT II
 It is often not possible to determine which of the meanings is intended; for example, a NULL for
the home phone of a person can have any of the three meanings.
 Hence, SQL does not distinguish between the different meanings of NULL.
 When a NULL is involved in a comparison operation, the result is considered to be UNKNOWN
(it may be TRUE or it may be FALSE).
 Hence, SQL uses a three-valued logic with values TRUE, FALSE, and
 UNKNOWN instead of the standard two-valued (Boolean) logic with values TRUE or FALSE.
 It is therefore necessary to define the results (or truth values) of three-valued logical expressions
when the logical connectives AND, OR, and NOT are used.

Comparisons Using Null Values:

 We usually use IS NULL and IS NOT NULL operators to compare with NULL values.
Syntax:

select col_names from tablename where col_name IS NULL;


select col_names from tablename where col_name IS NOT NULL;
Disallowing Null Values:
 Another constraint on attributes specifies whether null values permitted or not
• For example, if every STUDENT tuple must have a valid, non-null value for the Name
attribute, then Name of STUDENT is constrained to be NOT NULL.

 We can Disallow Null values by specifying the part as NOT NULL.


 The Field in the Primary key are not allowed as NULL.
PK ≠ null for any Tuple
 Not Null clause is used in Preventing null values from being entered in a column
 Null values can be used to Explicitly allow Null values in a Column also.
NEC CSE 9
DBMS UNIT II
Constraints in Relational Data:

 Constraint is a very important feature in relational model. In fact, relational model support a well-
defined theory of constraint on attributes or tables.
 Constraint is useful because it allows designer to specify the semantics of data in database and it
is the rules to enforce DBMSs to check that new data satisfies the semantics.
Constraints:

 Every relation has some conditions that must hold for it to be a valid relation. These conditions
are called Relational Integrity Constraints. There are three main integrity constraints.
 Entity Constraints
 Domain constraints
 Referential integrity
constraints

1. Entity Constraints:

 There must be at least one


minimal subset of attributes in the
relation, which can identify a tuple
uniquely. This minimal subset of
attributes is called key for that
relation. If there are more than one
such minimal subsets, these are
called candidate keys.

Key constraints forces that:


1. In a relation with a key attribute, no two tuples can have identical value for key attributes. i.e. key
value must be unique in each tuple
2. Key attribute cannot have NULL values.
Key constrains are also referred to as Entity Constraints.
Entity constraint-Primary key:
 An attribute or set of attributes which are used to uniquely identify a row from table are called as
Primary key.
 No attribute in the primary key can be NULL. This is because, NULL values for the primary key
means we cannot identify some tuples.

NEC CSE 10
DBMS UNIT II
2. Domain constraints

 Attributes have specific values in real-world scenario. For example, age can only be positive
integer.
 The same constraints has been tried to employ on the attributes of a relation.
 Every attribute is bound to have a specific range of values. For example, age can not be less than
zero and telephone number can not be a outside 0-9.
 Domain constraints specify that the value of each attribute A must be an atomic value from
the domain dom(A).
 The data types associated with domains typically include standard numeric data types for
integers (such as short-integer, integer, long-integer) and real numbers (float and double-
precision float).
 Characters, fixed-length strings, and variable-length strings are also available, as are date,
time, timestamp, and money data types.

3. Referential integrity constraints:

 This integrity constraints works on the concept of Foreign Key. A key attribute of a relation can
be referred in other relation, where it is called foreign key.
 A set of attributes FK in a relation schema R1 is foreign key if
 The attributes in FK correspond to the attributes in the primary key of another relation
schema R2.
 The values for FK in each tuple of R1 either occur as values of primary key of a tuple in
R2 or is entirely NULL.
 Referential Integrity is based on a concept called “Foreign Key”
 It is Property of data which, when satisfied, requires every value of one attribute(column) of a
relation(table) to exist as a value of another attribute in a different(or the same) relation(table)

 The referential integrity constraint states that a tuple in one relation that refers to another relation
must refer to an existing tuple in that relation. In the figure below DNO is the primary key in
department table whereas it is the foreign key(FK) in Employee table. Here, department is called
as referenced relation whereas employee is called as referencing relation. The primary key (PK)
of referenced relation & FK of referencing relation must have same domain
NEC CSE 11
DBMS UNIT II

BASIC SQL:Simple Database schema, data types, table definitions (create, alter), different DML
operations (insert, delete, update).
BASIC SQL
Introduction:

 The name SQL is presently expanded as Structured Query Language.


 Originally, SQL was called SEQUEL (Structured English Query Language) and was designed and
implemented at IBM Research as the interface for an experimental relational database system called
SYSTEM R.
 SQL is used to access and manipulate databases
 SQL is not case sensitive. Generally SQL keywords are written in uppercase.
 We can place a single SQL statement on one or multiple text lines.
 It can process a single record as well as sets of records at a time.
 You can perform most of the action in a database with SQL statements.
 All SQL statements define what is to be done rather than how it is to be done. (Non-procedural)
 SQL depends on relational algebra and tuple relational calculus.
 All relational database management systems like MySQL, MS Access, Oracle, Sybase, Informix, postgres
and SQL Server use SQL as standard database language.
 The standard SQL commands to interact with relational databases are CREATE, SELECT, INSERT,
UPDATE, DELETE and DROP.
 These commands can be classified into groups based on their nature as
o Data definition language (DDL) - (create, alter, drop, truncate)
o Data Manipulation Language (DML)-(select, insert, update, delete)
o Data control language (DCL)-( Grant, Revoke)
o Transaction control Language (TCL)-( commit , Rollback, Savepoint)

SQL Data types:

In MySQL there are three main data types: text, number, and date.

Text data types: There are so many types used. A few are given below.

Data type Description


CHAR(size) Holds a fixed length string (can contain letters, numbers, and special
characters). The fixed size is specified in parenthesis. Can store up to 255
characters
VARCHAR(size) Holds a variable length string (can contain letters, numbers, and special
characters). The maximum size is specified in parenthesis. Can store up to
255 characters.

NEC CSE 12
DBMS UNIT II

Note: If you put a greater value than 255 it will be converted to a TEXT type
TINYTEXT Holds a string with a maximum length of 255 characters
TEXT Holds a string with a maximum length of 65,535 characters
BLOB For BLOBs (Binary Large OBjects). Holds up to 65,535 bytes of data
Number data types:

Data type Description


SMALLINT(size) -32768 to 32767 normal. 0 to 65535 UNSIGNED*. The maximum number of digits
may be specified in parenthesis
INT(size) -2147483648 to 2147483647 normal. 0 to 4294967295 UNSIGNED*. The
maximum number of digits may be specified in parenthesis
BIGINT(size) -9223372036854775808 to 9223372036854775807 normal. 0 to
18446744073709551615 UNSIGNED*. The maximum number of digits may be
specified in parenthesis
FLOAT(size,d) A small number with a floating decimal point. The maximum number of digits may
be specified in the size parameter. The maximum number of digits to the right of the
decimal point is specified in the d parameter
DOUBLE(size,d) A large number with a floating decimal point. The maximum number of digits may
be specified in the size parameter. The maximum number of digits to the right of the
decimal point is specified in the d parameter
DECIMAL(size,d) A DOUBLE stored as a string , allowing for a fixed decimal point. The maximum
number of digits may be specified in the size parameter. The maximum number of
digits to the right of the decimal point is specified in the d parameter

Date data types:

Data type Description

DATE() A date. Format: YYYY-MM-DD


Note: The supported range is from '1000-01-01' to '9999-12-31'
DATETIME() A date and time combination.
Format is: YYYY-MM-DD HH:MI:SS
Note: The supported range is from '1000-01-01 00:00:00' to '9999-12-31 23:59:59'
TIMESTAMP() A timestamp. TIMESTAMP is also a date and time combination.
Format is: YYYY-MM-DD HH:MI:SS
Note: The supported range is from '1970-01-01 00:00:01' UTC to '2038-01-09
03:14:07' UTC
TIME() A time. Format: HH:MI:SS
Note: The supported range is from '-838:59:59' to '838:59:59'
YEAR() A year in two-digit or four-digit format.
Note: Values allowed in four-digit format: 1901 to 2155. Values allowed in two-digit
format: 70 to 69, representing years from 1970 to 2069
SQL Commands:

The standard SQL commands to interact with relational databases are CREATE, SELECT, INSERT,
UPDATE, DELETE and DROP. These commands can be classified into groups based on their nature as
Data definition language (DDL)
Data Manipulation Language (DML) commands.

NEC CSE 13

You might also like