Database Lab - Tips and Tricks
Database Lab - Tips and Tricks
Weather Sta,on
Database
h2p://home.hit.no/~hansha/?lab=database
1
Table of Contents
1.Introduc,on
2.Database
Systems
3.Database
Design
-
Visio
4.SQL
Server
5.LabVIEW
2
1. IntroducSon
System
Overview
Server
Client
Visio 2012
Database
LabVIEW 2012
(Windows Server)
(Windows 7/8)
SoXware
In
this
Assignment
you
need
the
following
SoRware:
MicrosoX
Visio
2010
and
MicrosoR
SQL
Server
2012
are
free
to
use
as
they
are
included
in
the
MicrosoX
DreamSpark
Premium
SubscripSon
available
for
students
at
Telemark
University
College.
In
our
assignment
it
is
recommended
to
use
the
MicrosoX
SQL
Server
2012
Express
version
which
is
free
of
charge
and
can
be
downloaded
here:
hZp://www.microsoR.com/en-us/download/details.aspx?id=29062
SQL
Toolkit
makes
it
easy
to
access
Database
Systems
from
LabVIEW.
The
Toolkit
is
created
by
Hans-PeZer
Halvorsen
and
is
free
of
charge
from
my
web
site.
5
Task Overview
h2p://home.hit.no/~hansha/?lab=database
6
Tutorials,
etc.
Structured
Query
Language
Database
CommunicaSon
in
LabVIEW
Use
these
Tutorials
to
get
a
beZer
understanding
of
the
topics
involved
in
the
assignment
Example
Code
The
Example
Code
help
you
solve
the
assignment!
You
nd
Tutorials,
Example
Code
and
Addi,onal
Resources
here:
h2p://home.hit.no/~hansha/?lab=database
Hardware
We
will
use
a
NI
USB-TC01
Thermocouple
Measurement
Device
in
order
to
log
Temperature
Data
into
the
Database
NI
USB-TC01
Thermocouple
Measurement
Device
using
LabVIEW
The
USB-TC01
Device
works
similar
as
the
USB-6008
DAQ
device,
i.e.,
you
use
the
DAQ
Assistant
Temperature
SimulaSon
If
you
dont
have
a
TC01
device,
you
can
create
a
simple
Temperature
Simulator
instead.
Example
of
such
as
Simulator:
While
Loop
While Loop
Case Structure
Case Structure
2.
Database
Systems
11
Database
Systems
A
Database
is
a
structured
way
to
store
lots
of
informa,on.
The
informa,on
is
stored
in
dierent
tables.
-
Everything
today
is
stored
in
databases!
Examples:
Bank/Account
systems
Informa,on
in
Web
pages
such
as
Facebook,
Wikipedia,
YouTube,
etc.
Fronter,
TimeEdit,
etc.
lots
of
other
examples!
12
Oracle
MySQL
(owned
by
Oracle,
but
previously
owned
by
Sun
Microsystems)
-
MySQL
can
be
used
free
of
charge
(open
source
license),
Web
sites
that
use
MySQL:
YouTube,
Wikipedia,
Facebook
MicrosoR
Access
IBM
DB2
Sybase
etc.
We
will
use
SQL
server
because
it
is
very
popular
in
the
industry
today,
and
we
can
use
it
for
free
via
the
MicrosoX
DreamSpark
Premium
SubscripSon
which
is
available
for
the
students
and
sta
at
Telemark
University
College,
or
use
the
Express
version
which
is
available
for
free
for
everybody.
13
14
3.
Database
Design
-
Visio
15
MicrosoX
Visio
Tools
for
Design
and
Modeling
Database
Systems:
ERwin
(very
good!
But
expensive!
~$5000)
Note!
CA
ERwin
Data
Modeler
Community
Edi,on
is
a
free
edi,on
that
contains
a
subset
of
the
standard
product
Toad
Data
Modeler
MS
Visio
lots
of
other
tools
We
will
use
MS
Visio
because
it
has
everything
we
need
and
it
is
a
very
popular
part
of
the
MS
Oce
package
(and
you
probably
already
have
it
installed).
We
can
also
use
it
for
free
via
the
MicrosoX
DreamSpark
Premium
SubscripSon
available
for
the
students
and
sta
at
Telemark
University
College
16
Column
Names
Primary
Key
Primary
Key
Foreign
Key
Rela,onal
Database.
In
a
rela,onal
database
all
the
tables
have
one
or
more
rela,on
with
each
other
using
Primary
Keys
17
(PK)
and
Foreign
Keys
(FK).
Note!
You
can
only
have
one
PK
in
a
table,
but
you
may
have
several
FKs.
Table Name
PK
PK
FK
Table
Name
PK
FK
Table
Name
PK
FK
FK
PK
FK
PK-FK
RelaSonships
Table
Name
PK
FK
Table
Name
PK
FK
FK
Table
Name
PK
FK
FK
18
19
1
2
20
Table Name
Visio - Example
21
22
4. SQL Server
23
24
1
2
Your Database
Your
Tables
25
26
28
2
Next,
the
table
designer
pops
up
where
you
can
add
columns,
data
types,
etc.
29
30
select
SchoolName,
CourseName
from
You
link
Primary
Keys
and
Foreign
Keys
together
SCHOOL
inner
join
COURSE
on
SCHOOL.SchoolId
=
COURSE.SchoolId
31
Create View:
32
4
Add
necessary
tables
Save
the
View
33
Stored Procedure
Procedure
Name
Input
Arguments
Internal/Local
Variables
Note!
Each
variable
starts
with
@
34
Trigger
A
Trigger
is
executed
when
you
insert,
update
or
delete
data
in
a
Table
specied
in
the
Trigger.
Inside
the
Trigger
you
can
use
ordinary
SQL
statements,
create
variables,
etc.
Internal/Local Variables
SQL
Code
(The
body
of
the
Trigger)
GO
Note!
INSERTED
is
a
temporarily
table
containing
the
latest
inserted
data,
and
it
is
very
handy
to
use
inside
a
trigger
35
5. LabVIEW
36
GUI/HMI Example:
App
#1
Logg
data
from
TC-01
Device
and
save
the
data
into
the
database
Code
Example:
37
2
Acquire
Signals
Analog
Input
Temperature
Thermocouple
5
Click
OK
to
Finish
38
App #2
GUI/HMI Example:
39
App #3
GUI/HMI Example
40
ODBC
ODBC
(Open
Database
Connec,vity)
is
a
standardized
interface
(API)
for
accessing
the
database
from
a
client.
You
can
use
this
standard
to
communicate
with
databases
from
dierent
vendors,
such
as
Oracle,
SQL
Server,
etc.
The
designers
of
ODBC
aimed
to
make
it
independent
of
programming
languages,
database
systems,
and
opera,ng
systems.
Control
Panel
Administra,ve
Tools
Data
Sources
(ODBC)
We
will
use
this
ODBC
Connec,on
later
in
LabVIEW
in
order
to
open
the
Database
Connec,on
from
LabVIEW
41
Select
the
Database
you
are
using
for
the
Library
Use
either
Windows
or
SQL
Server
authen,ca,on
(Windows
is
simplest
to
use!)
Test
your
connec,on
to
see
if
its
works
42
43
Your
ODBC
Connec,on
Example
2:
Write
Data
to
Database
from
LabVIEW:
3
45
GUI/HMI
Code:
LabVIEW
Example
This
example
shows
is
a
Library
Management
System,
but
a
system
for
manipula,ng
Weather
Parameters
will
work
the
same
way
Menu Ring
Mul,column
Listbox
1
3
Insert
the
Book
informa,on
in
the
Database
using
a
Stored
Procedure
47
ShiR Register
The Programming Technique shown above is called a State Machine and it is recommended that you use this method!
48
Grading/Submission
-
Checklist
Submission
in
Fronter
(one
.ZIP
File
Not
.RAR!!!!!!)
The
Report
in
PDF
format
is
included
in
the
ZIP
File
The
LabVIEW
Code
is
included
in
the
ZIP
File
The
ER
diagram
in
Visio
is
included
in
the
ZIP
File
The
SQL
Code/SQL
Scripts
(Views,
Triggers,
Stored
Procedure,
etc.)
are
included
in
the
ZIP
File
A
Printed
Copy
of
the
Report
is
in
the
Mail
Box
outside
my
Oce
Note!
The
Database
Lab
counts
25%
of
the
nal
grade
in
the
course
49
E-mail:
[email protected]
Blog:
h2p://home.hit.no/~hansha/
50