0% found this document useful (0 votes)
11 views19 pages

DBMS U2 D

The document provides an overview of the Relational Model (RM) in databases, explaining key concepts such as attributes, tables, tuples, and constraints. It details the importance of NULL values, various integrity constraints, and introduces relational algebra with its basic and extended operators. Additionally, it discusses SQL commands and the structure of database schemas.
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)
11 views19 pages

DBMS U2 D

The document provides an overview of the Relational Model (RM) in databases, explaining key concepts such as attributes, tables, tuples, and constraints. It details the importance of NULL values, various integrity constraints, and introduces relational algebra with its basic and extended operators. Additionally, it discusses SQL commands and the structure of database schemas.
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

lOMoARcPSD|61770107

UNIT - 2
Introduction to the Relational Model
Relational Model (RM) represents the database as a collection of relations. A relation is nothing
but a table of values. Every row in the table represents a collection of related data values. These
rows in the table denote a real-world entity or relationship. The data are represented as a set of
relations. In the relational model, data are stored as tables. However, the physical storage of
the data is independent of the way the data are logically organized.

Relational Model Concepts in DBMS


1. Attribute: Each column in a Table. Attributes are the properties which define a relation.
e.g., Student_Rollno, NAME,etc.
2. Tables – In the Relational model the, relations are saved in the table format. It is stored
along with its entities. A table has two properties rows and columns. Rows represent
records and columns represent attributes.
3. Tuple – It is nothing but a single row of a table, which contains a single record.
4. Relation Schema: A relation schema represents the name of the relation with its
attributes.
5. Degree: The total number of attributes which in the relation is called the degree of the
relation.
6. Cardinality: Total number of rows present in the Table.
7. Column: The column represents the set of values for a specific attribute.
8. Relation instance – Relation instance is a finite set of tuples in the RDBMS system.
Relation instances never have duplicate tuples.
9. Concept Of Domain- The domain of a database is the set of all allowable values (or)
attributes of the database. Ex: Gender (Male, Female, Others).
10. Distinct Tuples: Each relation must be specified using distinct tuples, which can be in
any order. This is the requirement of the relational model.

Downloaded by Karridivya 26 (karridivya32@[Link])


lOMoARcPSD|61770107

Importance of NULL Value


It is essential to understand that a NULL value differs from a zero or an empty string.
A NULL value represents missing or undefined data. Since it is often not possible to
determine which interpretation applies, SQL treats all NULL values as distinct and does not
distinguish between them. Typically, it can have one of three interpretations:
1. Value Unknown: The value exists but is not known.
2. Value Not Available: The value exists but is intentionally withheld.
3. Attribute Not Applicable: The value is undefined for a specific record.
Principles of NULL values
• Setting a NULL value is appropriate when the actual value is unknown, or when a
value is not meaningful.
• A NULL value is not equivalent to a value of ZERO if the data type is a number and is
not equivalent to spaces if the data type is a character.
• A NULL value can be inserted into columns of any data type.

Example
CREATE TABLE Studnt (SId INT PRIMARY KEY, FirstName VARCHAR(15) NOT NULL,
LastName VARCHAR(15), Email VARCHAR(20) UNIQUE, DOB DATE, Course CHAR(7),
Age INT CHECK(Age>=18), Branch VARCHAR(10) DEFAULT 'DS');
INSERT INTO Studnt (SId, FirstName, LastName, Email, DOB, Course, Age, Branch)
VALUES(1,'Dennis', 'Ritchie ', 'DennisRe@[Link]', DATE '2002-03-15', 'BTECH', 21,
'AI');
INSERT INTO Studnt VALUES (2,'Ken', 'Thompson ', NULL, DATE '2003-04-16', 'BTECH',
22, 'ML');
INSERT INTO Studnt VALUES (3,'James', 'Gosling', NULL, DATE '2004-05-17', 'BTECH',
19, 'DS');

Constraints
In DBMS, constraints refer to limitations placed on data or data processes. This indicates that
only a particular type of data may be entered into the database or that only a particular sort of
operation can be performed on the data inside. Constraints thereby guarantee data accuracy in
a database management system (DBMS).

Downloaded by Karridivya 26 (karridivya32@[Link])


lOMoARcPSD|61770107

Domain Constraints
1. Every domain must contain atomic values(smallest indivisible units) which means
composite and multi-valued attributes are not allowed.
2. We perform a datatype check here, which means when we assign a data type to a column
we limit the values that it can contain.

If we assign the datatype of attribute age as int, we can’t give it values other than int
datatype.
Entity integrity constraints
• The entity integrity constraint states that primary key value can't be null.
• This is because the primary key value is used to identify individual rows in relation and
if the primary key has a null value, then we can't identify those rows.
• A table can contain a null value other than the primary key field.

Downloaded by Karridivya 26 (karridivya32@[Link])


lOMoARcPSD|61770107

Referential Integrity Constraint


Referential Integrity Constraint ensures that there must always exist a valid relationship
between two relational database tables. This valid relationship between the two tables confirms
that a foreign key exists in a table. It should always reference a corresponding value or attribute
in the other table or be null. In the Referential integrity constraints, if a foreign key in Table 1
refers to the Primary Key of Table 2, then every value of the Foreign Key in Table 1 must be
null or be available in Table 2.

Key constraints
Keys are the entity set that is used to identify an entity within its entity set uniquely. tables. An
entity set can have multiple keys, but out of which one key will be the primary key. A primary
key can contain a unique and null value in the relational table.

NOT NULL:
NOT NULL constraint makes sure that a column does not hold NULL value. When we don’t
provide value for a particular column while inserting a record into a table, it takes NULL value
by default. By specifying NULL constraint, we can be sure that a particular column(s) cannot
have NULL values.
UNIQUE: UNIQUE Constraint enforces a column or set of columns to have unique values. If
a column has a unique constraint, it means that particular column cannot have duplicate values
in a table.
DEFAULT: The DEFAULT constraint provides a default value to a column when there is no
value provided while inserting a record into a table.

Downloaded by Karridivya 26 (karridivya32@[Link])


lOMoARcPSD|61770107

CHECK: This constraint is used for specifying range of values for a particular column of a
table. When this constraint is being set on a column, it ensures that the specified column must
have the value falling in the specified range.
Relational Algebra
Relational Algebra is a procedural query language. Relational algebra mainly provides a
theoretical foundation for relational databases and SQL. The main purpose of using Relational
Algebra is to define operators that transform one or more input relations into an output relation.
Given that these operators accept relations as input and produce relations as output, they can
be combined and used to express potentially complex queries that transform potentially many
input relations (whose data are stored in the database) into a single output relation (the query
results). As it is pure mathematics, English Keywords are not used in Relational Algebra, and
operators are represented using symbols.

BASIC OPERATORS
Unary Operators
SELECT (σ): The SELECT operation is used for selecting a subset of the tuples according to
a given selection condition. Sigma(σ)Symbol denotes it. It is used as an expression to choose
tuples which meet the selection condition. Select operation selects tuples that satisfy a given
predicate. It is denoted by sigma (σ).

Notation: σ p(r)
Where: σ is used for selection prediction r is used for relation p is used as a propositional logic
formula which may use connectors like: AND OR and NOT. These relational can use as
relational operators like =, ≠, <, >, ≥, ≤.

Downloaded by Karridivya 26 (karridivya32@[Link])


lOMoARcPSD|61770107

Projection(π)
The projection eliminates all attributes of the input relation but those mentioned in the
projection list. The projection method defines a relation that contains a vertical subset of
Relation. This helps to extract the values of specified attributes to eliminates duplicate values.
(pi) The symbol used to choose attributes from a relation. This operation helps you to keep
specific columns from a relation and discards the other columns.
Notation: ∏ A1, A2, An (r) Where A1, A2, A3 is used as an attribute name of relation r.

Rename (ρ): Rename (ρ) operation can be used to rename a relation or an attribute of a
relation. Rename (ρ)
Syntax: ρ(new_relation_name, old_relation_name)
Rename (ρ) Example: Lets say we have a table customer, we are fetching customer names
and we are renaming the resulted relation to CUST_NAMES.
It is denoted by rho (ρ).

Downloaded by Karridivya 26 (karridivya32@[Link])


lOMoARcPSD|61770107

Example: We can use the rename operator to rename STUDENT relation to STUDENT1.
ρ(STUDENT1, STUDENT)

Binary Operators
Cartesian product (X): Cartesian Product is denoted by X symbol. Lets say we have two
relations R1 and R2 then the cartesian product of these two relations (R1 X R2) would combine
each tuple of first relation R1 with the each tuple of second relation R2.
Syntax of Cartesian product (X) R1 X R2
Example

Query: Lets find the cartesian product of table R and S.


RXS

Union operation (U)


UNION is symbolized by ∪ symbol. It includes all tuples that are in tables A or in B. It also
eliminates duplicate tuples. So, set A UNION set B would be expressed as:
Notation: The result <- A ∪ B
R and S must have the attribute of the same number. Duplicate tuples are eliminated
automatically.
For a union operation to be valid, the following conditions must hold –
• R and S must be the same number of attributes.
• Attribute domains need to be compatible.

Downloaded by Karridivya 26 (karridivya32@[Link])


lOMoARcPSD|61770107

• Duplicate tuples should be automatically removed.

π (Name, Age)X1 U π(Name, Age)X2

π (Name, Age)X1 U π(Name, Age)X2


Set Difference (-) –
Symbol denotes it. The result of A - B, is a relation which includes all tuples that are in A but
not in B.
• The attribute name of A has to match with the attribute name in B.
• The two-operand relations A and B should be either compatible or Union compatible.
• It should be defined relation consisting of the tuples that are in relation A, but not in
B.

Downloaded by Karridivya 26 (karridivya32@[Link])


lOMoARcPSD|61770107

EXTENDED/DERIVED OPERATORS
Intersection Operator (∩)
Intersection operator is denoted by ∩ symbol and it is used to select common rows (tuples)
from two tables (relations). Lets say we have two relations R1 and R2 both have same columns
and we want to select all those tuples(rows) that are present in both the relations, then in that
case we can apply intersection operation on these two relations R1 ∩ R2.
Syntax of Intersection Operator: (∩)- table_name1 ∩ table_name2

Division Operation
The Division Operation is a binary operation in relational algebra that answers “for all” type
queries. It finds tuples in one relation that are associated with all tuples in another relation.
R÷S

Downloaded by Karridivya 26 (karridivya32@[Link])


lOMoARcPSD|61770107

Join Operations: The join operation is one of the most useful operations in relational algebra
and is the most commonly used way to combine information from two or more relations.
Although a join can be defined as a cross-product followed by selections and projections, joins
arise much more frequently in practice than plain cross-products.
• Inner joins
• Outer joins
Inner Join: An inner join is the widely used join operation and can be considered as a default
join-type. The inner JOIN is used to return rows from both tables which satisfy the given
condition.
Inner Join further divided into three subtypes:
• Theta join
• Natural join
• EQUI join
Theta Join: Theta Join allows you to merge two tables based on the condition represented by
theta. Theta joins work for all comparison operators. The general case of JOIN operation is
called a Theta join. It is denoted by symbol θ
Syntax: A ⋈θ B, θ is the condition for join
Theta join can use any conditions in the selection criteria.

EQUI join: When a theta join uses only equivalence condition, it becomes an equi join.

Downloaded by Karridivya 26 (karridivya32@[Link])


lOMoARcPSD|61770107

Equi join
When Theta join uses only equality comparison operator, it is said to be equijoin.

STUDENT ⋈[Link] = [Link] SUBJECT

Natural Join (⋈): Natural join does not utilize any of the comparison operators. In this type
of join, the attributes should have the same name and domain. In this type of join, there should
be at least one common attribute between two relations. It performs selection forming equality
on those attributes which appear in both relations and eliminates the duplicate attributes.

Downloaded by Karridivya 26 (karridivya32@[Link])


lOMoARcPSD|61770107

Outer Join: An outer join doesn't require each record in the two join tables to have a matching
record. In this type of join, the table retains each record even if no other matching record exists.
Three types of Outer Joins are:
• Left Outer Join
• Right Outer Join
• Full Outer Join

Left Outer Join : The LEFT JOIN returns all the rows from the table on the left even
if no matching rows have been found in the table on the right. Where no matching record found
in the table on the right, NULL is returned.

Right Outer Join ( ): RIGHT outer JOIN is the opposite of LEFT JOIN. The RIGHT
JOIN returns all the columns from the table on the right even if no matching rows have been

Downloaded by Karridivya 26 (karridivya32@[Link])


lOMoARcPSD|61770107

found in the table on the left. Where no matches have been found in the table on the left, NULL
is returned.

Full Outer Join ( ): In a full outer join, all tuples from both relations are included in the
result, irrespective of the matching condition.

Simple database Schema:


• A database schema is a structure that represents the logical storage of the data in the
database.
• It represents the organization of data and provides information about the relationships
between the tables in a given database.
• A database schema is the logical representation of a database, which shows how the
data is stored logically in the entire database.
• It contains list of attributes and instruction that informs the database engine that how
the data is organized and how the elements are related to each other.
• A database schema contains schema objects that may include tables, fields, packages,
views, relationship, primary key, foreign key.
• In actual, the data is physically stored in files that may be in unstructured form, but to
retrieve it and use it, we need to keep them in a structured manner. To do this a database
schema is used. It provides knowledge about how the data is organized in a database
and how it is associated with other data.
A database schema object includes the following:
• Consistent formatting for all data entries.
• Database objects and unique keys for all data entries.
• Tables with multiple columns, and each column contains its names and datatypes.

Downloaded by Karridivya 26 (karridivya32@[Link])


lOMoARcPSD|61770107

SQL Commands:
SQL commands are categorized into three types.

1. Data Definition Language (DDL): used to create (define) a table.

2. Data Manipulation Language (DML): used to update, store and retrieve


data from tables.

3. Data Control Language (DCL): used to control the access of database


created using DDL and DML.

SQL DATATYPES :
SQL data type is used to define the values that a column can contain. Every column is
required to have a name and data type in the database table.
DATA TYPES OF SQL:
SQL DATA
TYPES

Binary data type Numeric data Extract numeric String data type Date data type
type data type

1. BINARY DATATYPES:
There are three types of binary data types which are given below

DATA TYPE DESCRIPTION


Binary It has a maximum length of 800 bytes. It contains a
fixed- length binary data

Downloaded by Karridivya 26 (karridivya32@[Link])


lOMoARcPSD|61770107

Var binary It has a maximum length of 800 bytes. It contains a


variable - length binary data
Image It has a maximum length of 2,147,483,647 bytes. It
contains a variable - length binary data

2. NUMERIC DATATYPE:
DATA TYPE FROM TO DESCRIPTION

Float -1.79 E 1.79 E It is used to specify a floating-point


+308 +308 value.
Ex: 6.2, 2.9 etc
Real -3.40 E 3.40 E It specifies a single precision floating
+38 +38 point number.

3. EXACT NUMERIC DATA TYPE:


DATA TYPE DESCCRIPTION

Int It is used to specify an integer value


Small int It is used to specify small integer value
Bit It has the number of bits to store
Decimal It specifies a numeric value that can have a decimal
number
Numeric It is used to specify a numeric value

4. DATE AND TIME DATATYPES:


DATA TYPE DESCRIPTION
Date It is used to store the year, month, and days value
Time It is used to store the hour, minute, and seconds value
Time stamp It stores the year, month, hour, minute, and the second
value

5. STRING DATATYPE:
DATA TYPE DESCRIPTION
Char It has a maximum length of 8000 characters. It contains fixed-length
non-Unicode characters.
Varchar It has a maximum length of 8000 characters. It contains variable-
length non-Unicode characters.
Text It has a maximum length of 2,147,483,647 characters. It contains
variable length non-Unicode characters.

Downloaded by Karridivya 26 (karridivya32@[Link])


lOMoARcPSD|61770107

TABLE DEFINITIONS: (CREATE, ALTER)


SQL TABLE: SQL table is a collection of data which is organized in terms of rows and
columns.
• In DBMS, the table is known as relation and row as a tuple
• Let’s see an example of the “EMPLOYEE “table
EMP_ID EMP_NAME CITY PHONE_ID
1 Kristen Washington 7289201223
2 Anna Franklin 9378282882
3 Jackson California 9264783838
4 Daniel Hawaii 9638482678
• In the above table, “EMPLOYEE” is the table name, “EMP_ID,
“EMP_NAME”, “CITY”,” PHONE-NO” are the column names.
• The combination of data of multiple columns forms a row
Eg: 1, “Kristen”, “Washington” and “7289201223 “are the data of one row
OPERATIONS ON TABLE:
1. Create table
2. Alter table
3. Drop table

[Link] table: SQL create table is used to create a table in the database. To define the
table, you should define the name of the table and also define its column and column‟s
data type.

SYNTAX:

Create table table_name (“column1” “datatype”,

“column2” “datatype”,

“column3” “datatype”,

….

“column N” “datatype”);

EXAMPLE:

SQL > create table employee (emp_id int, emp_name varchar (25), phone_no int,
address char (30));

• If you create the table successfully, you can verify the table by looking at the
message by the sql server. else you can use DESC command as follows
SQL > DESC employee;
FIELD TYPE NULL DEFAULT EXTRA

Downloaded by Karridivya 26 (karridivya32@[Link])


lOMoARcPSD|61770107

Emp_id Int (11) No NULL


Emp_name Varchar (25) No NULL
Phone_no No Int (11) NULL
address yes NULL Char(30)

2. ALTER TABLE:

• The alter table command adds, delete or modifies columns in a table


• The alter table command also adds and deletes various constraints in a table
• The following SQL adds an “EMAIL” column to the “EMPLOYEE “table
SYNTAX:

ALTER table table_name add column1 datatype;


EXAMPLE:
ALTER table employee add email varchar (255);

SQL > DESC employee;


FIELD TYPE NULL DEFAULT EXTRA
Emp_id Int (11) No NULL
Emp_name Varchar (25) No NULL
Phone_no No Int (11) NULL
Address Yes NULL Char (30)
Email Varchar (255) NULL

3. DROP TABLE:
• The drop table command deletes a table in the data base
• The following example SQL deletes the table “EMPLOYEE”
SYNTAX:

DROP table table_name;

EXAMPLE:

DROP table employee;

• Dropping a table results in loss of all information stored in the table.


Different DML Operations (insert, delete, update):
➢ DML-Data Manipulation Language.
➢ Data Manipulation Commands are used to manipulate data to the database.
➢ Some of the data manipulation commands are
1. Insert
2. Update
3. Delete

Downloaded by Karridivya 26 (karridivya32@[Link])


lOMoARcPSD|61770107

Insert:
SQL insert statement is a sql query. It is used to insert a single multiple records in a
table.

Syntax:
Insert into table name values (value 1, value 2, value 3);

Let’s take an example of table which has 3 records within it.


• insert into student values (“alekhya”,501, ‟hyderabad”);
• insert into student values (“deepti”,502, ‟guntur”);
• insert into student values (“ramya”,503, ‟nellore”);
The following table will be as follows:
NAME ID CITY
Alekhya 501 Hyderabad
Deepti 502 Guntur
Ramya 503 Nellore
Update:
• The SQL Commands update is used to modify the data that is already in the database.
• SQL Update statement is used to change the data of records held by tables which rows
is to be update, it is decided by condition to specify condition, we use “WHERE”
clause.
• The update statement can be written in following form:
Syntax:
Update table_name set column_name=expression where condition;
Example:

Let’s take an example: here we are going to update an entry in the table.
Update students set name=‟rasi‟ where id=503;

After update the table is as follows:


NAME ID CITY
Alekhya 501 Hyderabad
Deepti 502 Guntur
Rasi 503 Nellore

3. Delete: The SQL delete statement is used to delete rows from a table.
➢ Generally, delete statement removes one or more records from a table.

Syntax:
delete from table_name [where condition];

Example:

Let us take a table named “student” table


Delete from students where id=501;

Downloaded by Karridivya 26 (karridivya32@[Link])


lOMoARcPSD|61770107

Resulting table after the query:


NAME ID CITY
Deepti 502 Guntur
Rasi 503 Nellore

Downloaded by Karridivya 26 (karridivya32@[Link])

You might also like