0% found this document useful (0 votes)
12 views53 pages

DBS Chapter Four Part Three

Uploaded by

Mieraf Tadesse
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)
12 views53 pages

DBS Chapter Four Part Three

Uploaded by

Mieraf Tadesse
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/ 53

Chapter Four

Database Application Development

Physical Database Design


For MS_SQL Server

1 Database Management Systems


Physical Data Model
 A physical data model is usually derived from a logical data model for a particular relational database
management system (RDBMS), thus taking into account all technology-specific details.
 It is the actual design blueprint of a relational database.

 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.

2 Database Management Systems


Translating the logical Data model into the physical Data
Model(for target DBMS)
 Once the tables are normalized, the logical model needs to be converted to the physical model by
choosing a target DBMS .
 To identify database objects (Database ,Relation/Table and Columns) they should be named.

 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 .

3 Database Management Systems


Add Relational Integrity constraints and Business rules
 Relational Integrity constraints in DBMS are referred to conditions which must be present for a
valid relation. These Relational constraints in DBMS are derived from the rules in the mini-world
that the database represents.
 Relational constraints are the restrictions imposed on the database contents and operations.

 They ensure the correctness of data in the database.

 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

• Referential Integrity Constraints

4 Database Management Systems


Domain Constraints
A domain is a unique set of values permitted for an attribute in a table.
 Domain constraint defines the domain or set of values for an attribute.
 Domain is specified as data types which include standard data types (integers, real numbers,
characters, Booleans, variable length strings , etc) and user defined data types including constraints
(NOT NULL / UNIQUE / CHECK / DEFAULT)
 Domain constraint specifies that the value taken by the attribute must be an atomic value from its
domain. This means within each tuple(Record), the value of each attribute must be atomic and
from the specified domain .
 Make sure the column types are supported by the DBMS
 Domain constraints can be violated if an attribute value is not appearing in the corresponding
domain or it is not of the appropriate data type.
 Domain constraints are the most elementary form of integrity constraint. They are tested easily by
the system whenever a new data item is entered into the database

5 Database Management Systems


 Make sure domain constraints are assigned ( data type, length, null ability, etc

 In a physical database, each column must be assigned a data type.

 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.

6 Database Management Systems


SQL Data types
 Not all data types are supported by every relational database vendor and every relational database
vendor has its own maximum size limit for different data types
 Certain data types require a maximum length to be specified.
 In SQL Server, each column, local variable, expression, and parameter has a related data type.
 A data type is an attribute that specifies the type of data that the object can hold
 data type refers to the type of value a variable has and what type of mathematical, relational or logical
operations can be applied
 the most popularly used data types integer data, character data, monetary data, date and time data,
binary strings, and so on.
 Data types in SQL Server are organized into the following categories:
• Numeric ( Exact numeric , Approximate numeric)
• Date and time
• Character strings
• Unicode character strings
• Binary strings , Other data types

7 Database Management Systems


8 Database Management Systems
9 Database Management Systems
10 Database Management Systems
timestamp Stores a unique number that gets updated every time a row gets created or modified. The
timestamp value is based upon an internal clock and does not correspond to real time. Each
table may have only one timestamp variable

11 Database Management Systems


12 Database Management Systems
13 Database Management Systems
 In SQL Server, based on their storage characteristics, some data types are designated as:
• Large value data types: varchar(max), and nvarchar(max)
• Large object data types: text, ntext, image, varbinary(max), and xml

14 Database Management Systems


15 Database Management Systems
NULL
 A NULL value represents the absence of a value for a record in a field ,
 Null has no bounds, it can be used for string, integer, date, etc. fields in a database.
 A field with a NULL value is a field with no value. NULL value is different from a zero value or a
field that contains spaces.
 An empty value is a "field-formatted" value with no significant data in it.
 NULL isn't allocated any memory, the string with Null value is just a pointer which is pointing to
nowhere in memory however, empty is allocated to a memory location, although the value stored in
the memory is "".
 If a field has no value, use Null, not an empty string.
 Null is the database's determination of an absence of a value logically.

16 Database Management Systems


 NOT NULL

• The field is never allowed to be empty.

• Data must be entered at the time the row is created.


Note : Keep in mind that if you specify “Not Null” as a constraint, you cannot save the row of data if that
column is blank.
 Unique: No duplicate data across rows.

 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.

CUSTOMER(CustomerNumber int NOT NULL UNIQUE CHECK(CustomerNumber>0) ,


CustName varchar(25) NOT NULL,
Country varchar(20) DEFAULT ‘Ethiopia‘)

17 Database Management Systems


Comparison Operators
Operator Description

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

+ (Addition) Adds values on either side of the operator.

Subtracts right hand operand from left hand operand.


- (Subtraction)

Multiplies values on either side of the operator.


* (Multiplication)

/ (Division) Divides left hand operand by right hand operand.

Divides left hand operand by right hand operand and returns remainder.
% (Modulus)

19 Database Management Systems


Logical Operators
Sr.No. Operator & Description
ALL
1
The ALL operator is used to compare a value to all values in another value set.
AND
2 The AND operator allows the existence of multiple conditions in an SQL statement's WHERE clause.
ANY
3
The ANY operator is used to compare a value to any applicable value in the list as per the condition.
BETWEEN
4
The BETWEEN operator is used to search for values that are within a set of values, given the minimum value and the maximum value.
EXISTS
5 The EXISTS operator is used to search for the presence of a row in a specified table that meets a certain criterion.
IN
6 The IN operator is used to compare a value to a list of literal values that have been specified.
LIKE
7 The LIKE operator is used to compare a value to similar values using wildcard operators.
NOT
8 The NOT operator reverses the meaning of the logical operator with which it is used. Eg: NOT EXISTS, NOT BETWEEN, NOT IN, etc.
This is a negate operator.
OR
9
The OR operator is used to combine multiple conditions in an SQL statement's WHERE clause.
IS NULL
10
The NULL operator is used to compare a value with a NULL value.
UNIQUE
11 The UNIQUE operator searches every row of a specified table for uniqueness (no duplicates).
20 Database Management Systems
SQL BETWEEN Operator
• The BETWEEN operator selects values within a given range. The values can be numbers, text, or
dates.
• The BETWEEN operator is inclusive: begin and end values are included.

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'))

21 Database Management Systems


SQL LIKE Operator
• The LIKE operator is used in a condition to search for a specified pattern in a column.

• There are wildcards often used in conjunction with the LIKE operator:

 The percent sign (%) represents zero, one, or multiple characters


 The underscore sign (_) represents one, single character
 [] (Square Brackets): Represents any single character within brackets.
– (Hyphen): Specify a range of characters inside brackets.
Example
StuEmail VARCHAR(30) NOT NULL UNIQUE CHECK(StuEmail Like '%_@__%.__%')

22 Database Management Systems


Key Constraints
 Key Constraints are also called uniqueness constraints

 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

23 Database Management Systems


Surrogate key is an automatically generated unique identifier that does not have any business or
contextual meaning.
IDENTITY
 Identity column of a table is a column whose value increases automatically. The value in an identity
column is created by the server. A user generally cannot insert a value into an identity column.
Identity column can be used to uniquely identify the rows in the table.
IDENTITY [( seed, increment)] Where
 Seed: Starting value of a column. Default value is 1.
 Increment: Incremental value that is added to the identity value of the previous row that was
loaded. The default value 1
Example EmpId INT PRIMARY KEY IDENTITY(1,1)
Sequence
A sequence is a user-defined schema bound object that generates a sequence of numeric values
according to the specification with which the sequence was created.
Sequences, unlike identity columns, are not associated with specific tables.

24 Database Management Systems


Globally Unique Identifiers, or GUIDs,
 GUID is a 16 byte binary SQL Server data type that is globally unique across tables,
databases, and servers values and are represented by unique identifiers.
 The term GUID stands for Globally Unique Identifier and it is used interchangeably with
UNIQUEIDENTIFIER.
 To create a GUID in SQL Server, the NEWID() function is used
Example
ID uniqueidentifier PRIMARY KEY DEFAULT NEWID()

25 Database Management Systems


Referential Integrity Constraints :
 The Referential integrity constraints is specified between two relations or tables and
used to maintain the consistency among the tuples in two relations.
 This constraint is enforced when a foreign key references the primary key of another
relation or itself(for recursive relation).
 It specifies that all the values taken by the foreign key must either be available in the
referenced relation or be null, but it can’t be empty. It means the reference from a row
in one table to another table must be valid.
 Referential Integrity constraints are enforced to ensure that there are no orphan
records in relations .
 An orphan record is a child row whose foreign key FK value is not found in the
corresponding parent relation where the PK is located.

26 Database Management Systems


 Additional foreign key rules may be added when setting referential integrity, such as what to do
with the child rows when the record with the PK, part of the parent , is deleted or changed
(updated).
 Most relational databases have declarative referential integrity. In other words, when the tables
are created the referential integrity constraints are set up.
 Referential integrity : using foreign key constraint enforces referential integrity.

• 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.

27 Database Management Systems


Applying necessary compromises /Trade-Offs

 Apply safe compromises Trade-Offs to the model,

Safe compromises are those trade-offs that do not compromise the integrity and non-
redundancy of the data
• Split one table into multiple tables,

• Merge multiple tables into a single table,

 Apply aggressive compromises/trade offs


aggressive compromises are those trade-offs that compromise the integrity of the model, such
as adding redundant data.
 Storing derived data (If needed frequently- store it , If needed rarely- calculate it)

• Adding redundant data or relationships and

• Adding surrogate keys.

28 Database Management Systems


Relational Model documentation

29 Database Management Systems


Physical Model documentation

30 Database Management Systems


31 Database Management Systems
File Organization and Indexes
A database consists of a large amount of data. The data is grouped in a table of the RDBMS and
data records are assigned to each table.
A user can see that the data is stored in tabular form, but in reality this large amount of data is stored
in the form of files in physical memory.
File organization :
 The logical relationship among various records that constitute the file, particularly with
respect to the means of identification and access to any specific record
 Storing the files in certain order is called file organization
 The physical arrangement of data in a file into blocks and pages on secondary storage
 defines how file records are mapped onto disk blocks.
 is used to describe the way in which the records are stored in terms of blocks, and the
blocks are placed on the storage medium.
Note
Page , block or physical record : the unit of transfer between disk and primary storage and vice
versa

32 Database Management Systems


Access methods
When a file is used, information is read and accessed into computer memory and there are several
ways to access this information of the file.
the steps involved in storing and retrieving records from a file
There are three ways to access a file into a computer system: Sequential-Access, Direct Access, Index
sequential Method.
 Sequential Access

• Information in the file is processed in order, one record after the other.

• Data is accessed one record right after another record in an order.

• When we use read command, it move ahead pointer by one

• When we use write command, it will allocate memory and move the pointer to the end of the file

33 Database Management Systems


Direct Access
• A filed-length logical record that allows the program to read and write record rapidly, in no
particular order.
• The direct access is based on the disk model of a file since disk allows random access to any file
block.
• For direct access, the file is viewed as a numbered sequence of block or record

• There is no restriction on the order of reading and writing for a direct access file.

Index sequential method


• It is the other method of accessing a file which is built on the top of the sequential access method.

• These methods construct an index for the 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.

34 Database Management Systems


Types of file organization
Sequential file organization
 files are stored sequentially.
Pile File Method

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.

o Is a fast and efficient method for the huge amount of data.

o This method is mostly used for report generation or statistical calculations.

35 Database Management Systems


 Heap file organization/unordered

• It works with data blocks.


• Records are placed on disk in no particular order.
• In heap file organization, the records are inserted at the file's end.
• When the records are inserted, it doesn't require the sorting and ordering of records.
• It is a very good method of file organization for bulk insertion.

 Hash file organization

• 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.

36 Database Management Systems


 Indexed sequential access method (ISAM)
• ISAM method is an advanced sequential file organization.

• 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

38 Database Management Systems


Indexes
 An index is an on-disk structure associated with a table or view that speeds retrieval of rows from
the table or view.
 An index contains keys built from one or more columns in the table or view. These keys are
stored in a structure (B-tree) that enables SQL Server to find the row or rows associated with the
key values quickly and efficiently.
 Indexing is used to optimize the performance of a database by minimizing the number of disk
accesses required when a query is processed.
 An index is a structured collection of key value & address pairs. The purpose of an index is to
facilitate access to a collection of records.

39 Database Management Systems


 The index is a type of data structure. It is used to locate and access the data in a database table
quickly.
 Indexes can be created using some database columns.

 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.

41 Database Management Systems


Guidelines for choosing the clustering index:
 a column that is often searched for a range of values – because the logical records are likely to be in the
same block.
 column with the Unique and Not NULL values as a useful index key candidate
 a column that is used frequently to sort the data retrieved from a table, then that column is a candidate
for a clustering index.
 a primary key or other unique column – because it makes queries that need to find a specific value in
the column very fast.
 attribute that is used most often for join operations - this makes join operation more efficient
Non-clustered Index
 Non-clustered indexes have a structure separate from the data rows. It means the ordering key for the
physical records on disk is not the same as the search key in the index
 A non-clustered index contains the non-clustered index key values and each key value entry has a
pointer to the data row that contains the key value.
 The pointer from an index row in a non-clustered index to a data row is called a row locator.

42 Database Management Systems


Indexes are either Dense index or Sparse
Dense index:
 Every search key value has an entry in the index.

 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.

43 Database Management Systems


Sparse index

 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

main table in a gap.


 To find a logical data record based on a key value:

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

44 Database Management Systems


Clustering Index
 A clustered index can be defined as an ordered data file. Sometimes the index is created on non-
primary key columns which may not be unique for each record.
 In this case, to identify the record faster, we will group two or more columns to get the unique
value and create index out of them. This method is called a clustering index.
 The records which have similar characteristics are grouped, and indexes are created for these
group.

45 Database Management Systems


 Create a clustered index before building non-clustered indexes.
 Both clustered and non-clustered indexes can be unique means no two rows can have the same value
for the index key. Unless indicated in the definition multiple rows can share the same key value.
 Indexes are automatically created when PRIMARY KEY and UNIQUE constraints are defined on table
columns.
 If a column is configured to be a PRIMARY KEY, Database Engine automatically creates a unique
clustered index, unless a clustered index already exists or creates a non clustered index otherwise.
 When a column in a table is created with a UNIQUE constraint, Database Engine automatically creates
a non-clustered index.
 Note :When the index is created as the part of a constraint, the name of the index is the same as the
constraint name. When the constraint is dropped, the indexes present on the constraint are
automatically deleted.
 Indexes are automatically maintained for a table or view whenever the table data is modified.

46 Database Management Systems


Choose Non clustered indexes
 Have to balance overhead involved in maintenance and use of non clustered indexes against performance
improvement gained when retrieving data.
 Index management overhead includes:
 adding an index record to every non clustered index whenever tuple is inserted;
 updating non clustered index when corresponding tuple updated;
 increase in disk space needed to store non clustered index;
 possible performance degradation during query optimization to consider all non clustered indexes.
 Do not index small relations.
 Add non clustered index to any attribute( including FK) that heavily and frequently used queries .
 Add non clustered index on attributes involved in selection or join criteria; ORDER BY; GROUP BY; and
other operations involving sorting (such as UNION or DISTINCT).
 . Avoid indexing an attribute or relation that is frequently updated.
 Avoid indexing attributes that consist of long character strings like columns with text, image, ntext,
varchar(max), nvarchar(max) and varbinary(max).

47 Database Management Systems


Estimate disk space requirements
To estimate the amount of disk space that will be required by the database. Consider a number of
issues like
 Number of table

 Number of attributes in each table

 Size of bytes reserved for each attribute

 Number of Records per each table

 The percentage of growth in the number of records in each table.

48 Database Management Systems


Design User Views , Stored Procedures and user defined Functions
Design reports , operations and tasks that were identified during the requirements analysis( initial
study) stage using views , stored procedures and user defined functions.
VIEW
 Like a table, a view consists of a set of named columns and rows of data.
 A view is a virtual table whose contents are defined by a query (SQL Statement).
 The query that defines the view can be from one or more tables or from other views in the current
or other databases.
 The rows and columns of a view come from tables referenced by a query and also can contain
values of derived attributes which are not stored (calculated fields )
 A VIEW does not require any storage in a database because it does not exist physically. In a
VIEW, we can also control user security for accessing the data from the database tables. We can
allow users to get the data from the VIEW, and the user does not require permission for each table
or column to fetch data.
 Unless indexed, a view does not exist as a stored set of data values in a database..

49 Database Management Systems


 Example for the database with the following relations we can create a view VStudentEnroll
containing columns from three of the relations and also a calculated field TotalResult
 tblSTUDENT(StudentID,StuName,StuFname,StuGFName,StuGender,StuDoB,StuTelNo,StuNationality)

 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

50 Database Management Systems


Advantages of views
 Security
You can restrict users to access directly to a table and allow them to access a subset of data via
views.
For example, you can allow users to access customer name, phone, email via a view but restrict
them to access the bank account and other sensitive information.
 Simplicity
A relational database may have many tables with complex relationships e.g., one-to-one and one-to-
many that make it difficult to navigate.
However, you can simplify the complex queries with joins and conditions using a set of views.
 Consistency
Sometimes, you need to write a complex formula or logic in every query.
To make it consistent, you can hide the complex queries logic and calculations in views.
Once views are defined, you can reference the logic from the views rather than rewriting it in
separate queries.

51 Database Management Systems


Important Points
 Unless indexed, a view does not exist as a stored set of data values in a database.

 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.

 The data from a view is generated dynamically when a view is referenced.

 Views are used as a security mechanism to mask the underlying base tables and permit user
access only to the view.

52 Database Management Systems


SQL Stored Procedures and user defined functions
 A stored procedure is a saved SQL code, and called to execute it repeatedly.
 It is also possible to pass parameters to a stored procedure , so that the stored procedure/ function
can act based on the passed parameter value(s) and return multiple values in the form of output
parameters to the calling program.
 user-defined functions are routines that accept parameters, perform an action, such as a complex
calculation, and return the result of that action as a value. The return value can either be a single
scalar value or a result set.
Advantages
Modular programming. You can create the function once, store it in the database, and call it any
number of times in your program.
Faster execution : the user-defined function doesn't need to be reparsed and reoptimized with each
use, resulting in much faster execution times.
Reduce network traffic

53 Database Management Systems

You might also like

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy