You are on page 1of 11

Database Programming With Visual Basic 6

Database
A database is an application that manages data and allows fast storage and retrieval of that
data. There are different types of database but the most popular is a relational database
that stores data in tables where each row in the table holds the same sort of information. In
the early 1970s, Ted Codd, an IBM researcher devised 12 laws of normalization. These apply
to how the data is stored and relations between different tables.
Database Management Systems
A database management system (DBMS) consists of software that operates databases,
providing storage, access, security, backup and other facilities. The most popular DBMS is
Oracle, Microsoft SQL Server, Microsoft Access and MySQL.
RDBMS components
(RDBMS) include Data Definition Language (DDL) for defining the structure of the
database, Data Control Language (DCL) for defining security/access controls, and Data
Manipulation Language (DML) for querying and updating data.
Types of database
Operational database
These databases store detailed data about the operations of an organization. They are
typically organized by subject matter, process relatively high volumes of updates
using transactions. Essentially every major organization on earth uses such databases.
Examples include customer databases that record contact, credit, and demographic
information about a business' customers, personnel databases that hold information such as
salary, benefits, skills data about employees, manufacturing databases that record details
about product components, parts inventory, and financial databases that keep track of the
organization's money, accounting and financial dealings.
Data warehouse
Data warehouses archive historical data from operational databases and often from external
sources such as market research firms. Often operational data undergoes transformation on
its way into the warehouse, getting summarized, reclassified, etc. The warehouse becomes
the central source of data for use by managers and other end-users who may not have
access to operational data.
Analytical database
Analysts may do their work directly against a data warehouse, or create a separate analytic
database for Online Analytical Processing. For example, a company might extract sales
records for analyzing the effectiveness of advertising and other sales promotions at an
aggregate level.
Distributed database
These are databases of local work-groups and departments at regional offices, branch
offices, manufacturing plants and other work sites. These databases can include segments of
both common operational and common user databases, as well as data generated and used
only at a user’s own site.
Database Programming With Visual Basic 6
Tables/Entity
Tables are a collection of rows and columns that identify occurrences of the entity the table
is representing. It is these tables that consume the space required to hold the information
of our business applications.
Database Field/Columns/Attribute
In database management systems, fields are the smallest units of information you
can access and every field has a name, called the field name. A field can
be required, optional, or calculated. A required field is one in which you must enter data,
while an optional field is one you may leave blank. A calculated field is one whose value is
derived from some formula involving other fields. You do not enter data into a calculated
field; the system automatically determines the correct value.A collection of fields is called
a record.
Database Row/Tuple
Row also called a record or tuple represents a single, implicitly structured data item in
a table. In simple terms, a database table can be thought of as consisting
of rows and columns or fields. Each row in a table represents a set of related data, and
every row in the table has the same structure.
Column 1 Column 2
Row 1 Row 1, Column 1 Row 1, Column 2
Row 2 Row 2, Column 1 Row 2, Column 2
Row 3 Row 3, Column 1 Row 3, Column 2

Primary Key Definition


The primary key of a relational table uniquely identifies each record in the table. It can
either be a normal attribute that is guaranteed to be unique (such as Social Security
Number in a table with no more than one record per person) or it can be generated by the
DBMS (such as a globally unique identifier, or GUID, in Microsoft SQL Server). Primary keys
may consist of a single attribute or multiple attributes in combination.
Foreign Key Definition
Column or combination of columns in a table, whose values are related to a primary key in
another table.
Referential Integrity
Referential integrity deals with the governing of data consistency. We mostly think of it as
keeping the relations between tables valid; that is, an order may not have a customer id that
does not exist; a transaction can not be posted for an illegal (non-existent) account.
Data Type
Identifies the kind of information that an Attribute/column in an entity/table on a specific
database platform represents. These are actual physical representations and are dependent
on the actual RDBMS’s. The data type should be given by the domain definition for that
attribute/column.
Database Programming With Visual Basic 6
Join/Relationship
A join/relationship is a collection of information from two or more tables. The join is
performed by relating columns which are foreign key columns in one table with equivalent
columns which are primary key columns in the other table.
Relationship types
You share many relationships with members of your family. For instance, you and your
mother are related. You have only one mother, but she may have several children. You and
your siblings are related you may have many brothers and sisters and, of course, they'll have
many brothers and sisters as well. Database relationships are very similar in that they're
associations between tables. There are three types of relationships:
One-to-one: Both tables can have only one record on either side of the relationship. Each
primary key value relates to only one (or no) record in the related table. They're like spouses
—you may or may not be married, but if you are, both you and your spouse have only one
spouse.
One-to-many: The primary key table contains only one record that relates to none, one, or
many records in the related table. This relationship is similar to the one between you and a
parent. You have only one mother, but your mother may have several children.
Many-to-many: Each record in both tables can relate to any number of records (or no
records) in the other table. For instance, if you have several siblings, so do your siblings
(have many siblings). Many-to-many relationships require a third table, known as an associate
or linking table, because relational systems can't directly accommodate the relationship.
Business Rules
Specific business-related information that is associated with database objects. The
information can be business restrictions (allowable values), facts, or calculation rules for
given business situations.
Index
An index is a physical mechanism applied to one (or a combination of) column(s). The purpose
of the index is for the database system to use the index as a look-up mechanism instead of
reading the whole row. Indexes are a prime resource for optimalization (and thereby
increasing speed) of searches in the database.
MetaData
'Data about Data'. This is the documentation stored in the database repository, and which
holds information about your database objects. In Oracle, for example, the table
USER_TABLES holds vital information about your tables.
Database Programming With Visual Basic 6
Creating Database with Microsoft Access 2003
You can create a blank database and then add the tables, forms, reports, and other objects
later this is the most flexible method, but it requires you to define each database element
separately. Either way, you can modify and extend your database at any time after it has
been created.
1. Click New on the toolbar.
2. In the New File task pane, under New, click Blank Database.
3. In the File New Database dialog box, specify a name and location for the database,
and then click Create.
Create a Table in Design View
1. Click Tables under Objects, and then click New on the Database window toolbar.
2. Double-click Design View.
3. Define each of the fields in your table.
1. Click in the Field Name column and type a unique name for the field.
2. In the Data Type column, keep the default (Text); or click in the Data Type
column and select the data type you want.
3. In the Description column, type a description of the information this field will
contain. This description is displayed on the status bar .The description is
optional.
Define Primary key in Design View
1. Open a table in Design view.
2. Select the field or fields you want to define as the primary key.
3. To select one field, click the row selector for the desired field.To select multiple
fields hold down the CTRL key and then click the row selector for each field.
4. Click Primary Key on the toolbar.
5. When you are ready to save your table, click Save on the toolbar, and then type a
unique name for the table.
Database Programming With Visual Basic 6
DataType Property
You can use the DataType property to specify the type of data stored in a table field. Each
field can store data consisting of only a single data type.
The DataType property uses the following settings.
Setting Type of data Size
Text (Default) Text or combinations of text and Up to 255 characters or the
numbers, as well as numbers that don't require length set by the FieldSize
calculations, such as phone numbers. property.
Memo Lengthy text or combinations of text and Up to 65,535 characters.
numbers.
Number Numeric data used in mathematical calculations. 1, 2, 4, or 8 bytes
Date/Time Date and time values for the years 100 through 8 bytes.
9999.
Currency Currency values and numeric data Accurate to 8 bytes.
15 digits on the left side of the decimal
separator and to 4 digits on the right side.
AutoNumber A unique sequential (incremented by 1) number 4 bytes
assigned by Microsoft Access whenever a new
record is added to a table.
Yes/No Yes and No values and fields that contain only 1 bit.
one of two values (Yes/No, True/False, or
On/Off).

Define relationships between tables


When you create a relationship between tables, the related fields don't have to have the
same names. However, related fields must have the same data type.
1. Close any tables you have open. You can't create or modify relationships between
open tables.
2. Press F11 to switch to the Database window.
3. Click Relationships on the toolbar.
4. If you haven't yet defined any relationships in your database, the Show Table dialog
box is automatically displayed. If you need to add the tables you want to relate and
the Show Table dialog box isn't displayed, click Show Table on the toolbar.
5. Double-click the names of the tables you want to relate, and then close the Show
Table dialog box. To create a relationship between a table and itself, add that table
twice.
6. Drag the field that you want to relate from one table to the related field in the other table.
To drag multiple fields, press the CTRL key, click each field, and then drag them.
7. The Edit Relationships dialog box is displayed. Check the field names displayed in the
two columns to ensure they are correct. You can change them if necessary.
Set the relationship options if necessary.
Database Programming With Visual Basic 6

8. Click the Create button to create the relationship.


9. Repeat steps 5 through 8 for each pair of tables you want to relate.
When you close the Relationships window, Microsoft Access asks if you want to save
the layout. Whether you save the layout or not, the relationships you create are
saved in the database.
Database Programming With Visual Basic 6
Creating Database with Microsoft SQL Server 2000
To create a database
1. Expand a server group, and then expand a server.
2. Right-click Databases, and then click New Database.
3. Enter a name for the new database.
The primary database and transaction log files are created using the database name
you specified.The initial sizes of the database and transaction log files are the same
as the default sizes specified for the model database.
4. To change the default values for the new primary database file, click the General tab.
To change the defaults values for the new transaction log file, click the Transaction
Log tab.
5. To change the default values provided in the File name, Location, Initial size (MB)
columns, click the appropriate cell to change and enter the new value.
6. To specify how the file should grow, select from these options:
• To allow the currently selected file to grow as more data space is needed,
select Automatically grow file.
• To specify that the file should grow by fixed increments, select In megabytes
and specify a value.
• To specify that the file should grow by a percentage of the current file size,
select By percent and specify a value.
7. To specify the file size limit, select from these options:
• To allow the file to grow as much as necessary, select Unrestricted filegrowth.
• To specify the maximum size the file should be allowed to grow to, select
Restrict filegrowth (MB) and specify a value.
Database Programming With Visual Basic 6

To create a Table
1. Expand a server group, and then expand a server , and then expand a Database
2. Right-click Tables, and then click New Table.
3. Click in the Column Name and type a unique name for the field.
4. In the Data Type column select the data type you want.
5. In the Length column, type a length according data type.
6. When you are ready to save your table, click Save on the toolbar, and then type a
unique name for the table.

DataType

DataType Description Storage Size


BigInt Holding numbers between -9,223,372,036,854,775,807 8 bytes
and 9,223,372,036,854,775,807.
Int An INT (integer) column holds integer numbers between 4 bytes
+2,147,483,647 and -2,147,483,648, inclusive.
SmallInt A SMALLINT (small integer) column holds whole numbers 2 bytes
between +32,767 and -32,768, inclusive.
TinyInt A TINYINT (tiny integer) column holds whole numbers 1 bytes
between 0 and 255
Float A FLOAT type is called an “approximate” data type as it 8 bytes
will only store data to a certain size, and for repeating
data to the right of the decimal it is very hard for it to
accurately store that data
Real Use this datatype for positive and negative decimal 4 bytes
numbers and for numbers with exponents when less
precision than float is required.
Money A Money column stores dollar and cent values between 8 bytes
+922,337,203,685,447.5808 and
-922,337,203,685,447.5808
SmallMoney A SmallMoney column stores dollar and cent values 4 bytes
between +214,748.3648 and -214,748.3647
Char(n) A Char(n) (character) column holds any combination of up
Database Programming With Visual Basic 6
to 8192 bytes (8K) letters, numbers, and symbols
VarChar(n) A VarChar(n) (variable-length character column) can hold
any combination of up to 8192 (8K) letters, numbers, and
symbols.
Text A Text column is of variable length and can hold up to 2
gigabytes of printable characters.
DateTime A DateTime column can hold date and time of day. 5 bytes
SmallDateTime A SmallDateTime column can hold dates and times of day 4 bytes
with less precision than is provided by datetime.
Bit A Bit column holds either a "0" or a "1". 1 bytes
NText Same as the Text data type except you can store up to
approximately 1 gigabyte of data.
NVarChar(n) Same as the VarChar(n) data type except you can store up
to 4000 bytes of data.
NChar(n) Same as the Char(n) data type except you can store up to
4000 bytes of data.
Binary(n) A Binary(n) column can hold up to 8192 (8K) bytes of
fixed-length binary data. The binary datatypes are used
for storing bit patterns or binary files.
VarBinary(n) A VarBinary(n) column can hold up to 8192 (8K) bytes of
variable-length binary data.
Image The Image data type is a variable-length column that holds
up to 2 gigabytes of binary data.
Database Programming With Visual Basic 6
Creating a Foreign Key Constraint
To create a foreign key constraint for a new table using the SQL Server Enterprise
Manager you will need to use the Database Diagram tool.
1. Click on the Diagrams folder under the EmployeeInformation database.
2. Right mouse click and choose New Database Diagram.
3. This will fire the Create Database Diagram Wizard. In the first screen after
the opening screen choose the tables you wish to relate to one another.
4. In this example, select the Department table and Employee table from the
available table’s list box and move it to the Tables to add to the diagram list
box by clicking on the Add button.
5. Click on the Next button and finally click on the Finish button.
6. You will now see both tables appear on database diagram.
7. Click on the Dept_Code column in the Department table, and drag and drop
the column over to the Employee table.
8. This will now pop-up a dialog box showing you the two fields related to one
another. It will only be able to relate the two correct fields if you have
created the same field names in the two tables.
9. Click the OK button to create the relationship.
10. Now click on the Save button on the database diagram to save the diagram
and the relationship. You will be asked to name this diagram.
You would now repeat this process for all of the tables that you need to relate to one
another.
Database Programming With Visual Basic 6
Overview of SQL Query Analyzer
Microsoft® SQL Server™ 2000 SQL Query Analyzer is a graphical tool that allows you to:
• Create queries and other SQL scripts and execute them against SQL Server
databases.
• Quickly create commonly used database objects from predefined scripts.
• Execute stored procedures without knowing the parameters.
• Debug stored procedures.
• Quickly insert, update, or delete rows in a table.
You can run SQL Query Analyzer directly from the Start menu, or you run it from inside
SQL Server EM. You can also run SQL Query Analyzer from the command prompt by
executing the isqlw utility.
Connecting to SQL Server
If you access SQL Query Analyzer through a connection in SQL Server Enterprise Manager,
a query window opens automatically. If you access SQL Query Analyzer without first
establishing a connection, the Connect to SQL Server dialog box is displayed to allow you to
specify a database server.
To connect to SQL Server
1. In the Connect to SQL Server dialog box, enter the name of the database server in
the SQL Server box. To select the local server, select (local).To specify another
server or another instance of the server, enter the server name in the SQL Server
box. Click the browse button (...) to display a list of active servers. The servers are
listed using the format servername\instancename.
2. Click Windows NT Authentication to connect using Windows NT Authentication. Or
Click SQL Server Authentication to connect using SQL Server Authentication.

You might also like