0 - 1 Lab Exercises - Ms Access 2010
0 - 1 Lab Exercises - Ms Access 2010
Principles of Databases
Lab Exercises
https://www.halvorsen.blog
Relational Databases
A relational database matches data using common characteristics found within the data set.
The resulting groups of data are organized and are much easier for people to understand.
For example, a data set containing all the real-estate transactions in a town can be grouped
by the year the transaction occurred; or it can be grouped by the sale price of the
transaction; or it can be grouped by the buyer's last name; and so on.
Such a grouping uses the relational model (a technical term for this is schema). Hence, such
a database is called a "relational database."
The software used to do this grouping is called a relational database management system.
The term "relational database" often refers to this type of software.
Relational databases are currently the predominant choice in storing financial records,
manufacturing and logistical information, personnel data and much more.
2.1 Tables
The basic units in a database are tables and the relationship between them. Strictly, a
relational database is a collection of relations (frequently called tables).
Below we see how a relationship between two tables are defined using Primary Keys and
Foreign Keys.
5
6 Relational Databases
A unique key must uniquely identify all possible rows that exist in a table and not only the
currently existing rows. Examples of unique keys are Social Security numbers or ISBNs.
A primary key is a special case of unique keys. The major difference is that for unique keys
the implicit NOT NULL constraint is not automatically enforced, while for primary keys it is
enforced. Thus, the values in unique key columns may or may not be NULL. Another
difference is that primary keys must be defined using another syntax.
Likewise, unique keys can be defined as part of the CREATE TABLE SQL statement.
The referencing and referenced table may be the same table, i.e. the foreign key refers back
to the same table. Such a foreign key is known as self-referencing or recursive foreign key.
A table may have multiple foreign keys, and each foreign key can have a different referenced
table. Each foreign key is enforced independently by the database system. Therefore,
cascading relationships between tables can be established using foreign keys.
Improper foreign key/primary key relationships or not enforcing those relationships are
often the source of many database and data modeling problems.
Foreign keys can be defined as part of the CREATE TABLE SQL statement.
...
)
4. Views
In database theory, a view consists of a stored query accessible as a virtual table composed
of the result set of a query. Unlike ordinary tables in a relational database, a view does not
form part of the physical schema: it is a dynamic, virtual table computed or collated from
data in the database. Changing the data in a table alters the data shown in subsequent
invocations of the view.
Syntax:
2.5 Functions
In SQL databases, a user-defined function provides a mechanism for extending the
functionality of the database server by adding a function that can be evaluated in SQL
statements. The SQL standard distinguishes between scalar and table functions. A scalar
function returns only a single value (or NULL), whereas a table function returns a (relational)
table comprising zero or more rows, each row with one or more columns.
User-defined functions in SQL are declared using the CREATE FUNCTION statement.
Syntax:
RETURNS <datatype>
AS
…
Stored procedures are not part of the relational database model, but all commercial
implementations include them.
CALL procedure(…)
or
EXECUTE procedure(…)
Stored procedures can return result sets, i.e. the results of a SELECT statement. Such result
sets can be processed using cursors by other stored procedures by associating a result set
locator, or by applications. Stored procedures may also contain declared variables for
processing data and cursors that allow it to loop through multiple rows in a table. The
standard Structured Query Language provides IF, WHILE, LOOP, REPEAT, CASE statements,
and more. Stored procedures can receive variables, return results or modify variables and
return them, depending on how and where the variable is declared.
2.7 Triggers
A database trigger is procedural code that is automatically executed in response to certain
events on a particular table or view in a database. The trigger is mostly used for keeping the
integrity of the information on the database. For example, when a new record (representing
a new worker) added to the employees table, new records should be created also in the
tables of the taxes, vacations, and salaries.
Diagrams created using this process are called entity-relationship diagrams, or ER diagrams
or ERDs for short.
There are many ER diagramming tools. Some of the proprietary ER diagramming tools are
ERwin, Enterprise Architect and Microsoft Visio.
Microsoft SQL Server has also a built-in tool for creating Database Diagrams.
14
15 Database Modelling
In the Database menu Visio offers lots of functionality regarding your database model.
“Reverse Engineering” is the opposite procedure, i.e., extraction of a database schema from
an existing database into a database model in Microsoft Visio.
CUSTOMER
o CustomerId (PK)
o FirstName
o LastName
o Address
o Phone
o PostCode
o PostAddress
PRODUCT
o ProductId (PK)
o ProductName
o ProductDescription
o Price
o ProductCode
ORDER
o OrderId (PK)
o OrderNumber
o OrderDescription
o CustomerId (FK)
ORDER_DETAIL
o OrderDetailId (PK)
o OrderId (FK)
o ProductId (FK)
[End of Example]
4.3 ERwin
ERwin is a professional database modelling tool. A Community edition is also available for
free. The Community edition is limited to work with max 25 objects.
With Erwin and other professional database modelling tools you can directly import the
database model into the database system such as SQL Server, MySQL, etc.
Microsoft Access is used by programmers and non-programmers to create their own simple
database solutions.
2. Example Database
I will present an example database in Microsoft Access 2007 which will be used in some of
the examples and exercises in this document.
CUSTOMER
o CustomerId (PK)
o FirstName
o LastName
o Address
o Phone
o PostCode
o PostAddress
PRODUCT
24
25 Microsoft Office Access
o ProductId (PK)
o ProductName
o ProductDescription
o Price
o ProductCode
ORDER
o OrderId (PK)
o OrderNumber
o OrderDescription
o CustomerId (FK)
ORDER_DETAIL
o OrderDetailId (PK)
o OrderId (FK)
o ProductId (FK)
ODBC Connection:
The following SQL Query inserts some example data into these tables:
--CUSTOMER
INSERT INTO [CUSTOMER] ([FirstName],[LastName],[Address],[Phone],[PostCode],[PostAddress])
VALUES ('Per', 'Nilsen', 'Vipeveien 12', '12345678', '1234', 'Porsgrunn')
GO
27
28 Creating and Using Tables
--PRODUCT
INSERT INTO [PRODUCT] ([ProductName],[ProductDescription],[Price],[ProductCode]) VALUES
('Product A', 'This is product A', 1000, 'A-1234')
GO
INSERT INTO [PRODUCT] ([ProductName],[ProductDescription],[Price],[ProductCode]) VALUES
('Product B', 'This is product B', 1000, 'B-1234')
GO
INSERT INTO [PRODUCT] ([ProductName],[ProductDescription],[Price],[ProductCode]) VALUES
('Product C', 'This is product C', 1000, 'C-1234')
GO
--ORDER
INSERT INTO [ORDER] ([OrderNumber],[OrderDescription],[CustomerId]) VALUES ('10001', 'This is
Order 10001', 1)
GO
INSERT INTO [ORDER] ([OrderNumber],[OrderDescription],[CustomerId]) VALUES ('10002', 'This is
Order 10002', 2)
GO
INSERT INTO [ORDER] ([OrderNumber],[OrderDescription],[CustomerId]) VALUES ('10003', 'This is
Order 10003', 3)
GO
--ORDER_DETAIL
INSERT INTO [ORDER_DETAIL] ([OrderId],[ProductId]) VALUES (1, 1)
GO
INSERT INTO [ORDER_DETAIL] ([OrderId],[ProductId]) VALUES (1, 2)
GO
INSERT INTO [ORDER_DETAIL] ([OrderId],[ProductId]) VALUES (1, 3)
GO
INSERT INTO [ORDER_DETAIL] ([OrderId],[ProductId]) VALUES (2, 1)
GO
INSERT INTO [ORDER_DETAIL] ([OrderId],[ProductId]) VALUES (2, 2)
GO
INSERT INTO [ORDER_DETAIL] ([OrderId],[ProductId]) VALUES (3, 3)
GO
INSERT INTO [ORDER_DETAIL] ([OrderId],[ProductId]) VALUES (3, 1)
GO
INSERT INTO [ORDER_DETAIL] ([OrderId],[ProductId]) VALUES (3, 2)
GO
INSERT INTO [ORDER_DETAIL] ([OrderId],[ProductId]) VALUES (3, 3)
GO
[End of Example]
Just as functions (in programming) can provide abstraction, so database users can create
abstraction by using views. In another parallel with functions, database users can manipulate
nested views, thus one view can aggregate data from other views.
Syntax:
30
Introduction to Database
Systems
Hans-Petter Halvorsen
Copyright © 2017
E-Mail: [email protected]
Web: https://www.halvorsen.blog
https://www.halvorsen.blog