0% found this document useful (0 votes)
5 views

DBMS-2 Relational Model-1

Uploaded by

Anuj kushwaha
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
5 views

DBMS-2 Relational Model-1

Uploaded by

Anuj kushwaha
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 21

Resources:

Constraints on Relational Database Model - GeeksforGeeks

Top 50 RDBMS Interview Questions and Answers (internshala.com)

Top Relational Databases Interview Questions and Answers You Should Learn (interviewkickstart.com)

45 RDBMS Interview Questions (With Sample Answers) | Indeed.com


3. The Relational Data Model and Relational Database Constraints

The relational model represents the database as a collection of relations.

● Informally, each relation resembles a table of values or, to some extent, a flat file of records.

● It is called a flat file because each record has a simple linear or flat structure.

When a relation is thought of as a table of values, each row in the table represents a collection of related
data values.
A row represents a fact that typically corresponds to a real-world entity or relationship.

In the formal relational model terminology, a row is called a tuple, a column header is called an attribute, and
the table is called a relation. The data type describing the types of values that can appear in each column is
represented by a domain of possible values.

Chatgpt
The relational data model is a conceptual model used to organize data into a structure of tables
(relations) consisting of rows (tuples) and columns (attributes).
Tables (Relations): Tables are the basic building blocks of the relational model. They consist of rows and
columns, where each row represents a record, and each column represents an attribute of that record.

Attributes (Fields): Attributes define the properties of the data stored in a table. Each column in a table
corresponds to an attribute, such as a person's name, age, or address.

Tuples (Rows): Tuples are individual records in a table. Each tuple represents a single instance of the entity
being modeled and contains values for each attribute defined in the table's schema.

Keys: Keys are used to uniquely identify tuples within a table. The primary key is a special type of key that
uniquely identifies each tuple in the table. Other types of keys include candidate keys and foreign keys.

Relationships: Relationships define associations between tables. These relationships are typically established
through keys, where the value of a key in one table corresponds to the value of a key in another table.

Constraints
Relational database constraints are rules that enforce data integrity and consistency within a relational
database. These constraints are used to maintain the accuracy and validity of the data stored in the
database. Some common relational database constraints include:
Primary Key Constraint: This constraint ensures that the values in a specified column (or columns) are
unique and not null. It uniquely identifies each tuple in a table.

Foreign Key Constraint: This constraint establishes a relationship between two tables by enforcing referential
integrity. It ensures that values in a column (or columns) of one table match values in another table's primary key
or unique key column(s).

Unique Constraint: This constraint ensures that all values in a specified column (or columns) are unique,
except for null values.

Check Constraint: This constraint enforces a condition that must be true for every tuple in a table. It is used to
enforce business rules or data integrity rules.

Not Null Constraint: This constraint ensures that a column does not contain null values.

These constraints help to maintain the consistency and integrity of data within a relational database, ensuring
that the data remains accurate and reliable.

Data integrity and consistency are crucial aspects of maintaining the quality and reliability of data within a
database:
Data Integrity:
Data integrity refers to the accuracy, completeness, and reliability of data stored in a database. It ensures that
the data accurately represents the real-world entities and relationships it is meant to model. There are several
aspects to data integrity:

● Entity Integrity: This ensures that each record (tuple) within a table is uniquely identifiable. Typically, this
is enforced using primary key constraints, which ensure that each tuple has a unique identifier.

● Referential Integrity: This ensures the consistency of relationships between tables. Referential integrity
is maintained by foreign key constraints, which ensure that values in a column (or columns) of one table
match values in another table's primary key or unique key column(s).

● Domain Integrity: This ensures that data values stored in the database meet certain predefined criteria
or rules. This can be enforced through data type constraints, check constraints, and other validation rules.

● User-defined Integrity: This refers to any additional integrity rules or constraints that are specific to the
business logic or requirements of the database application.
Data Consistency:
Data consistency refers to the correctness and reliability of data across the entire database. It ensures that data
is accurate and up-to-date and that it reflects the latest changes made to the database. Data consistency is
maintained through various means:

● Transaction Management: Database management systems (DBMS) use transaction mechanisms to


ensure that database operations are performed in an atomic, consistent, isolated, and durable (ACID)
manners. This ensures that transactions are completed successfully or rolled back entirely if an error
occurs, preserving data consistency.

● Concurrency Control: In multi-user database environments, concurrency control mechanisms are used
to manage simultaneous access to data by multiple users or processes. These mechanisms prevent
conflicts and ensure that data remains consistent despite concurrent transactions.

● Database Constraints: Constraints such as primary key, foreign key, unique, and check constraints
enforce data integrity rules, which in turn help maintain data consistency by preventing invalid or
inconsistent data from being entered into the database.

● Data Validation and Cleansing: Regular validation and cleansing processes can help identify and
correct inconsistencies or errors in the data, ensuring that it remains consistent and accurate over time.

Overall, data integrity and consistency are essential for ensuring that a database provides reliable, accurate, and
trustworthy information to support critical business processes and decision-making.
3.1 Domains, Attributes, Tuples, and Relations

A domain D is a set of atomic values. By atomic we mean that each value in the domain is indivisible as far as
the formal relational model is concerned.
A common method of specifying a domain is to specify a data type from which the data values forming the
domain are drawn.
■ Usa_phone_numbers. The set of ten-digit phone numbers valid in the United States.

A relation of degree seven, which stores information about university students, would contain seven attributes
describing each student. as follows: STUDENT(Name, Ssn, Home_phone, Address, Office_phone, Age, Gpa)
A relation (or relation state) r of the relation schema R(A1, A2, ..., An), also denoted by r(R), is a set of
n-tuples r = {t1, t2, ..., tm}. Each n-tuple t is an ordered list of n values t =<v1,v2,v3,...vn>

The earlier definition of a relation can be restated more formally using set theory concepts as follows. A
relation (or relation state) r(R) is a mathematical relation of degree n on the domains dom(A1), dom(A2), ...,
dom(An), which is a subset of the Cartesian product (denoted by ×) of the domains that define R:
r(R) ⊆ (dom(A1) × dom(A2) × ... × dom(An))

It is possible for several attributes to have the same domain. The attribute names indicate different roles, or
interpretations, for the domain. For example, in the STUDENT relation, the same domain
USA_phone_numbers plays the role of Home_phone, referring to the home phone of a student, and the
role of Office_phone, referring to the office phone of the student. A third possible attribute (not shown) with
the same domain could be Mobile_phone.

3.1.2 Characteristics of Relations


● Ordering of Tuples in a Relation. A relation is defined as a set of tuples. Mathematically, elements of a
set have no order among them; hence, tuples in a relation do not have any particular order.

● Values and NULLs in the Tuples. Composite and multivalued attributes are not allowed. This model is
sometimes called the flat relational model.
Multivalued attributes must be represented by separate relations, and composite attributes are represented
only by their simple component attributes in the basic relational model.
● We can have several meanings for NULL values, such as value unknown, value exists but is not
available, or attribute does not apply to this tuple (also known as value undefined).

The exact meaning of a NULL value governs how it fares during arithmetic aggregations or
comparisons with other values.

For example, a comparison of two NULL values leads to ambiguities—if both Customer A and B
have NULL addresses, it does not mean they have the same address.
3.2 Relational Model Constraints and Relational Database Schemas

So far, we have discussed the characteristics of single relations.


In a relational database,there will typically be many relations, and the tuples in those relations are usually
related in various ways.
● The state of the whole database will correspond to the states of all its relations at a particular point in time.
There are generally many restrictions or constraints on the actual values in a database state.
● These constraints are derived from the rules in the miniworld that the database represents.

Constraints on databases can generally be divided into three main categories:

1. Constraints that are inherent in the data model. We call these inherent model-based constraints or
implicit constraints.( refer to the restrictions or rules that are inherent in the data model itself.)
They are derived from the rules of the miniworld that the database represents.
Examples:

a. Uniqueness Constraint: Duplicate tuples are not allowed in a relation.


b. Referential Integrity: Foreign keys must match primary keys.
c. Domain Constraints: Attributes must adhere to specified data types (e.g., numeric, characters,
dates).
These constraints are enforced automatically by the database management system.
2. Constraints that can be directly expressed in schemas of the data model, typically by specifying them in
the DDL (data definition language). We call these schema-based constraints or explicit constraints.
Examples:

a. Functional Dependencies: Express relationships between attributes.


b. Key Constraints: Specify uniqueness (e.g., primary keys).
c. Check Constraints: Define conditions that must hold for valid data.

These constraints are part of the schema definition.

3. Constraints that cannot be directly expressed in the schemas of the data model, and hence must be
expressed and enforced by the application programs. We call these application-based or semantic
constraints or business rules.
Examples:

a. “This year’s salary increase can be no more than last year’s.”


b. “A customer’s credit limit cannot be exceeded.”
c. “A product’s price must be positive.”
The schema-based constraints:
The schema-based constraints include domain constraints, key constraints, constraints on NULLs, entity
integrity constraints, and referential integrity constraints.

domain constraints,
key constraints,
constraints on NULLs,
entity integrity constraints,
referential integrity constraints.

3.2.1 Domain Constraints


Domain constraints specify that within each tuple, the value of each attribute A must be an atomic value from the
domain dom(A)(This means that composite or multi-valued attributes are not allowed).

The data types associated with domains typically include standard numeric data types
● for integers (such as short integer, integer, and long integer)
● and real numbers (float and double precision float).
● Characters, Booleans, fixed-length strings, and variable-length strings are also available,
● as are date, time, timestamp, and money, or other special data types.
EID Name Phone

01 Anuj 9696406570
02 Shivam 8871253247
01 Akash 3245698710 (Violation: Composite
attribute "Name")

3.2.2 Key Constraints and Constraints on NULL Values


A relation is defined as a set of tuples. By definition, all elements of a set are distinct; hence, all tuples in a
relation must also be distinct.
● This means that no two tuples can have the same combination of values for all their attributes.

● Suppose that we denote one such subset of attributes by SK; then for any two distinct tuples t1 and t2 in
a relation state r of R, we have the constraint that: t1[SK]≠ t2[SK]
Any such set of attributes SK is called a superkey of the relation schema R.

● A superkey SK specifies a uniqueness constraint that no two distinct tuples in any state r of R can have
the same value for SK.
● Every relation has at least one default super key—the set of all its attributes.

● A superkey can have redundant attributes, however, so a more useful concept is that of a key, which has
no redundancy. A key K of a relation schema R is a superkey of R with the additional property that
removing any attribute A from K leaves a set of attributes K that is not a superkey of R any more. Hence, a
key satisfies two properties:
1. Two distinct tuples in any state of the relation cannot have identical values for (all) the attributes in
the key. This first property also applies to a superkey.

2. It is a minimal superkey—that is, a superkey from which we cannot remove any attributes and still
have the uniqueness constraint in condition hold. This property is not required by a superkey.

Whereas the first property applies to both keys and superkeys, the second property is required only for keys.
Hence, a key is also a superkey but not vice versa.

In general, any superkey formed from a single attribute is also a key. A key with multiple attributes must
require all its attributes together to have the uniqueness property.

A relation schema may have more than one key.In this case, each of the keys is called a candidate key.

For example, the CAR relation has two candidate keys: License_number and Engine_serial_number.
It is common to designate one of the candidate keys as the primary key of the relation.

We use the convention that the attributes that form the primary key of a relation schema are underlined.
Questions:
1. What are the three core features of an RDBMS, and what do each of them represent?
○ The three main features are:
■ Name: Represents the specific name for each relation (table) in the database.
■ Attributes: Refer to each column in a relation.
■ Tuples: Correspond to the rows in the relation1.
2. What are the four types of keys in an RDBMS?
○ The key types are:
■ Primary Key: Uniquely identifies each tuple in a relation.
■ Candidate Key: Alternative keys that could serve as the primary key.
■ Foreign Key: Establishes relationships between tables by referencing primary keys from other tables.
■ Super Key: A set of one or more attributes that uniquely identify tuples1.
3. What is an index, and what are the different types of indexes?
○ Indexes are methods to enhance table performance for faster record retrieval.
○ The three types of indexes are:
■ Unique Index: Ensures uniqueness of values in a column.
■ Clustered Index: Determines the physical order of data rows in a table.
■ Non-Clustered Index: Provides a separate structure for faster data access1.
4. What is a buffer manager, and what are its primary responsibilities?
○ A buffer manager is a module that collects data from storage disks.
○ It determines which data to cache in memory to increase processing speeds1.
5. Explain the relational database model.
○ The relational model defines the relationship among various databases and their connections.
○ It represents how data is stored using tables (relations) with rows (tuples) and columns (attributes)2.
6. What is the difference between a primary key and a unique key?
○ Primary Key: It uniquely identifies each tuple in a relation. There can be only one primary key per table, and it cannot
contain NULL values.
○ Unique Key: It ensures uniqueness but allows NULL values. A table can have multiple unique keys.
7. Explain the concept of referential integrity.
○ Referential integrity ensures that relationships between tables are maintained. When a foreign key references a
primary key, it must match an existing value in the referenced table.
8. What is the purpose of the CHECK constraint?
○ The CHECK constraint restricts the values that can be inserted into a column based on a specified condition. For
example, you can use it to enforce age limits or valid date ranges.
9. Describe the difference between a candidate key and a super key.
○ Candidate Key: It is a minimal super key (i.e., a set of attributes that uniquely identifies tuples). It can be chosen as
the primary key.
○ Super Key: It is any set of attributes that uniquely identifies tuples, including the candidate key.
10. How does normalization improve database design?
○ Normalization reduces data redundancy and ensures efficient storage by organizing data into separate tables. It
minimizes anomalies and maintains data integrity.

1. What is the purpose of the CASCADE option in foreign key constraints?


○ The CASCADE option ensures that changes (such as deletion or update) to the referenced primary key propagate to
related foreign keys. For example, if a referenced record is deleted, CASCADE will delete related records in other
tables.
2. Explain the difference between a one-to-one relationship and a one-to-many relationship in database design.
○ In a one-to-one relationship, each record in one table corresponds to exactly one record in another table.
○ In a one-to-many relationship, each record in one table can be associated with multiple records in another table.
3. What is the purpose of the UNIQUE constraint?
○ The UNIQUE constraint ensures that values in a column are unique across all rows in a table. Unlike the primary key, it
allows NULL values.
4. What is denormalization, and when might you choose to denormalize a database?
○ Denormalization involves intentionally introducing redundancy into a database design to improve query performance.
It’s often done in data warehouses or reporting databases.
5. What is the difference between a natural join and an equijoin?
○ An equijoin is a join based on equality between specified columns.
○ A natural join automatically joins tables based on columns with the same name.
6. Explain the concept of functional dependency in the context of database design.
○ A functional dependency describes the relationship between attributes in a relation. If attribute A determines attribute
B (i.e., knowing A uniquely determines B), we say that A functionally determines B.
7. What is the purpose of the CHECK constraint? Provide an example.
○ The CHECK constraint restricts the range of valid values for a column. For instance, you can use it to enforce that
salaries must be positive.
8. What is a surrogate key, and why might you use one?
○ A surrogate key is an artificial primary key (usually an auto-incremented integer) introduced for efficiency or security
reasons. It has no business meaning but ensures uniqueness.
9. Describe the ACID properties in the context of database transactions.
○ ACID stands for Atomicity, Consistency, Isolation, and Durability. These properties ensure reliable and robust database
transactions.
10. What is a self-join, and when would you use it?
○ A self-join occurs when a table is joined with itself. It’s useful for hierarchical data structures (e.g., an employee
reporting to another employee).

You might also like