Database Notes
Database Notes
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.
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.
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
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.
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.
Page | 5
STEPS FOR PLANNING AND CREATING A DATABASE
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.
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.
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
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.
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.
Page | 14
Relationship created between the two tables
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
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
Page | 18
Notice that from the design table below data is collected from both
Studenttbl and Feestbl
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.
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.
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.
DataSet
Page | 21
The DataSet class is present in the System.Data namespace. The following table describes
all the components of DataSet –
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
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
Page | 23
Select a server name and the database name in the Add Connection dialog box.
Page | 24
Add a DataGridView on the form.
Page | 25
Choose DataSet as the database model.
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
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.
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
Page | 31
Clicking the Fill button displays the table on the data grid view control −
Page | 32