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

Database Management Study Guide - DBMM-16 V1.1

Uploaded by

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

Database Management Study Guide - DBMM-16 V1.1

Uploaded by

adams.radiy
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 161

Database Management

DBMM-16
Database Management

DBMM-16

Compiled by: Luke Burgess, Trudie Lotz, Lance Whyte, Cedric Maenetja
and Nceba Matandabuzo

Quality assured by: Gert Ehler

Edited by: Cameron Dalton, Kim Randleff-Rasmussen, Norman Baines and


Ali Parry

Version 1.1

© November 2015 CTI Education Group


Table of contents

Introduction 1
Description 1
Assessment for pass 2
Reference books 3
How to approach this course 3
Structure of a unit 4
Beginning a project 5
System requirements 10
Icons used in the study guide 11

Unit 1 – Getting Started with SQL Server 2014 12


1.1 Relational database design – A refresher 12
1.2 Relationships 13
1.3 Normalisation 14
1.4 A practical approach to database design 15
1.5 Installing SQL Server 2014 16
1.6 What is new in SQL Server 2014 26

Unit 2 – Transact-SQL 29
2.1 SQL Server storage concepts 29
2.2 Transact-SQL 31
2.3 GO statements 38
2.4 Basic modifiers 42
2.5 Integrity 46
2.6 Indentation and neatness 51

Unit 3 – Populating a Database 54


3.1 Inserting data 54
3.2 UPDATE 56
3.3 DELETE 57
3.4 Apply your knowledge 58

Unit 4 – Data Manipulation 61


4.1 SELECT statements 61
4.2 SELECT INTO 70
4.3 Inserting from one table to another 71
4.4 Control of flow statements 71
4.5 Shortcuts 73
4.6 Test yourself 74

Unit 5 – Building and Maintaining Indexes 75


5.1 Pages and extents 75
5.2 Creating an index 76
5.3 Apply your knowledge 81

Unit 6 – Joins and Views 83


6.1 Joining tables 83
6.2 Sub-queries 85
6.3 Views 86
6.4 Apply your knowledge 88

Unit 7 – Stored Procedures and Iteration Cursors 92


7.1 Stored procedures 92
7.2 User-defined functions 98
7.3 Iteration 99
7.4 Cursors 101
7.5 String functions 105
7.6 Date functions 107
7.7 Test yourself 108

Unit 8 – Triggers, Transactions 109


8.1 Triggers 109
8.2 Transactions 112
8.3 Test yourself 115

Unit 9 – Database Administration and Security 116


9.1 SQL Server Security 116
9.2 Planning the use and structure of SQL Server roles 116
9.3 RAID 128
9.4 Other types 130
9.5 Database backups and recovery 130
9.6 Backup types 131
9.7 How to back up a database and zip it 133
9.8 Test yourself 135

Unit 10 – Project 137


Project 1 – MMORPG 139
Project 2 – Tygervalley Pet Shelter 142
Project 3 – Online Flight Booking 145

Unit 11 – Test your knowledge 148

Glossary 152

Bibliography 153

Unit Exercise Checklist 154

Database Management Evaluation Form 154


Introduction
Description
The aim of this course is to introduce students to the concepts of databases as
well as a practical approach to developing databases using MS SQL Server.

The course starts with the fundamentals of database design concepts. From
there on, the concepts of creating a database are covered as well as populating
and implementing the functions of a database.

The concepts of creating a database are: creating a database, altering a


database and creating tables. These tables are created with certain
constraints, such as primary keys and foreign keys. The course then looks at
how to insert data into these tables.

Detailed explanations and exercises are given to demonstrate how to query


information (i.e. retrieve information) from a database. Data is selected from a
table and inserted into another table. The course also explains how to update
and delete information in a table.

A number of other topics are covered, including indexes, joins, views, stored
procedures and triggers, which are all used either to retrieve or to update data
in a database, as well as database security and how to back up a database.

At the end of the course, students will have to incorporate the concepts taught
into a project. This project will also require user documentation. The user
documentation will consist of author details, installation details, design details
and any extra information that may be relevant to the project.

Students will also have to incorporate their knowledge into a theoretical and a
practical examination. The theory will be written at the end of the course and
the practical will be done after the theory examination. The practical
examination tests the student’s ability to create a database and apply functions
to the database. Although not all concepts are tested in one practical
examination as this would take too long to complete, the most important
concepts will be tested.

Database Management Study Guide | V1.1 November 2015 Page 1 of 168


Assessment for pass
A pass is awarded for the course on the achievement of all the pass assessment
criteria.

Table 1 – Learning outcomes

Learning outcomes Assessment criteria for pass


To achieve each outcome a student must
demonstrate the ability to:
P1.1 Create a database
P1.2 Alter a database
P1. Create a database P1.3 Create tables in the correct database
P1.4 Use the GO statement
P1.5 Use the USE statement
P2.1 Inset data
P2. Populate a database
P2.2 Use the UPDATE statement
P2.3 Use the DELETE statement
P3.1 Use the SELECT statement
P3. Query a database
P3.2 Use the DISTINCT statement
P3.3 Use the SELECT INTO statement
P4.1 Use indexes
P4.2 Use joins and views
P4. Implement functions
P4.3 Use stored procedures and iterations
P4.4 Use triggers
P4.5 Understand database security

Welcome to CTI’s Microsoft SQL Server course. SQL stands for Structured
Query Language and it is pronounced by sounding out each letter or by saying
‘seequil’. This book will be your study guide for the next few weeks. You do not
need any prior SQL Server knowledge other than what you learnt in the
Database Design Concepts course. Microsoft Windows experience is not
required but it is useful because many of the optional features of SQL Server
require interaction with the Windows environment.

Database Management Study Guide | V1.1 November 2015 Page 2 of 168


Reference books

The following textbook is required to complete this course:

 Rankins R., Bertucci P., Gallelli C. & Silverstein A T.


2014. Microsoft SQL Server 2014 UNLEASHED, ISBN
978-0-67233729-1

Supplementary reference books that may be borrowed from the library for
further understanding will be listed in the Bibliography. This supplementary
reading is not mandatory, but it will certainly help you to answer some of the
intermediate and advanced questions in the exams. This reference material will
be a great aid if you would like to know more or gain a different perspective on
what you have learnt in this course.

To establish the Help Library manager, select Documentation and Community


from Microsoft SQL Server 2014 in the Windows Start menu and then click
Manage Help settings.

NOTE
To access SQL Server Book online for Microsoft SQL Server 2014, ensure the
documentation feature is installed when installing the program. For more
details about this, visit Aaron Bertrand’s blog:
http://sqlblog.com/blogs/aaron_bertrand/archive/2014/04/23/yes-you-can-
install-sql-server-2014-books-online-locally.aspx
Instructions are provided.

How to approach this course


Examinations
There is one theory examination and one practical examination for this course.
The theory and practical examinations must be done at the end of this course
once you have completed the project. The theory and practical examinations
will cover all the work done.

Structure of this course


Once you have completed the study material for each unit, you will have to
answer some questions. At the end of the study guide there will also be a
project for you to complete which will enable you to practically apply the
theoretical knowledge that you have acquired. Your project must be handed in
to be marked.

Database Management Study Guide | V1.1 November 2015 Page 3 of 168


Your lecturer will be available to help you with your project. It is very
important that you spend time on your project and that you feel confident in
the SQL Server 2014 environment, as you will need to complete a practical
examination which will contribute 30% toward your final mark for this course.

After your project has been marked, you will have time to prepare for and
write the examination. You have a certain number of days to complete the
course. The course breakdown is shown in Table 2.

Table 2 – Course breakdown

Section Unit Days % Notes


100% Units 1 & 2 1 Reading and exercises
3 3 Reading and exercises
4 2 Reading and exercises
5 3 Reading and exercises
6 1 Reading and exercises
7 2 Reading and exercises
8 2 Reading and exercises
9 1 Reading and exercises
10 2 Reading and exercises
11 (Project) 5 10 Complete project
Examination Examination covering all the
2 60 and 30
and Practical work
Total 24 100

Structure of a unit
All the units follow the same structure. You will be presented with the
outcomes for each unit. These outcomes can be used as an indication of what
is important and what you should focus on when going through the unit’s
material. Notes will follow this. Read these sections carefully. Key terms will
list what you should have read and understood in the unit.

You will be presented with exercises that will require you to apply your
knowledge of the material. Ensure that you understand the exercises. Ask for
help if you are unsure of what to do. Revision questions will give you an
indication of what to expect in the exam, although you should not rely on these
questions as your only reference. Some exam questions will undoubtedly be more
difficult than the revision questions.

Database Management Study Guide | V1.1 November 2015 Page 4 of 168


Suggested reading will list additional resources. It is strongly recommended
that you have a look at some of these resources, as they will provide you with
additional information that may come in handy.

You will find the project specifications at the end of the study guide. Please
do not start working on the project until you have completed (and understood)
all the exercises. Once you have covered all the topics from the previous units
and you are satisfied that you have met the outcomes, you may start the
project.

Use the project specifications as a guide. They will list everything that you
need to do to comply with the project’s requirements. If your project does not
comply with these requirements, it will not be marked. Please see the mark
sheets for each project. These indicate how the marks will be allocated.

Beginning a project
It is suggested that you read through your project as soon as you receive your
new study guide. This will give you an idea of what is expected in the project.
As you progress through the guide, you will start identifying how to approach
the project. You should only start on your project once you have worked
through the entire study guide and have a good understanding of the material.

Performing the following steps will help to ensure that you complete your
project successfully:

1. Read the relevant section in the study guide to familiarise yourself with the
material.
2. Type the examples. This might seem pointless, but it is an easy way to
familiarise yourself with the language. Experiment with the code; make
deliberate mistakes to get a feel for the way in which the compiler handles
errors.
3. Complete the exercises at the end of every chapter in the reference book. The
best way to learn a language is to practise it. This will also help you in the
examination.
4. Only start with the project if you feel comfortable with the material.
5. Make use of textbooks when the need arises.

If you work according to these guidelines, you should find that the projects are
not too difficult.

How projects are evaluated


Your lecturer will mark your projects and you will find the project evaluation
forms at the back of your study guide. Your lecturer can also supply these to
you on request. The general expectations for each of the sections are provided
below.

Database Management Study Guide | V1.1 November 2015 Page 5 of 168


Project specification
The project specification provides a detailed description of what the project
requires and how the project must function. This specification must be printed
and included with your final project submission. This includes any projects
submitted electronically.

Program design
You must follow the project specifications. For example, if the project
specifications instruct you to use a specific class, you will need to use that
class. You will be allowed to be creative, but you will be required to incorporate
certain elements into the project.

Various techniques may be employed in the planning stage of the project,


according to the project specifications. A structured plan will be an invaluable
aid to developing a fully-functional project.

The first step in any program development cycle should be a design phase –
what exactly it is that you want to do, and how you will achieve it.

This will include:

 Giving the program a name


 Stating the goal of the program
 Stating the features of the program
 The program design must be printed and submitted with your final project
submission (this includes any projects submitted electronically).

Source code
For any serious program that you develop, it is almost guaranteed that either
you or another programmer will have to return to the code on occasion to
correct a problem or add a new feature. It is thus important that your code is
easy to read. One way to make code easy to read is by using indentation and
white space.

Your code should also contain comments that clearly explain what is intended
by structures and blocks in the code. If a programmer who has to maintain the
code has to try to understand what it was that you intended originally, it not
only wastes time but also opens up the possibility that the intention may be
misunderstood, which may lead to further problems.

A well-documented program will be easier to follow, modify, debug and


maintain than a program that does not contain sufficient documentation.
Comments stating what is intended should be included for each program flow
or control statement (e.g. for, if, while, etc.). Any block of code (in braces or
surrounded by blank lines) should have a short explanation of its purpose.

Database Management Study Guide | V1.1 November 2015 Page 6 of 168


The following lists what is expected from your source code:

 The program source code must be clear and well laid out.
 You must follow a set naming standard for variable and method names.
 Method and variable names should be descriptive.
 Each file must be clearly labelled, with a prologue displaying the following
details in comments:
o Name of the file
o Name of the author
o Date created
o Operating system
o Version
o Description of the code

For example:

Example 1 – File prologue

 You will need to provide comments for all of the following:


o Each class
o Each field
o Each method

 You will need to provide the following when commenting on a method as


well:
o Author
o Version
o Date
o Return statements (if required)
o Parameters (if required)
o Exceptions that are thrown (if required)

 The source code must be printed and submitted with your final project
submission (this includes any projects submitted electronically).

Database Management Study Guide | V1.1 November 2015 Page 7 of 168


Program content
Marks will be deducted if:

 The program does not run or there are warnings of any kind. Your program
must compile and work properly before you submit it. If your program
does not run correctly, you will fail the project and you will need to
redo it.

If you follow the project specification and your program runs without any
extra effort, you should receive at least 60% for the project.

Possible deductions are listed below:


o Project returned and resubmitted (−10%)
o Project copied (resubmit and −20%)
o Program does not run or crashes (resubmit and −20%)
o Redundant code (−10%)

 Syntax errors occur, e.g. ‘;’ at the end of if statements.


 There is redundant code.
 There is invalid use of arguments or return code.
 There are logic or structural errors.
 There are calculation errors.
 There are invalid variable conversions, e.g. float with an integer variable.

User interface
 Display money values with an appropriate symbol (e.g. R, £ or $) and
percentages with the percentage symbol (%).

User documentation
All programs should be adequately documented:

 Those who have to maintain the code must know what was intended by
every single line of code (called program design documentation and source
code documentation).
 Those who have to use the program must know how to run it, what to
expect and what output will be displayed or printed (called user
documentation).
 You should supply extensive descriptions for your classes and members.
 Documentation must be in the form of a Word document unless stated
otherwise.
 The user documentation must adequately explain how to install and run the
application, working on the assumption that the end-user has no prior
experience in doing so.
 The user documentation must explain how to use each feature of the
application. The screenshots included must clarify each operation.

Database Management Study Guide | V1.1 November 2015 Page 8 of 168


 The user documentation should consist of the following:
o An index page
o A description of the program
o Instructions on which operating system to use
o The type and version of programs used
o How to install and set up the program
o How to run the program
o What values to enter when asked for user input
o How to handle any error messages
o Screenshots of all operations
o Bibliography

The user documentation must be printed and submitted with your final project
submission (this includes any project submitted electronically).

Project documentation
Your project documentation must include all of the following when your project
is submitted:

 Project specification
 Program design
 Project source code
 User documentation
 Databases (if any)
 Resources (if any)
 System requirements (if any)
 Rough work (if any)

NOTE
All projects must be presented in a professional format. Treat your project
submissions as you would in the working environment. Documentation
should be neat and typed. Ensure that you run a spell check on your project
before you submit. All your projects will make up your portfolio of evidence
which you can present to your future employer to show what you have
completed as a student.

Database Management Study Guide | V1.1 November 2015 Page 9 of 168


System requirements
Windows
 GHz Intel Pentium III Processor (or equivalent ) and later
 Microsoft Windows 7 Service Pack 1 Home Premium, Business, Ultimate, or
Enterprise
 1 GB RAM
 DVD-ROM
 Internet connection
 1024 x 768, 16-bit display
 6 GB of available disk space

Database Management Study Guide | V1.1 November 2015 Page 10 of 168


Icons used in the study guide

Denotes the start of each main subsection in the study guide.

Denotes the outcomes of the unit, i.e. the knowledge and


skills that you should have acquired after each section.

Points out the keywords for each section. Ensure that you can
name and explain all the keywords before proceeding to the
next section.

Recommended exercises for each section.

Test your understanding. Answers to these revision questions


are provided in the lecturer guide.

This icon is used to indicate required reading from the


textbook.

This icon is used to indicate supplementary reading from other


sources that you can use to broaden your knowledge.

Database Management Study Guide | V1.1 November 2015 Page 11 of 168


Unit 1 – Getting Started with SQL Server 2014

At the end of this unit you will be able to:

 Install SQL Server 2014.


 Understand relational databases.
 Use SQL Server tools and utilities.

1.1 Relational database design – A refresher


The ability of an application to be enhanced, available and preserved requires
that its underlying database is scalable, available and maintainable. In 1969 E.
F. Codd, then a researcher at IBM, conceived the relational database model.
This model stands in contrast to earlier models which were not very flexible
and did not allow for relational storage of data, making manipulation more
complicated. SQL Server, developed by Microsoft, works far more efficiently
when a large database has followed the relational database rules.

There are several advantages to using the relational database model:

 Data manipulation and reporting are more efficient.


 A database’s behaviour is predictable.
 A database’s contents are to an extent self-documenting.
 The design of a database is easy to change.
 It helps the database manager to work faster and more efficiently.

1.1.1 Tables, uniqueness and keys


Each row in a table must be unique and therefore assigned a primary key.
Keys can be simple or composite, depending on whether they contain one or
more columns. A composite primary key is made up of two or more identities
which in combination are unique. Figure 1 shows an example of a table,
displaying columns and rows. In this case, learnerID is the primary key and is
unique. learnerName and learnerSurname do not have to be unique, but the
primary key does.

Database Management Study Guide | V1.1 November 2015 Page 12 of 168


Figure 1 - Table example

Characteristics of primary keys are:

 A primary key does not change, and is explicitly generated when data is
created.
 Only one primary key can be assigned per table.
 Primary keys must represent unique values.
 A primary key should be data free. This helps when database data changes
as the primary key will not have to be changed.

1.1.1.1 Foreign keys


A column that references a primary key in another table is called a foreign key.
It is used to create a relationship between two tables that have a logical
connection, so that a table is not isolated from the rest. Almost every table in a
relational database will be linked to one or more tables. The link between one
or more tables is called a relationship.

1.2 Relationships
Relationships are the link between two tables. Three types of relationships can
exist between any two tables: one-to-one, one-to-many and many-to-
many.

1.2.1 One-to-one relationship


This relationship is used if, for each row in one table, there exists at most one
related row in the other table. It is not often found since this type of
relationship is rare in the real world. It is mainly used to spread connected
information over two or more tables or to ensure that some information, which
may be sensitive or confidential, is kept separate.

1.2.2 One-to-many relationship


This relationship applies if, for each row in one table, there are many related
rows in the other table. For each row in the second table, however, there may
exist only one related row in the first table. Lookup tables are often linked to

Database Management Study Guide | V1.1 November 2015 Page 13 of 168


base tables using this kind of relationship. It is also referred to as a parent-
child or master-detail relationship.

1.2.3 Many-to-many relationship


This relationship is used if, for each row in one table, many related rows exist
in the other table and for each row in the second table, many related rows
exist in the first table. In relational database programs, this relationship cannot
be directly modelled. It is broken down into two one-to-many relationships. A
third table, sometimes referred to as a bridging table or intersection entity, is
created to keep track of the references between the two main tables.

1.3 Normalisation
Normalisation is used to ensure an optimum and efficient structure for any
database. It consists of a set of rules that improve a database’s design. Using
a number of ‘forms’, the design is refined with each higher normal form. One
of the main reasons that it improves efficiency is that it resolves many-to-
many relationships.

In simple terms, normalisation is the process of altering database tables to


bind them to accepted normal forms. There are five different normal forms, but
most database solutions are implemented using the third normal form. 4NF
and 5NF are rarely used and hence will not be dealt with here.

1.3.1 First normal form


The first normal form dictates that lists of values in a single column are
prohibited and repeating groups of columns are not allowed. It is all about
eliminating these repeating groups of data and guaranteeing atomicity (data
is self-contained and independent).

1.3.2 Second normal form


The table must meet the rules of the first normal form. In the second normal
form, all the values in a row of a table should depend only on the primary key,
which may be a composite key consisting of multiple fields. The primary key of
that table must be a descriptor for the concerned object or entity. Also, the
table may not violate the first normal form.

1.3.3 Third normal form


In the third normal form a table must be in the second normal form, and in
addition all the non-key columns must be independent of one another. Any
column that determines its value based on another non-key column violates
the third normal form.

Database Management Study Guide | V1.1 November 2015 Page 14 of 168


1.3.4 Higher normal forms
Over and above the third normal form defined by Codd, higher normal forms
have been developed to further improve database design. Fourth and fifth
normal forms exist beyond the forms described above. It is important to
remember that each normal form is a superset of all forms of a lower order. It
is sometimes not possible to normalise past the third normal form.

NOTE
Please refer back to the Database Design Concepts course for a reminder
on normalisation. The following video will also be a useful reminder:
Richard Grant, Implementing 1NF, 2NF, 3NF here [Accessed: 25
September 2015]

1.4 A practical approach to database design


The relational database model provides a guide on how to design a database.
When starting on the design, it is very important to understand the
organisation for which the database is being created. This requires time,
experience and an insight into the core of the business. Important steps to
follow when designing an efficient database include:

1. Formulate a mission statement for the system that you are designing. Also
specify the system requirements.

2. Draw up rough diagrams of the data entry forms envisaged for the
system.

3. Convert these diagrams into rough tables.

4. Make rough drafts of the reports envisaged for the system.

5. Make sure that the tables in Step 3 include the data in the reports.

6. Add a few rows of real data to your tables.

7. Begin with the normalisation of the tables. Determine primary keys.

8. Identify and specify the foreign keys. Create relationships between tables.

9. Check whether all tables are in first normal form. Make adjustments if
necessary.

10. Check whether all tables are in second normal form. Make adjustments if
necessary.

Database Management Study Guide | V1.1 November 2015 Page 15 of 168


11. Check whether all tables are in the third normal form. Make adjustments if
necessary.

12. Re-evaluate the relationships using the normalised forms as a basis.

13. Create the improved tables and add the sample data.

14. Draw up conceptual forms, queries and reports. Adjust your design if
necessary.

15. Obtain feedback from the users. Adjust and re-normalise if necessary.

16. Set up business rules applicable to the database.

17. Create the refined forms, queries and reports. Design and develop the
associated application. Adjust your design if necessary.

18. Obtain feedback through user testing. Adjust your design if necessary.

1.5 Installing SQL Server 2014


SQL Server 2014 is more complicated to install than most Windows programs.
The SQL Server 2014 client uses the .NET framework and therefore this needs
to be installed prior to installing SQL Server 2014.

There are a few things you should know before installing SQL Server 2014:

 If you are planning to use one server, all previous versions of SQL Server
must be removed. There is also an option to upgrade from these older
versions, which could prove to be rather useful if you would like to migrate
all your current data to the new server.
 If a Windows version earlier than Windows 7 service pack 1 is used, the
.NET framework must be installed before the process of installing SQL
Server 2014 can begin (version 3.5 SP1 is recommended).
 Windows 8 comes with .NET 3.5 framework; to enable this feature, see the
note block after these installation guidelines.
 Depending on what version of SQL Server is being installed, Internet access
may be required for the installation to complete successfully because SQL
may need to download files that are out of date or missing.
 You must be an administrative user.

The steps to install SQL Server 2014 are as follows:

First the .NET framework SP1 must be installed. This is sometimes included on
the SQL Server 2014. If so, it will run automatically. If your disk did not come
with the .NET framework, the setup will very likely try to download the
necessary files. If it does not, you will need to download and run them
manually from www.microsoft.com/NET. Before the .NET framework starts

Database Management Study Guide | V1.1 November 2015 Page 16 of 168


installing, you will need to read and accept the licence agreement, as shown in
Figure 2 – Accepting .NET framework licence agreement.

Figure 2 – Accepting .NET framework licence agreement

Once you have accepted the licence agreement and clicked on Install, the
setup will start downloading and installing the necessary files, as shown in
Figure 3 – Installing .NET framework. The .NET framework will be explained in
the next unit.

Database Management Study Guide | V1.1 November 2015 Page 17 of 168


Figure 3 – Installing .NET framework

After this, it will indicate that the setup has completed successfully. If it does
not, make sure that your Internet connection is set up correctly. Your
computer may need to restart in order for the changes to take effect. Once
your computer has rebooted, you may proceed to install SQL Server 2014.

NOTE
In case you are working on Windows 8, there will be no need for you to
download the .NET framework. Just follow the simple steps to enable the
feature in the control panel. In this regard, visit the following link: here
This will require a connection to the Internet because the file needs to
download.

To see the hardware and software requirements for installing MS SQL


Server 2014, visit the following link: here

The first screen that you will be presented with is the SQL Server Installation
Center, when the SQL Server 2014 setup is run. Click on the Installation
option. You will be presented with a list of different installation options, as
shown in:

Database Management Study Guide | V1.1 November 2015 Page 18 of 168


Figure 4 – SQL Server Installation Center

The other options include other useful features that Microsoft offers in relation
to their product, such as support. If you click on the Resources option, useful
help material such as Books Online will be displayed. You may do your own
research into the other options but they will not be covered in this course.

Books Online is similar to a reference textbook on your PC. It can be very


useful when you need to look up syntax or examples, and it has a search
function. Other resources are available that can also prove useful.

Database Management Study Guide | V1.1 November 2015 Page 19 of 168


Figure 4 – SQL Server Installation Center

Click on the Installation option and select New SQL Server stand-alone
installation or add features to an existing installation.

The next step is to select the product type you wish to use. Here the options
are Evaluation and Product key version (which can be Enterprise or
Standard edition).

Click NEXT and read through the MICROSOFT LICENSE SOFTWARE TERMS
and check the box to accept the terms. You should choose to turn on customer
experience improvement as well; then click NEXT.

The next step will first run a test on your system and attempt to predict
whether or not the installation might fail. It should pass all tests of the Setup
Support Rules before you can proceed.

Click OK, and then NEXT on the next screen. It will take a few minutes to
perform a second batch of tests (see Figure 5 – Setup Support Rules).

Database Management Study Guide | V1.1 November 2015 Page 20 of 168


Figure 5 – Setup Support Rules

Figure 5 – Setup Support Rules

Database Management Study Guide | V1.1 November 2015 Page 21 of 168


NOTE
If you want to install the full version, you will need to enter your product
key (if it is not already entered for you). However, CTI does not condone
piracy and you will need to buy this key legally.

The next step in the SQL Server 2014 Setup is selecting which features of the
SQL Server you would like to install (see Figure 6 – Feature selection).

Figure 6 – Feature selection

Click on Select All so that all the available features will be installed. However,
if you do not have enough space, consult one of the prescribed books for
selection of the components. The next screen that is displayed configures the
SQL Server instance. Some businesses require more than one instance of SQL
Server to be running at any given time; that is why you can install many SQL
Servers on one computer as long as they are specified as separate instances.

There can only be one default instance and the rest have to be named. Each
instance must have an instance ID as well as a unique instance directory. The
pane at the bottom of the window shows the current instances installed on
your PC (see Figure 7 – Instance Configuration). Make your instance of SQL
Server the default instance and leave the rest as it is. Click Next to continue.

Database Management Study Guide | V1.1 November 2015 Page 22 of 168


NOTE

The Enterprise edition is used for this demonstration, but installations of


the Express edition follow the same steps. If you encounter problems in
your installation, follow the recommended textbooks and links.

Figure 7 – Instance Configuration

The next screen will inform you of how much space is required and how it is
allocated. Click Next to go to the Server Configuration screen to set up the
passwords. Click on the Use the same account for all SQL Server services
button. Enter a valid user name and password for your PC (preferably
Administrator) and click OK. All the services will be set to the same account,
as shown in Figure 8 – Server Configuration.

NOTE
Please note that the account username is hidden for security reasons.
However, this is your Windows administrative account or server if you are
connected to a network server.

Database Management Study Guide | V1.1 November 2015 Page 23 of 168


Figure 8 – Server Configuration

Click Next. If errors appear at the bottom of your window, you have not
entered a correct user name and password. Once a valid user name and
password have been accepted, you will be presented with the database engine
configuration window.

Windows authentication mode is recommended because it uses the account of


the logged-in user. You can add your own SQL-specific account details by
selecting Mixed Mode and creating an account by providing the password you
will remember. You also need to specify SQL Server administrators. Click Add
Current User to add the user that you are logged on as at the moment (see
Figure 9 – Database Engine Configuration).

Database Management Study Guide | V1.1 November 2015 Page 24 of 168


Figure 9 – Database Engine Configuration

On the next screen (Analysis Services Configuration) you can also add the
current user to the Account Provisioning pane.

On the next window (Reporting Services Configuration), leave the settings as


default. You can skip the next window (Error and Usage Reporting) unless you
would like to send Microsoft details of how you use its product.

The next window performs more tests. Check it and proceed to the next
window, which summarises exactly what is going to be installed. When you
have checked this and are ready to install, click the Install button. SQL Server
2014 will begin installing, as shown in Figure 10 – Installation Progress.

Database Management Study Guide | V1.1 November 2015 Page 25 of 168


Figure 10 – Installation Progress

After the installation is complete, you will need to restart your PC manually.
SQL Server will not run properly until you have restarted your PC.

NOTE
If you encounter any problems or issues during the installation process,
see the prescribed textbook or check other resources: Check the YouTube
video: SmodTech, SQL SERVER 2014 Installation Free: here [Accessed:
20 September 2015], or alternatively on the Microsoft website here
[Accessed: 25 September 2015] or the SQLCoffee website: here
[Accessed: 25 September 2015]

1.6 What is new in SQL Server 2014


SQL Server Management Studio (SSMS) is an integrated application that allows
for access to most graphical tools which can be used to perform administrative
and development tasks. SSMS first came out in SQL Server 2005, replacing the
Enterprise Manager, Query Analyser and Analysis Manager which were found in
the 2000 version. All these are incorporated into one.

Database Management Study Guide | V1.1 November 2015 Page 26 of 168


Microsoft SQL Server 2014 is Microsoft’s information platform for the next
generation, with new features that deliver faster performance and expand
capabilities in the cloud. SQL Server 2014, like the previous versions, has the
components that can be implemented separately or as a group. Organisations
are moving to cloud-based solutions and are looking for trusted, cost-effective,
and scalable database platforms to mission-critical and flexibility. Below we
highlight some new features of SQL Server 2014:

Containment Database: This feature was introduced in SQL Server 2012.


It is a database that is isolated from the instance of SQL Server which hosts
the database and other databases. The Contained Database Authentication
must be enabled at the server and database level. This will be discussed later
in this guide.

Registered servers: This is a new concept in SQL Server 2014 which depicts
a division managing servers and registering servers. To view registered
servers, click the VIEW tab ->Register servers. This feature allows
developers to register servers that are running older versions of SQL Server.
Registered servers have three server groups, namely: Database Engine,
Analysis Services, and Reporting Services and Integration Services. This
feature mainly allows DBAs who are running many servers to manage their
servers efficiently. In order to register a server or group of servers, you need
to have administrative privileges on the machine you are working on. Under
Register Servers ->right-click on Local Server Groups-> choose Register
Server or New Server registration for a single server. You can learn more
about this feature in due course.

Backing-Up SQL Server Databases to Azure: This feature is available in


both SQL Server 2012 and 2014. There are several benefits of backing up a
database in the cloud:

 Databases can be backed up without managing devices and storage


capacity on those devices.
 You can back up databases to Windows Azure in view of the database
activity.
 There are lower managing and operating costs, no hardware management
and low cost storage.

There are conditions or considerations that need to be looked at before using


the Windows Azure backup strategy:

 The backup and restore times will depend on the network and bandwidth.
 A backup file has a limitation of 1 TB.
 To back up databases from earlier versions than SQL Server 2012, a tool
called “Microsoft Backup to Microsoft Azure Tool” can be used.

Database Management Study Guide | V1.1 November 2015 Page 27 of 168


The first step in using the Azure backup for databases is to create a Windows
Azure account. For more details on this new exciting feature, see SQLCoffee:
here

Updateable columnstore Indexes: This is another one of Microsoft’s high


in-memory technology. It is used to improve performance for data
warehousing types of queries.

AlwaysOn Availability Group: This integration feature allows the creation of


asynchronous availability group replicas in Windows Azure for disaster
recovery.

Some of the new features were present in SQL Server 2012 but have been
enhanced in this new release of SQL Server. There are other new features that
are not relevant to this course and therefore will not be discussed here.

Required reading

Chapter 2, 3, 8 & 38 - Microsoft SQL Server 2014 UNLEASHED.

Database Management Study Guide | V1.1 November 2015 Page 28 of 168


Unit 2 – Transact-SQL

At the end of this unit you will be able to:

 Understand SQL Server storage concepts.


 Use Transact-SQL.
 Create and modify tables.

2.1 SQL Server storage concepts


2.1.1 File types
SQL Server uses different types of files to store its data. Three of these will be
discussed in this course: primary, secondary and log files. Every database
must contain the primary and log files.

Primary data files (.mdf) are the first files created in a database and can
contain user-defined objects as well as system tables. The primary data file
also contains the links to all secondary data files and log files. According to
Microsoft, most databases will work very well with only a single primary data
file and log file. The only reason to use multiple secondary files and file group
is to obtain specific performance gains on servers with multiple physical disks.

If the database becomes too big or the first hard disk runs out of space,
secondary data files (.ndf) can be created to spread data over multiple disks.
When using multiple data files, Microsoft recommends that the primary data
file should not contain any user data and that you should preferably store all
user data in the secondary data files. Secondary files can be grouped together
into file groups. Whereas database files are the actual physical files, file groups
are logical groups of the database files. Any object in a database can be
created on a specific file group. By creating a file group on a specific database
file, you can control which objects are written to specific physical disks. This
enables you to spread the load of data access across multiple physical disks,
which can enhance performance.

Transaction log files (.ldf) do not actually contain any objects. To understand
transaction log files, you should understand how SQL Server writes data to
disk. When a user wants to make changes to data in a table, SQL Server does
not write that change directly to the data file. What actually happens is that
the data to be modified is extracted and placed in memory. Approximately
every five minutes, SQL Server takes all the changes in memory and writes
them to the transaction log file. Then SQL Server writes the changes to the
database file. This is known as a write-ahead log because SQL Server writes to
the log file before it writes to the database file.

Database Management Study Guide | V1.1 November 2015 Page 29 of 168


If the server fails because of a power failure or some other problem, SQL
Server automatically recovers the database by applying committed
transactions and rolling back incomplete transactions.

The following is an overview of how SQL Server uses the transaction log:

1. Some sort of change is made to the database. Most modifications are


logged depending on the situation. Examples of logged modifications
include:

 Direct changes to the data caused by the execution of an INSERT,


UPDATE or DELETE statement.
 Creation of a database object.
 Page and extent allocations caused when information is added to a table
or index.

2. The data to be modified is loaded into memory.

3. As each modification is made, the change is written to the transaction log


before the change is applied to the data.

4. The SQL Server CHECKPOINT process looks at the database very frequently
and flushes any modified pages in memory to the disk. CHECKPOINT marks
the transaction log to indicate which transaction was last written to disk. In
the event of a system failure, SQL Server performs automatic recovery on
startup. SQL Server looks at the transaction log after the last CHECKPOINT
because those are database changes that have not yet been written to disk.

There are two reasons for writing to a log file first. Firstly, memory is about
100 times faster than hard disk, so if the data is read off the disk and all the
changes are made in memory, the changes occur about 100 times faster. The
second reason is for recoverability. Suppose that your hard disk containing the
data fails. If you were not using a transaction log, you would lose all the
changes that were made since the last backup. However, if you have your log
file (which should be on a separate disk), you can recover all the data right up
to the moment of the failure.

NOTE
Non-logged operations operate significantly faster than logged operations
because there is no additional overhead of writing log records. Non-logged
operations are usually used for handling large amounts (in bulks) of data at
one time. This means that the transaction log is useless for database
recovery. Therefore, the database should be backed up fully before and
after performing a non-logged operation.

Database Management Study Guide | V1.1 November 2015 Page 30 of 168


2.1.2 The .NET Framework
SQL Server 2014 is the fourth official version of SQL Server to run on the .NET
framework. The .NET (dot-net) framework is developed and distributed by
Microsoft. The .NET framework has something called a Common Language
Runtime (this is a type of virtual machine) and this is the environment that
has to be running in the background in order for .NET applications to be run.
When a .NET application is compiled, the compiler converts the code into
another language called Common Intermediate Language. This is then
stored and, when it is run, the .NET framework converts it directly to machine
code which the operating system can understand. A major advantage of this is
that any language that is compiled with the .NET framework is converted into
this Common Intermediate Language and can therefore interact very easily
with other .NET programs no matter what language they were written in
(because they are all the same language after compilation). Microsoft decided
to make SQL Server 2014 part of the .NET framework because it makes
interaction with other .NET programs (such as C# or VB) much easier.

2.2 Transact-SQL
Transact-SQL or T-SQL is the term used for the extended SQL language in
SQL Server. This language is used to create, update, insert into, query from
and delete databases on the server. T-SQL also provides error and exception
handling. You will be working with this language and should be familiar with it
by the end of this course. Help on the syntax of T-SQL can be found either in
Books Online or in other resources that are offered by Microsoft. The syntax is
included in this guide, but it is a simplified version, and you may need to look
up the more complex syntax when going into more depth.

The client program that is used to connect to the SQL Server is called the
Microsoft SQL Server Management Studio which runs in the .NET environment.
It is compliant with any other .NET programs, meaning that it is easily
integrated with programming languages such as C# and VB.

NOTE
For those of you who are upgrading from SQL Server 2008: the Microsoft
SQL Server Management Studio replaces the Enterprise Manager, Query
Analyser and the Profiler, combining them all into one program.

To access the Microsoft SQL Server Management Studio:

 Click on Start > All Programs > Microsoft SQL Server 2014 > SQL
Server 2014 Management Studio

This will open up the client application. A pane that asks which server you
would like to connect to will be presented, as shown in
Figure 11 – Connect to Server.

Database Management Study Guide | V1.1 November 2015 Page 31 of 168


Figure 11 – Connect to Server

 Server type – This gives the option of what type of server you would like
to connect to. Database Engine is the only one that will be used in this
course.

 Server name – This is the name of the server that you are going to
connect to. If the down arrow is clicked, a list of the local servers that are
running on your PC will be shown. If you click on <Browse for more...>,
you will get a screen with two tabs, one being the list of local servers and
the other network servers. Connecting to network servers is very useful
because many computers can be connected to one SQL Server, making a
central database.

 Authentication – This is selecting whether Windows Authentication or SQL


Server Authentication should be used. Windows Authentication is advised
because that is how the server was set up from the beginning. Later on in
the course, you will learn how this option can be changed.

Click the Connect button to connect to the server that has been selected (you
should select your local server in this case). If you are unsure of what the local
server name is, type (local) in the Server name box. This will automatically
detect the local server and connect to it.

Once inside the SQL Server Management Studio, you will see something similar
to Figure 12 – Microsoft SQL Server Management Studio. Labels are inserted to
help you to understand what the different sections are for.

Database Management Study Guide | V1.1 November 2015 Page 32 of 168


Figure 12 – Microsoft SQL Server Management Studio

The meaning of the labels is as follows:

1. Object Explorer – This is where you can see and manage all the
databases running on the server. This means that you can also create, edit
and delete these databases using the mouse. You can also review security
settings and other useful options that SQL Server offers.

2. Output pane – All the output of the SQL Server is displayed here, whether
it is queries, printouts or error messages.

3. Code area – This area is used to type your code for creating new queries.
Any T-SQL code that you will use will be inside this query pane. To create a
new query, click on the New Query button on the taskbar at the top left.

4. Menu/taskbar – Additional options are displayed here.

Database Management Study Guide | V1.1 November 2015 Page 33 of 168


2.2.1 Creating a database
Creating a database in T-SQL is simple. It is merely a matter of understanding
the syntax and applying it to your situation. When creating a database, you
need to specify the primary and log files. The syntax structure for creating a
database is as follows:

CREATE DATABASE database_name


ON PRIMARY
(
--file_specs
)
LOG ON
(
--file_specs
)
GO

The above is the syntax that will be used most of the time, but, if you were to
go into more depth, the following is the full syntax to create a database:

CREATE DATABASE database_name


[ON
[<filespec> [,...n ]]
[, <filegroup> [,...n]]
]
[LOG ON { <filespec> [,...n] }]
[COLLATE collation_name]
[FOR LOAD | FOR ATTACH]

<filespec> ::=
[PRIMARY]
( [NAME = logical_file_name,]
FILENAME = 'os_file_name '
[, SIZE = size]
[, MAXSIZE = { max_size | UNLIMITED }]
[, FILEGROWTH = growth_increment]
) [,...n ]

<filegroup> ::=
FILEGROUP filegroup_name < filespec > [,...n]

NOTE
The square brackets [] are primarily used to determine identifiers. This is
necessary if the column name uses reserved keywords or contains special
characters such as space or hyphen. It is a good practice not to include
any spaces when naming columns, tables or a database.

In the above definition of creating a database, [] (square brackets) indicate


optional modifiers, the | (pipe) symbol indicates a choice and may be read as

Database Management Study Guide | V1.1 November 2015 Page 34 of 168


‘or’, and the {} (braces) indicate a number of required modifiers of which one
must be chosen. Thus [, MAXSIZE = {max_size | UNLIMITED }] means that
MAXSIZE is an optional modifier, but if used, it must be followed by max_size
(a number) or the modifier UNLIMITED.

Every database has at least two files – a primary file and a transaction log file.
Although os_file_name can be any valid operating system file name, the
name more clearly reflects the purpose of the file if the recommended file
extensions are used, as listed in Table 3 - File types.

Table 3 - File types

File type File name extension


Primary data file .mdf
Secondary data file .ndf
Transaction log file .ldf

When a simple CREATE DATABASE <database_name> statement is specified


with no additional parameters, the database is made the same size as the
model database which you can see just beneath the master database in the
Object Explorer. All databases have a primary file group. All system tables are
allocated in the primary file group. Below is an example of creating a simple
database in T-SQL:

1 USE master
2 GO
3
4 CREATE DATABASE lead_by_example
5 ON PRIMARY
6 (
7 NAME = 'lead_by_example_data',
8 FILENAME = 'c:\sql2014\lead_by_example_data.mdf',
9 SIZE = 5MB,
10 FILEGROWTH = 10%
11 )
12 LOG ON
13 (
14 NAME = 'lead_by_example_log',
15 FILENAME = 'c:\sql2014\lead_by_example_log.ldf',
16 SIZE = 5MB,
17 FILEGROWTH = 10%
18 )
19 GO

Line 1 is the USE command. This will be explained in detail later in the course.
It ensures that the master database is used. Only the master database has
permission to allow the CREATE DATABASE statement.

Database Management Study Guide | V1.1 November 2015 Page 35 of 168


Line 2 uses the GO statement. This will be explained later on in the course.

Line 4 contains the CREATE command along with what needs to be created, in
this case DATABASE. The name of the database is then defined (in the
database list).

Line 5 is the declaration of the primary data file that the database uses.

Line 6 contains an open bracket showing that it is the beginning of the file
specifications for the primary data file. Every opening bracket must have a
corresponding closing bracket.

Line 7 declares the name of the primary data file for referencing purposes.

Line 8 declares the file name for the primary data file that is stored on your
drive.

Line 9 declares the initial size of the file.

Line 10 declares at what rate the file should grow.

Line 11 closes the opening bracket.

Line 12 declares that the log file is being created and the file specifications will
follow. The same rules apply to the log file as to the primary data file.

NOTE
The directory (‘c:\sql2014\’ in this case) must exist before you run your
script file, otherwise it will fail because it will not be able to find the
specified folder.

2.2.2 Altering a database


When the ALTER statement is used on a database, the properties of that
database can be edited. The database must already exist in order for this
command to work. Also, files or file groups (explained next) may be added to
the database. When altering a database, the master database should be used
because only this database can give permission to add and alter databases. To
alter an existing database the syntax is as follows:

USE master
ALTER DATABASE lead_by_example
MODIFY Name = SuperStore; --this will be new database name
GO

An example of altering a database will be shown in the next section.

Database Management Study Guide | V1.1 November 2015 Page 36 of 168


2.2.2.1 File groups
It is possible to divide your database files into multiple groups. This enables
you to store a database in more than one location. It enables you to expand
the size of your database with extra files. To add a file group to a database,
the ALTER DATABASE command needs to be used, although it is possible to do
it when creating your database. Files can be added to your user-defined file
group and then that specific file can be used. Remember that every file created
in a database is a secondary file unless it is the primary file (there can only be
one primary file).

Unless the database’s AUTOGROW feature is turned off, the database files will
grow automatically until disk space runs out.

Multiple files increase maintenance, but there are several advantages:

 Files can be placed on separate physical hard disks.


 Performance can be improved because reads and writes to the database
have a better chance of going to separate disk controllers.
 Database files can be backed up independently of each other.
 If file groups are used, specific portions of data can be placed in specific
files. For example, the payroll table could be placed in its own file group in
its own file.

1 USE master
2 GO
3
4 ALTER DATABASE lead_by_example
5 ADD FILEGROUP custom_filegroup
6 GO
7
8 ALTER DATABASE lead_by_example
9 ADD FILE
10 (
11 NAME = custom_file,
12 FILENAME = 'c:\sql2014\custom_filegroup.ndf',
13 SIZE = 5MB
14 )
15 TO FILEGROUP custom_filegroup
16 GO
17
18 ALTER DATABASE lead_by_example
19 MODIFY FILEGROUP custom_filegroup DEFAULT
20 GO

Line 1 uses the master database.

Line 4 alters the lead_by_example database.

Database Management Study Guide | V1.1 November 2015 Page 37 of 168


Line 5 adds a file group to the database.

Line 9 adds a file, specifying its attributes.

Line 15 tells the compiler that the file created must be added to the
custom_filegroup file group.

Line 19 modifies the file group to make it the default file group (a file group
cannot be modified if it does not contain files).

2.3 GO statements
The GO statement is responsible for separating sections of code. When your
code will not run or compile, the error will point to the first line of every
section. This means that if no GO statements are put in a piece of code, all the
errors will point to line 1, and you will have no idea what is causing the error.
Certain statements require GO statements after them, but the rest is up to the
programmer.

The syntax is as follows:

Section of code
GO

Section of code
GO

Section of code

You will be using the GO statement often in this course and it will become
easier to know where to separate the code as you go along. A basic rule of
thumb is that code that does the same thing must be grouped together, e.g.
all the code inserting into the table ‘Person’ would be in a group and therefore
have no GO statements between it. A GO statement must always appear on its
own line.

2.3.1 USE statements


The USE statement is responsible for telling the compiler which database to
use when selecting, updating, deleting, altering and manipulating data. If the
USE statement is not included, the database that is currently selected (the
default, Master) will be used, and this is not likely to be the database that is
needed. The GO statement must be used after the USE statement. The syntax
is simple:

USE database_name
GO

Database Management Study Guide | V1.1 November 2015 Page 38 of 168


2.3.2 Apply your knowledge
You will now be able to create your own database. First, make
a folder on your C: drive called sql2014. Second, open up the
Microsoft SQL Server Management Studio and connect to your
local server.

Click on New Query.

You should see a large window open up where you can type T-SQL. Type the
following:

USE master
GO

CREATE DATABASE lead_by_example


ON PRIMARY
(
NAME = 'lead_by_example_data',
FILENAME = 'c:\sql2014\lead_by_example_data.mdf',
SIZE = 5MB,
FILEGROWTH = 10%
)
LOG ON
(
NAME = 'lead_by_example_log',
FILENAME = 'c:\sql2014\lead_by_example_log.ldf',
SIZE = 5MB,
FILEGROWTH = 10%
)
GO

The blue tick on the toolbar is a Parse button. It checks for potential errors in
your code before compiling it. Click it, wait a few seconds, and you will see a
message at the bottom saying that your query compiled successfully. If not,
you have mis-typed something. If the parse gave no errors, click the Execute
button to compile your code. This should also display no errors. If no errors are
displayed, you have officially created your own database. To see your
database, click on the plus (+) sign next to Databases on your Object
Explorer.

You should see your database lead_by_example listed there; if not, refresh
the Object Explorer. At this stage, this database is empty, but tables will be
added later. You will now add your own file group and add a file to it. Continue
writing in the same script file. Type the following after you have created your
database:

ALTER DATABASE lead_by_example


ADD FILEGROUP employers_filegroup

Database Management Study Guide | V1.1 November 2015 Page 39 of 168


GO

ALTER DATABASE lead_by_example


ADD FILE
(
NAME = employers_info,
FILENAME = 'c:\sql2014\employers_info.ndf',
SIZE = 5MB
)
TO FILEGROUP employers_filegroup
GO

NOTE
The employers_filegroup was not modified to be the default file group.
You will see how to add objects to a file group manually later in this course
(if a file group is the default file group, then objects are added to it
automatically).

2.3.3 SQL Server data types


A data type defines what type of information is stored. In most programming
languages, data types have to be defined because the compiler needs to know
how the data can be manipulated. It is crucial that the programmer knows
what data type he or she is manipulating.

SQL Server has several built-in data types. Data types are used to define:

 The type of information stored in a table’s column.


 The type of information used in a local variable.
 The type of information in a stored procedure’s parameters (more on this
later).

The following is an explanation of each data type available in SQL Server:

bigint Integer (whole number) data from –263


to 263 –1
int Integer (whole number) data from –231
to 231 – 1
smallint Integer data from –215 (–32 768) to
215 – 1 (32 767)
tinyint Integer data from 0 through 255
bit Integer data with either a 1 or 0 value
decimal Fixed precision and scale numerical
data from –1038 + 1 to 1038 – 1
numeric Functionally equivalent to decimal

Database Management Study Guide | V1.1 November 2015 Page 40 of 168


money Monetary data values from –263 to 263
– 1, with accuracy to a ten-thousandth
of a monetary unit
smallmoney Monetary data values from –2 147 483
648 to +2 147 483 647, with an
accuracy to a ten-thousandth of a
monetary unit
float Floating precision number data from
–1.79E + 308 to 1.79E + 308
real Floating precision number data from
–3.40E + 38 to 3.40E + 38.
datetime Date and time data from 1 January
1753 to 31 December 9999, with an
accuracy of three-hundredths of a
second or 3.33 milliseconds
smalldatetime Date and time data from 1 January
1900 to 6 June 2079, with an accuracy
of one minute
date New to SQL Server 2014, enabling the
user to specify a date without a time
time New to SQL Server 2014, enabling the
user to specify a time without a date
datetime2 New to SQL Server 2014, a more
efficient date-time data type
char Fixed-length Unicode character data
with a maximum length of 8 000
characters
varchar Variable-length non-Unicode data with
a maximum of 8 000 characters
text Variable-length non-Unicode data with
a maximum length of 231 – 1
(2 147 483 647) characters
nchar Fixed-length Unicode data with a
maximum length of 4 000 characters
nvarchar Variable-length Unicode data with a
maximum length of 4 000 characters
ntext Variable-length Unicode data with a
maximum length of 230 – 1 characters
binary Fixed-length binary data with a
maximum length of 8 000 bytes
varbinary Variable-length binary data with a

Database Management Study Guide | V1.1 November 2015 Page 41 of 168


maximum length of 8 000 bytes
image Variable-length binary data with a
maximum length of 231 – 1 bytes
cursor A reference to a cursor
sql_variant A data type that stores values of
various SQL Server-supported data
types, except text, ntext, timestamp
and sql_variant
table A special data type used to store a
result set for later processing
timestamp A database-wide unique number that is
updated every time a row is updated
uniqueidentifier A globally unique identifier (GUID)

NOTE
Unicode characters are non-English characters, such as Chinese or Arabic.
Unicode characters are twice the size of standard characters.

Different data types have different memory usage. When selecting which data
type to use, rather choose the more efficient one than the largest one. This
this plays an important role in keeping large databases running smoothly and
efficiently. For example, if you wanted to have someone’s name as a column,
varchar would be a better data type than text, because it is very unlikely that
someone will have a name over 8 000 characters long, and the varchar data
type uses far less memory than the text data type. If, however, you were
going to have an essay column (very unlikely), you would have to use the text
data type because 8 000 characters would not be enough. The same applies to
all the data types; for example, smallint is a lot more efficient than real or
float.

2.4 Basic modifiers


Modifiers are extra options that customise the columns further than just the
data type. Some are essential and others are optional (to be used at the user’s
discretion). The essential one is declaring the nullity of the column. Nullity
determines whether the column is optional or mandatory. If a column is
optional then if data is not entered a NULL value will be put in its place. NULL
means ‘no value’. If a column is mandatory, then data has to be entered;
otherwise, an error will be thrown. To make a column optional, the word NULL
needs to be entered after the data type in the declaration. To make it
mandatory, it must be declared as NOT NULL. There is another modifier called
IDENTITY, which will be explained later (section 2.2.6).

Database Management Study Guide | V1.1 November 2015 Page 42 of 168


2.4.1 Creating tables using Transact-SQL
Tables are the main feature of databases; they are the actual 'containers’ of
the information being stored. Each table contains columns and rows. The table
is the entity and the columns are the attributes. Each row is an entry into the
table. Tables can be linked to one another logically using keys. All data in a
database is stored in one or more tables.

2.4.2 Declaring a table in T-SQL


To declare a table in T-SQL, the CREATE command is used. Make sure that the
USE statement is executed before the start of the code; otherwise, it will
create a table in the wrong database. The columns are listed inside the
declaration. The syntax is as follows:

--Memory optimized CREATE TABLE Syntax


CREATE TABLE
[database_name . [schema_name ] . | schema_name . ]
table_name
( { <column_definition>
| [ <table_constraint> ] [ ,... n ]
| [ <table_index> ]
[ ,... n ] }
[ PERIOD FOR SYSTEM_TIME (
system_start_time_column_name
, system_end_time_column_name ) ]
)
[ WITH ( <table_option> [ ,... n ] ) ]
[ ; ]

<column_definition> ::=
column_name <data_type>
[ COLLATE collation_name ]
[ GENERATED ALWAYS AS ROW { START | END } [ HIDDEN ] ]
[ NULL | NOT NULL ]
[
[ CONSTRAINT constraint_name ] DEFAULT
memory_optimized_constant_expression ]
| [ IDENTITY [ ( 1, 1 ) ]
]
[ <column_constraint> ]
[ <column_index> ]

<data type> ::=


[type_schema_name . ] type_name [ (precision [ , scale ]) ]

<column_constraint> ::=
[ CONSTRAINT constraint_name ]
{ PRIMARY KEY { NONCLUSTERED HASH WITH (BUCKET_COUNT =
bucket_count) | NONCLUSTERED } }

Database Management Study Guide | V1.1 November 2015 Page 43 of 168


< table_constraint > ::=
[ CONSTRAINT constraint_name ]
{ PRIMARY KEY { { NONCLUSTERED HASH (column [ ,... n ] ) WITH
(BUCKET_COUNT = bucket_count) | NONCLUSTERED (column [ ASC |
DESC ] [ ,... n ] ) } }

<column_index> ::=
INDEX index_name
{ [ NONCLUSTERED ] HASH WITH (BUCKET_COUNT = bucket_count) |
[ NONCLUSTERED ] }

<table_index> ::=
INDEX index_name
{ [ NONCLUSTERED ] HASH (column [ ,... n ] ) WITH
(BUCKET_COUNT = bucket_count)
| [ NONCLUSTERED ] (column [ ASC | DESC ] [ ,... n ] )
[ ON filegroup_name | default ]
| CLUSTERED COLUMNSTORE
[ ON filegroup_name | default ]

<table_option> ::=
{
[MEMORY_OPTIMIZED = {ON | OFF}]
| [DURABILITY = {SCHEMA_ONLY | SCHEMA_AND_DATA}]
| [ SYSTEM_VERSIONING = ON [ ( HISTORY_TABLE = schema_name
. history_table_name
[, DATA_CONSISTENCY_CHECK = { ON | OFF } ] ) ] ]

The following code shows an example of creating a table:

1 USE lead_by_example
2 GO
3
4 CREATE TABLE campus
5 (
6 iden INT NOT NULL ,
7 cName VARCHAR(40) NOT NULL,
8 area VARCHAR(40) NULL,
9 telephone VARCHAR(40) NULL,
10 PRIMARY KEY (iden) ,-- this will be explained next
11 CONSTRAINT ConCampusID UNIQUE(iden,cName)
12 )
13 GO

Database Management Study Guide | V1.1 November 2015 Page 44 of 168


Line 1 shows that the lead_by_example database must be used.

Line 2 uses the GO statement.

Line 4 creates a table called ‘campus’.

Line 5 shows the beginning of the column declaration.

Line 6 declares a column called ‘campusID’ of the INT data type and makes it
NOT NULL (mandatory).

2.4.2.1 Primary keys


A primary key is the column in a table that uniquely identifies each record in
that table. Typical examples would include ID numbers or email addresses. A
composite primary key is made up of two or more columns and these columns
together form a unique key. No two entries in a table can have exactly the
same primary key because they have to be unique. The only exception to this
rule is when an attribute is involved in a composite primary key. It does not
have to be unique, as long as the composite primary key is unique as a whole.
A primary key is known as a unique identifier (UID). A table will most likely be
referenced by its primary key because it is guaranteed that only one result will
be received.

In T-SQL, the code for creating a primary key is as follows:

PRIMARY KEY (column_name)


or
PRIMARY KEY (column1, column2 …) --for composite primary keys
or
CONSTRAINT constraint_name PRIMARY KEY (column_name)

2.4.2 2 IDENTITY modifier


The IDENTITY modifier is used to auto-generate unique numbers for a column.
It cannot be edited, unless it is explicitly stated. The IDENTITY modifier comes
directly after the nullity modifier. There is an example later on that uses
constraints. By default, IDENTITY starts at 1 and increases by 1 every time,
but you can specify where it must start, and what the increment will be by
using brackets after the keyword, e.g. IDENTITY (2, 5). This will start at 2 and
increase by 5 every time.

2.4.2.3 Foreign keys


A foreign key is a field in one table that uniquely identifies a row of another
table. It must reference a unique column, otherwise it will fail. The foreign key
usually references the primary key of the other table, but does not necessarily
have to; it can reference any unique column.

Database Management Study Guide | V1.1 November 2015 Page 45 of 168


The code for creating a foreign key in SQL is as follows:

column_name modifiers REFERENCES table_name (column_name)


CONSTRAINT PK_Temp FOREIGN KEY(TempID)

Modifiers can include the data type of the column or its nullity. The following
(line 9) is an example of creating a foreign key:

1 USE lead_by_example
2 GO
3 CREATE TABLE student
4 (
5 StuID INT NOT NULL PRIMARY KEY,
6 fName VARCHAR(50) NOT NULL,
7 sName VARCHAR(50) NOT NULL,
8 campusID int NOT NULL,
9 CONSTRAINT FK_tempStudent FOREIGN KEY(campusID)
10 REFERENCES campus(iden)
11 ON DELETE CASCADE
12 )
13 GO

NOTE
When creating tables in SQL Server, you must create the primary key before
the foreign key that references it. The reason for this is that a column cannot
be referenced if it contains no value. Therefore, it is recommended that you
start with the most central table and work your way outwards.

2.5 Integrity
The relational model specifies two general integrity rules. They are referred to
as general rules because they apply to all databases. They are entity integrity
and referential integrity.

The entity integrity rule is very simple. It says that primary keys cannot
contain null (missing) data. The reason for this rule should be obvious. A row
in a table cannot be uniquely identified or referenced if the primary key of that
table can be null. It is important to note that this rule applies to both simple
and composite keys. For composite keys, none of the individual columns can
be null.

The referential integrity rule says that the database must not contain any
unmatched foreign key values. This implies that:

 A row may not be added to a table with a foreign key unless the referenced
value exists in the referenced table.

Database Management Study Guide | V1.1 November 2015 Page 46 of 168


 If the value in a table that is referenced by a foreign key is changed (or the
entire row is deleted), the rows in the table with the foreign key must not
be ’orphaned’.

In general, three options are available when a referenced primary key value
changes or a row is deleted. The options are:

 Disallow – The change is completely disallowed.


 Cascade – For updates, the change is cascaded to all dependent tables.
For deletions, the rows in all dependent tables are deleted.
 Nullify – For deletions, the dependent foreign key values are set to null.

2.5.1 Altering a table


Altering a table is what will be done when one needs to change the columns of
the table, either adding columns or changing them in some way. The syntax is
simple:

ALTER TABLE table_name


ADD
add_type
DROP
drop_type

ALTER column_name
alter_type
GO

The add_type is anything that needs to be added in the table, whether it is an


extra column or constraints (explained next). The drop_type is the same as the
add_type, but instead of being added, it is removed. The ALTER column_name
has to be included inside the ALTER table_name because it is part of the table.

2.5.2 Constraints
Constraints determine the way in which a column behaves. A primary key is a
type of constraint put onto a column, but many others exist. Constraints can
be added to columns when creating or altering a table. The constraints dealt
with in this course are as follows:

 PRIMARY KEY – Defines the unique identifier of the table.


 FOREIGN KEY – References a column of another table.
 UNIQUE – Makes sure that the information entered into this column is
unique. There are two types of unique constraint – clustered and non-
clustered. Non-clustered is the default option.
 DEFAULT – Specifies a default value into a column (cannot be used with
UNIQUE).
 CHECK – Performs a check on the information before it is entered into the
column, then throws an error if it does not match the check.

Database Management Study Guide | V1.1 November 2015 Page 47 of 168


The PRIMARY KEY and FOREIGN KEY constraints have already been used, but
the others are also very useful. Below, a new table is created and some of the
constraints are used together with the IDENTITY modifier:

1 USE lead_by_example
2 GO
3
4 CREATE TABLE employee
5 (
6 empNum INT NOT NULL IDENTITY, --auto generated numbers
7 empEmail VARCHAR(30) NOT NULL,
8 empName VARCHAR(40) NOT NULL,
9 campusID INT NOT NULL REFERENCES campus(iden),
--foreign key constraint
10 CONSTRAINT emp_EMAIL_ADD UNIQUE (empEmail),
11 PRIMARY KEY(empNum)--primary key constraint
12 )
13 GO

The three constraints used in this example are:

Line 9 – This foreign key can also be created by using the CONSTRAINT
keyword.

Line 10 – This column uses the CONSTRAINT keyword and converts the
empEmail into a unique column. A name is given to the constraint for internal
referencing (emp_EMAIL_ADD). This unique constraint uses the non-clustered
option by default.

Line 11 – This uses the PRIMARY KEY constraint and it can also be created by
using the line: CONSTRAINT prKey_empNum PRIMARY KEY (empNum).

2.5.3 Apply your knowledge


In this section you will create tables for your database
lead_by_example that you created earlier. Create a new
query in the Management Studio, and type the following code
to create four tables:

Database Management Study Guide | V1.1 November 2015 Page 48 of 168


USE lead_by_example
GO

CREATE TABLE storeBranch


(
storeID INT NOT NULL CHECK(storeID > 0),

storeName VARCHAR(30) NOT NULL,


storeArea VARCHAR(30) NOT NULL,
CONSTRAINT pk_IDStore PRIMARY KEY(storeID)
)
GO

CREATE TABLE employee


(
empID FLOAT NOT NULL,
empName VARCHAR(30) NOT NULL,
empStatus VARCHAR(30) NOT NULL,
placeWorkID INT NOT NULL,
CONSTRAINT fk_WORK FOREIGN KEY (placeWorkID) REFERENCES
storeBranch(storeID),
PRIMARY KEY(empID)
)
GO

CREATE TABLE species


(
speciesID INT NOT NULL IDENTITY,
speciesText VARCHAR(30) NOT NULL,
CONSTRAINT pk_Speci PRIMARY KEY(speciesID)
)
GO

CREATE TABLE pets


(
petID INT NOT NULL,
petName VARCHAR(30) NOT NULL,
speciesType INT NOT NULL,
CONSTRAINT fk_specie FOREIGN KEY(speciesType) REFERENCES
species(speciesID),
store INT NOT NULL,
CONSTRAINT fk_store FOREIGN KEY (store) REFERENCES
storeBranch(storeID)
ON DELETE CASCADE
--Sold at which store
)
GO

If the code compiled without any errors, four tables will have been created:
storeBranch, employee, species and pets. As you can see, the PRIMARY

Database Management Study Guide | V1.1 November 2015 Page 49 of 168


KEY has been used for all the tables, so that they can be referenced. The
CHECK constraint is also used in this example, and when information that does
not match the check is entered into this column (in this case, it must be
greater than 0), an error will be shown.

NOTE

If you create or insert into tables, but the script compiles with errors, you
must always make sure that the tables are deleted before you re-run the
script. This is because the script runs the different sections individually (in
between the GO statements) and only the section with the errors will not
compile. This means that some tables could have been created and, if they
are already in existence, they cannot be overwritten. They must be deleted
before the script is run again. To delete a table you can go to the object
browser, right-click on the tables, and select Delete

Extra constraints can now be added to the tables that have been created. Look
at the following code:

USE lead_by_example
GO

ALTER TABLE species


ADD
CONSTRAINT Unique_text UNIQUE (speciesText)
GO

ALTER TABLE pets


ADD
CONSTRAINT pets_Pri_Key PRIMARY KEY(petID)
GO

This adds a unique and a primary key constraint to two of the tables. Data will
be inserted into these tables in the next unit.

2.5.4 A little secret (dropping an object)


The word DROP means to delete. Databases, tables, views and procedures can
be dropped. A database cannot be created if it already exists, so it needs to be
dropped first. This is useful if one needs to re-run the script file. The syntax for
dropping an object is as follows:

DROP object_name
GO

A database cannot be dropped if it does not exist so a check needs to be


performed first. This is done using the following syntax:

Database Management Study Guide | V1.1 November 2015 Page 50 of 168


IF EXISTS (SELECT name FROM master.dbo.sysdatabases
WHERE name='database_name')
DROP DATABASE database_name
GO

This code first checks whether or not the database exists. If it does, it is
dropped. Placing this code directly above your CREATE DATABASE statement
lets you start fresh every time the script file is run.

2.5.5 Comments in your code


Comments in code are one of the most important features that SQL Server
provides. Most programming languages provide support for this feature
because of the usefulness of commenting. A comment is simply text in the
code that the compiler ignores. This text does not make a difference to the
output. What is the point of commenting then? It is designed to help the
developer to understand a section of code without having to look through the
code and work out what it does. It can also be used to specify the name of the
author, why something was done in a certain way, or anything else that the
developer wants the reader of the code to know.

Two types of comments are single-line comments and multi-line comments.


Single-line comments are used for commenting next to a line of code, or if
something needs to be put on one line without disrupting the layout. Multi-
line comments are used for explaining something on more than one line, or if
you want to group a few lines of comments together.

A single-line comment is started with a double dash, followed by the comment


text.

A multi-line comment is started by a slash and an asterisk. Every line after


that will be a comment until the multi-line comment is ended with an asterisk
and a slash. See the following code sample for an example of using comments
in SQL Server.

/*
This is a multi-line comment that
is not processed by the compiler.
*/

USE lead_by_example --USE statement


GO --GO statement

2.6 Indentation and neatness


It is important in any coding language that the code is neat and readable. To
ensure that the code is readable, the following rules should be adhered to:

Database Management Study Guide | V1.1 November 2015 Page 51 of 168


Make sure that every clause or main keyword falls on a new line. Keep the
keywords that SQL uses in capital letters. This makes sure that one can
distinguish code from table names and other identifiers. Use indentation.
Indent some code further than other code. See the following example of using
indentation:

insert into item values(25,'Serpent boots','Adds 5 million


agility'),(75,'Dark helmet','Makes the hosts head invisible
to any other player'),(3,'Sword of 1 000 truths','Kills
everything within a 72 million mile radius')

The above example will compile without errors, but it is rather hard to read.
Using indention makes it far more readable.

INSERT INTO item VALUES


(25,'Serpent boots','Adds 5 million agility'),
(75,'Dark helmet','Makes the hosts head invisible to any
other player'),
(3,'Sword of 1 000 truths','Kills everything in a 72
million mile radius')

Using indentation, the beginning of each record can now be clearly


distinguished.

2.6.1 Print statement


Sometimes it may be necessary to print out certain information to the user, or
even yourself. It may be useful to make the code print a line when something
is complete. This can be done as follows:

USE master
GO

PRINT('using master database')--prints a line

CREATE DATABASE print_example


ON PRIMARY
(
NAME = 'print_example_data',
FILENAME = 'c:\sql2014\print_example_data.mdf',
SIZE = 5MB,
FILEGROWTH = 10%
)
LOG ON
(
NAME = 'lead_by_example_log',
FILENAME = 'c:\sql2014\print_example_log.ldf',
SIZE = 5MB,
FILEGROWTH = 10%
)

Database Management Study Guide | V1.1 November 2015 Page 52 of 168


GO

PRINT ('Database print_example created correctly')


--prints a line

When this is executed it will print out ‘using master database’ after the
command has been completed. It will also print ‘Database print_example
created correctly’ after the whole database has been created correctly.

Required reading

Chapter 20, 21 & 23 - Microsoft SQL Server 2014 UNLEASHED.

Database Management Study Guide | V1.1 November 2015 Page 53 of 168


Unit 3 – Populating a Database

At the end of this unit you will be able to:

 Insert data into tables.


 Delete data from tables.
 Update tables.

3.1 Inserting data


3.1.1 Inserting by value
Inserting data into a table can be done manually by using the INSERT
statement. The same rule about the USE command applies to this. It needs to
be put in, as otherwise there is no knowing what database must be used. The
syntax is as follows:

[ WITH <common_table_expression> [ ,...n ] ]


INSERT
{
[ TOP ( expression ) [ PERCENT ] ]
[ INTO ]
{ <object> | rowset_function_limited
[ WITH ( <Table_Hint_Limited> [ ...n ] ) ]
}
{
[ ( column_list ) ]
[ <OUTPUT Clause> ]
{ VALUES ( { DEFAULT | NULL | expression } [ ,...n ]
) [ ,...n ]
| derived_table
| execute_statement
| <dml_table_source>
| DEFAULT VALUES
}
}
}
[;]

Below is an example of the syntax in practical use:

USE lead_by_example
GO

INSERT INTO campus


VALUES (1, 'Randburg', 'Gauteng')
GO

Database Management Study Guide | V1.1 November 2015 Page 54 of 168


INSERT INTO campus
VALUES (2, 'Durbanville', 'Western Cape')
GO

INSERT INTO campus


VALUES (3, 'Oompaloompa', 'Western Cape')
GO

INSERT INTO employee


VALUES ('[email protected]', 'Moonman',1)
GO

INSERT INTO employee


VALUES ('[email protected]', 'Zuese',1)
GO

INSERT INTO employee


VALUES ('[email protected]', 'Buzz',2)
GO

INSERT INTO employee


VALUES ('[email protected]', 'Lance',2)
GO

INSERT INTO student(fName, sName, iden, campusID)


VALUES ('Chris', 'Lake', 6809030101924, 1)
GO

INSERT INTO student(fName, sName, iden, campusID)


VALUES ('Deen', 'Fuel', 7810030102344, 2)
GO

INSERT INTO student(fName, sName, iden, campusID)


VALUES ('Deen', 'Gomez', 7815030109374, 2)
GO

INSERT INTO student(fName, sName, iden, campusID)


VALUES ('Mark', 'Knight', 2434030101326, 1)
GO

Data is entered to the campus table and the student table in the
lead_by_example database. If you insert data into the table in an order
different to the existing column order, then you must give the column names
in the correct order after the table name. The values that are being inserted
have to correspond with the order that was specified. When inserting into the
campus database, the columns to be inserted into are not specified, but the
order of the data being entered must be exactly the same as the column order.
The VARCHAR data type has to be enclosed in single quotation marks, as
otherwise it will not compile. While character data types need single quotes,

Database Management Study Guide | V1.1 November 2015 Page 55 of 168


number types do not (see the example). There are GO statements after every
INSERT statement. This is not required but it is recommended because it
makes troubleshooting easier.

NOTE
One cannot insert data that references non-existent data. Therefore, in this
case, one has to insert into the campus table before you insert into the
student table.

3.1.2 Inserting from another table


There are times when it is necessary to insert into your table from another
table. This will be further explained in Unit 4.3.

3.2 UPDATE
3.2.1 Difference between UPDATE and ALTER
ALTER is not the same as UPDATE. ALTER TABLE lets one add columns and
change aspects of the table design. UPDATE TABLE lets one alter the
information that is stored in the table – it does not change the design of the
table. An example would be if a woman gets married and her surname
changes. The UPDATE TABLE statement is used to change her surname in a
table.

The syntax to update a table is as follows:

UPDATE table_name
SET column_name = value
WHERE column_name = value

This will update one or more records already in the table. This can be made
more specific by combining more than one matching value, i.e.:

UPDATE table_name
SET column_name = value
WHERE column_name = value AND column_name = value

An example of using the UPDATE statement is as follows:

USE lead_by_example
GO

UPDATE student
SET fName = 'Dean'
WHERE fName = 'Deen' AND sName = 'Fuel'
GO

Database Management Study Guide | V1.1 November 2015 Page 56 of 168


This finds the records in the table student where the column fName is equal
to ‘Deen’, but there are two records equal to that name, and in this case, only
the one specific one must be changed so the sName column needs to be
specified. In the example, this is equal to ‘Fuel’. This only returns one record
and therefore sets the value of the fName column in that record to ‘Dean’.
More than one table can be updated at a time if they can be related somehow
(this will be explained in Unit 6 ‘Joins and Views’).

3.3 DELETE
3.3.1 Difference between DELETE and DROP
The DELETE statement is very different to the DROP statement because the
DELETE statement is used to remove one or more records from one or more
tables at a time, as opposed to deleting an entire object (table/view/database,
etc.). To delete from more than one table at a time, the tables must be
logically related and the JOIN statement needs to be used (see Unit 6). The
DROP statement is used to remove a table or user definition from a database
or a database itself, when a table is DROP all data, indexes, relationships,
constraints, privileges etc.

The syntax example for the DROP statement is as follows:

USE lead_by_example
GO
DROP table Employee;
GO
--Dropping a database
USE MASTER;
GO
DROP DATABASE lead_by_example;
GO

The syntax for the DELETE statement is as follows:

DELETE FROM table_name


WHERE column_name = value

Criteria must be specified so that a specific row may be dropped. The more
criteria specified, the more specific it becomes, i.e. the DELETE statement will
delete every record that matches the criteria.

See the following example for more information:

USE lead_by_example
GO

Database Management Study Guide | V1.1 November 2015 Page 57 of 168


DELETE FROM campus
WHERE cName = 'Oompaloompa'
GO

This will check the table campus for all records that have ‘Oompaloompa’ as a
cName value and remove them. Since there is only one record that matches
the criteria, only that one record will be removed. Use the AND operator for
multiple criteria.

NOTE
I recommend that you watch this video to learn efficient SQL rules and help
you write SQL queries more easily: WiseOwlTutorials, SQL Server Part1 –
Writing Basic Queries: here [Accessed: 21 September 2015]

3.4 Apply your knowledge


Insert data into the tables that you created earlier. Feel free to
add your own records. Remember that you cannot insert into a
foreign key column unless there is already data in its referenced
primary key.

USE lead_by_example
GO

INSERT INTO storeBranch


VALUES (001, 'Randpark','Gauteng')
GO

INSERT INTO storeBranch


VALUES (002, 'Durban','KZN')
GO

INSERT INTO storeBranch


VALUES (003, 'Tweebuffelsfontein','Freestate')
GO

--CREATE MORE BRANCHES OF YOUR OWN

INSERT INTO employee


VALUES (8763749987412, 'Davey','Jones', 001)
GO

INSERT INTO employee


VALUES (7763749987413, 'Jack','Sparra', 002)
GO
INSERT INTO employee
VALUES (8753653987416, 'Will','Turnip', 001)

Database Management Study Guide | V1.1 November 2015 Page 58 of 168


GO

INSERT INTO employee


VALUES (1763749787411, 'Elizabeth','Swamp', 002)
GO

--CREATE MORE OF YOUR OWN NAMES

INSERT INTO species


VALUES ('Frog') -- Notice that no value is entered for
-- the identity column
GO

INSERT INTO species


VALUES ('Rat')
GO

INSERT INTO species


VALUES ('Dog')
GO

INSERT INTO species


VALUES ('Cat')
GO

-- This code can be made simpler by not specifying


-- INSERT INTO so many times:

INSERT INTO pets


VALUES (012, 'Freddy',4,001),
--Freddy the cat at the Randpark branch
(013, 'Darryn',2,001),
--Darryn the rat at the Randpark branch
(014, 'Jonny',1,002),
--Jonny the frog at the Durban branch
(015, 'Felix',3,002)
--Felix the dog at the Durban branch
GO

When the species table was inserted into, information was not entered into
the IDENTITY column. You need an explicit statement to force data entry into
an identity column. See the following:

SET IDENTITY_INSERT table_name ON


INSERT INTO table_name
VALUES ( Identity_value, other_value, other_value. . .)

This forces the entry into the IDENTITY column of the table (be warned: you
have to make sure that the value you are going to insert is unique).

Database Management Study Guide | V1.1 November 2015 Page 59 of 168


Now that you have populated your database with information, you can delete
or change this information as follows:

USE lead_by_example
GO

UPDATE species
SET speciesText = 'Lion'
WHERE speciesID = 4
GO

The table species has had its value for the speciesText changed from ‘Cat’ to
‘Lion’. A more detailed UPDATE statement using JOINs will be used in Unit 6.

Try to work out how to DELETE a record from one of your tables. The following
code shows how this is done:

USE lead_by_example
GO

DELETE FROM storeBranch


WHERE storeID = 003
GO

This deletes the record 003, Tweebuffelsfontein, Freestate from the table
storeBranch. This is acceptable because there are no records referencing it. If
a record that is being referenced needs to be deleted, the referencing record
would need to be deleted first. This can be done easily by using the JOIN
statement (explained in Unit 6).

Required reading
Chapter 20, 21 & 23 - Microsoft SQL Server 2014 UNLEASHED.

Database Management Study Guide | V1.1 November 2015 Page 60 of 168


Unit 4 – Data Manipulation
At the end of this unit you will be able to:

 Fully understand queries.


 Use different aspects of SELECT statement.
 Fully understand control of flow statements.

4.1 SELECT statements


A query retrieves a set of data from a database. It can be from one table or
many tables. The type of data that is retrieved depends on what the query
specifies. Queries can be used to retrieve specific data.

NOTE
In this unit, a database called lead_by_example is used.

In T-SQL, the SELECT statement is used to query the database. The syntax is
as follows:

SELECT column_list
FROM table_name

This syntax is only for querying data from one table. Querying from more than
one table will be discussed at a later stage. Ensure that the database that
needs to be queried from is the one that is being used by the SQL Server
Management Studio (USE statement). The asterisk (*) can be used to select all
the columns. The syntax is as follows:

USE lead_by_example
GO

SELECT *
FROM student
GO

This retrieves every row (all of the columns) from the table student. Take
note that the lead_by_example database is being used. If this database was
not specified, it would give the error that no table by that name exists.

The results are shown in Figure 13.

Database Management Study Guide | V1.1 November 2015 Page 61 of 168


Figure 13  Results from student

4.1.1 The WHERE clause


The standard SELECT statement, without qualification, will retrieve all of the
rows from the specified table. If, however, only specific records need to be
retrieved, the WHERE clause needs to be used. The syntax is as follows:

SELECT column_list
FROM table_name
WHERE column_name = value
GO

This will select the whole record that has the value of the specified column
equal to the value specified. An example is:

USE lead_by_example
GO

SELECT *
FROM employee
WHERE campusID = 1
GO

This retrieves all the records under employee that are at the Randburg campus
(id 1). The result is shown in Figure 14.

Figure 14 – Results of employees at campus id 1

The LIKE command can be used if it is unclear what needs to be queried. This
is a very useful command that is easy to use:

USE lead_by_example
GO

SELECT *
FROM employee
WHERE empName LIKE '%u%'

Database Management Study Guide | V1.1 November 2015 Page 62 of 168


GO

This will select all the names that contain the letter u.

The LIKE wild cards are explained in Table 4 - LIKE wildcards.

Table 4 - LIKE wildcards

Wildcard Function
% Percentage Any number of characters
_ Underscore Any single character
[] Square brackets Single character within in a range
[^ ] A carat enclosed by square Excluding a single character within a
brackets range

4.1.2 Column order


The columns that you see can be put in a custom order if desired. This is useful
if the e-mail column needs to be first, and some columns need to be left out.
The way to do this is to specify the columns that need to be included in the
desired order.

When selecting, it is possible to use the OR and AND operators. Use the AND
operator to further limit the result set. The AND operator requires that all
conditions are met. The OR operator returns rows that meet any of the given
conditions. This is an example of putting columns in a custom order:

USE lead_by_example
GO

SELECT empEmail, empName


FROM employee
WHERE campusID = 1
GO

The result of the code is shown in Figure 15- Including certain columns.

Figure 15- Including certain columns

Notice that it has left out the empNum and campusID columns because
those columns are not specified in the query.

Database Management Study Guide | V1.1 November 2015 Page 63 of 168


4.1.3 Built-in functions
In T-SQL, there are a number of built-in functions. Among these built-in
functions are aggregate functions and scalar functions.

4.1.3.1 Aggregate functions


Aggregate functions are generally used to work with numbers and return a
single value by calculating values in columns. Some aggregate functions in SQL
are listed in Table 5 – Aggregate functions.

Table 5 – Aggregate functions

Function Return value


AVG() Average value
COUNT() Number of rows
MAX() Largest value
MIN() Smallest value
SUM() Sum

4.1.3.2 Scalar functions


Scalar functions require an input to calculate a value. They return only one
value, and are usually used with text data types. A few scalar functions that
could prove useful are listed in Table 6 - Useful scalar functions.

Table 6 - Useful scalar functions

Function Return value


PI Pi
SQRT The square root of a number
CAST Casts a value from one data type to another
GETDATE The current date and time

See Books Online for more built-in functions. String functions, for example, can
be useful. With these functions, text data can be manipulated extensively.

4.1.3.3 User-defined functions


In some instances, the built-in functions do not match the needs of the user;
this calls for a user-defined function. User-defined functions can be called
anywhere in the database. Stored procedures and user-defined functions will
be covered in Unit 7.2.

Database Management Study Guide | V1.1 November 2015 Page 64 of 168


4.1.4 GROUP BY and HAVING
The GROUP BY clause is used to create groups of data that have a column
value in common. In other words, it combines all the records that have the
same value in a specified column into one record. Let us first make and
populate our tables.

USE lead_by_example
GO
CREATE TABLE products
(
productID INT NOT NULL,
productName VARCHAR(25) NOT NULL,
price MONEY NOT NULL CHECK(price>0),
proDescrp TEXT ,
CONSTRAINT products_pk PRIMARY KEY(productID)
)
GO

CREATE TABLE sales


(
salesNO INT NOT NULL,
sales_quant INT NOT NULL,
quantSold INT ,
salesPrice MONEY NOT NULL CHECK(salesPrice>0)
CONSTRAINT sales_pk PRIMARY KEY(salesNO)
CONSTRAINT sale_fk FOREIGN KEY(sales_quant) REFERENCES
products(productID) ON DELETE CASCADE
)
GO
--Populate the tables

USE lead_by_example
GO

INSERT INTO storeBranch


VALUES (101, 'Zama Zama store','Vorna Velly', '0119876453'),
(100, 'Checkers store','Durban', '0419876401'),
(111, 'Pick n Pay','Sunny side', '0129870054'),
(104, 'Nandos','Fort gale', '04719844352')

GO

INSERT INTO products


VALUES (012, 'Bread','12', 'Brown Bread')
GO

INSERT INTO products


VALUES (013, 'Milk','18', 'Full Cream')
GO

Database Management Study Guide | V1.1 November 2015 Page 65 of 168


INSERT INTO products
VALUES (014, 'Chocolate','199', 'Creamy Toasted')
GO
INSERT INTO products
VALUES (015, 'TV set','5000', '3D Plasma')
GO

INSERT INTO sales


VALUES
(101, 012, 100,'stocked', 12),
(102, 013, 200,' ', 8),
(103, 014, 600, ' ',122),
(104, 015, 900, ' ', 99)

GO

Now, this example shows the difference between when GROUP BY is used and
when it is not:

USE lead_by_example
GO

SELECT storeBranch.storeName, sales.sales_quant AS quantSold


FROM sales
JOIN storeBranch
ON sales.salesNO = storeBranch.storeID

The result of this code is shown below in Figure 16 - The store names and
quantity sold.

Figure 16 - The store names and quantity sold

The GROUP BY clause can now be used to add up the total qtySold for each
stor_name. See the following example:

USE lead_by_example
GO

SELECT storeBranch.storeName, SUM(sales.sales_quant) AS


quantSold
FROM sales
JOIN storeBranch
ON sales.salesNO = storeBranch.storeID

Database Management Study Guide | V1.1 November 2015 Page 66 of 168


GROUP BY storeBranch.storeName

The result of GROUP BY is shown in Figure 17  GROUP BY.

Figure 17  GROUP BY

This may look like a very complicated query but it is a lot easier to understand
if it is broken down into simpler components. The GROUP BY clause groups the
stores together by stor_name of the stores table (specified on the last line).
The purpose is only to display each stor_name once and the SUM(sales.qty)
for each stor_name. The SUM command adds up the values that are put into
the brackets, sorted according to the GROUP BY clause, i.e. in this case, it
would add the quantities sold for each stor_name. An aggregate function such
as SUM may be accompanied by a GROUP BY clause.

The AS keyword next to the SUM function is called an alias. Aliases are used to
simplify long or complex table names or column names. It temporarily names
the column or table to the specified value - in this case, qtySold.

HAVING works in a similar manner as the WHERE clause, but HAVING can use
aggregate functions and WHERE cannot. The HAVING clause must also be
accompanied by the GROUP BY clause. The following example performs exactly
the same action as the previous query, but it uses the HAVING clause and alias
naming; the results are shown in Figure 18.

USE lead_by_example
GO

SELECT st.storeName, SUM(sa.sales_quant) AS quantSold


FROM sales AS sa
JOIN storeBranch AS st
ON sa.salesNO = st.storeID
GROUP BY st.storeName
HAVING SUM(sales_quant) >12

Figure 18  Only where SUM (sales.qty) >12

Database Management Study Guide | V1.1 November 2015 Page 67 of 168


The HAVING clause makes sure that the rows retrieved are only those that
have the SUM (sales.qty) greater than 12.

4.1.5 CUBE and ROLLUP


CUBE and ROLLUP are used for summarising data, and getting more
information for quick and easy viewing. They are similar, but CUBE is slightly
more detailed than ROLLUP. They essentially add up a total for each column,
including a summary of each row. CUBE and ROLLUP have to be used with the
GROUP BY clause.

4.1.6 ORDER BY
The ORDER BY clause is very different to the GROUP BY clause. The ORDER BY
clause sorts the rows retrieved by the specified column in ascending or
descending order. Use the keyword DESC to sort the rows in descending order.
If DESC is not specified, it is set to ascending order by default.

See the following example (results shown in Figure 19 - Descending order):

USE lead_by_example
GO
SELECT storeName
FROM storeBranch
ORDER BY storeName DESC

GO

Figure 19 - Descending order

4.1.7 TOP N
The N in TOP N is a placeholder for a number. TOP N displays the top N of a
query. By itself, it provides no organisation and will simply search the tables
and retrieve the number of columns in the order that they are in the table.
When TOP N is combined with ORDER BY, a real representation of the top
values specified in the ORDER BY statement can be seen. See the following
example (results shown in Figure 20  TOP N).

USE lead_by_example
GO
SELECT TOP 3 storeName

Database Management Study Guide | V1.1 November 2015 Page 68 of 168


FROM storeBranch
GO

Figure 20  TOP N

This has retrieved the top three records as they appear in the table. Using
ORDER BY, the query first sorts the results and then retrieves the top three
records according to stor_name (results shown in Figure 21).

USE lead_by_example
GO
SELECT TOP 3 storeName
FROM storeBranch

ORDER BY storeName DESC

GO

Figure 21 – TOP N with ORDER BY

This has sorted the results in descending (z to a) order and then retrieved the
top three.

4.1.8 DISTINCT
The DISTINCT keyword is used only if unique values need to be selected. This
means that no duplicate values will be retrieved. This is usually used with the
ORDER BY clause. As soon as the first unique record is found, it will ignore all
other records. The syntax for this is:

SELECT DISTINCT column_names


FROM table_name
ORDER BY column_name

Database Management Study Guide | V1.1 November 2015 Page 69 of 168


4.2 SELECT INTO
SELECT INTO takes the results of the SELECT statement and transforms those
results into a permanent or temporary table. Most of the syntax of the SELECT
INTO statement is the same as the SELECT statement. The only difference is
the INTO clause. Specify a new table name for the data to go into. The table
name cannot be one that already exists. Executing the SELECT INTO statement
will create the specified table. The name of each column will be the same as
those specified in the SELECT list. SELECT INTO can also be used to create a
temporary table. To do this, insert a hash symbol (#) before the first character
of the table name. Temporary tables are useful when you need to keep track of
information for the duration of a procedure. SQL Server automatically removes
temporary tables when it has finished using them.

NOTE
Tables created with SELECT INTO do not have indexes, primary keys, foreign
keys, default values or triggers. If any of these are required, the table should
be created using the CREATE TABLE statement and then the INSERT
statement should be used to fill the table with data.

Using SELECT INTO with temporary tables is useful when a very large data set
is reduced into a smaller set. For example, you may want to select a subset of
400 rows (sorted in three different ways) from a data set of 1 000 000 rows.
You can create a temporary table with only the 400 rows that you need, and
then perform the rest of the work using the temporary table. The syntax is as
follows:

SELECT column_name(s)
INTO new_table_name
FROM old_tablename

Here is an example of this:

USE lead_by_example
GO

SELECT *
INTO #tempTable
FROM employee
WHERE campusID = 1

In this example, the employees are selected from one campus and put into a
temporary table, which can be recognised by the # sign in front of the new
table name.

Database Management Study Guide | V1.1 November 2015 Page 70 of 168


4.3 Inserting from one table to another
Sometimes it may be required to only transfer certain information from an
existing table into another table. This can be done by combining the SELECT
statement with the INSERT statement. The syntax is as follows:

INSERT INTO table_name


SELECT column_name(s)
FROM table_name

4.4 Control of flow statements


Transaction statements are normally executed in sequential order. For
instance, on long store procedure or any statement created, line 1 will run
followed by line 2 and line 3, and so on. It is also normal that when deciding to
interrupt this process of flow, some special data processing may be needed
depending on received results or some action may need to be taken when a
certain condition is met.

Control statements are used to control execution flow within a T-SQL.


The following are some of the control statements we have in SQL Server:

 IF: this is one of the most frequently-used control of flow keywords. Like in
other programming languages, IF is used to test a Boolean condition. When
the result of the IF statement is True, the next proceeding line is executed.
 ELSE: the ELSE keyword can be combined with IF to execute logic when the
IF statement returns False. Else is an optional statement.

The following example demonstrates the use of IF and ELSE statements:

USE lead_by_example
GO
IF EXISTS(SELECT name FROM Employee WHERE surname = 'Zuma')
PRINT 'Found it Zuma'

ELSE
IF EXISTS(SELECT name FROM Employee WHERE name = 'Smith')
PRINT 'Found it Smith'

ELSE
PRINT 'None is found Found Smith or Jones'

 BEGIN…END: the BEGIN and END keywords are used to group multiple lines
into one statement block. An example of when statement blocks are
required is in the result of an IF ELSE statement. In this example, two
PRINT lines are wanted on True result. Nothing should be returned on a
False result.

Database Management Study Guide | V1.1 November 2015 Page 71 of 168


USE lead_by_example
GO

IF EXISTS(SELECT name FROM Employee WHERE name = 'Smith')


BEGIN
PRINT 'Found it'
PRINT 'Last name of Smith'
END

 GOTO: this defines a label and allows the code to jump to that label from
some other point. Cursors usually make use of the GOTO statement.
However, a GOTO can also be used alone.
 RETURN: when the RETURN keyword is reached, statement execution ends,
unconditionally. Any lines following a RETURN are not executed.

An example of a GOTO and RETURN statement:

DECLARE @Count int


SET @Count = 0

do_it_again:
IF @Count < 100
BEGIN
PRINT ‘Counting not complete'
RETURN
SET @Count = @Count + 1
END

IF @Count < 100


GOTO do_it_again

 WHILE: WHILE creates a loop that keep on executing until a Boolean False
condition is received. Like IF statements, WHILE statements execute while
the condition is true.

An example of a WHILE loop is:

DECLARE @Count int


SET @Count = 0

WHILE @Count < 100


BEGIN
PRINT 'Count up to 100'
SET @Count = @Count + 1
END

 BREAK and CONTINUE are used to exit, or continue executing WHILE or IF


statements.

DECLARE @Count int


SET @Count = 0

WHILE @Count < 100

Database Management Study Guide | V1.1 November 2015 Page 72 of 168


BEGIN
PRINT 'Count is less than 100'
SET @Count = @Count + 1
IF @Count > 10
BREAK
ELSE
CONTINUE
END

One of the problems with control of flow statements is that they can be
misused and one of the common errors is a missing BEGIN and END. Since T-
SQL does not indent flow statements well, confusion can occur if great care is
not taken. Regardless of these points, well-used control of flow statements can
transform Transact SQL statements into powerful programming structures.

4.5 Shortcuts
A few shortcut keys, which you might find useful when writing queries, are
listed in Table 7 – Shortcuts.

Table 7 – Shortcuts

Keyboard shortcut Function


CTRL + R Shows/hides the results pane.
Compiles and runs the current query. If there
F5 is text selected, it will only run the selected
text.
CTRL + SHIFT + U Makes the selected text uppercase.
F1 Gets help on the selected text.
CTRL +N Displays the New File dialog to create a file.
Displays the New Project dialog to create a
CTRL+SHIFT+N
new project.
CTRL+O Opens File dialog to open an existing file.
F4 Displays the Properties window.
CTRL+ALT+O Displays the Output window.
CTRL+Tab Toggle between opened tabs

See Books Online or search on the Web for more shortcuts.

Required reading
Chapter 34 2, 3 & 8 - Microsoft SQL Server 2014 UNLEASHED.

Database Management Study Guide | V1.1 November 2015 Page 73 of 168


4.6 Test yourself
1. You have been assigned to develop a database for the CTI Education
Group. The database must consist of the following tables: campuses,
employees, students, sales and exams.
2. Draw an ER diagram for all your tables.
3. Use appropriate constraints and populate your tables.
4. Display the top 10 students with an exam mark that is greater than 70
from a specific campus.

Database Management Study Guide | V1.1 November 2015 Page 74 of 168


Unit 5 – Building and Maintaining Indexes

At the end of this unit you will be able to:

 Fully understand pages and extents.


 Create and manage indexes.
 Ensure data integrity.

5.1 Pages and extents


When the SQL Server data engine stores the data, it uses objects named
pages and extents to order the data in an efficient way.

5.1.1 Pages
In an SQL Server data file, pages are the smallest unit of storage. A page
belongs to a specific object. Each page starts with a page header of 96 bytes
and is 8 192 bytes (8 KB) in total. This means that each page can hold 8 096
bytes of data. Before SQL Server 7.0, a page was 2 KB. Two types of pages
will be discussed:

 Data pages – These pages contain most of the data that is inserted into
tables. The only user-entered data that is not stored in a data page is text
and image data, because both are usually large enough to warrant their
own pages.

 Index pages – These pages are used to accelerate data access. Indexes are
stored separately from data in their own page type.

5.1.2 Extents
To avoid fragmentation, SQL Server assigns space in extents. An extent is a
collection of eight contiguous pages (64 KB). When more space within a data
file is needed for an object, at least one full extent is allocated. Even though a
page belongs to a specific object, an extent can contain pages belonging to
many different objects.

There are two types of extents.

 Uniform extents are entirely owned by one object. For example, if a


single table owns all eight pages, it would be considered a uniform extent.

 Mixed extents are used for objects that are too small to fill eight pages by
themselves.

Database Management Study Guide | V1.1 November 2015 Page 75 of 168


5.2 Creating an index
An index is a data structure that improves data retrieval speed in a database
table. Indexes can be categorised into two primary types: clustered and non-
clustered.

Clustered indexes determine the order of rows in a table. There can only be
one clustered index for each table because the rows can only be in one order.
These are used by default in a table when a primary key is created and
therefore are unique by default.

Non-clustered indexes do not change the order of rows in a table and


therefore more than one non-clustered index can be created per table. Non-
clustered indexes are useful if the index is highly selective; especially if one
record will be returned.

The main difference between these two indexes is that clustered indexes are
unique. Any given table can only have one clustered index. The leaf level of a
clustered index is the actual data. On the other hand, the non-clustered index
leaf level is a pointer to the data in rows; so we can have as many non-
clustered indexes as we like on a database.

The purpose of clustered and non-clustered indexes is to improve the way


Microsoft SQL Server Database Engine accesses the data. Indexes are created
on a column or columns on tables and views.

5.2.1 Choosing columns to index


Choosing which columns to index is very important. Good candidates for
indexes include:

 Columns on which the ORDER BY clause is used.


 Columns on which the GROUP BY clause is used.
 Columns on which the WHERE clause is used, i.e. columns with high
selectivity.

In general, one should index columns that are included in the search criteria
using conditions that do not include functions or calculations.

Searchable arguments (SARGs) are the types of arguments that can be used
by SQL Server using indexes. For example, WHERE emp_lname LIKE ‘W%’
is searchable, but WHERE emp_lname LIKE ‘%w%’ is not. In the first
example, the emp_lname column is searched for values that begin with a W.
The second example searches the emp_lname column for values that contain
a W. Imagine if one tried to do a search like this in the White Pages telephone
book. The second condition would not be found easily and SQL Server would
have the same problem. Searching for only the first letter narrows the search
quite considerably.

Database Management Study Guide | V1.1 November 2015 Page 76 of 168


Not every column is a good candidate for indexing. The following columns
should not be indexed:

 Text, image or bit data-type columns.


 Columns that are not unique (such as male and female).
 Columns that are large in size (such as char (200)).

Each index introduces maintenance, space usage and performance issues.


Generally, it is recommended that you do not have more than three or four
indexes on a table.

The syntax for creating an index is as follows:

CREATE [ UNIQUE ] [ CLUSTERED | NONCLUSTERED ] INDEX


index_name
ON <object> ( column [ ASC | DESC ] [ ,...n ] )
[ INCLUDE ( column_name [ ,...n ] ) ]
[ WHERE <filter_predicate> ]
[ WITH ( <relational_index_option> [ ,...n ] ) ]
[ ON { partition_scheme_name ( column_name )
| filegroup_name
| default
}
]
[ FILESTREAM_ON { filestream_filegroup_name |
partition_scheme_name | "NULL" } ]

[ ; ]

<object> ::=
{
[ database_name. [ schema_name ] . | schema_name. ]
table_or_view_name
}

NOTE
SQL Server has a data structure called b-tree that keeps data sorted for
easy searching. Indexes are organised as B-trees, and each page in the
index is called an index node. The bottom level nodes are called leaf nodes,
while the top level is called the root node.

The following options are not all the options that are available. See Books
Online for more options.

FILLFACTOR = x is used for determining to what percentage the index page is


filled.

Database Management Study Guide | V1.1 November 2015 Page 77 of 168


PAD_INDEX is used for determining whether or not the data page must follow
the FILLFACTOR of the index page.

IGNORE_DUP_KEY is used for ignoring the error that the table will throw when
a value that needs to be unique conflicts with another. The table will give a
warning and continue, not inserting that row. If this option is not specified, the
whole statement will crash.

DROP_EXISTING is used for increasing the performance when adding a


clustered index by converting from an existing non-clustered index.

Use lead_by_example
GO

CREATE INDEX idx_emp


ON employee(empEmail)
GO

This will create a non-clustered index on the column empEmail in the table
employee. If clustered is not mentioned, the compiler automatically uses a
non-clustered index. The reason behind this is that a clustered index defaults
to the primary key column.

It is possible to create a composite index (an index made up of more than one
column). This is used when one column is not unique enough for a clustered
index. An example would be to combine a name and a surname into one index.
If a composite index is created, all that needs to be done is to separate the two
columns inside the brackets with a comma.

An example of creating a clustered index is:

USE lead_by_example
GO
-- Create a table if you do not have an existing table.

CREATE table dbo.student


(stuNumber int NOT NULL,
name nvarchar (10) NOT NULL,
surname nvarchar (10) NOT NULL,
course nvarchar (10) NOT NULL
)
GO
-- Creating a clustered index called IX_student_StuNumber

CREATE CLUSTERED INDEX IX_student_stuNumber


ON dbo.Student
( stuNumber );
GO

Database Management Study Guide | V1.1 November 2015 Page 78 of 168


5.2.2 Create non-clustered index in SQL Server Studio
 In Object Explorer, expand the table exams on which you want to create a
non-clustered index.
 Right-click the Indexes folder, point to New Index and select Non-
Clustered Index.
 On the New Index dialog box, on the General page, enter new Index
IX_ExamsDates in the Index name box.
 Under Index key columns from, click Add.
 In the Select Columns from exams table box, select the checkbox on
table column to be added to the non-clustered index, and click OK.
 In the New Index dialog box, click OK.

Figure 23 – Creating an index

As we mentioned before, MS SQL Server allows creation of multiple non-


clustered indexes on a single table. It is important to consider replacing an
existing index before adding new indexes. The following code segment shows
the creation of a non-clustered index:

-- Creating a Non clustered index of the exams table


USE lead_by_example
GO
CREATE NONCLUSTERED INDEX IX_examDate_Date
ON exams
(
examDate
);

5.2.2.1 Adding filters to indexes


Filtered indexes optimise the performance of non-clustered indexes by using
filtered predicate to refine data down to small subsets. This results in a smaller
index which requires less storage and maintenance and offers improved
performance. Filtered indexes are good for columns that contain smaller sets of
relevant values for queries.

5.2.2.2 Steps to follow to add a filter index using SSMS


 Open SSMS and connect to a server.
 Expand the Databases folder and expand the Lead_by_example database.

Database Management Study Guide | V1.1 November 2015 Page 79 of 168


 Expand the Tables folder and expand the exams table.
o Expand Indexes folder and right-click the IX_ExamDate_Date index
and select Properties.
o Click Filter in the Select a Page section of the index Properties dialog
box.
o Type OnlineExamFlag = 0 in the Filter Expression text box and click
OK twice.

To add a filter to an index using T-SQL, open Query Editor in SQL Server
Management Studio and type the following T-SQL code:

-- Creating a Non clustered index of the exams table


USE lead_by_example
GO
CREATE NONCLUSTERED INDEX IX_examDate_Date

ON exams

examDate(courseId, examNumber)

WHERE(OnlineExamFlag=0)
WITH(DROP_EXISTING = ON);

5.2.2.3 Disabling and dropping indexes


After an index is created, a database administrator or developer may realise it
is no longer needed. It is only safe to delete it if it will no longer be needed. In
cases of performance evaluation it may be good to disable the index without
removing it. These actions can be done by using either SSMS or T-SQL. We will
use the T-SQL code to demonstrate the process.

When an index is disabled or dropped, the table name that the index is
attached to needs to be specified. This can be done as follows:

--Disable IXExamDate index on the eexams table


USE lead_by_example
GO
ALTER INDEX [IXExameDate_Date]
ON exams DISABLE
GO
--Now let us enable the index
-- Enable an IXExamDate_Date index on the exams table
ALTER INDEX[IXExamDate_Date]
ON exams REBUILD

Now let us delete the index:

Database Management Study Guide | V1.1 November 2015 Page 80 of 168


--Deletes the IXExamDate_Date index on the exams table.
USE lead_by_example
GO
DROP INDEX IXExamDate_Date
ON exams
GO

This will drop the index that was created earlier on the exams table.

5.3 Apply your knowledge


You can now create an index of your own. The following example
creates a composite index for the database lead_by_example:

USE lead_by_example
GO

CREATE UNIQUE INDEX idxcomp_branch


ON storeBranch(storeName, storeArea)
WITH IGNORE_DUP_KEY
GO

This creates a UNIQUE non-clustered index on the storeBranch table. This


means that the combination of both the storeName and the storeArea must
be unique. The IGNORE_DUP_KEY option is also used here.

The space in a database is organised into pages and extents.

5.3.1 Other questions


1. Space inside a database is organised into ___________ and
_____________.

2. What is the size of an extent?

a) 2 KB
b) 8 KB
c) 64 KB
d) 128 KB

3. An index is a/n ________ that amends data retrieval speed in a database


table.

4. An extent is a collection of eight contiguous pages. Describe the two types


of extents.

5. Indexes can be categorised into two primary types. What are these types?

Database Management Study Guide | V1.1 November 2015 Page 81 of 168


6. How would you differentiate between the two types of indexes identified
above?

7. Choosing columns to index is very important. Provide 3 columns that can


be a good choice for this process.

8. With the use of T-SQL code, demonstrate the process of disabling and
deleting an index.

Required reading
Chapter 22, 3 & 8 - Microsoft SQL Server 2014 UNLEASHED.

Database Management Study Guide | V1.1 November 2015 Page 82 of 168


Unit 6 – Joins and Views

At the end of this unit you will be able to:

 Join tables.
 Create subqueries.
 Create views.

6.1 Joining tables


So far you have seen the JOIN statement used when a query selects from
more than one table, but this has not been examined thoroughly. The JOIN
operation can also be used to UPDATE and DELETE, not only SELECT.

Five different types of JOIN statements exist:

 INNER JOIN
 LEFT OUTER JOIN
 RIGHT OUTER JOIN
 FULL OUTER JOIN
 CROSS JOIN

6.1.1 INNER JOIN


INNER JOIN iterates through each row in the two tables and matches the rows
that are common to both. It looks at each row in both of the tables and, if the
condition is met, the rows are included in the result set. This is the JOIN most
commonly used in SQL. When the type of JOIN is not specified, then SQL
automatically uses INNER JOIN. Syntax is as follows:

SELECT column_list
FROM table_name1
INNER JOIN table_name2
ON table_name1.column_name = table_name2.column_name

UPDATE table_name
WHERE column_name
INNER JOIN table_name2
ON table_name1.column_name = table_name2.column_name

6.1.2 OUTER JOINS


RIGHT OUTER JOIN, LEFT OUTER JOIN, and FULL OUTER JOIN are used for
including rows that do not have anything in common with the other table. This
is useful when it is necessary to see the rows that have things in common

Database Management Study Guide | V1.1 November 2015 Page 83 of 168


together with the ones that do not. RIGHT OUTER JOIN includes the rows from
both tables that are common to one another and also all the rows in the right
table that do not correspond with rows on the left. LEFT OUTER JOIN also
includes the rows from both tables that are common and rows in the left table
that do not correspond with ones on the right. FULL OUTER JOIN includes all
the rows in both tables that correspond with each other as well as all rows that
do not. The rows that do not have a corresponding row will be given a NULL
value where the corresponding columns should be.

SELECT column_list
FROM table_name1
OUTER_JOIN_TYPE table_name2
ON table_name1.column_name = table_name2.column_name

6.1.3 CROSS JOIN


CROSS JOIN is used for including every single possible combination of rows
involved with the query. A CROSS JOIN has no ON statement. If there are x
rows in the first table and y rows in the second table, the result set will contain
x * y rows. The syntax is as follows:

SELECT column_list
FROM table_name1
CROSS JOIN table_name2

INNER JOIN is the most common join and will be used most often in this
course.

NOTE
Since more than one table is involved in JOIN statements, the table name
needs to be specified before each column; otherwise, the compiler will not
know which table the column belongs to. This only applies to columns that
have the same name, but it is good practice to do it for all columns, i.e.
table_name.column_name

6.1.4 Examples of using INNER JOIN


The following code shows examples of using INNER JOIN. The other JOINs will
not be shown, but the Internet is full of examples of using different types of
JOINs.

Using JOIN to link tables when selecting:

Database Management Study Guide | V1.1 November 2015 Page 84 of 168


USE lead_by_example
GO

SELECT student.fName, student.sName, campus.cName


FROM student
INNER JOIN campus
ON student.campusID = campus.iden

Line 5 shows the first (left) table in the JOIN statement.

Line 6 shows that an INNER JOIN is being used. Note that only the word JOIN
is used and SQL Server automatically assumes it to be an INNER JOIN. The
second (right) table that is involved in the JOIN is also mentioned here.

Line 7 specifies where the two tables must be joined. The student.campusID
is the foreign key in the table student. This is linked to the campus.iden,
which is the primary key in the table campus. Note that the JOIN does not
have to be the same as the relational link of the two tables, but it is most often
like that.

The result set is shown in Figure 24 – Result of JOIN.

Figure 24 – Result of JOIN

Four records are retrieved. The first column is the student name and the last
column is the campus name from the campus table. The link is the campusID
in both tables.

6.1.5 Joining multiple tables


It is possible to join multiple tables in the same statement. Multiple tables
need to be joined when selecting from tables that have an intersection table in
between them. An example will be shown later in this unit.

6.2 Sub-queries
6.2.1 Normal sub-queries
A SELECT statement nested inside another SELECT statement is commonly
referred to as a subquery. A subquery can produce the same results as a JOIN
operation and is always enclosed within parentheses. Most SELECT statements
that have been used so far have retrieved data from individual tables.

Database Management Study Guide | V1.1 November 2015 Page 85 of 168


Suppose someone wanted a list of all the authors who live in the same state as
the bookstore that sells their books. This is what they would have to do:

1. Retrieve the states from the stores table.


2. Retrieve the author information for all the authors who live in the same
state as was returned in the previous step.

Each step can be executed as a single query by using the results returned from
one SELECT statement to populate the WHERE clause of the next SELECT
statement, or both queries can be combined into a single statement. See the
following example:

USE lead_by_example
GO

SELECT * FROM employee


WHERE campusID IN(SELECT campusID FROM student
WHERE fName = 'Dean')
GO

The above code retrieves the employees from the campuses that have
students named Dean. This can be adapted in any way. Try to experiment with
it in order to familiarise yourself with the syntax.

6.2.2 Correlated sub-queries


A correlated subquery (also known as a repeating subquery) references a table
from the outer query and evaluates each row for the outer query. In this
aspect, a correlated subquery varies from a normal subquery because it
depends on values from the outer query. A normal subquery is executed
independently of the outer query. This means that a correlated subquery is
executed repeatedly, once for each row that might be selected by the outer
query.

6.3 Views
Views can be thought of as virtual tables or stored SELECT statements. The
only difference between a view and a select statement is that a view does not
actually contain any values. The values that appear to be in a view are actually
pointers to the real values in a table or tables. In other words, when the value
in the table is changed, the values in the view referencing it will also change.
The same thing is true if the values in a view are changed because the values
in the table are actually changed.

The syntax for creating views is the following:

Database Management Study Guide | V1.1 November 2015 Page 86 of 168


CREATE VIEW [owner.]view_name
[WITH attribute]
AS select_statement
[WITH CHECK OPTION]

attribute ::=
{ENCRYPTION | SCHEMABINDING}

Table 8 - Encryption and schemabinding

Attribute Description
Encrypts the CREATE VIEW statement in the syscomments
system table. Once a view definition is encrypted, it
Encryption
cannot be seen by anyone (even the view owner) and the
only way to decrypt it is to drop the view and recreate it.
The SCHEMABINDING attribute forces one to use two-part
names (owner.object) to refer to the base tables
Schemabinding referenced in your view’s SELECT statement. When this is
done, the schema of the base table cannot be changed or
dropped unless the view is dropped or altered.

Here is an example of a view (without encryption or schemabinding):

USE lead_by_example
GO

CREATE VIEW vw_Example


AS
SELECT employee.empName, campus.cName
FROM employee
JOIN campus
ON employee.campusID = campus.iden
GO

This creates a view that will contain all the last names of the employees and
the corresponding campuses. To see this, query everything from the view:

USE lead_by_example
GO

SELECT * FROM vw_Example


GO

This should retrieve all the rows of the view, as shown in Figure 25 –
vw_Example.

Database Management Study Guide | V1.1 November 2015 Page 87 of 168


Figure 25 – vw_Example

An advantage of this is that one can specify exactly what certain people are
allowed to access. Limiting the number of people who have access to the
database directly helps protect data against data redundancy and
inconsistencies.

6.4 Apply your knowledge


Create your own view for the database lead by example.
Create a new query and type the following:

USE lead_by_example
GO

CREATE VIEW vw_firstView


AS
SELECT pets.petName, species.speciesText,
storeBranch.storeName
FROM pets
JOIN species
ON pets.speciesType = species.speciesID
JOIN storeBranch
ON pets.store = storeBranch.storeID
GO

SELECT * FROM vw_firstView


GO

This script creates a view that will select every pet and show what species it is,
also including the store at which it is available. It then queries the whole view.
The output is shown in Figure 26 – vw_firstView.

Figure 26 – vw_firstView

Database Management Study Guide | V1.1 November 2015 Page 88 of 168


6.4.1 The following example shows how one can use the UPDATE
statement to update a view
 First create a database with the following tables:
o CLIENT
o INVOICEDETAILS
o STOCK
o INVOICE

CREATE TABLE stock


(
stockID SMALLINT PRIMARY KEY IDENTITY NOT NULL,
stockName VARCHAR(100) NOT NULL,
stockLevel SMALLINT NOT NULL CHECK (stockLevel > 0),
maxLevel SMALLINT NOT NULL CHECK (maxLevel > 0),
price SMALLMONEY NOT NULL CHECK (price > 0.00)
)
PRINT 'stock table created...'
GO

--Create client table


CREATE TABLE client
(
clientID SMALLINT PRIMARY KEY IDENTITY NOT NULL,
clientName VARCHAR(30) NOT NULL,
clientAddress VARCHAR(50) NOT NULL,
clientPhoneNo VARCHAR(14) NOT NULL
)
PRINT 'client table created...'
GO

--Create invoices table


CREATE TABLE invoice
(
invoiceID SMALLINT IDENTITY PRIMARY KEY NOT NULL,
clientID SMALLINT NOT NULL REFERENCES client(clientID),
invoiceDate SMALLDATETIME NOT NULL DEFAULT GETDATE()
)
PRINT 'invoices table created...'
GO

--Create invoiceDetails table


CREATE TABLE invoiceDetails
(
stockID SMALLINT NOT NULL REFERENCES stock(stockID),
invoiceID SMALLINT NOT NULL REFERENCES invoice(invoiceID),

Database Management Study Guide | V1.1 November 2015 Page 89 of 168


quantity SMALLINT NOT NULL DEFAULT 1,
PRIMARY KEY(stockID, invoiceID)
)
PRINT 'invoiceDetails table created...'
GO

 Next, insert data into the tables that you created earlier. Feel free to add
your own records. Remember that you cannot insert into a foreign key
column unless there is already data in its referenced primary key.

INSERT client
VALUES ('John Doe', ' 20 Small Str', '535-6548'),
('Sipho Gumede', '14 E Ville', '555-7548'),
('John Claw', ' 19 Ellof Str', '555-6785'),
('Musa Ndaba', '13 DM Ville', '555-4532'),
('Thuli Gumede', '12 Mandela drive', '555-2467'),
('Jonas Ntlapo', '09 Sisulu Str', '555-4761'),
('Jimmy Dludlu', ' 18 Walker street', '555-3479')
GO

--insert data into the invoices table


INSERT invoice
VALUES (6, '13-Jan-2014'),
(5, '21-Apr-2014'),
(3, '07-Feb-2014'),
(7, '26-Jul-2014'),
(4, '24-Sep-2015')
GO

--insert data into the stock table


INSERT stock
VALUES ('Hand Drill', 5, 2, 6500.00),
('Screw Driver Set', 3, 2, 3500.00),
('Safety Gloves', 3, 1, 340.00),
('Safety Hat', 3, 4, 150.00),
('Hammer', 4, 4, 20),
('Ear Plugs', 3, 1, 33),
('Yamahai 5 Star Drill bits', 2, 1, 18500.00),
('Air compressor', 6, 3, 9500.00)
GO

--insert data into the invoicedetails table


INSERT InvoiceDetails
VALUES (3, 4, 1),
(4, 4, 1),

Database Management Study Guide | V1.1 November 2015 Page 90 of 168


(5, 4, 1),
(6, 4, 1),
(5, 3, 1),
(4, 2, 2),
(7, 3, 1),
(1, 1, 1),
(2, 2, 1),
(4, 3, 1)
GO

 Using the sample data you inserted, create a view that will find all items
that are equal to, or under, their reorder levels. The view must return the
stockID, stockName, StockLevel and MaxLevel.

CREATE VIEW vwOrder


AS
SELECT stockID, stockName, stockLevel, maxLevel
FROM stock
WHERE stockLevel < MaxLevel
GO

SELECT * FROM vwOrder

Using the view you created, create a stored procedure that will restock all the
items in the stock table that need reordering by 4 units more than the
maximum level of each item. So, if the current stock level of a certain item is
3, and the reorder level is 10, then the new stock level must be 14.

CREATE PROC spRestock


AS
BEGIN
UPDATE vwOrder
SET stockLevel = maxLevel + 4
END

The new stock level must be 14 after the procedure has run successfully.

Required reading
Chapter 24 - Microsoft SQL Server 2014 UNLEASHED.

Database Management Study Guide | V1.1 November 2015 Page 91 of 168


Unit 7 – Stored Procedures and Iteration
Cursors

At the end of this unit you will be able to:

 Understand stored procedures.


 Create stored procedures.
 Fully utilise iteration.
 Understand how cursors work.

7.1 Stored procedures


Stored procedures are a set of SQL statements typically grouped together to
perform a specific routine. They usually provide the best performance because
they are pre-compiled, i.e. they do not have to be compiled every time they
are used. There is no need to write stored procedures over and over again;
hence they increase reusability of code. Permissions can be granted for stored
procedures, so this increases security. Stored procedures can be created in any
user-defined database and system database except the resource database. A
stored procedure can also be sent values that need processing. Microsoft SQL
Server 2014 has four types of stored procedures:

 User defined
 System
 Temporary and extended user-defined

The extended user-defined stored procedures have been replaced by common


language runtime (CLR) procedures. To read more about CLR procedures, visit
SQL Server Books Online on the ‘CLR Stored Procedure’ section.

The syntax for a stored procedure is as follows:

CREATE PROC[EDURE] procedure_name [; number]


[ {@parameter_name data_type}
[VARYING] [= default] [OUTPUT]
] [,...n]
[WITH {RECOMPILE | ENCRYPTION | RECOMPILE, ENCRYPTION}]
[FOR REPLICATION]
AS
sql_statement [...n]

7.1.1 Simple stored procedures


An example of a simple stored procedure would be:

Database Management Study Guide | V1.1 November 2015 Page 92 of 168


USE lead_by_example
GO

CREATE PROCEDURE spSimple


AS
SELECT *
FROM employee
ORDER BY empName

This will create a stored procedure that retrieves a simple query. Once created,
it may be executed using the EXEC keyword:

USE lead_by_example
GO

EXEC spSimple

This will execute the stored procedure that was created. It should give the
same results of the processing that the stored procedure did (see Figure 27 –
Results of spSimple).

Figure 27 – Results of spSimple

7.1.1.1 Working with stored procedures


Stored procedures can be created using T-SQL, but Microsoft SQL Server
Management Studio provides templates as a starting point.

 Open SSMS and connect to a server.


 On the Object Explorer window, expand the server node and expand the
Databases folder.
 Expand the Lead_by_example database and expand the Programmability
folder.
 Right-click the Stored Procedures folder and select New Stored
Procedure.
 A query window will open with a template that you can use as a starting
point for creating a stored procedure. You can modify the template to meet
your needs.

Database Management Study Guide | V1.1 November 2015 Page 93 of 168


Figure 28 – Create New Procedure

This template provides you with a well-detailed and preferred procedure


structure. In this way you do not have to worry about indentation and other
things like a comment section. You just insert your code and comments on the
provided sections.

7.1.2 Groups
After the procedure name, there is an option to specify the procedure’s
number. This is used when procedures need to be in a group. All the stored
procedures in a group are given the same name but a different number must
be given for each. See the following example:

USE lead_by_example
GO

CREATE PROCEDURE spGroup;1


AS
SELECT *
FROM employee
ORDER BY empName
GO

CREATE PROCEDURE spGroup;2


AS
SELECT *
FROM campus
GO

CREATE PROCEDURE spGroup;3


AS

Database Management Study Guide | V1.1 November 2015 Page 94 of 168


SELECT fName
FROM student

This has created three stored procedures in a single group spGroup. To


execute a procedure in a group, the name is specified and then the number is
specified, e.g. EXEC spGroup;2.

A procedure cannot be dropped from a group; the whole group needs to be


dropped. To do this, you merely reference the group name without any
numbers, e.g. DROP PROCEDURE spGroup.

7.1.3 Variables and parameters


A variable is a place in memory that is declared and given a name. This
variable can be referenced at any time inside the scope in which it was
declared. There are two types of variables: local variables and global variables.
A local variable has to be used inside a scope. To show the compiler that it is
a local variable, the @ symbol must be in front of the name. A global variable
uses @@ as the prefix to the variable name. When creating a variable, the
DECLARE keyword must be typed before the name of the variable. The
variable’s data type must follow its name.

In the simple stored procedure that was created earlier, a simple SELECT
statement was used. No matter how many times the procedure is executed, it
will always produce the same output. If a stored procedure needed to process
different information depending on the situation, the stored procedure would
require input. Parameters are used for this purpose. When specifying
parameters in a stored procedure, the @ symbol must be used because a
parameter is a type of variable and needs to be stored in memory temporarily.
See the following example:

USE lead_by_example
GO

CREATE PROCEDURE spAdd


@num1 INT,
@num2 INT,
@result INT OUTPUT
AS
SELECT @result = @num1+@num2

This example makes use of parameters and is quite a simple example that
does not need any extra variables declared within it. The parameters are three
numbers – two for input and one for output. Values need to be supplied to
execute this stored procedure:

Database Management Study Guide | V1.1 November 2015 Page 95 of 168


USE lead_by_example
GO

DECLARE @ans INT


EXEC spAdd 2, 4, @ans OUTPUT
print @ans

In this execution, two numbers are supplied for input and one local variable for
output. The variable has been declared beforehand as an integer. It is
necessary to use a variable here because that value is used later in the print
statement. When the execute statement is run, 6 is returned as the value of
the variable @ans.

7.1.4 Validation
Validation plays an important role in SQL Server. Validation refers to checking
whether the values about to be processed are valid according to the
requirements. An IF statement can be used to check whether a condition is
true or not. Use the BEGIN and END keywords if you want more than one thing
to happen if the condition is met. A WHILE loop will execute if a condition is
met and will repeat the code until the condition is not met.

For example, to calculate the randdollar exchange rate, the number entered
must be positive:

USE lead_by_example
GO

CREATE PROCEDURE spDollarToRand


@Dollars REAL,
@Rate REAL,
@Rands REAL OUTPUT
AS
IF(@Rate<0)
BEGIN
PRINT 'The Rand is stronger than the Dollar'
END
ELSE
BEGIN
PRINT 'The Dollar is stronger than the Rand'
END

IF(@Dollars<0)
BEGIN
RAISERROR('You cannot enter a negative value for
Dollars',16,1)
RETURN
END

Database Management Study Guide | V1.1 November 2015 Page 96 of 168


SELECT @Rands = @Dollars*@Rate
GO

This converts an amount of dollars into rands, depending on the rate of


exchange. There are two validation checks here. The first one checks whether
the rate is positive or negative. It prints out the stronger one, so that the user
can make sure that he or she entered the correct rate (positive or negative).
The second validation checks whether or not the number of dollars entered is
positive. If not, it raises an error and uses the RETURN statement to make
sure that it does not continue executing the code. The final SELECT statement
will only execute if the dollar amount is greater than zero and is used to assign
a value to the variable @Rands. When the end of the stored procedure is
reached, it returns the value of @Rands.

NOTE
The difference between results and output is that results return values in
table format. Output shows the print statements, warnings and errors in text
format.

Global variables can be used for retrieving a value as if it were a constant.


Global variables can be user-defined, but SQL Server has some useful built-in
global variables. Some are listed in Table 9 - Global variables.

Table 9 - Global variables

Variable Description
Returns the number of successful and non-
@@CONNECTIONS
successful logins since the start of the server.
Returns the error code for the last T-SQL
@@ERROR
statement that was executed.
Returns the last value that was inserted into a
@@IDENTITY
column with the IDENTITY property.
Returns the number of rows that were affected
@@ROWCOUNT
by the last T-SQL statement executed.
Returns information about the current instance of
@@SERVERNAME
SQL Server that is running.
Returns the current version of the database
@@VERSION
software.
Contains the number of active transactions for
@@TRANCOUNT
the current user.
Returns a code used in a cursor to check whether
or not fetching a row of data was successful. The
@@FETCH_STATUS
codes are:
0 – no error

Database Management Study Guide | V1.1 November 2015 Page 97 of 168


−1 – failed, or row out of range
−2 – row is missing

7.2 User-defined functions


When there is not a built-in function that matches the needs of the user, create
a user-defined function. The difference between a function and a procedure is
that a procedure may have many or no outputs, whereas a function always
returns one value. Creating your own function means that it can be called
anywhere in that database. When a function is created, a value must be
returned (it can be scalar or a table). If a function returns a table, it can be
one of two types:

 Inline table value functions – Return a table from a single SELECT


statement.
 Multi-statement table value functions – Created by more than one
SELECT statement; for example, some processing may be done on some
data or a temporary table could be created.

The following code shows how to create a user-defined function:

USE Lead_by_example
GO

CREATE FUNCTION RoyaltiesByAuthor()


RETURNS TABLE
AS
RETURN
(
SELECT authors.au_fName, authors.au_lname, titles.title,
(titles.ytd_sales * titles.price * titles.royalty /
100)AS 'RDate'
FROM authors, titles, titleauthor
WHERE authors.au_id = titleauthor.au_id
AND titles.title_id = titleauthor.title_id
)

This function returns a table. This means that when it is called, it should be
part of a query:

USE Lead_by_example
GO

SELECT au_fName, RDate FROM dbo.RoyaltiesByAuthor()

This will call the function and select what is specified from the table that is
returned. To create a function that returns a scalar value is quite simple. See
the following code:

Database Management Study Guide | V1.1 November 2015 Page 98 of 168


CREATE FUNCTION sumRDates()
RETURNS INT
AS
BEGIN
DECLARE @num INT
SELECT @num = SUM(RDate)
FROM dbo.RoyaltiesByAuthor()

RETURN @num
END
GO

This function simply adds up all the royalties of all the authors (using the
previous user-defined function). The variable is needed to store an integer
value from the table, and then return the value. A mere SELECT statement
cannot be returned because it will return a temporary table, and this is not
what was specified.

7.3 Iteration
SQL is a set-based language, meaning that it retrieves sets of data. This is
what makes it so different from other languages. Sometimes you need to
iterate through the rows in a table. This is very important when you need to
print a report for each row - for example, a list of all users with their details.

Use a standard WHILE loop and assign a new column to a table to show if that
row has been processed or not. See the following example:

USE lead_by_example
GO

--Create a temp copy of table


SELECT *
--the empNum column could have been selected by itself
INTO #tempEmp
FROM employee
GO

--Add a column called Printed


ALTER TABLE #tempEmp
ADD
Printed SMALLINT
GO

--Set all the values in that column equal to 0


UPDATE #tempEmp
SET Printed = 0
GO

Database Management Study Guide | V1.1 November 2015 Page 99 of 168


-- SQL does not print column values directly, requires
variables.

--Declares variables for use in printing


DECLARE @EmpEmail VARCHAR(30)
DECLARE @EmpName VARCHAR(30)
DECLARE @Campus VARCHAR(30)
DECLARE @ID INT
--Creates a while loop to iterate through the rows
WHILE EXISTS (SELECT *
FROM #tempEmp
WHERE Printed = 0)
BEGIN
SELECT @ID = MIN(empNum)
FROM #tempEmp
WHERE Printed = 0
SELECT @EmpEmail = #tempEmp.empEmail,
@EmpName = #tempEmp.empName,
@Campus = campus.cName
FROM #tempEmp
JOIN campus
ON #tempEmp.campusId = campus.iden
WHERE @ID = #tempEmp.empNum

--Prints out the report for each employee


PRINT 'Hi '+@EmpName
PRINT ''
PRINT 'We have emailed this letter to '+@EmpEmail
PRINT 'Let us know if this changes'
PRINT 'Let us know about any problems that you are
experiencing in the' + @Campus + ' campus'
PRINT 'Regards, Head Office'

UPDATE #tempEmp
SET Printed = 1
WHERE @ID = empNum
END
GO

--Drops the temp table table


DROP TABLE #tempEmp
GO

As long as there are rows that have the column Printed equal to 0, the code
inside the loop will repeat. The first statement in the loop sets the variable
@ID to the lowest primary key that has its Printed value equal to 0. One may
think that this is a useless statement because the empNum column increases
in ascending order (1, 2, 3, 4, etc.), but most of the time the primary key
column is not consistent (e.g. 1, 3, 5, 8, 9). This statement is prepared for any
situation.

Database Management Study Guide | V1.1 November 2015 Page 100 of 168
Now that the variable @ID has been given a value; the whole row is selected,
including the campus name that the employee belongs to. The values from
each column in that row are assigned to the variables that were declared
earlier.

The last step is to print out the information in a formal manner and finally
change the value of Printed to 1 to indicate that it has been printed and does
not need to be read again. The next time the @ID variable is set to the lowest
primary key, it will exclude this row.

After the loop is complete, the temporary table is dropped.

7.4 Cursors
Cursors allow you to work with data iteratively (one row at a time), rather than
in sets. Traditionally, SQL is a set-based language. For example, when you
execute a SELECT statement, it returns a set of rows. The most fundamental
data manipulation statements (SELECT, UPDATE, INSERT and DELETE)
normally work with entire sets of data at a time. Cursors enable you to define
a set of data and work with that data one row at a time. To work with data in
this way, you generally need to use some form of a loop to iterate through the
set, which is why using cursors is called an iterative approach. A cursor is a set
of rows together with a pointer that identifies a current row.

SQL Server can work with data as a set faster than with an iterative series of
commands. Most iterative solutions can be expressed as a set; therefore,
cursors should be avoided. Thinking in terms of whole sets of data is what
makes Structured Query Language so different from traditional programming
languages.

There are two ways to declare a cursor: you can use a syntax based on the
ANSI SQL standard or an extended syntax which is not ANSI SQL-compatible.

The ANSI SQL syntax for declaring a cursor (as specified in SQL Server Books
Online) is as follows:

DECLARE cursor_name [INSENSITIVE] [SCROLL] CURSOR


FOR select_statement
[FOR {READ ONLY | UPDATE [OF column_list]}]

For a complete listing of keywords and their explanations (including the


extended syntax), refer to the section ‘DECLARE CURSOR’ in SQL Server Books
Online.

Database Management Study Guide | V1.1 November 2015 Page 101 of 168
NOTE
If the SCROLL option is not used, the cursor only positions itself one row at a
time in the forward direction. This is known as a forward-only cursor.
Because positioning in this situation is so predictable, no keysets need to be
created and stored in tempdb, allowing the cursor to be opened more quickly
and positioning speed to be much faster. The predictability of scrolling through
a forward-only cursor is also why such a cursor can be updateable even if no
unique indexes are available.

The system database tempdb is a global resource where temporary tables and
other values are stored. Data is stored per user per session, so that any
number of users may, for example, create a table called #tempTable, each
instance of which will be visible only to the user who created it. A user’s
temporary tables and other data in tempdb are lost when that user’s session
ends.

This is the extended syntax for declaring a cursor (as specified in SQL Server
Books Online):

DECLARE cursor_name CURSOR


[LOCAL | GLOBAL]
[FORWARD_ONLY | SCROLL]
[STATIC | KEYSET | DYNAMIC | FAST_FORWARD]
[READ_ONLY | SCROLL_LOCKS | OPTIMISTIC]
[TYPE_WARNING]
FOR select_statement
[FOR UPDATE [OF column_list]]

The DECLARE command does all the work of defining a cursor, but it does not
actually create a cursor as an object that can be used. The cursor is not
created until it is opened with the OPEN command. The OPEN command
requires only the name of the cursor:

OPEN [GLOBAL] cursor_name

When the cursor is opened, the SELECT statement in the DECLARE command is
executed and any results needed for the cursor are collected. If the cursor is
INSENSITIVE, all rows are collected and stored in tempdb, ensuring that the
data remains unchanged from when the cursor is first opened. If the cursor is
FORWARD-ONLY, almost no information is collected. The OPEN command does
not implicitly perform an initial FETCH on the data. Once a cursor has been
opened, you can use the @@CURSOR_ROWS global variable to retrieve the
number of rows in the cursor.

Database Management Study Guide | V1.1 November 2015 Page 102 of 168
The FETCH command retrieves requested rows from the cursor’s result set.
The data from these rows can be stored in variables specified in the FETCH
command. The syntax is as follows (as specified in SQL Server Books Online):

FETCH [[NEXT | PRIOR | FIRST | LAST


| ABSOLUTE {N | @nvar} | RELATIVE {N | @nvar}]
FROM [GLOBAL] cursor_name
[INTO @variable_name1, @variable_name2, . . .]

For a complete listing of keywords and their explanations, refer to the section
FETCH in SQL Server Books Online.

The @variable_name parameters must be declared first. They correspond to


the columns from the SELECT statement. The variables are optional. It is not
possible to specify which variable receives data from which column, except by
position. Therefore, any columns not stored into variables start from the right-
most in the SELECT list of the declaration.

After each FETCH, the @FETCH_STATUS global variable indicates success or


failure. A value of 0 indicates a successful FETCH. A value of –1 indicates that
the position requested was before the beginning or beyond the end of the
cursor. A value of –2 indicates that although the position requested existed
when the cursor was opened, it has since been deleted. This would not occur in
a FORWARD-ONLY or INSENSITIVE cursor because they keep no record and so
cannot say whether any given row used to exist or not, or they maintain their
own data set which cannot be modified.

The CLOSE and DEALLOCATE commands take the cursor name as their only
parameter. The CLOSE command releases any data being held by the cursor
since it was opened. No further positioning or editing can be done on a closed
cursor. The DEALLOCATE command removes the definition of the cursor from
the server’s memory. After a DEALLOCATE command, the cursor cannot be
reopened.

Updateable cursors enable updates to individual columns and deletions of


rows. This is done through UPDATE and DELETE commands, with the CURRENT
OF keywords as the only phrase in the WHERE clause. For example:

DELETE FROM table_name


WHERE CURRENT OF cursor_name

Here is an example of how a cursor would be declared and used. Please note
that in order for this code to compile and run successfully, the necessary
tables, columns and constraints need to be in place.

Database Management Study Guide | V1.1 November 2015 Page 103 of 168
CREATE PROCEDURE sp_OrderDetails
@orderID INT
AS
BEGIN
DECLARE @custNumber INT
DECLARE @custName VARCHAR(30)
DECLARE @orderDate SMALLDATETIME
DECLARE @orderTotal SMALLMONEY
DECLARE @productName VARCHAR(100)
DECLARE @productPrice SMALLMONEY
DECLARE @quantity SMALLINT
DECLARE detailCursor CURSOR
FOR SELECT Products.ProdName, Products.Price,
OrderDetails.Quantity
FROM Products
INNER JOIN OrderDetails ON OrderDetails.ProductID =
Products.ProductID
INNER JOIN Orders ON Orders.OrderID = OrderDetails.OrderID
WHERE Orders.OrderID = @orderID
FOR READ ONLY

SET @orderTotal = 0
SELECT @custNumber = Customer.CustID, @custName =
Customers.CustName,
@orderDate = ISNULL(Orders.OrderDate, GETDATE())
FROM Orders
INNER JOIN Customers ON Customers.CustID = Orders.CustID
WHERE Orders.OrderID = @orderID

PRINT ' '


PRINT 'Customer Number : ' + CAST(@custNumber AS VARCHAR)
PRINT 'Customer Name : ' + ISNULL(@custName, 'Unknown')
PRINT 'Order Date : ' + CONVERT(VARCHAR, DATEPART(day,
@orderDate)) +
'/' + CONVERT(VARCHAR, DATEPART(Month, @orderDate)) + '/'
+
CONVERT(VARCHAR, DATEPART(year, @orderDate))
PRINT ' '
PRINT 'Description of products purchased'
PRINT '---------------------------------'

OPEN detailCursor
FETCH FROM detailCursor INTO @productName, @productPrice,
@quantity

Database Management Study Guide | V1.1 November 2015 Page 104 of 168
WHILE @@FETCH_STATUS = 0
BEGIN
PRINT @productName + ' @ R' + CONVERT(VARCHAR, @productPrice)
+ ' x ' +
CONVERT(VARCHAR, @quantity)
SET @orderTotal = @orderTotal + (@productPrice * @quantity)
FETCH FROM detailCursor INTO @productName, @productPrice,
@quantity
END
PRINT ' '
PRINT 'Order Total : R' + CONVERT(VARCHAR, @orderTotal)
CLOSE detailCursor
DEALLOCATE detailCursor
END

7.5 String functions


There are a few ways to improve the way text looks. A number of built-in
functions have been included for this reason (see Table 10). A lot of these
functions can be useful when the text needs to be in a more formal format.

Table 10 - Useful string functions

Function Description
LOWER(char_expr) Converts char_expr to lowercase.
Converts char_expr to
UPPER(char_expr)
uppercase.
Returns the number of characters
LEN(string_expr) of the specified string expression,
excluding trailing blanks.
Returns data without leading
LTRIM(char_expr)
blanks.
Returns data without trailing
RTRIM(char_expr)
blanks.
Returns the starting position of
PATINDEX('%pattern%', expression) the first occurrence of a pattern
in the specified expression.
Everywhere that string2 appears
REPLACE('string1', 'string2', 'string3') in string1 it is replaced with
string3.
REPLICATE(char_expr, no) Repeats char_expr no. times.
REVERSE(char_expr) Returns the reverse of char_expr.

Database Management Study Guide | V1.1 November 2015 Page 105 of 168
Returns part of a character or
SUBSTRING(expression, start, length)
binary string.

The following are some examples of how to use these string functions:

DECLARE @x VARCHAR(30)

SET @x = ' Hello, my name is Death.'

PRINT 'Normal: '


PRINT @x
PRINT ''

PRINT 'Upper case: '


PRINT UPPER(@x)
PRINT ''

PRINT 'Lower case: '


PRINT LOWER(@x)
PRINT ''

PRINT 'Remove leading spaces: '


PRINT LTRIM(@x)
PRINT ''

PRINT 'First index of "Death": '


PRINT CAST (PATINDEX('%Death%', @x) AS VARCHAR)
PRINT ''

PRINT 'Replace "Death" with "Cake": '


PRINT REPLACE(@x, 'Death', 'Cake')
PRINT ''

PRINT 'Reverse the char string:'


PRINT REVERSE(@x)
PRINT ''

PRINT 'Use of substring:'


PRINT SUBSTRING(@x, 20, 8)

PRINT 'LEN' + 'IS VERY USEFUL'


PRINT 'LEN' + REPLICATE(' ', 5 - LEN('LEN')) + 'IS VERY
USEFUL'

The result of this is the following:

Normal:
Hello, my name is Death.

Upper case:

Database Management Study Guide | V1.1 November 2015 Page 106 of 168
HELLO, MY NAME IS DEATH.

Lower case:
hello, my name is death.

Remove leading spaces:


Hello, my name is Death.

First index of "Death":


23
Replace "Death" with "Cake":
Hello, my name is Cake.

Reverse the char string:


.htaeD si eman ym ,olleH

Use of substring:
is Death

LEN IS VERY USEFUL


LEN IS VERY USEFUL

Combining these string functions together can greatly improve how the text
looks. The CAST() function in the above example converts from one data type
to another. This CAST converts the result of PATINDEX () from an INT to a
VARCHAR.

The REPLICATE and LEN functions are very useful together when used to print
out a list of items. This will ensure that all values are aligned one below the
other.

There is another way to improve the output of text when using iteration. The
line (n row(s) affected) that appears in the output of the iteration can be
removed. This line can get in the way and make things look unprofessional.
The following command will tell the compiler to not print this line:

SET NOCOUNT ON
GO

The line can be inserted just before the CREATE PROCEDURE statement.

7.6 Date functions


When working with dates, it can be quite difficult to break the date up and
work with the specific months, days, or years. There are built-in date functions
that address this, and some are shown in Table 11 - Useful date functions.

Database Management Study Guide | V1.1 November 2015 Page 107 of 168
Table 11 - Useful date functions

Function Description
DAY(date) Returns an integer value representing the day.
MONTH(date) Returns an integer value representing the month.
YEAR(date) Returns an integer value representing the year.
GETDATE() Returns the current date and time.

These functions can be used as follows:

DECLARE @y DATE

SET @y = GETDATE()

PRINT 'Normal: ' + CAST (@y AS VARCHAR)


PRINT 'Day: ' + CAST (DAY(@y) AS VARCHAR)
PRINT 'Month: ' + CAST (MONTH(@y) AS VARCHAR)
PRINT 'Year: ' + CAST (YEAR(@y) AS VARCHAR)

The result is similar to the following:

Normal: 2013-09-06
Day: 6
Month: 9
Year: 2013

There are a few more things that can be done with string and date functions
and you are encouraged to do more research into this.

7.7 Test yourself


1. Name at least three advantages of stored procedures.
2. What are the four types of stored procedure?
3. How can you differentiate between a view and a stored
procedure?
4. How would you describe the term “validation” in SQL Server?

Required reading
Chapter 25, 26 & 28 - Microsoft SQL Server 2014 UNLEASHED.

Database Management Study Guide | V1.1 November 2015 Page 108 of 168
Unit 8 – Triggers, Transactions

At the end of this unit you will be able to:

 Create a trigger.
 Understand how triggers work.

8.1 Triggers
Triggers enable you to ensure data integrity, domain integrity and referential
integrity. There are several rules about how and when they should be created.
For example, they can be used for validation purposes.

A trigger is a special type of stored procedure that is automatically based on


occurrence of a database event. Unlike stored procedures, triggers are only
executed when a user or application attempts to modify data using Data
Manipulation Language (DML). DML includes INSERTs, UPDATEs and DELETEs.
Triggers do not have parameters and cannot be explicitly invoked. By default,
all triggers fire after data modification is performed. They are known as AFTER
triggers and were the only type available in prior versions of SQL Server. SQL
Server 2000 introduced the INSTEAD OF trigger, which fires instead of the
intended data modification.

Triggers have relatively low overheads. Referencing other tables uses up most
of the time involved in running a trigger. Referencing is fast if the other tables
are in memory or a bit slower if they must be read from a disk. Triggers are
always considered a part of the transaction. If the trigger or any other part of
the transaction fails, it is rolled back.

There are three types of triggers:

 DML trigger

There are two kinds of DML triggers:

a) Instead of Trigger: is fired in place of the triggering action, such as an


insert, update or delete.
b) After Trigger: executes following the triggering action, such as insert,
update or delete.

 DDL trigger: This type of trigger is fired against DDL statements like Drop
Table, Create Table or Alter Table. DDL Triggers are always after Triggers.

 Logon trigger: This type of trigger is fired against a LOGON event before a
user session is established to the SQL Server.

Database Management Study Guide | V1.1 November 2015 Page 109 of 168
Triggers use the inserted and deleted tables, both of which contain the same
structure as the base table where the trigger was created. These tables reside
in RAM because they are logical tables.

The syntax for creating a trigger is as follows:

CREATE TRIGGER trigger_name


ON table_name | view_name
{FOR | AFTER | INSTEAD OF} {[INSERT] [,] [UPDATE] [,]
[DELETE]}
[NOT FOR REPLICATION]
AS
sql_statement [...n]

The trigger can be set up on a table or a view, meaning that when the user
performs an action specified in this trigger, the trigger will fire.

There are various uses of triggers. The first is the FOR, AFTER and INSTEAD
OF options. These will specify when the code will be executed once the trigger
has been fired. Using the FOR option specifies that something will happen
before the event that fired the trigger. The AFTER option specifies that the
code will execute after the event that fired the trigger. The INSTEAD OF option
will not let the event happen but will do whatever is specified instead.

The INSERT, UPDATE and DELETE options are used for specifying when the
trigger will fire, i.e. if the INSERT option is used, the trigger will fire when data
is inserted into the specified table or view.

When the NOT FOR REPLICATION option is used in a trigger, it is specifying


that the trigger must not be executed when a replication agent fires it. The
trigger will only execute if a user fires the trigger. This option is often used for
other objects besides triggers.

AS specifies that the next SQL statement is to be executed when the trigger is
fired, for example:

USE lead_by_example
GO

CREATE TRIGGER tr_test


ON campus
INSTEAD OF INSERT
AS
print 'NO, Inserting has been disabled on table CAMPUS'
GO

INSERT INTO campus


VALUES (3,'Bloubosrand','JHB')
GO

Database Management Study Guide | V1.1 November 2015 Page 110 of 168
This code creates a trigger that is set up to fire when someone attempts to
insert data into the table campus. When the trigger fires it will print out a
message instead of inserting data.

Directly after the trigger, an attempt to test this trigger is done by inserting a
new row into the table campus. When this code is executed, the following
should be printed out:

NO, Inserting has been disabled on table CAMPUS

(1 row(s) affected)

When the information in table campus is checked, the information from the
test will not be there.

8.1.1 Apply your knowledge


Create a new query in SQL Server Management Studio and type the following:

USE lead_by_example
GO

CREATE TABLE errors --New table for errors to be recorded


(
errorCode INT NOT NULL,
errorDisc VARCHAR(100) NOT NULL
)
GO

CREATE TRIGGER tr_notify


ON employee
AFTER INSERT
AS
print 'Employee inserted successfully'
GO

CREATE TRIGGER tr_edit_store


ON storeBranch
INSTEAD OF INSERT, DELETE, UPDATE
AS
RAISERROR ('You cannot edit the information in that
table', 16, 10);

INSERT INTO errors


VALUES(123,'USER TRIED TO EDIT TABLE STOREBRANCH.
TIME: '+CAST(GETDATE() AS VARCHAR))
GO

Database Management Study Guide | V1.1 November 2015 Page 111 of 168
This code creates two triggers, i.e. tr_notify and tr_edit_store. The first one
prints out a notification that inserting into the table employee was a success.
The second one is fired when someone tries to insert into, delete from or
update the storeBranch table. It will first throw a custom error using the
RAISERROR keyword and then insert into the table errors. This table was
created to store types of errors that occur during the lifetime of the database.
The GETDATE function is cast into a varchar when it is inserted into the errors
table.

Attempt to insert data into both tables:

USE lead_by_example
GO

INSERT INTO storeBranch


VALUES(3,'Reg pets','USA')
GO

INSERT INTO employee


VALUES (7485738293847,'Orlando','Pirate',2)
GO

The following should be the output:

Msg 50000, Level 16, State 10, Procedure tr_edit_store, Line


6
You cannot edit the information in that table

(1 row(s) affected)

(1 row(s) affected)
Employee inserted successfully

(1 row(s) affected)

You can see the triggers taking effect here when they display the messages
that you specified.

8.2 Transactions
A transaction is one or more SQL statements that must be completed as a unit
of work. Transactions provide a way of collecting and associating multiple
actions into a single all-or-nothing, multiple-operation action. All operations in
transitions must be fully completed or not performed at all. Consider an
example of a bank transaction in which the customer transfers R500 from a
cheque account to a savings account. This transaction has two operations: a
decrement of cheque account and an increment of savings account. What if the
bank server went down after completing the first step and never went to the

Database Management Study Guide | V1.1 November 2015 Page 112 of 168
second? When the two operations are combined as a transaction, they either
both succeed or both fail as a single complete unit of work.

SQL Server 2014 comes with the ability to reduce transaction latency by
designing some or all transactions as delayed durable. A delayed durable
transaction (lazy commit) returns control to the client before the transaction
log record is written to disk. Fully durable transactions return control to the
client only after the log records for the transactions are written to disk. This is
default transaction behaviour in SQL Server.

A transaction is a generation of one or more changes to the database, e.g.


when creating a record or deleting a record from the table, you are performing
a transaction. In order to maintain data integrity and to handle database
errors, transactions should be controlled.

Transactions have the following four standard properties, normally referred to


by the acronym ACID:

 Atomicity: ensures that all operations within the work unit are completed
successfully; alternatively, the transaction is aborted at the point of failure,
and previous operations are rolled back to their former state.
 Consistency: guarantees the stated change of database after a successful
transaction.
 Isolation: allows transactions to operate independently of, and transparently
to, each other.
 Durability: ensures that the result or effect of a committed transaction
remains in case there is a system failure.

8.2.1 Transaction control


Transaction controls are used to control transactions in order to maintain data
integrity, as mentioned above. In SQL Server transactions are defined in three
ways:

 AutoCommit: every T-SQL statement is its own transaction and


automatically commits when it finishes. This is the default mode which SQL
Server operates.
 Explicit: this method provides programmatic control of the transaction,
using BEGIN TRAN and COMMIT/ROLLBACK TRAN/WORK commands.
 Implicit: in this mode, when a certain SQL command is issued, SQL
automatically starts the transaction. The transaction must be finished by
explicitly issuing the COMMIT/ROLLBACK TRAN/WORK commands.

The commands used to control transactions are:

 COMMIT: to save the changes.


 ROLLBACK: to rollback the changes.

Database Management Study Guide | V1.1 November 2015 Page 113 of 168
 SAVEPOINT: creates points within groups of transactions in which to
ROLLBACK.
 SET TRANSACTION: places a name on a transaction.

Examples: The following table is used as an example to demonstrate the use of


transaction commands.

Employee table
ID Name Age Address Salary
1 Eddie 33 18 Albert Street 20000
2 Jan 41 91 Albertina Sisulu Street 60000
3 Peter 35 12 Lilia Ngoyi Street 34000
4 John 22 15 Pretorius Street 800
5 Sipho 25 21 William Nicol Drive 20000

1. The COMMIT and ROLLBACK command:

USE lead_by_example
GO

BEGIN TRAN

UPDATE Employee
SET Name = 'John'
WHERE iden= '321'

UPDATE Employee
SET Name = 'JohnY'
WHERE city = 'Johannesburg'

IF @@ROWCOUNT = 5
COMMIT TRAN
ELSE
ROLLBACK TRAN

3. The SAVEPOINT command:

USE lead_by_example
GO
BEGIN TRANSACTION
GO
DELETE FROM Employee
WHERE Age = 35;
GO
SAVEPOINT TRANSACTION;
GO

Database Management Study Guide | V1.1 November 2015 Page 114 of 168
4. The SET TRANSACTION syntax:

BEGIN { TRAN | TRANSACTION }


[ { transaction_name | @tran_name_variable }
[ WITH MARK [ 'description' ] ]
]
[ ; ]

Example:
USE lead_by_example
GO
SET TRAN READ ONLY NAME ‘John’;
COMMIT;

8.3 Test yourself


1. How would you describe a cursor?
2. What are cursors used for?
3. How would you describe a trigger?

Required reading
Chapter 27 - Microsoft SQL Server 2014 UNLEASHED.

Database Management Study Guide | V1.1 November 2015 Page 115 of 168
Unit 9 – Database Administration and Security

At the end of this unit you will be able to:

 Back up your database.


 Understand how users, roles and permissions work.

9.1 SQL Server Security


Microsoft SQL Server offers three data security choices: standard security
setting (default), integrated security and mixed security. The standard
security setting requires each user to supply a valid login ID and password to
connect to the SQL Server.

Integrated security enables users to use the network login and password
supplied to Microsoft Windows NT as a security mechanism to access their data
server. Once user validity with a login and password is verified by Microsoft
Windows NT, the user can connect to the server. Keep in mind that connecting
to the server does not mean the user has access to the database.

Mixed security is useful when users need to supply only one login and
password to be on the network and connect to the server. This method is
popular in today’s business of mixed legacy environments.

Our main focus in this section will be on the standard security setting.

The previous releases of SQL Server had server logins mapped to users, but
SQL Server 2014 has an alternative connection method with the concept of
contained databases. This will be discussed later on in this unit.

9.2 Planning the use and structure of SQL Server roles


With SQL Server roles, groups of users can be categorised. With these
categories, one can assign, deny and revoke permissions to all members of the
group. Members inherit permissions of the entire role. If a member’s job
situation changes, he or she can be deleted and/or be added to another group.

Roles make administration much easier because one does not have to set
permissions for every user. Users can belong to more than one role. The four
types of server roles are:

 Fixed server
 Fixed database
 Public database role
 User-defined database

Database Management Study Guide | V1.1 November 2015 Page 116 of 168
All users in the database belong to that database’s public role. If all members
of that database need to share the same permissions, the permissions can be
assigned to the public database role. Fixed server, fixed database and public
roles are built-in roles and cannot be dropped.

9.2.1 Fixed server roles


Fixed server roles have permissions on all databases in a server. All members
of this role gain all permissions applied to it. The fixed server roles are listed in
Table 12 – Fixed server roles.

Table 12 – Fixed server roles

Fixed server role Description

Has full access to all database objects and is able to


sysadmin
perform any task.

Can shut down server and set all server-related


serveradmin
options.

Can install replication and have full access to extended


setupadmin
procedures.

Has access to set login and CREATE DATABASE


securityadmin
permissions.

processadmin Can manage all SQL Server-related processes.

Has authority to create and alter all the databases in


dbcreator
the server.

diskadmin Has full access to all disk files.

9.2.2 Fixed database roles


The fixed database role only exists at the database level. Every member added
to this role gains all permissions applied to it. The fixed database roles are
listed in Table 13 – Fixed database roles.

Table 13 – Fixed database roles

Fixed database role Description

db_owner Has full access to the database.

db_accessadmin Can manage user IDs.

db_datareader Can give the read (SELECT) permissions to any

Database Management Study Guide | V1.1 November 2015 Page 117 of 168
database object.

Can give the write (INSERT, UPDATE and DELETE)


db_datawriter
permissions on any database object.

Can issue all DDL (Data Definition Language)


db_ddladmin statements, which are used to define and declare
all database objects.

db_securityadmin Can manage all permissions, ownerships and roles.

The opposite of db_datareader. Can deny the read


db_denydatareader
(SELECT) permissions on any database object.

The opposite of db_datawriter. Can deny the write


db_denydatawriter (INSERT, UPDATE and DELETE) permissions on any
database object.

9.2.3 User-defined database roles


There may be a group of users who wish to perform a particular task or tasks
and are not classified in the fixed database or fixed server roles. If this is the
case, a user-defined database role can be created. This role is similar to the
fixed database role in that it exists only in the database level and only affects
the database in which it is created.

9.2.4 Schemas
Schemas are used for defining what any given user owns (grouped together).
You may have noticed during the course that there was occasionally an owner
specified as a prefix to objects in the database, e.g. dbo.table_name. This is
the schema to which that object belongs.

A default schema is set to a database, meaning that the database engine uses
this first when resolving object names in that database. The three types of
default schemas that exist are:

 dbo – Used by default if no schema is specified.


 guest – Users without accounts can access the database.
 sys – Contains all the system objects like system tables and catalogue
views.

INFORMATION_SCHEMA contains all the information schema views.

9.2.5 Creating a Windows-based login using MSSMS


A login is a security principal referring to credentials required to obtain access
to the server or database. Server permissions such as CREATE DATABASE and

Database Management Study Guide | V1.1 November 2015 Page 118 of 168
BACKUP DATABASE can be granted to logins. By default the server login
principal is created during SQL Server installation.

 Open Microsoft SQL Server Management Studio (MSSMS) and connect to a


server.
 Open Object Explorer if it is not already opened and expand the server tree.
 Right-click the security folder and select New > Login.

Figure 29 – Windows New Login

 You already have an existing Windows account created in Active Directory


or your local machine.
 On the New Login page, enter a Windows account in the Login Name text
box. Enter the account in the following format: domain\username. For
example, if the Windows account is nceba, you will enter domain\nceba.
Alternatively, click the search button to find a Windows entity that you want
to enter.
 Since this is a Windows account, ensure that Windows Authentication option
is selected. If you use an SQL Server login, you should select SQL Server
Authentication option, which will require your password and confirmation of
such password.
 There is an option to select Windows Active Directory policies and password
expiration policies on this account. Enforcing these means they will be
enforced for SQL Server login.

Database Management Study Guide | V1.1 November 2015 Page 119 of 168
Figure 30 – Windows Login Authentication

 On the default language drop-down list, you can leave the language as
default or you can select the language of preference.
 Now select Server Roles on the Select a page pane. On the listed Server
Roles, select sysadmin which allows the system administrator to execute
any task on the server, since public is selected already.

NOTE
The sysadmin role has unrestricted permissions to all databases without the
need to create database users. Therefore, it is important to consider who to
grant this role to.

If your machine is not connected to any domain, the username or name of the
PC will work fine.

Database Management Study Guide | V1.1 November 2015 Page 120 of 168
Figure 31 – SQL Server Roles

 In the Select a page pane, select User Mapping.


 To explicitly grant this user access to a specific database, you can select
that database from the list at the top of the page. The user can also be
assigned database roles. Creating database users will be discussed in the
next sections.
 In the Select a page pane, select Securables. This page lists items that can
be secured and the corresponding permissions that can be assigned to that
login.
 Click the Search button for a list of three choices:
o The specific objects option allows you to add items of different types.
o The All objects option holistically grants permissions to all objects of a
specific type.
o You provide access to every item by choosing The server ‘Server Name’
option.

Database Management Study Guide | V1.1 November 2015 Page 121 of 168
Figure 32 – SQL Server Objects

 For the purpose of our demonstration, click Cancel in the above window.
 In the Select a page pane, select Status. On this page you can grant or
deny the login permission to connect to the server. Ensure the Grant option
is selected.
 Ensure that the Enabled option is selected.
 Finally, click OK and the login is created.

The steps above can also be performed using T-SQL.

9.2.6 Creating user-defined server roles


SQL Server 2014 provides the ability to create user-defined server roles. We
can create server roles that combine the capabilities of existing server roles or
we can grant explicit permission to specific securables and create fine-grained
server roles.

 Once you are connected to the SQL Server, go to Object Explorer.


 Expand the server tree and expand the Security folder.
 Right-click the Server Roles folder and select New Server Role.
 In the New Server Role dialog box, type BulkAdminAndDBCreater.
 In the Owner text box, type sa or click the ellipsis button and search for a
login.
 In the Securables section, select the checkbox next to Servers.
 In the Explicit section, select the checkbox in the Grant column for both
Administer Bulk Operations and Create Database permissions.
 Select Members in the Select a page pane. Click Add.
 Enter or Select Object Names domain\nceba, where domain name is your
domain.
 Click OK.

Database Management Study Guide | V1.1 November 2015 Page 122 of 168
NOTE
A domain is an environment or location of all values that data elements may
contain.

A domain name is a way of identifying and locating resources connected to


MS SQL Server. In a server there can be may users with different domains.

If you are confused or not sure about your domain, do the following:

 Go to Object Explorer on the left hand side.


 Place your mouse curser on top of the server name, which is the first line
under Object Explorer.
 A popup line showing your server name and domain name will appear, as
shown below:

Figure 33 – Domain name

 In most cases, the domain name will be the username that you use to login
to Microsoft SQL Server, e.g. PEROOT\nceba.

Database Management Study Guide | V1.1 November 2015 Page 123 of 168
Figure 33 – SQL Server Role member

Select Memberships in the Select a page pane.

You can add the role to one of the built-in server roles if you want, but for now
leave all boxes clear and click OK.

User-defined server roles can also be created using T-SQL. Execute the
following query:

USE [master]
GO
CREATE SERVER ROLE [BulkAdminAndDBCreator]
GO
ALTER SERVER ROLE [BulkAdminAndDBCreater] ADD MEMBER [DOMAIN\nceba]
GO

Database Management Study Guide | V1.1 November 2015 Page 124 of 168
USE master
GRANT ADMINISTER BULK OPERATIONS TO [BulkAdminAndDBCreater]
GO
USE master
GRANT CREATE ANY DATABASE TO [BulkAdminAndDBCreater]

9.2.7 Creating database users


SQL Server 2014 brought a new feature that allows creation of users without
having to create SQL Server login first. For now, we will focus on creating a
user based on an existing login.

Two database-level principals will appear in every database:

 By default, every user belongs to the public database role. The user will
inherit all permissions of this role until explicit permissions are granted.
 A guest user is disabled by default. Since a guest is inherited by any login
that has access to the database, any permission granted to this account is
also inherited by those users.

9.2.8.1 Creating a user using SQL Server Management Studio (SSMS)


 Open SSMS and connect to a server.
 In Object Explorer, right-click on Databases and create a database named
AdventureWorks2014. (This can be downloaded here.)
 Expand the Adventure database and right-click on Security folder, and
select New > User from the menu. The Database user dialog will open.
 In the top drop-down list, select Windows User.
 In the Username and login Name textbox, enter domain\nceba.
 In the default schema textbox, enter dbo.
 In the Select a page pane, select Owned Schemas. Specifying a user as an
owner of the schema gives that user full control. Do not select any
schemas.
 In the Select a Page pane, select Securables. On the Securables page,
click the Search button. This is similar to when you created a database
server login user; you grant or deny access at the same levels.
 Select the Specific Objects option and click OK.

Database Management Study Guide | V1.1 November 2015 Page 125 of 168
Figure 34 – User permissions

 In the Select Objects dialog box, select Tables and click OK.
 Click the Browse button and select the boxes next to the dbo.Cars and
dbo.Shapes tables and click OK.
 In the Securables section, select the Employee table.
 In the Explicit section, select the boxes in the Grant section for the
following permissions: Insert, Select, Update and Delete.
 Repeat the above two steps for JobCandidate table.

NOTE
When creating a user role, you may have to use a different user name
because your default login name may have already been in existence as
another role.

 In the Select a Page pane, select Extended Properties. You can include
metadata about the login here if you like; for now, leave it and click OK.

Database Management Study Guide | V1.1 November 2015 Page 126 of 168
We can also create a user using T-SQL. Also note that you can use the SQL
Server Authentication instead of Windows Authentication.

9.2.9 Configuring contained databases


In the introduction to this unit we mentioned contained databases as a new
feature in SQL Server 2014. Containment means that a database is isolated
from the instance of SQL Server which is its host. Some information will be
specific to the database and does not depend on any features outside the
database. In SQL Server 2014, databases can be partially contained, meaning
a database may use features that are outside the database.

A contained user can be created from a Windows login or SQL Server with a
password. Using a database user on an existing login will not create a
contained database. Contained Database Authentication must be enabled at
the server and database level before any contained user is created. Let us now
create a contained database user:

 Open SSMS and connect to the server. On the Object Explorer, right-click
the server name and select Properties from the menu.
 Select Advanced from the Select a page pane. In the Containment section,
set the Enable Contained Databases option to True and click OK.
 Now open a new query window.
 Type and execute the following T-SQL statement:

USE master
GO
CREATE DATABASE MyContainedDB

USE master
GO
Exec sp_configure 'contained database authentication', 1;
GO
Reconfigure;
USE [master]
GO
ALTER DATABASE [MyContainedDB] SET CONTAINMENT = PARTIAL
GO

NOTE
SP_CONFIGURE is a tool to display and change SQL Server settings. When
changing the settings you should be careful because they affect your
instance and any dubious action can affect instance performance,
availability, and so on.

Database Management Study Guide | V1.1 November 2015 Page 127 of 168
Figure 35 – SQL Server Properties

Benefits and limitations of contained databases:

Benefits Limitations
 Data can now be easily  Data is inaccessible while files
migrated. are being moved.
 When AlwaysOn is configured,
in the event of failover a user
can connect to the server
without any security issues.

9.3 RAID
RAID (Redundant Array of Inexpensive Devices) is a standard for hard drive
fault tolerance. RAID uses a multiple hard drive configuration. The amount and
type of fault tolerance provided depends on which RAID level is implemented.
Microsoft Windows NT/2000 uses RAID levels 0, 1 and 5.

Database Management Study Guide | V1.1 November 2015 Page 128 of 168
NOTE
Fault-tolerant systems using RAID should never be used as a replacement for
regular backup of servers and local hard disks.

9.3.1 Mirroring RAID Level 1


A partition is duplicated from one disk onto another physical disk. This provides
fault tolerance by keeping data stored on two different disks in case of drive
failure.

9.3.2 Duplexing RAID Level 1


A partition is duplicated from one disk onto another physical disk, which is
connected to another hard drive controller. It provides fault tolerance by
keeping data stored on two different disks in case of drive failure. It uses two
hard drive controllers in case one of the controllers fails.

9.3.3 Disk striping with parity – RAID Level 5


RAID Level 5 places a portion of parity information on each drive. The data and
parity information are always on different disks (see Figure 36 – RAID 5 (disk
striping with parity)). If one of the disks crashes, the data is rebuilt from the
parity information. A minimum of three disks is required and a maximum of 32
disks is supported.

DISK 1 DISK 2 DISK 3

DATA DATA PARITY

PARITY DATA DATA

DATA PARITY DATA

Figure 36 – RAID 5 (disk striping with parity)

9.3.4 Disk striping without parity – RAID Level 0


Data is divided into 64 k blocks and spread equally among all disks in array. A
minimum of two disks is required. Disk striping without parity does not provide
fault tolerance but improves disk read and write performance.

Database Management Study Guide | V1.1 November 2015 Page 129 of 168
9.4 Other types
9.4.1 Volume sets
Combining from 2 to 32 areas of unformatted free space (on one or more
physical drives) creates a volume set. These spaces form one large, logical
volume set that is treated like a single partition. Volume sets do not provide
fault tolerance and are simply a means of organising hard disk space.

9.4.2 Tape backup


Tape backups are removable and provide offline storage. They have slower
read and write speeds than hard drives. However, tape backups are an
excellent choice because they are relatively cheaper and tapes can be stored
offsite for protection.

9.5 Database backups and recovery


Backing up a database should be part of your database solution, no matter the
size of your database. It is important to decide which backup strategy to use
even if the backup is done once a week or month.

9.5.1 Planning a backup strategy


When planning a backup strategy, one must decide on the following:

 Which backup method to use.


 Which files and folders to back up.
 Whether to perform a network backup or multiple local backups.
 How often to back up.

NOTE
When it comes to deciding which files and folders to back up, a general rule
of thumb is: ‘if you cannot do without it, back it up’.

9.5.2 Backup media options


Backup media options include:

 Small-capacity removable drives


 Large-capacity removable drives
 Removable optical drives
 Magnetic tape

When deciding on whether to perform a network backup or multiple local


backups, consider which computers on the network are used for storing critical
data:

Database Management Study Guide | V1.1 November 2015 Page 130 of 168
 Perform a network backup when the critical data is on multiple servers.
 Perform multiple local backups when the critical data is on workstations.

For the advantages and disadvantages of network backups and local backups,
see Table 14 – Comparing network and local backups.

Table 14 – Comparing network and local backups

Advantages Disadvantages
 One user can perform the  Users must copy their
backup. important files to the
Network  Requires fewer tape server.
backup drives.  Increases network traffic.
 Entire network is backed  Requires greater
up. planning.
 Each user is responsible
 Fewer network resources
Local backup for backing up data on
committed.
individual workstations.

How frequently one does backups should depend on:

 How critical the data is to the organisation – the more critical, the more
frequent.
 How frequently the data changes.

NOTE
It is good practice to schedule backups when network usage is low.

9.6 Backup types


There are many different backup types. The most common are: full, differential
and incremental backups (see Table 15 - Full, differential and incremental
backup types).

Efficient backup policy uses a combination of these backup types, usually


implemented in a rotation method called the child, parent and grandparent
method (see Table 16).

Database Management Study Guide | V1.1 November 2015 Page 131 of 168
9.6.1 Full, differential, transactional log, file groups and
incremental backup types

Table 15 - Full, differential and incremental backup types

Archive Number of sets to


Type of
Backup data attribute restore after server
backup
cleared? crash
Backs up all data,
whether or not it has
Full Yes Full only
changed since the last
full backup.
Backs up all data that
Full plus last
Differential has changed since the No
differential
last full backup.
The full and bulk-
logged recovery
models are the only
Transaction log backups models that support
Transaction capture records written transaction log
Log to the transaction log No backups. These models
file(s) defined for a cause transaction
database. events to be retained
in the transaction log
so that they can be
backed up.
File and file group File and file group
backups are targeted at backups are often used
databases that contain for larger databases
more than one file where the creation
group. In these time for a full database
File and File
situations, the file group backup takes too long
group
or files in the file groups - or the resulting backup
Backups
can be backed up is too large. In these
independently. If a file situations, you can
group is backed up, all stagger the backups of
the files defined in the the files or file groups
file group are backed and write them to
up. different locations.
Copy-only backups
If a full database
allow a backup of any
backup is taken, any
Copy-Only type to be taken without
subsequent differential
Backups affecting any other -
database backups use
backups. Normally, a
this full database
database backup is
backup as their base.
recorded in the

Database Management Study Guide | V1.1 November 2015 Page 132 of 168
database itself and is
identified as part of a
chain that can be used
for restore.
Backs up all data that Full plus every daily
Incremental has changed since the Yes incremental since the
last backup of any kind. last full backup

9.6.2 Tape rotation schedule


Using rotating tapes is the most practical way to manage a tape backup
scheme. Using a new tape for every backup is not cost effective.

A common tape rotation method called the child, parent and grandparent
method makes management of the tapes a lot easier. This method is explained
in Table 16 – Child, parent, grandparent rotation method.

Table 16 – Child, parent, grandparent rotation method

Name of How often Storage Description


backup performed location
Keep four daily backup tapes and
rotate them each week. Label the
four tapes Monday, Tuesday,
Child Daily Onsite Wednesday and Thursday. A Friday
(child) is not made because the
parent backup will be made on the
Friday.
Perform the weekly backup on
Friday. Keep five weekly backup
tapes, one for each Friday of the
Parent Weekly Offsite month, and rotate them each
month. Label the tapes Friday 1,
Friday 2, Friday 3, Friday 4 and
Friday 5.
Perform the monthly backup on the
Offsite, in
last Friday of the month. Keep 12
a
Grandparent Monthly tapes, one for each month. Rotate
fireproof
them each year. Label the tapes
vault
January, February, and so on.

9.7 How to back up a database and zip it


To make a backup of a certain database, you need to open up the Microsoft
SQL Server Management Studio, and then find the database that you want to
back up in the Object Explorer (see Figure 37).

Database Management Study Guide | V1.1 November 2015 Page 133 of 168
Figure 37 – Object Explorer

Right-click on the database that you need to back up and select Tasks >
Backup. You will be presented with the screen shown in Figure 38.

Figure 38 – Backup

Ensure that the backup is set properly and click OK. It should back up to the
specified folder. A dialog box will tell you whether or not it was successful.
When successful, use Windows Explorer to navigate to the folder that contains

Database Management Study Guide | V1.1 November 2015 Page 134 of 168
the database backup that you have created. Once there, right-click on your
backup file and select Send To > Compressed (zipped) folder (see Figure
39).

Figure 39 – Zipping the backup

It is recommended that you keep the same name as that of the backup. You
have now successfully created a backup of your database and zipped it.

9.8 Test yourself


1. Microsoft Server offers three data security choices. Name these
choices and choose the best one for a database that may need to
be accessed by users in different locations.
2. SQL Server roles make the administration of the server easier. Do
you agree with this statement? Explain why you hold this view.
3. What are the four types of server roles that you have learnt
about?

Required reading

Database Management Study Guide | V1.1 November 2015 Page 135 of 168
Chapter 9, 11, 15 19 & 38 - Microsoft SQL Server 2014 UNLEASHED.

Database Management Study Guide | V1.1 November 2015 Page 136 of 168
Unit 10 – Project

This project will test your understanding of all the sections covered in the
previous units. You can choose any one of the projects given (consult your
lecturer in this regard). A mark sheet for each project will also be provided.

You need to supply user documentation on how to set up and run your
program. All your project content must be submitted digitally on a CD, DVD or
flash drive.

No matter which project you do, you must create the following script files. To
create a script file (in Microsoft SQL Server Management Studio), select Save
from the File menu when you have finished typing all the necessary code and
save it to disk. Script files should have a .SQL file extension.

Complete the following:

 Create an ERD from the scenario and hand it in to your lecturer.

 Create a script file that includes the code for creating the database and all
of the tables required in the project. PRIMARY KEY, FOREIGN KEY, UNIQUE
and CHECK constraints must be included. Make use of the IDENTITY
property, DEFAULT values and constraints. Remember that master/parent
tables must be created first.

 Create a script file to insert data into your user-created tables. Add at least
four records for each table without foreign keys and seven records for
each table that does contain a foreign key.

 Create a script file to create the views as specified.

 Create a script file to create the stored procedures as specified.

 Create a script file to create at least two triggers for your project. There
are no specifications for what types of triggers you must create. You should
apply your knowledge and create triggers that you think would be
appropriate.

 Create a script file to delete the database that you create.

 Create appropriate indexes on your tables.

 You should validate all applicable arguments in the stored procedures


where arguments are received.

 Make use of iteration where appropriate.

Database Management Study Guide | V1.1 November 2015 Page 137 of 168
 Using the GUI, create a full backup of your database and send it to a zipped
folder.

 Each script file must have a header section (using comments) that contains
the following information:
o Script file name
o Programmer name
o Date
o A short description of what the script file does

 When commenting your code, both styles of commenting (-- or /* */)


may be used. Just remember to maintain consistency throughout your
project.

Save all of the script files together with the backup onto a disk and hand it
in along with printouts of the script files. Provide clear instructions in your
user documentation on how to execute the views and stored procedures
(including the arguments that need to be passed to the stored procedures).
In your user documentation you should also include author and date details,
together with the purpose and description of the project.

Database Management Study Guide | V1.1 November 2015 Page 138 of 168
Project 1 – MMORPG
A new company has developed a breakthrough Massive Multiplayer Online Role
Playing Game (MMORPG). An MMORPG is a computer game that is played over
the Internet, connecting to a server where other players from around the world
are also playing the same game. You are able to ‘see’ and communicate with
these other players when online.

This company has recruited you to be their database designer. The database
that you must design will manage the accounts of all players registered for this
online game. First, each account must have a unique name. This will be the
name that players use to log on to the game server. Players need to pay a fee
every month for having an account and when their time runs out, the account
must be blocked and only unblocked when they have purchased more time.
Each account has a number of unique characters that have been created by
the user. All characters have names and have a skill level. There must be a
table in the database that logs any errors that occur. Make sure that certain
errors have a certain type so that they are easier to find.

When playing the game with one of your account’s characters, you may find
and pick up items. These items are then stored in your character’s inventory.
No item is unique, unfortunately, so other characters may have the same item
as you. An item has a name and a description. The inventory has eight open
slots for different types of items to go into. Items that are the same can stack
in the same slot; therefore, you must keep a record of the quantity.

Specifications
Create the following views:

 vwBlockedAccounts – All the accounts that are currently blocked.


 vwTopSkill – The top 5 characters with the highest skill levels and their
account details.
 vwTopStackedItems – A view of 10 items that are stacked most frequently.
Include the characters whose inventory they are stacked in.
 vwPopItems – The five most popular items that are contained in the
character’s inventory, including the number of characters that have the
item.

Create the following stored procedures:

 spRegister – Registers a new account. All the account details must be


stored as arguments. Make sure that the name does not already exist in
the table.
 spAddTime – Adds the specified time to the specified account. Makes sure
the account exists beforehand. Arguments: Account name, Length of time
in days.

Database Management Study Guide | V1.1 November 2015 Page 139 of 168
 spAddItem – Adds an item to a character’s inventory. Make sure there is
space before adding the item.
 spAddChar – Adds a specified character to a specified account.
 spSendLetter – Prints out a letter for each account, indicating how much
time they have left. An optional argument that can be used to send latest
news must also be included.

Database Management Study Guide | V1.1 November 2015 Page 140 of 168
Project Evaluation – Project 1 (DBMM-16)

Student: Lecturer: ______________


Date submitted: _Date marked: ____

Achievement: % Possible Achieved


Source code documentation
Comments (author, date, purpose) for each script file 3
Descriptions for individual code segments 3
Correct indentation of code 3
Keywords capitalised, table and column names in lowercase 3
Each clause of an SQL statement appears on a different line 3
All variables declared as the correct type 3
Descriptive naming of variables 3
Subtotal: 21
Project content
Database created correctly. Tables created in same script 6
Tables created correctly (use of PRIMARY KEY, FOREIGN KEY,
10
UNIQUE and CHECK constraints and IDENTITY, DEFAULT)
Stored procedures are correct and work as specified 16
Views are correct and work as specified 12
Triggers are correct and work as specified 4
Sample data inserted correctly 5
Indexes are correct and used efficiently 2
WHILE loops are used appropriately and efficiently 10
Backup restores correctly and is done according to specifications 2
ER diagram included and correct 5
User documentation included and sufficient 5
General code efficiency 8
Subtotal: 85
Output
Stored procedures produce logical and meaningful output 4
Views produce logical and meaningful output 3
Subtotal: 7
Error handling
Validation of required arguments 4
Error messages are used in a clear and helpful way 3
Subtotal: 7
General comments

Total: 120

Database Management Study Guide | V1.1 November 2015 Page 141 of 168
Project 2 – Tygervalley Pet Shelter
Tygervalley Pet Shelter (TPS) receives food donations from a group of pet food
manufacturers. TPS relies on each manufacturing company for a particular
need. For example, Veggie Land supplies all vegetables and Premier Seed
Suppliers supply all seeds.

The different food types are allocated to the animals according to their
category. For example, food type ‘seeds’ is allocated to category ’Birds’, while
food type ‘vegetables’ is allocated to categories ‘Mammals’ and ‘Birds’.

Each animal category is subdivided into different pet types. For example, the
mammal record could relate to pet types ‘Dog’ and ‘Cat’.

TPS has recruited you to be their database designer. The database that you
must design will store information on their suppliers, food types, animal
categories and pet types.

Record a unique, auto-incrementing ID, company name, contact number and


an e-mail address for each manufacturer. When recording information about
the different food types, ensure that the expiry date is also stored. When
recording how much food will be allocated per animal category, specify the
quantity as two separate fields. For example, if 8.5 kilograms of Roast Chicken
Pellets are allocated to category ‘Mammals’, ‘8.5’ and ‘kg’ will be two different
fields. The stock level for each pet type must also be stored together with the
pet type so that they know there are 10 dogs and 20 cats for category
‘Mammals’, or 40 parrots for category ‘Birds’.

Specifications
Create the following views:

 vw_ManufacturerDetails – Select manufacturing company details, the food


type, food ID and amount per category.
 vw_PetsPerType – Select each animal type, total animals in stock, the
animal category ID and category name.
 vw_ExpiredFoodDetails – Select the company name, contact number, food
ID, name, expiry date, amount per category, measurement and the
category name. This only applies to expired products that are in use (have
dependent records). HINT: Use the GETDATE() function to compare the
expiry date against today’s date.
 vw_LowestFoods – Select the category name and the sum of the total
animals in the category. Only the three records with the lowest number of
pets must be selected.

Create the following stored procedures:

Database Management Study Guide | V1.1 November 2015 Page 142 of 168
 sp_NewPetType – Insert a new pet type record. Remember to check
whether or not the primary key argument is an existing category primary
key.
 p_UpdateStock – Update an existing pet type record. The procedure must
make use of three arguments – 1: pet type primary key, 2: the number of
animals to be added/subtracted, 3: bit number representing addition (1) or
subtraction (0).
 sp_DeleteFoodType – Delete a specified food type and all dependent/child
records. However, only delete the type if it is contained in the
vw_ExpiredFoodDetails view.
 sp_Report – Print a specified manufacturing company’s details and all its
expired products in use. The report should also display a header as well as
the current date and time. Retrieve the products by using the
vw_ExpiredFoodDetails view. The report should be displayed as follows:

EXPIRED PRODUCTS REPORT:


__________________________________________________

Generated:
Jan 18 2013 8:05AM

Company ID: 2
Company Name: Veggie Land
Contact Number: (051) 861 2571
Email: [email protected]
__________________________________________________
Food ID Food Type
__________________________________________________
1 Green Vegetable Mix
6 Carrots
____________________
Total Records: 2
____________________

Database Management Study Guide | V1.1 November 2015 Page 143 of 168
Project Evaluation – Project 2 (DBMM-16)

Student: Lecturer: ____


Date submitted: Date marked: ____
Achievement: % Possible Achieved
Source code documentation
Comments (author, date, purpose) for each script file 3
Descriptions for individual code segments 3
Correct indentation of code 3
Keywords capitalised, table and column names in lowercase 3
Each clause of an SQL statement appears on a different line 3
All variables declared as the correct type 3
Descriptive naming of variables 3
Subtotal: 21
Project content
Database created correctly. Tables created in same script. 6
Tables created correctly (use of PRIMARY KEY, FOREIGN KEY,
10
UNIQUE and CHECK constraints and IDENTITY, DEFAULT)
Stored procedures are correct and work as specified 16
Views are correct and work as specified 12
Triggers are correct and work as specified 4
Sample data inserted correctly 5
Indexes are correct and used efficiently 2
While loops are used appropriately and efficiently 10
Backup restores correctly and is done according to specifications 2
ER diagram included and correct 5
User documentation included and sufficient 5
General code efficiency 8
Subtotal: 85
Output
Stored procedures produce logical and meaningful output 4
Views produce logical and meaningful output 3
Subtotal: 7
Error handling
Validation of required arguments 4
Error messages are used in a clear and helpful way 3
Subtotal: 7
General comments

Total: 120

Database Management Study Guide | V1.1 November 2015 Page 144 of 168
Project 3 – Online Flight Booking
A South African Flight Booking Agency wants to develop its online booking
system to afford its clients a self-service facility. The system will allow
customers to check and reserve flights as and when required. Customers will
be able to view flights according to dates, prices, and departure and arrival
points. Any customer will be able to view flights on the system but only
registered customers/members can make a reservation.

On registration, a customer is requested to provide their South African ID or


passport number as a login ID, as well as their e-mail address, telephone
number, names, and an optional postal address. Customers with an invalid e-
mail address, telephone number and/or ID number will be rejected. The
customer is also required to specify the class of flight when booking and
ensure that information such as dates, and places of departure and arrival are
correct. Flights are marked by flight number, name, class and availability.
There are three flight classes: 1st (executive class), 2nd (business class) and 3rd
(economy class).

A ticket can only be reserved for available flights. Each client/customer can
only reserve one ticket per journey and there can be more than one passenger
per ticket. The system keeps track of flights by customers’ names, dates and
destinations. The system will allocate a status of registered and not registered
to a customer when an ID number is entered. A booking is made using the
customer’s name, flight number and date, while an optional discount is
available for seniors and minors. Based on their ID number, any customer over
65 years is considered a senior citizen and anyone under 16 years of age is a
minor. Minors may not have ID numbers, which would make it impossible to
secure a discount when booking online. Customers are given a choice of
payment options.

Specifications
Create the following views:

• vw_Flight – All flights appearing on the website, their availability and prices
per reservation.
• vw_AffordableFlight – Select the top three flights with unchanged reservation
and booking price.
• vw_SeniorUsers - Select all senior users who qualify for a discount based on
their age.
• vw_OnboardCustomers – Select all registered member names as well as the
total number of classes that each member will qualify for. HINT: Use the
COUNT function.

Create the following stored procedures:

Database Management Study Guide | V1.1 November 2015 Page 145 of 168
 sp_AddNewFlights – Insert new flights as they become available.
 sp_UpdateFlyingTimes – Update the flying times as requested by customers
based on demand.
 sp_DeleteTicket – Delete a ticket record. A ticket may only be deleted when
it has been used or if the date has expired.
sp_Report – Print the details of a specified flight class and each registered
member’s name and contact number assigned to the class. Your report's
output should have exactly the same format as that shown below:

FLIGHT BOOKING REPORT:


___________________

Book ID: 3
customer name: Janne [Date: Oct 22 2015]
flight number: 4 Journey: One way

No. Customer Name Flight Price


__________________________________________________
1. Jannie R2500
2. Calvin R1200
3. Siphiwo R12000

Use a counter to print the ’No.’ column values, and ensure that the column
values are properly aligned.

Database Management Study Guide | V1.1 November 2015 Page 146 of 168
Project Evaluation – Project 3 (DBMM-16)

Student: Lecturer: ____


Date submitted: Date marked: ____
Achievement: % Possible Achieved
Source code documentation
Comments (author, date, purpose) for each script file 3
Descriptions for individual code segments 3
Correct indentation of code 3
Keywords capitalised, table and column names in lowercase 3
Each clause of an SQL statement appears on a different line 3
All variables declared as the correct type 3
Descriptive naming of variables 3
Subtotal: 21
Project content
Database created correctly. Tables created in same script. 6
Tables created correctly (use of PRIMARY KEY, FOREIGN KEY,
10
UNIQUE and CHECK constraints and IDENTITY, DEFAULT)
Stored procedures are correct and work as specified 16
Views are correct and work as specified 12
Triggers are correct and work as specified 4
Sample data inserted correctly 5
Indexes are correct and used efficiently 2
WHILE loops are used appropriately and efficiently 10
Backup restores correctly and is done according to specifications 2
ER diagram included and correct 5
User documentation included and sufficient 5
General code efficiency 8
Subtotal: 85
Output
Stored procedures produce logical and meaningful output 4
Views produce logical and meaningful output 3
Subtotal: 7
Error handling
Validation of required arguments 4
Error messages are used in a clear and helpful way 3
Subtotal: 7
General comments

Total: 120

Database Management Study Guide | V1.1 November 2015 Page 147 of 168
Unit 11 – Test your knowledge

This section will prepare you for your theory examination.


Complete this section and hand it in to your lecturer for
marking.

1. What is the purpose of the transaction log?

2. When creating a database with the CREATE DATABASE command, in


which units can the file size be expressed? Select all the options that
apply.

3. What data type should be used to store a feedback field in a table for
which the user is allowed to enter comments of varying length?

4. Explain how an IDENTITY column works.

5. What is the difference between the UNIQUE constraint and the PRIMARY
KEY constraint?

6. Which element of a standard SELECT statement is compulsory?

a. WHERE
b. FROM
c. ORDER BY
d. INTO

7. When creating a table, how would you define a column so that it is


unique?

8. The values of an _________column are managed by the server and


usually cannot be edited.

9. How are variables declared in SQL Server?

10. How can two columns of different data types be concatenated?

11. Why can using an alias for a column in a SELECT statement be


beneficial?

12. A _____ is a clustered index by default.

13. How many clustered indexes may a table have?

Database Management Study Guide | V1.1 November 2015 Page 148 of 168
14. Name three advantages of using indexes.

15. How do you add a default value to a column?

16. What does it mean when a variable has the @@ prefix?

17. What is a view?

18. ________is short for execute.

19. What is the difference between the -- and /*…*/ comment symbols?

20. ___________functions always return a single value.

21. At which point are AFTER triggers fired?

22. Name three advantages of stored procedures.

23. How many types of relationships exist in an ERD?

24. SQL Server 2014 runs inside a type of framework. What is this
framework?

a. JDK
b. CLR
c. .NET
d. Java runtime environment

25. What file extension is used for the primary data file?

26. What file extension is used for the secondary data file?

27. What file extension is used for the transaction log file?

28. What does the ‘T’ in T-SQL stand for?

29. What is the client tool used to communicate with the server called?

30. What is the purpose of the GO statement?

31. Which data type is used for checking when the row was last updated?

32. Which keyword is used to create a foreign key?

a) REFERENCES
b) FOREIGN_KEY
c) UNIQUE
d) INDEXER

Database Management Study Guide | V1.1 November 2015 Page 149 of 168
33. What does the keyword DROP do?

34. What is the difference between DROP and DELETE?

35. What is the difference between ALTER and UPDATE?

36. What wildcard character is used in the place of any number of


characters?

37. What are the two types of built-in functions?

38. Which of the following is similar to the HAVING clause?

a) ORDER BY
b) SELECT
c) WHERE
d) ON

39. What is SELECT INTO used for?

40. List two ways to iterate through rows inside a table.

41. What keyword is used to display a custom error to the user?

42. What is @@IDENTITY used for?

43. What is RAID used for?

44. What are fixed server roles used for?

45. What is the difference between network backups and local backups? Give
advantages and disadvantages.

46. When does a trigger fire?

a) At a random time specified at compilation.


b) At a fixed time.
c) When the trigger conditions are met.
d) When the user specifies.

47. What are the benefits of using views?

48. Microsoft SQL Server offers three security choices which are:

a) INTEGRATED SECURITY
STANDARD SECURITY
MIXED SECURITY

Database Management Study Guide | V1.1 November 2015 Page 150 of 168
b) MULTIPLE SECURITY
SIMPLE SECURITY
COMPLEX SECURITY

c) ORDERED SECURITY
MIXED SECURITY
SEQUENTIAL SECURITY

49. How would you describe an index?

50. Can a table in a database have as many clustered indexes as the user
wants?

51. _________indexes cannot determine the order of rows inside a table.

52. An ______can either be disabled or dropped when it is no longer


needed.

53. An index can be created using either _______or SQL Server Studio.

54. Indexes are organised as________, and each page in the index is called
an index node.

55. How would you differentiate between a view and a stored procedure?

Database Management Study Guide | V1.1 November 2015 Page 151 of 168
Glossary

Term Description
Fields corresponding to the attributes of the objects
Columns
described by the table.
The SQL command which marks the finalisation of a
COMMIT
database transaction.
Short for Current Set Of Records in some database
Cursor languages; a database object pointing to a current
selected set of records.
Data Information collected and formatted in a specific way.
Collection of information organised into related tables of
Database
data and definitions of data objects.
A key that identifies records in a table by matching a
Foreign key primary key in a different table. The foreign keys are
used to cross-reference tables.
A database feature (a list of keys or keywords), allowing
Index
searching and locating data quickly within a table.
INSERT The SQL command used to add a new record to a table.
The SQL command used to retrieve data from 2 or more
JOIN database tables within an existing relationship based
upon a common attribute.
The primary system database. Without it, SQL Server
Master database
cannot start.
Short for Open Database Connectivity, a standard
database access technology developed by Microsoft. The
OBDC
purpose of OBDC is to allow the accessing of any DBMS
from any application.
The SQL command which is the primary means of
SELECT
retrieving data from an RDBMS.
A set of SQL statements stored within a database server
and executed as a single entity. Using stored procedures
has several advantages over using inline SQL
Stored procedure
statements, like improved performance and separation
of the application logic layer from the database layer in
n-tier applications.
Data in an RDBMS, arranged in rows and columns, which
Table
defines a database entity.
A group of SQL database commands regarded and
Transaction
executed as a single entity.

Database Management Study Guide | V1.1 November 2015 Page 152 of 168
A special type of stored procedure executed
Trigger
automatically when certain events take place.

Bibliography

Textbooks
Rankins R., Bertucci P., Gallelli C. & Silverstein A.T. 2014. Microsoft SQL
Server 2014 - UNLEASHED. ISBN 978-0-67233729-1.

Suggested reading
LeBlanc, P. 2013. Microsoft SQL Server 2012 Step by Step. Sebastopol,
California: O’Reilly Media, Inc. ISBN 978-0-7356-6386-2.

Websites
W3schools: www.w3schools.com/sql/
About.com: databases.about.com/od/sqlserver/
WiseOwl: https://www.youtube.com/watch?v=2-1XQHAgDsM
SQLCoffee: http://www.sqlcoffee.com/SQLServer2014_0006.htm

Database Management Study Guide | V1.1 November 2015 Page 153 of 168
Unit Exercise Checklist
(DBMM-16 V1.0)

Student _____ Start date

Please note that unless all of your exercises have been signed off by the
lecturer you will not be allowed to book for the exam.

Date Sign Date Sign Date Sign


Section 4.6 Section 5.3.1 Section 5.3. 1 (contd)

1 1 6
2 2 7
3 3 8
4 4
5
Section 7.7 Section 8.3 Section 9.8
1 1 1
2 2 2
3 3 3
4

Database Management Study Guide | V1.1 November 2015 Page 154 of 168
Database Management Study Guide | V1.1 November 2015 Page 154 of 168
Database Management Evaluation Form
(DBMM-16 V1.0)

How would you evaluate the Database Management study guide? Place a  or  in one of the
five squares that best indicates your choice. Your response will help us to improve the quality
of the study guides and courses, and will be much appreciated.

Very
Poor Fair Good Excellent
poor

The study guide is clear and understandable.

The text is clear and understandable.

The exercises help you to grasp the material.

The projects help you to understand the material.

You know what to expect in the examination.


The practical exercises test your knowledge and
ability.
Your lecturer was able to help you.

What did you enjoy most?

What did you enjoy least?

General comments (what would you add, leave out, etc.?).

Please note any errors that you found in the study guide.

Campus Lecturer Date

Please remove this evaluation form and return it to your lecturer or senior lecturer so that it can
be forwarded to the Division for Courseware Development.

Thank you.

Alternatively, please follow the link below to evaluate the module:


CTI Education Group Module Evaluation
Note: Google Chrome browser must be installed in the device used to view the
link

Database Management Study Guide | V1.1 November 2015 Page 155 of 168
Contact details

Bedfordview Campus Bloemfontein Campus


1st Floor, 14 Skeen Boulevard, Bedfordview Tourist Centre, 60 Park Avenue, Willows, Bloemfontein
P.O. Box 1389, Bedfordview, 2008 P.O. Box 1015, Bloemfontein, 9300
Tel: +27 (0)11 450 1963/4, Fax: +27 (0)86 686 4950 Tel: +27 (0)51 430 2701, Fax: +27 (0)51 430 2708
Email: [email protected] Email: [email protected]

Cape Town Campus


Durban Campus
The Brookside Building, 11 Imam Haron Str
59 Adelaide Tambo Drive, Durban North
(old Lansdowne Road), Claremont
P.O. Box 20251, Durban North, 4016
P.O. Box 2325, Clareinch, 7740
Tel: +27 (0)31 564 0570/5, Fax: +27 (0)31 564 8978
Tel: +27 (0)21 674 6567, Fax: +27 (0)21 674 6599
Email: [email protected]
Email: [email protected]

Durbanville Campus East London Campus


Kaapzicht, 9 Rogers Street, Tyger Valley 12 Stewart Drive, Berea, East London
P.O. Box 284, Private Bag X7 PostNet Suite 373
Tyger Valley, 7536 Private Bag X9063, East London, 5200
Tel: +27 (0)21 914 8000, Fax: +27 (0)21 914 8004 Tel: +27 (0)43 721 2564, Fax: +27 (0)43 721 2597
Email: [email protected] Email: [email protected]

Port Elizabeth Campus


Nelspruit Campus
50 Murray Street, Nelspruit Building 4, Ascot Office Park
P.O. Box 9497, Sonpark, Nelspruit, 1206 Cnr Ascot & Conyngham Roads, Greenacres
Tel: +27 (0)13 755 3918, Fax: +27 (0)13 755 3918 P.O. Box 40049, Walmer, 6065
Email: [email protected] Tel: +27 (0)41 374 7978, Fax: +27 (0)41 374 3190
Email: [email protected]

Pretoria Campus
Potchefstroom Campus
Menlyn Corporate Park, Building A
16 Esselen Street, Cnr Esselen Street
175 Corobay Avenue Cnr Garsfontein, Pretoria
& Steve Biko Avenue, Die Bult, Potchefstroom
PostNet Suite A147, Private Bag X18,
P.O. Box 19900, Noordbrug, 2522
Lynnwood Ridge, 0040
Tel: +27 (0)18 297 7760, Fax: +27 (0)18 297 7783
Tel: +27 (0)12 348 3060, Fax: +27 (0)12 348 3063
Email: [email protected]
Email: [email protected]

Randburg Campus Vanderbijlpark Campus


6 Hunter Avenue, Cnr Bram Fischer Drive Building 2, Cnr Rutherford & Frikkie Meyer Boulevards
Ferndale, Randburg Vanderbijlpark
P.O. Box 920, Randburg, 2125 P.O. Box 6371, Vanderbijlpark, 1900
Tel: +27 (0)11 789 3178, Fax: +27 (0)11 789 4606 Tel: +27 (0)16 931 1180, Fax: +27 (0)16 933 1055
Email: [email protected] Email: [email protected]

Group Head Office


Fourways Manor Office Park, Building 1
Cnr Roos & Macbeth Streets, Fourways
P.O. Box 1398, Randburg, 2125
Tel: +27 (0)11 467 8422, Fax: +27 (0)11 467 6528
Website: www.cti.ac.za

You might also like