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

Database Notes

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

Database Notes

Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 32

CHAPTER 9

Databases(Using MS Access)
DEFINITION OF A DATABASE
 A collection of related files stored on a computer under one
common name.
 A collection of related data elements that may be structured in
various ways to meet multiple processing and retrieval needs of
organizations or individuals.
 A single organized collection of data, stored with a minimum of
duplication of data items so as to provide a consistent and
controlled pool of data.

EXAMPLES OF MANUAL DATABASES (DATA RECORDED


ON PAPERS)
 Customer information at a company, Students information at a
school, Employee information Product details at a company ,
Telephone directory, Encyclopedia, Student report books, Library
catalogues, Vendor information, Order information, Inventory
information

BUILDING BLOCK OF COMPUTERISED DATABASES


BIT - Bit: A single binary digit like 0 or 1.

BYTE -Byte: A group of eight bits representing a


character for example 10010110

FIELD - Field: A specific category of information in a table


(database), for example Surname, Date of Birth,
etc. A collection of related characters or bytes

RECORD - Record: A collection of related fields describing


an entity, e.g. patient.

FILE - File: A collection of related records

DATABASE - Database: A collection of related files

DATABASE MODELS/SCHEMA/STRUCRURE
 There common models are relational, network, Hierarchical structure
and List structure

Relational database
A database with two or more tables each contains some
interconnected data.
Files are connected together by common field names held in a
number of interrelated files. It uses tables called relations. Records
appear in rows and columns. Rows are also called tuples and
columns are also called attributes. Tables are called files. A single
table can also be called a database.

Fname Surname Class


Tendai Moyo 1b
Mutsa Dhliwayo 1c
Tawanada Mendi 3c
Doit Philips 1a

DATABASE MANAGEMENT SYSTEM SOFTWARE


 Complex software used to construct, expand and maintain a
database.
These are the database application programs used to store, sort,
search and retrieve information on a computer. They are also used to

Page | 2
organize and control access to a database and provide an interface
between the user and the database.

Examples of DBMS
 MS ACCESS (relational model ), FoxPro, FoxBASE, Dbase, Paradox,
Data Ease, Rbase, System V, Informix, Codasyl ( network model), Oracle
etc

Functions of DBMS
-store data, -retrieve data, -update data, -create and maintain a data
dictionary, -manage data backups and data recovery, -Provide security
oF data (user ids, access levels and passwords)
-manage data sharing facilities, -provides security to the database
against corruption and unauthorized access , -Maintain indices to data
–indices used to retrieve and cross reference data

COMMON DATABSE TERMS


 Character-the smallest unit of data in a database also called a byte. A
letter, number or symbol.
 Fields- an organized collection of characters
 Field name- unique name given to a table column
 Field size- the number of characters the field (column can store) can be
fixed length or variable length.
 Record-related fields put together
 File- related records make up a file
 Object- files in a database that can contain data, e.g. the table, query,
macro, etc
 Table- basic element of a database that contains related data stored in
rows and column. Data represented in rows and column.
 Database- a table or all related tables making up a database. A
database made up of one table is called a flat file. Flat files may also
mean databases on small systems.
 Query- an extract of information for screen display –(a search condition
used to select specific records from a given database ( to select all form 3
out of all students in a school)
 Form- Graphical interface that is used to display or collect information.
It is a window providing labels and text boxes to collect or display data.
 Report- an extract of information in a printed format. (Extracted from
one or more tables.)
 Macro- a small program that tells the DBMS to perform one or more
actions such as running a query, opening a form or printing a report.

Page | 3
 Primary key- database field that uniquely identifies a record. Used
when search for a specific record or sorting records.
 Secondary key - a database field which can be used to organize records
e.g. sort students according to surname.
 Normalization-the processing of separating entities in a database so as
to create all possible relationship between the entities in a bid to remove
all repetition of data which in turn makes the database more efficient.
 File Management Systems- database systems on small computers like
the PC used with flat files.
 Sort -rearrange data in the order of an index.

ADVANTAGES AND DISADVANTAGES OF COMPUTERISED


DATABASES OVER THE CONVENTIONAL FILING SYSTEM

Advantages of computerized databases


 Minimum redundancy ( reduced duplication)
 Centralized updating of records( one update takes effect to all data items
linked)
 Integrity of data (correctness of data)
 Program or file independence( changes to the data does not affect file
structure or programs used)
 Increased production( queries allow instant answers and client
satisfaction)
 More information available to users ( all information need in one pool
time is saved)
 Centralized control of data(all users are catered for from one system
control point)
 More than one person can view the same data at the same time
 Quick retrieval of data
 Consistency of data ( structure does not allow erroneous data to be
input)

Disadvantages
 Expensive and difficult to install
 Large size-Used more hardware storage space
 Require a great deal of planning time and creation time
 May contain errors or failures
 complex and require a high level of education (specialist -DBA)
 Greater impact on system failure

Page | 4
MAIN FEATURES OF DATABASE PACKAGES (what we can do with
databases)
Allows us to:

Create records
Add new records
Append or insert records to already existing ones
Search specific records
Update or amend a record (e.g. change of address)
Sort records
Delete unwanted records
Display records in datasheet view, form view and as reports
Viewing records (navigate through records)
Export data to other related packages
Merge two or more data files

PLANNING A DATABASE
The following procedures are necessary to follow in planning and
creating a database.

o Know the DBMS you will use (study it)


o Identify the database name (what all the data is all about)
o Identify all the objects you want o use (tables, queries, macros, forms,
reports etc)
o Indentify the fields for each object
o Validate each fields for each table (field type, size, formats, etc)
o Enter data using a table or a form
o Identify key fields (primary and if need be the secondary keys)
o Make table relationship
o Make a list of all possible questions about the data (for query design)
o Design and run queries (forms are the best way of showing query
results)
o Determine the best way to show answers to the questions asked
above(reports)
o Design and show reports
o Design the suitable user interface (customize the database user
interface- switchboards and macros)

Page | 5
STEPS FOR PLANNING AND CREATING A DATABASE

LETS GET HANDS ON

1. Know the DBMS you will use (study it)


There are so may software for database creation. Awe will use Microsoft
Office Access 2007. Look for this program and load it

2. Identify the database name (what the all the data is all about)
Say we want to create a database about students at a school. I will call
the database Elise Gledhill Students Information.

Page | 6
The database will store information about student that the teaches or
school may require.

The details I want are


Student number, Surname, Firstname, Sex, Date of birth, Class,
Address, Home cell, Year, Fees paid, Term, Date fees were paid,
Balance.

3. Identify all the objects you want to use-ie the tables and create the
tables

I might want two tables: Student details table and Fees details table

Page | 7
Create another table then Save as Feestbl

4. Indentify the fields you want to use for each table. E.g.
Make sure you also identify primary key field that you can use to
reference the tables. In this case Student number is appropriate because
no two or three people can have the same number at the same time.

a) Student details table


-Student number
-Surname
-Firstname
-Sex
-Date of birth
-Class
-Address
-Home cell
-Year

b) Fees details table


-Student number
-Fees paid
-Date fees were paid
-Balance.

5. Design the fields for each table and validate them (field type, size,
formats, etc)
Here we want to specify what type of data it holds and how it should look
like so that its correct and acceptable (valid)

Page | 8
Page | 9
Save changes and Then Click on VIEW and enter Each student
details

NOW DO THE SAME FOR Feestbl but take note that Fees paid
and Balance fields are of Currency data type

Page | 10
6. Create a form for each record for easy data entry

Follow these steps

Create tab
Select More Forms
Form Wizard. You will see the form Wizard for step by step form
creation

Page | 11
Click
Next , next, next then
Name your form as Studentfrm then go to
Finish
Your form will appear like this

Page | 12
You can now go to form Design view to format your form and realign field
as well as add some Visual basic buttons to manipulate the records
Such as add record, view records, delete records, etc.

Now create the Feesfrm from the Feestbl using the same procedures.

7. Make table relationship


Since Studenttbl and Fesstbl are tables giving us information
about the same student, the two tables can be linked so that
information from the two tables can be combined.

Page | 13
Drag the Primary key field from Studenttbl and Drop it on Feestbl
primary Key. This is linking the primary key fields, telling the
computer that they are the same.

The click Create

Page | 14
Relationship created between the two tables

8. Now Create some Queries to you database.


Make a list of all possible questions about the data (for query
design) such as
a) Who paid fees greater than $60
b) Show female student only Surmane firstname and class
c) Show student in form 1T
d) ETC

9. Design and run queries (forms are the best way of showing
query results)

Go to Create
Query Wizard

Page | 15
Simple Query Wizard
OK
Select table and fields then OK
The query is called Femalestudents
Click Finish
Go to Design vie of the query

In or query case, the criteria id “f” for females only.

Then click RUN

Page | 16
After the run the following girls will be shown

Page | 17
NOW LETS CREATE A QUERY THAT PICKS INFROMATION FROM
BOTH TABLES
a) Who paid fees greater than $60

Go to Create on the menu bar


Query Design from the tool bars

Add the two tables and click Close on this view

Page | 18
Notice that from the design table below data is collected from both
Studenttbl and Feestbl

Running the query you will see the following results.

NOW TRY USING


UPDATE QUERY
APPEND QUERY
DELETE RECORD

10. Design reports to show data.


Use the report wizard for easy report creation.

Go to Create the
Report Wizard if you want to create a report from
specific table or query. The same way you create a
query is the same way you create a report using query
wizard.

Or

Page | 19
VB.Net - Database Access

Applications communicate with a database, firstly, to retrieve the data stored there and
present it in a user-friendly way, and secondly, to update the database by inserting,
modifying and deleting data.
Microsoft ActiveX Data Objects.Net (ADO.Net) is a model, a part of the .Net framework that
is used by the .Net applications for retrieving, accessing and updating data.

ADO.Net Object Model


ADO.Net object model is nothing but the structured process flow through various
components. The object model can be pictorially described as −

The data residing in a data store or database is retrieved through the data provider.
Various components of the data provider retrieve data for the application and update data.

An application accesses data either through a dataset or a data reader.


 Datasets store data in a disconnected cache and the application retrieves data from
it.
 Data readers provide data to the application in a read-only and forward-only mode.

Data Provider
A data provider is used for connecting to a database, executing commands and retrieving
data, storing it in a dataset, reading the retrieved data and updating the database.
The data provider in ADO.Net consists of the following four objects −
Sr.No Objects & Description
.

1 Connection
This component is used to set up a connection with a data source.

Page | 20
2 Command
A command is a SQL statement or a stored procedure used to retrieve, insert,
delete or modify data in a data source.

3 DataReader
Data reader is used to retrieve data from a data source in a read-only and
forward-only mode.

4 DataAdapter
This is integral to the working of ADO.Net since data is transferred to and from a
database through a data adapter. It retrieves data from a database into a
dataset and updates the database. When changes are made to the dataset, the
changes in the database are actually done by the data adapter.

There are following different types of data providers included in ADO.Net


 The .Net Framework data provider for SQL Server - provides access to Microsoft SQL
Server.
 The .Net Framework data provider for OLE DB - provides access to data sources
exposed by using OLE DB.
 The .Net Framework data provider for ODBC - provides access to data sources
exposed by ODBC.
 The .Net Framework data provider for Oracle - provides access to Oracle data
source.
 The EntityClient provider - enables accessing data through Entity Data Model (EDM)
applications.

DataSet

DataSet is an in-memory representation of data. It is a disconnected, cached set of


records that are retrieved from a database. When a connection is established with the
database, the data adapter creates a dataset and stores data in it. After the data is
retrieved and stored in a dataset, the connection with the database is closed. This is called
the 'disconnected architecture'. The dataset works as a virtual database containing tables,
rows, and columns.
The following diagram shows the dataset object model −

Page | 21
The DataSet class is present in the System.Data namespace. The following table describes
all the components of DataSet –

Sr.No Components & Description


.

1 DataTableCollection
It contains all the tables retrieved from the data source.

2 DataRelationCollection
It contains relationships and the links between tables in a data set.

3 ExtendedProperties
It contains additional information, like the SQL statement for retrieving data,
time of retrieval, etc.

4 DataTable
It represents a table in the DataTableCollection of a dataset. It consists of the
DataRow and DataColumn objects. The DataTable objects are case-sensitive.

5 DataRelation
It represents a relationship in the DataRelationshipCollection of the dataset. It is
used to relate two DataTable objects to each other through the DataColumn
objects.

6 DataRowCollection
It contains all the rows in a DataTable.

7 DataView
It represents a fixed customized view of a DataTable for sorting, filtering,
searching, editing and navigation.

Page | 22
8 PrimaryKey
It represents the column that uniquely identifies a row in a DataTable.

9 DataRow
It represents a row in the DataTable. The DataRow object and its properties and
methods are used to retrieve, evaluate, insert, delete, and update values in the
DataTable. The NewRow method is used to create a new row and the Add
method adds a row to the table.

10 DataColumnCollection
It represents all the columns in a DataTable.

11 DataColumn
It consists of the number of columns that comprise a DataTable.

Connecting to a Database

The .Net Framework provides two types of Connection classes −


 SqlConnection − designed for connecting to Microsoft SQL Server.
 OleDbConnection − designed for connecting to a wide range of databases, like
Microsoft Access and Oracle.

Example 1

We have a table stored in Microsoft SQL Server, named Customers, in a database named
testDB. Please consult 'SQL Server' tutorial for creating databases and database tables in
SQL Server.
Let us connect to this database. T

Take the following steps −


 Select TOOLS → Connect to Database

Page | 23
 Select a server name and the database name in the Add Connection dialog box.

 Click on the Test Connection button to check if the connection succeeded.

Page | 24
 Add a DataGridView on the form.

 Click on the Choose Data Source combo box.


 Click on the Add Project Data Source link.

 This opens the Data Source Configuration Wizard.


 Select Database as the data source type

Page | 25
 Choose DataSet as the database model.

 Choose the connection already set up.

 Save the connection string.

Page | 26
 Choose the database object, Customers table in our example, and click the Finish
button.

 Select the Preview Data link to see the data in the Results grid −

Page | 27
When the application is run using Start button available at the Microsoft Visual Studio tool
bar, it will show the following window −

Example 2
In this example, let us access data in a DataGridView control using code. Take the
following steps −
 Add a DataGridView control and a button in the form.
 Change the text of the button control to 'Fill'.
 Double click the button control to add the required code for the Click event of the
button, as shown below −

Imports System.Data.SqlClient

Page | 28
Public Class Form1
Private Sub Form1_Load(sender As Object, e As EventArgs) _
Handles MyBase.Load
'TODO: This line of code loads data into the 'TestDBDataSet.CUSTOMERS' table.
You can move, or remove it, as needed.

Me.CUSTOMERSTableAdapter.Fill(Me.TestDBDataSet.CUSTOMERS)
' Set the caption bar text of the form.
Me.Text = "tutorialspoint.com"
End Sub

Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click


Dim connection As SqlConnection = New sqlconnection()
connection.ConnectionString = "Data Source=KABIR-DESKTOP; _
Initial Catalog=testDB;Integrated Security=True"
connection.Open()
Dim adp As SqlDataAdapter = New SqlDataAdapter _
("select * from Customers", connection)
Dim ds As DataSet = New DataSet()
adp.Fill(ds)
DataGridView1.DataSource = ds.Tables(0)
End Sub
End Class
 When the above code is executed and run using Start button available at the
Microsoft Visual Studio tool bar, it will show the following window −

 Clicking the Fill button displays the table on the data grid view control −

Page | 29
Creating Table, Columns and Rows

We have discussed that the DataSet components like DataTable, DataColumn and DataRow
allow us to create tables, columns and rows, respectively.
The following example demonstrates the concept −

Example 3

So far, we have used tables and databases already existing in our computer. In this
example, we will create a table, add columns, rows and data into it and display the table
using a DataGridView object.

Take the following steps −


 Add a DataGridView control and a button in the form.
 Change the text of the button control to 'Fill'.
 Add the following code in the code editor.

Public Class Form1
Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
' Set the caption bar text of the form.
Me.Text = "tutorialspont.com"
End Sub

Private Function CreateDataSet() As DataSet


'creating a DataSet object for tables
Dim dataset As DataSet = New DataSet()
' creating the student table
Dim Students As DataTable = CreateStudentTable()
dataset.Tables.Add(Students)
Return dataset
End Function

Private Function CreateStudentTable() As DataTable

Page | 30
Dim Students As DataTable
Students = New DataTable("Student")
' adding columns
AddNewColumn(Students, "System.Int32", "StudentID")
AddNewColumn(Students, "System.String", "StudentName")
AddNewColumn(Students, "System.String", "StudentCity")
' adding rows
AddNewRow(Students, 1, "Zara Ali", "Kolkata")
AddNewRow(Students, 2, "Shreya Sharma", "Delhi")
AddNewRow(Students, 3, "Rini Mukherjee", "Hyderabad")
AddNewRow(Students, 4, "Sunil Dubey", "Bikaner")
AddNewRow(Students, 5, "Rajat Mishra", "Patna")
Return Students
End Function

Private Sub AddNewColumn(ByRef table As DataTable, _


ByVal columnType As String, ByVal columnName As String)
Dim column As DataColumn = _
table.Columns.Add(columnName, Type.GetType(columnType))
End Sub

'adding data into the table


Private Sub AddNewRow(ByRef table As DataTable, ByRef id As Integer,_
ByRef name As String, ByRef city As String)
Dim newrow As DataRow = table.NewRow()
newrow("StudentID") = id
newrow("StudentName") = name
newrow("StudentCity") = city
table.Rows.Add(newrow)
End Sub

Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click


Dim ds As New DataSet
ds = CreateDataSet()
DataGridView1.DataSource = ds.Tables("Student")
End Sub
End Class
 When the above code is executed and run using Start button available at the
Microsoft Visual Studio tool bar, it will show the following window –

Page | 31
 Clicking the Fill button displays the table on the data grid view control −

Page | 32

You might also like