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

DBMS 2nd Unit Notes

The relational model, introduced by Edgar Codd, is the dominant data model for database management systems (DBMS) and is characterized by its simple tabular representation of data. It emphasizes data integrity through various constraints, such as primary keys and referential integrity, and utilizes formal query languages like relational algebra and relational calculus for data manipulation. SQL is the primary language used for defining and managing relational databases, encompassing components like Data Definition Language (DDL) for structuring database objects.

Uploaded by

praveench1303
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
4 views

DBMS 2nd Unit Notes

The relational model, introduced by Edgar Codd, is the dominant data model for database management systems (DBMS) and is characterized by its simple tabular representation of data. It emphasizes data integrity through various constraints, such as primary keys and referential integrity, and utilizes formal query languages like relational algebra and relational calculus for data manipulation. SQL is the primary language used for defining and managing relational databases, encompassing components like Data Definition Language (DDL) for structuring database objects.

Uploaded by

praveench1303
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 14

Relational Model: It was introduced by Edgar Codd in early 90's.

Today, the relational model is by far the


dominant data Model and is the foundation for the leading DBMS products, including IBM s
DB2 family, Informix,Oracle, Sybase, Microsoft s Access and SQLServer, FoxBase, and Paradox.

Relational database Systems are ubiquitous in the marketplace and represent a multi-billion dollar industry.
The relational model is very simple and elegant; A database is a collection of one or more relations,
whereeach relation is a table with rows and columns. This simple tabular representation enables even
noviceusers to understand the contents of a database, and it permits the use of simple,
high-level languages to query the data. The major advantages of the relational model over the older data
models are its simple data representation and the ease with which even complex queries can be expressed.

Let use learn the following


How is data represented?
What kinds of integrity constraints can be expressed?
How can data be created and modified?
How can data be manipulated and queried?
How do we obtain a database design in the relational model?
How are logical and physical data independence achieved?
Relation:
A relation in a Database Management System (DBMS) is a fundamental concept that refers to a table
within a relational database. A relation consists of a relation schema and a relation instance.

Relation Schema:
The schema specifies the relation's name, the name of each field (or column, or attribute), and the
domain of each field.
Example:
Consider student data as an entity then the ID no., name, login, age, and gpa with thier type definitions
may can be considered as relation schema of student.

Students(sid: string, name: string, login: string, age: integer, gpa: real)

Relation Instance:
An instance of a relation is a set of tuples, also called records, in which each tuple has the same
numberof 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.
Example:
If we consider the above menctioned example of schema the student then its instance is as follows

Tuple: row or record


Its the synonym for 'row' in that are to be in the instance of a relation.
Example:
For the above student relation one of the truple is:

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. NULL values in SQL are
essential for representing missing or inapplicable data. Understanding their behavior, impact on constraints,
and three-valued logic is crucial for writing accurate queries. By using operators like IS NULL and IS NOT
NULL, we can effectively filter and update NULL values to maintain data integrity.

Typically, it can have one of three interpretations:


Value Unknown: The value exists but is not known.
Value Not Available: The value exists but is intentionally withheld.
Attribute Not Applicable: The value is undefined for a specific record.
Attribute: Column or field
Its the specifer for the field in the relation
Example:
in the above student relation the "sid" is an attribute
What is a Constraint?
Constraints in Database Management Systems (DBMS) are rules or conditions applied to the data within a
database to ensure data integrity, consistency, and adherence to business rules.

Types of Constraints in DBMS

Domain Constraints: Domain constraints specify the permissible values for a given attribute.
Example: A column for student age can only contain numeric values.

Tuple Uniqueness Constraints: This constraint ensures that all rows in a table are unique. No two rows can
have the same values for all columns simultaneously.
Example: A roll number of student must always differ in each row or tuple.

Primary Key Constriant: A primary key must be unique and cannot be null. all the attributes can not be
primary keys. Key constraints are applied to attributes that are considered primary keys.
Example: A roll number of student must be unique and never be null to consider it as primary key.

Entity Integrity Constraints: Entity integrity constraints state that primary key attributes must not
contain null values. This is crucial because primary keys are used to uniquely identify rows in a table.
Example: Age of student must be unique and never be null or zero.

Referential Integrity Constraints:Referential integrity constraints ensure the consistency and accuracy of
data between related tables. This is achieved through the use of primary and foreign keys. A foreign key
inone table must match a primary key in another table.
Example: The depatment id is a foreign key in student table.

Importance of Constraints
Constraints are essential in DBMS as they enforce data integrity, maintain data consistency, and prevent data
anomalies. They ensure that the data within a database adheres to predefined standards and business rules,
guaranteeing the accuracy, reliability, and validity of the data. Constraints can be modified or removed using
appropriate SQL statements or DBMS management tools. However, caution should be exercised to avoid
unintended consequences or data inconsistencies.

Foraml/Decalartive Language: This is the Language that only cares about What to get from the database
without getting into how to get the results. there are two such languages. as follows.

RELATIONAL ALGEBRA
Relational algebra is one of the two "formal query languages" associated with the relational model.
Queries in algebra are composed using a collection of operators. A fundamental property is that
every operator in the algebra accepts (one or two) relation instances as arguments and returns a relation
instance as the result.

We describe the basic operators of the algebra (selection, projection, union, cross-product, and
difference), as well as some additional operators.

Unary Operations: Selection, Projection and Rename


Relational algebra includes operators to select rows from a relation (σ) and to project columns (π).
These operations allow us to manipulate data in a single relation.

Selection (symbol: σ)
The selection operator σ specifies the tuples to retain through a selection condition. If we consider the
above student relation the We can retrieve rows corresponding to student with the operator σ as follows

The subscript age>15 specifies the selection criterion to be applied while retrieving tuples.
Projection (symbol: π)
The projection operator π allows us to extract columns from a relation; for example, we can find
out all students names and age by using π. The following expression shows the considered example.

Rename (symbol: ρ)
Rename is a unary operation used for renaming attributes of a relation. for example, if we want to rename the
'gpa' of the above considered example to 'grade'. the same can be done as follows.

the 'grade/gpa' designates the attribute that is being renamed from gpa to grade.

Binary/Set Operations: Union, Cross-product, intersection and set difference


The set operation name states that these opearations are performed on two entites. The following standard
operations on sets are also available in relational algebra.

Union (symbol: U)
The union operator is used to merge to entities into one entity. i.e., if we consider two entities stundent and
records entities. then we can merge them both into one entity as follows.

Explanation:
if we consider the Students(sid: string, name: string, login: string, age: integer, gpa: real) entity and the
Records(rid:string,sid:string,grade:real). then, the resulting entity is as follows.
student_records(sid:string,name:string,login:string,age:integer,gpa:real,rid:string,grade:real)

Intersection (symbol: ∩)
The insection operator provides the common rows froem the two entities. The following expression
shows this operation.

Explanation:
this is a selection option with reference to exists in both entities records. and hence it returns the
records where primary key of one entity has a coresponding foreign key in the other considered entity.
For example, if the records dose not have tuples with the rids provided in the students entity or the
students entity dose not have the coresponding rid in it that are in the records, such tuples are omited from
displayed view.

Difference/Minus (symbol: - )
The SQL MINUS operator is used to subtract one result set from another, returning only the unique rows .

Explanation:
this is a selection option with reference to not exist in the later entity for the considered two entities
records, and hence it returns the records where primary key of one entity has a coresponding foreign key
in the other considered entity.
For example, if the records have tuples with the rids provided in the students entity such tuples are
omited from displayed view.

Cross product/ Cartesian product (symbol: X )


The Cartesian Product (also known as the Cross Product) is a fundamental operation in relational algebra
and SQL. It combines two relations (tables) to form a new relation, where each tuple (row) from the first
relation is paired with every tuple from the second relation.
STUDENT RECORDS

VARDAN PREM KAVERI

TEJA RAJA RAMESH


UNION
SAMEER SAM MOHITH

YASWANT KEN POOJA

MINUS INTERSECTION

CROSS PRODUCT

STUDENT

VARDAN SUBJECT

TEJA DBMS

SAMEER SE

ASIF
Explanation:
this is a selection option with reference match the existing in the later entity for the considered two
entities and merge the two into one view.
For example, if the records have tuples with the rids provided in the students entity such tuples are
to be joined to the view.

RELATIONAL CALCULUS

What is Relational Calculus?


This is the secound Formal language of the two. guides users on what data is needed without specifying
how to obtain it. Commonly utilized in commercial relational languages like SQL-QBE and QUEL,
relational calculus ensures a focus on desired data without delving into procedural details. It is done in two
ways.

Types of Relational Calculus in DBMS


As said earlier Relational Calculus is of Two Types:
1) Tuple Relational Calculus (TRC)
2) Domain Relational Calculus (DRC)

Tuple Relational Calculus (TRC):


As the name suggests the checking is done on each tuple of the relation with the constraint satisfaction.
Syntax:
{t \| P(t) }
where,
t is the tuple of the relation
and P is the predicate logic or condition placed on t.

Let's take an example of a Customer Database

Example 1: Write a TRC query to get all the data of customers whose zip code is 12345.
TRC Query: {t \| t Customer t.Zipcode = 12345} or TRC Query: {t \| Customer(t) t[Zipcode] = 12345 }
Example 2: Write a TRC query to get the customer id of all the Customers.
TRC query: { t \| s (s ∈ Customer ∧ s.Customer_id = t.customer_id) }

Domain Relational Calculus (DRC):


Domain Relational Calculus uses domain Variables to get the column values required from the database
based on the predicate expression or condition.
Syntax:
{<x1,x2,x3,x4...> \| P(x1,x2,x3,x4...)}

where,
x1,x2,x3,x4 are the column names
and the P is the logic

Example 1: Write a DRC query to get the data of all customers with Zip code 12345.
DRC query: {<x1,x2,x3> \| <x1,x2> Customer x3 = 12345 }
Structured Query Language(S.Q.L.): This is the language used to create and organize a relational database.
this language is composed by five components . We for now will discuse about them briefly.
Data Definition Language (D.D.L.): This is a subset of SQL commands used to define and manage
the structure of database objects such as tables, indexes, views, and schemas. DDL commands are
essential for creating, modifying, and deleting database structures, but they do not manipulate the data within
those structures.

1) Create: used to create the definition of databases, entitites, and views.


Syntax:
create database <database_name> <database_definations>; --- for creating new DB.
create table <table_name> (attribute_1 datatype,..........,attribute_n datatype);---- for creating entity.

create view <view_name> as select <attribute1>,<attribute2>...... from <table_name> where <


conditions>;

Concepts of Domains: Domains act as constraints that enforce rules on the data that is beign managered in the
DB. These are sucha s follows.
1) Datatypes: Defines the type of the attribute.
2) Length: Size of the data for the attribute.
3) Range: Used to specify the starting and ending points within the partition.

Datatypes:

Number_datatypes:
NUMBER [ (precision [, scale ]) ]: Number having precision p and scale s.
The precision p can range from 1 to 38.
The scale s can range from -84 to 127.
Both precision and scale are in decimal digits.
A NUMBER value requires from 1 to 22 bytes.
FLOAT [ (precision) ]: A subtype of the NUMBER data type having precision p.
A FLOAT value is represented internally as NUMBER.
The precision p can range from 1 to 126 binary digits.
A FLOAT value requires from 1 to 22 bytes.
BINARY_FLOAT: 32-bit floating point number. This data type requires 4 bytes.
BINARY_DOUBLE: 64-bit floating point number. This data type requires 8 bytes.

Character_datatypes:
VARCHAR2 (size [ BYTE | CHAR ]):Variable-length character string having maximum length size
bytes or characters.Minimum size is 1 byte or 1 character. 32767 bytes or characters if
MAX_STRING_SIZE = EXTENDED 4000 bytes or characters if
MAX_STRING_SIZE = STANDARD NCHAR [ (size) ]
NVARCHAR2 (size): Variable-length Unicode character string having maximum length size characters.
The number of bytes can be up to two times size for AL16UTF16 encoding and three times
size for UTF8 encoding.
Maximum size is determined by the national character set definition, with an upper limit of:
32767 bytes if MAX_STRING_SIZE = EXTENDED
4000 bytes if MAX_STRING_SIZE = STANDARD
CHAR [(size [BYTE | CHAR])]: Fixed-length character data of length size bytes or characters.
Maximum size is 2000 bytes or characters.Default and minimum size is 1 byte.
BYTE and CHAR have the same semantics as for VARCHAR2.
NCHAR[(size)]: Fixed-length character data of length size characters.
The number of bytes can be up to two times size for AL16UTF16 encoding and three times size for
UTF8 encoding.
Datetime_datatypes:
DATE: Valid date range from January 1, 4712 BC, to December 31, 9999 AD.
The default format is determined explicitly by the NLS_DATE_FORMAT parameter or
implicitly by the NLS_TERRITORY parameter.
The size is fixed at 7 bytes.
This data type contains the datetime fields YEAR, MONTH, DAY, HOUR, MINUTE,
and SECOND.
It does not have fractional seconds or a time zone.
TIMESTAMP [ (fractional_seconds_precision) ] [ WITH [ LOCAL ] TIME ZONE ]: All values of
TIMESTAMP WITH TIME ZONE, with the following exceptions:
Data is normalized to the database time zone when it is stored in the database.
When the data is retrieved, users see the data in the session time zone.
The default format is determined explicitly by the NLS_TIMESTAMP_FORMAT parameter or
implicitly by the NLS_TERRITORY parameter.
The size is 7 or 11 bytes, depending on the precision.
INTERVAL YEAR [ (year_precision) ] TO MONTH: Stores a period of time in years and
months, here year_precision is the number of digits in the YEAR datetime field.
Accepted values are 0 to 9.
The default is 2.
The size is fixed at 5 bytes.
INTERVAL DAY [ (day_precision) ] TO SECOND [ (fractional_seconds_precision) ]: Stores a
period of time in days, hours, minutes, and seconds, where day_precision is the maximum number of
digits in the DAY datetime field.
Accepted values are 0 to 9.
The default is 2.
fractional_seconds_precision is the number of digits in the fractional part of the SECOND
field.
Accepted values are 0 to 9.
The default is 6.
The size is fixed at 11 bytes.

Rowid_datatypes:
ROWID: Base 64 string representing the unique address of a row in its table.
This data type is primarily for values returned by the ROWID pseudocolumn.
UROWID [ (size) ]: Base 64 string representing the logical address of a row of an
index-organized table.
The optional size is the size of a column of type UROWID.
The maximum size and default is 4000 bytes.

Large_object_datatypes:
BLOB: A binary large object. Maximum size is (4 gigabytes - 1) * (database block size).
CLOB: A character large object containing single-byte or multibyte characters.
Both fixed-width and variable-width character sets are supported, both using the database
character set.
Maximum size is (4 gigabytes - 1) * (database block size).
NCLOB: A character large object containing Unicode characters.
Both fixed-width and variable-width character sets are supported, both using the database
national character set.
Maximum size is (4 gigabytes - 1) * (database block size). Stores national character set data.
BFILE: Contains a locator to a large binary file stored outside the database.
Enables byte stream I/O access to external LOBs residing on the database server.
Maximum size is 4 gigabytes.

Long_and_raw_datatypes:
LONG: Character data of variable length up to 2 gigabytes, or 231 -1 bytes.
Provided for backward compatibility.
LONG RAW: Raw binary data of variable length up to 2 gigabytes.
RAW (size): Raw binary data of length size bytes. You must specify size for a RAW value.
Maximum size is:
32767 bytes if MAX_STRING_SIZE = EXTENDED
2000 bytes if MAX_STRING_SIZE = STANDARD

NOTE: every command in SQL must end with ";"

2) Alter: This is a DDL command used to alter or update the definition of an entity.using this command has
multiple purposes. they are as follows:
Add an attibute to the existing schema:
Syntax:
alter table <table_name> add <attribute_name> datatype;

Delete or Remove an attribute from an existing Schema:


Syntax:
alter table <table_name> drop column <attibute_name>;

Note: Oracle does not support deleting multiple columns in a single statement.This command can only
used in non sysdba acounts.
Rename an attribute in existing schema:
Syntax:
alter table <table_name> rename column <attribute_name_old> to <attribute_name_new>;

Redefine an attribute datatype:


Syntax:
alter table <table_name> modify column <column_name> <datatype>; ----for versions 10g of
software.
alter table <table_name> modify <column_name> <datatype>; ------after version 10g of
software.

Note: These operation can be performed on only one attribute at a time.

Rename the table : As the name suggests this command is to update the name of the entity
Syntax:
alter table <old_table_name> rename to <new_table_name>;

Note: the other way to this operation is to directly using the DDL command Rename.

3) Rename: As suggested above this can be used to rename entity. but, it can also rename views and DBs.
Syntax:
rename <old_table_name> to <new_table_name>;
4) Truncate: This command will remove the data permanently. But structure will not be removed.
Syntax:
truncate table <table_name>;

Diferrences between truncate and delete: By using truncate command data will be removed permanently
& will not get back. where as, by using delete command data will be removed temporally & get back
by using roll back command.
By using delete command data will be removed based on the condition. where as, by using truncate
command there is no condition.
Truncate is a DDL command & delete is a DML command.
Note: Never to use this as this can delete only the entity data not the entity itself. use drop instead.

5) Drop: This is used to delete the structure of a relation. It permanently deletes therecords in the table.
Syntax:
drop <table> /<view> <table_name>/<view_name>;

Data Manuplation Language(D.M.L.): This is a subset of SQL commands used to manipulate data within a
database. DML commands allow users to insert, update, delete, and retrieve data from database tables.
These commands are essential for managing and interacting with the data stored in a database.
1) Insert: This command is used to insert the data onto the entity. This has the following syntax.
Syntax:
method 1: single tuple insert.
insert into <table_name> values (value1,value2,......value_n);

method 2: single tuple insert.


insert into <table_name> (attribute_1,attribute_2,.....,attribuite_n) values (value1,value2,......
value_n);

method 3: multiple tuple insertion.


insert into <table_name> (attribute_1,attribute_2,.....,attribuite_n) values (value11,value12,......
value_1n),(value21,value22,......value_2n);

method 4: multiple tuple insertion.


insert all
into <table_name> (attribute_1,attribute_2,.....,attribuite_n) values (value11,......,value_1n)
into <table_name> (attribute_1,attribute_2,.....,attribuite_n) values (value21,......,value_2n)
select 1 from dual;
2) Update: This is used to modify the existing data for an entity.
Syntax:
update <table_name> set <attribute_1>=<value_1>,.........,<attribute_n>=<value_n>;

Note: If we omit the WHERE clause from the update query then all of the rows will get updated.

3) Merge: This command is used to two differernt entities into a single targeted entity.
Syntax:
merge <target_table> using <source_table> on <condition> when matched then <required_operation>
when not matched the <required_operation_2>;
to illistrate this we need two entites as shown in the following illistration.

4) Delete: This is used to clear the data from entity.


Syntax:
method 1: delete all tuples with a constraint
delete from table <table_name> where <condition>;

method 2: delete with out constraint


delete from table <table_name>;

method 3: delete from multiple entites.


begin
delete from <table1>;
delete from <table2>;
commit;
end;
/
Data Query Language (D.Q.L.): This is a subset of SQL commands used to query and retrieve data from a
database. The primary command in DQL is the SELECT statement, which allows users to fetch data from
one or more tables based on specified conditions.these as follows.
1) Select: To display the content of the entity
Syntax:
method 1: display all the data in the entity.
select * from <table_name>;

method 2: diplay all the specified attribute data from the enity.
select <attribute_names> from <table_name>;

method 3: display content based on constriant


select <attribute_names> from <table_name> where <condition>;

2) Description: To display the defination of the entity.


Syntax:
desc <table_name>;

Data Control Language (D.C.L.): is a subset of SQL commands used to control access to data in a
database. DCL commands are primarily used to grant or revoke permissions to users, ensuring that only
authorized users can perform specific actions on the database objects.
1) Grant: This command is used to provide specific privileges to a user or role.
Syntax:
grant <privilege> on <asset_name> to <user_name>;

2) Revoke: This command is used for revoking the privileges granted to a perticular user.
Syntax:
revoke <privilege_name> on <asset_name> from <user_name> ;
The privileges that can be granted for users are as follows.

Transaction Control Language (T.C.L.): These queries in SQL is used to manage transactions within a database.
A transaction is a sequence of operations performed as a single logical unit of work. TCL commands ensure the
integrity of data by allowing users to control the transactions. The primary TCL commands are COMMIT,
ROLLBACK, and SAVEPOINT
1) commit: This command is used to commit the changes made in the db.
Syntax:
commit;

2) Savepoint: This command allows you to set a point within a transaction to which you can later roll back.
Syntax:
savepoint savepoint_name;

3) Rollback: This command is used to undo transactions that have not been committed. It reverts the database to
the state it was in before the transaction began or to a specified savepoint.
Syntax:
rollback to savepoint_name;

You might also like