DBS Chapter Four Part Three
DBS Chapter Four Part Three
Sources of information for physical design process includes logical data model and documentation
that describes the model.
It is important to consider the convention and restriction of the actual database system in which the
database will be created.
Physical data models should be easy to transform into a SQL script that allows us to create the
database structure.
Physical data models describes the base relations, file organizations, and indexes used to achieve
efficient access to the data, and any associated integrity constraints and security measures.
Spaces are not allowed in object names example instead of (date of birth) use date_of_birth
Ensure proper database object names are used ,Names of Databases, tables , attributes , variables must
be different from the DBMS reserved words .
Reserved word a word in a programming language/DBMS which has a fixed meaning and cannot be
redefined by the programmer(Database designer).
SQL Reserved words (https://www.drupal.org/docs/develop/coding-standards/list-of-sql-reserved-
words)
The name of the relation and a list of simple attributes including the corresponding relational Integrity
constraints in brackets represent the relation .
There are many types of Integrity Constraints in DBMS. Constraints that are applied in the data
model is mostly divided into three main categories as :
• Domain Constraints
• Key Constraints
Assigning data types for columns is a mandatory and essential step in designing a table.
A data type is an attribute that specifies the type of data that the object can hold and tells the
DBMS how to interpret its value.
the most popularly used data types integer data, character data, monetary data, date and time data,
binary strings, and so on.
Determining data type restricts the user from entering any unexpected or invalid data.
Assigning an appropriate data type to variable or column will allocate only the required amount
of system memory for the respective column's data, which results efficient use of memory
A table with inappropriate data types can lead to various issues such as improper query
optimization, performance issues, data truncation.
CHECK used to impose Business rules . The CHECK constraint is used to limit the value range that can be
placed in a column.
Note :Business rules : imposes some form of constraint on a specific aspect of the database, such as a condition
for a particular field or the characteristics of a given relationship. Conditions can be written using coloumn names
and operators
DEFAULT a preselected option adopted by a program/ when no alternative is specified.
Checks if the values of two operands are equal or not, if yes then condition becomes true.
=
Checks if the values of two operands are equal or not, if values are not equal then condition becomes true.
!=
Checks if the values of two operands are equal or not, if values are not equal then condition becomes true.
<>
Checks if the value of left operand is greater than the value of right operand, if yes then condition becomes true.
>
Checks if the value of left operand is less than the value of right operand, if yes then condition becomes true.
<
Checks if the value of left operand is greater than or equal to the value of right operand, if yes then condition
>=
becomes true.
Checks if the value of left operand is less than or equal to the value of right operand, if yes then condition becomes
<=
true.
Checks if the value of left operand is not less than the value of right operand, if yes then condition becomes true.
!<
Checks if the value of left operand is not greater than the value of right operand, if yes then condition becomes true.
!>
18 Database Management Systems
Arithmetic Operators
Operator Description
Divides left hand operand by right hand operand and returns remainder.
% (Modulus)
Example
Age int Check (Age>=18 and Age <=100)
Age int Check (Age BETWEEN 18 and 100)
SQL IN Operator
The IN operator is used to compare a value to a list of literal values that have been specified.
The IN operator is a shorthand for multiple OR conditions
Example
Gender Char(1) check (Gender in('M','F'))
• There are wildcards often used in conjunction with the LIKE operator:
Tuple Uniqueness constraint specifies that all the tuples must be necessarily unique in any
relation.
Key Constraints ensures that every tuple in the relation should be unique.
A relation can have multiple keys or candidate keys, one of the keys is selected as a primary
key.
There is no any restriction on choosing the primary key out of candidate keys, but it is
suggested to go with the candidate key with less number of attributes.
Null values are not allowed in the primary key, hence Not Null constraint is also a part of key
constraint.
Primary Key: Adding the primary key constraint automatically includes not null constraint +
unique constraint (+ an index).Ensure keys truly provide uniqueness and stability
• Every foreign key must match a primary key or a unique constraint on another table.
• The sequence of data entry is affected here - you must enter data into the “main” table before it
can be used as a foreign key.
Safe compromises are those trade-offs that do not compromise the integrity and non-
redundancy of the data
• Split one table into multiple tables,
• Information in the file is processed in order, one record after the other.
• When we use write command, it will allocate memory and move the pointer to the end of the file
• There is no restriction on the order of reading and writing for a direct access file.
• The index, like an index in the back of a book, contains the pointer to the various blocks.
• To find a record in the file, we first search the index and then by the help of pointer we access the
file directly.
In this method, records are stored in a sequence, i.e., one after another. And records will be
inserted in the order in which they are inserted into tables.
Sorted File Method
o In this method, the new record is always inserted at the file's end, and then it will sort the
sequence in ascending or descending order.
o Sorting of records is based on any primary key or any other key.
• Hash File Organization uses the computation of hash function on some fields (the key field
)of the records.
• The hash function's output determines the location of disk block where the records are to be
placed.
• In this method, records are stored in the file using the primary key.
• An index value can be generated for each primary key and mapped with the record. This index
contains the address of the record in the file.
• In this method, each record has the address of its data block, searching a record in a huge
database is quick and easy.
• This method supports range retrieval and partial retrieval of records.
• This method requires extra space in the disk to store the index value.
• When the new records are inserted, then these files have to be reconstructed to maintain the
sequence.
• When the record is deleted, then the space used by it needs to be released. Otherwise, the
performance of the database will slow down.
37 Database Management Systems
B+ file organization
• B+ tree file organization is the advanced method of an indexed sequential access method. It uses a tree-like
structure to store records in File.
• is good in storing data for efficient retrieval in a block-oriented storage context—in particular, file systems. Like
in NTFS
• It uses the same concept of key-index where the primary key is used to sort the records. For each primary key, the
value of the index is generated and mapped with the record.
• The B+ tree is similar to a binary search tree (BST), but it can have more than two children.
Cluster file organization
• When the two or more records are stored in the same file, it is known as clusters. These files will have two or
more tables in the same data block, and key attributes which are used to map these tables together are stored only
once.
• This method reduces the cost of searching for various records in different files.
• The cluster file organization is used when there is a frequent need for joining the tables with the same condition.
Note
Some DBMSs may not allow selection of file organizations.
The DBMSs themselves take the optimal file organization strategy
The first column of the database is the search key that contains a copy of the primary key or
candidate key of the table. The values of the primary key are stored in sorted order so that the
corresponding data can be accessed easily.
The second column of the database is the data reference. It contains a set of pointers holding the
address of the disk block where the value of the particular key can be found.
Note:
A block, or physical record, is the smallest amount of data that can be transferred between secondary storage
and primary storage in a single access where as a Logical record is a row in a table
40 Database Management Systems
A table or view can contain Clustered and Non-clustered indexes:
Clustered Index
Clustered indexes sort and store the data rows in the table or view based on their key values( the
columns included in the index definition). This means that the physical order of records on disk
is the same as the order of values in the index
There can be only one clustered index per table, because the data rows themselves can be stored
in only one order.
The only time the data rows in a table are stored in sorted order is when the table contains a
clustered index.
When a table has a clustered index, the table is called a clustered table.
If a table has no clustered index, its data rows are stored in an unordered structure called a heap.
The dense index contains an index record for every search key value in the data file. It makes
searching faster.
In this, the number of records in the index table is same as the number of records in the main
table.
It needs more space to store index record itself. The index records have the search key and a
pointer to the actual record on the disk.
To find a logical data record based on a key value, find the key value in the index, then follow the
pointer to the block & record.
the index has entries only for some of the search key values.
In the data file, index record appears only for a few items. Each item points to a block.
In this, instead of pointing to each record in the main table, the index points to the records in the
1. find the largest key value that is less than or equal to the search value
2. follow the pointer to the block
3. and then follow the pointers inside the file until the logical record is found.
Sparse index takes up less space whereas dense index takes up more space but faster to find a logical
record as do not have to scan logical records when searching for a key value that is not in the index
tblCOURSE(CourseId,CourseTitle,CourseCode,CrdtHrs)
tblENROLLMENT(EnrollId,CourseId,StudentID,DateofEnrollment,MidExResult,ProjectResult,FinalExResult
)
CREATE VIEW VStudentEnroll AS
SELECT tS.StudentID,tS.StuName,tS.StuFname,tS.StuGFName,tS.StuGender,
tC.CourseTitle, tC. CrdtHrs,tE.MidExResult, tE.ProjectResult, tE.FinalExResult,
tE.MidExResult + tE.ProjectResult + tE.FinalExResult as TotalResult
FROM tblENROLLMENT tE
INNER JOIN tblSTUDENT tS ON tE.StudentID = tS.StudentID
INNER JOIN tblCOURSE tC ON tE.CourseId = tC.CourseId
Views can be created by using tables or other views in the current or other databases.
The SQL statements comprising the view are stored in the database and not the resulting data.
Views are used as a security mechanism to mask the underlying base tables and permit user
access only to the view.