Visual FoxPro
Visual FoxPro
Zap command: -
Syntax: [ZAP]
This command is used to erases all records of active database
except its structure. This is very dangerous command, so use it
carefully because lost data can't be recovered.
Example: - Zap
Erase command: -
Syntax: [DELETE FILE <Filespec>]
or
[ERASE <Filename>]
This command is used to delete a file physically with all that’s
records and structure.
Example: - Delete File Akshay.Dbf
Erase Akshay.Dbf
Recall command: -
Syntax: [Recall] <SCOPE> [FOR <Expression>] [<Logical
Operator> <Expression>]
This command is used to unmark the specified record which was
marked for deletion.
Example: - Recall All
Recall Next 7
Recall Rest
Recall Record 7
Recall All For State = "Bengal"
Recall All For State = "Bihar" .And. City = "Patna"
Recall All For Salary <=5000
Recall All For Male
Recall All For Dob={10/10/80}
Run command: We can execute any DOS command or non Visual
FoxPro program without quitting from Visual FoxPro.
A DOS command is by placing executed with RUN or exclamation
mark (!) before writing the actual DOS commands.
The result of a DOS command appears in a separate Visual FoxPro
RUN command window.
Set Default: Visual FoxPro uses its default drive and directory for
creating files, unless we specify the path separately. For example: SET
DEFAULT TO C:
We can specify the full path name of a directory, by using:
SET DEFAULT TO C:\LEARNFOX\DATA
Display and List command:
List: - Syntax: [LIST] [FIELD <Field List>] [FOR <Expression>] [<Logical
Operator> <Expression>] [OFF] [TO PRINT]
This command is used to see or print the contents of a database
file. "OFF" suffix is used here for not print record no. "TO PRINT" suffix is
used for print information through printer if it is ON.
Example: - List
List Field Name, Salary
List For State = "Bihar"
List For State = "Bihar" .And. City = "Patna"
List Off
List Off Field Name,F_Name,City,Salary To Print
List For Salary <=5000
List For Male
List For Dob={10/10/80}
List For Dob=CTOD("10/10/80")
List For "Kumar"$ Name
DISPLAY: - Syntax: [DISPLAY] <SCOPE> [FIELD <Field List>] [FOR
<Expression>] [<Logical Operator> <Expression>] [OFF] [TO PRINT]
[FILES] [STRUCTURE] [MEMORY] [STATUS] [DELETED ()]
This command is used to display the records and some other
information. "OFF" suffix is used here for not print record no. "TO PRINT”
suffix is used for print information through printer if it is ON.
Example: - Display All
Display Next 5
Display Rest
Display Record 10
Display All Field Name,Salary
Display All For State = "Bihar"
Display All For State = "Bihar" .And. City = "Patna"
Display All Off
Display All Name,State To Print
Display All Off Field Name,F_Name,City,Salary To Print
Display All For Salary <=5000
Display All For Male
Display All For Dob={10/10/80}
Display All For Dob=CTOD("10/10/80")
Display All For "Kumar"$ Name
Display Files
(Displays .DBF files of current
Path/Directory.)
Display Structure
(Display structure of active database.)
Display Memory
(Display Memory Variables.)
Display Status
(Display current status of FOXPRO.)
Display For Deleted ()
(Display only those records which are marked for deletion.)
Using Logical Expression: Logical expressions are either true or false. They are
generally used to find an individual record or to isolate records that meet certain
criteria.
Logical Functions: Some functions also return .T. i.e. true or .F. i.e. false,
although they are generally used in programming.
Mathematical Operators:-
Unary minus -
Exponentiation ^ or **
Division /
Multiplication *
Subtraction -
Addition +
Relational Operators: -
> Less Than
> Greater Than
= Equal To
<> Not Equal To
# Not Equal To
<= Less Than Or Equal To
=> Greater Than Or Equal To
Logical Operators: -
1. .AND. :- Select only those records who fulfill both the conditions.
2. .OR. :- Select all those records who fulfill either first or second
condition.
3. .NOT. :- Select all those records who do not fulfill specified condition.
The IIF() Function: IIF is the function from an IF…ELSE…ENDIF structure, which
has three parameters. The first defines the logical condition, the second is the
expression returned if condition is true, and third is the one returned if it false. The
Syntax for this function: IIF (condition, expression1, expression2)
The IIF() function can save space in our programs, because in one line it can
take the place of the IF…ELSE…ENDIF commands. Example: In IF command: - IF
Result = “P”
? “Pass”
ELSE
? “Fail”
ENDIF
In IIF() function: - ?IIF (Result = “P”, “Pass”, “Fail”)
Sorting and Indexing: We can arrange records in our table in a way that best
suits our needs. Visual FoxPro has two commands for organizing records in a table
namely SORT and INDEX. We can perform Sorting or Indexing tasks at the
command level from Command window.
Sort: - Sorting means grouping records in a particular order say ascending or
descending based on a key field of the records.
Sorting a table: - Syntax : [SORT ON] <Field Name [/A] [/C] [/D]> [TO
<New Filename>]
This command is used to arrange the records of the active database
file according to a field to another database file into ascending or
descending order.
Example :- Sort On Name To Rajiv.Dbf
Sort On Name/A To Rajiv.Dbf
Sort On Salary/D To Msal.dbf
Sort On Name/C To Nameasc.Dbf
Index: - Indexing is a process of arranging records in a particular order.
In indexing, original record no do not change. We will note that the
indexed file on the right side of figure retains the original record number.
Indexing a table: - Syntax: INDEX ON <field expr> TO <.IDX file> | TAG <tag
name> [OF <CDX file>] [FOR <condition>] [COMPACT]
[ASCENDING/DESCENDING] [UNIQUE] [ADDITIVE]
Example: - INDEX ON NAME TO NAMES
Set Index: An index that has just been created is active. But by using
the SET INDEX command, we can activate any of the indexes.
Set Order: To activate an index file use the SET ORDER command. With
SET ORDER, we use numbers such as, SET ORDER TO 2. The number 2
here indicates the order in which the index file was originally opened.
Reindex: This command recreates all the open index files. It rebuilds all
open index files in the current work area. If any changes have been made
to the table while the index file was closed, we can update with the
REINDEX command.
Use of Macro: Macro substitution is a programming technique used in
command files. The macro is just like an ordinary memory variable, but
has an ampersand (&) in front of it namely, &NAME, &FLD etc. When
Visual FoxPro encounters a macro in command file, it replaces that macro
with the contents of the memory variable.
Use of Multiple Tables: Visual FoxPro is a relational database
manager, and hence gives us the choice to use one or more than one
table at a time. It also defines relationships between two or more table.
Relating Files: - Using Visual FoxPro relational power, we can create a
relation to connect or join records from different tables and thus we can
access their information at the same time.
In a relation, one table is called the parent table and the other is
called as child table. The parent table controls the child so that when we
move the record pointer in the parent table, Visual FoxPro automatically
moves the record pointer to the first corresponding record in the child
table. If a matching record cannot be traced in the child table, then
records pointer moves to the end of the last record in child file.
Queries Views
Queries are simpler to create. Views are somewhat more than
difficult to create than Queries.
Outputs of Queries are read-only Views are not read-only which
which ensures that data in a query means that data in a view is
are not accidentally altered. modifiable. Also, the modifications
made in a view get automatically
reflected in the base tables. Hence,
Views are less safe than Queries.
Queries perform slightly better than Views perform less better than
Views. Queries.
Queries can be selected as input to
graph, cross-tab, report and label Outputs of views are limited to
creation. Browse windows and Screen Forms.
Query Designer: In Visual FoxPro, we create queries in special window
called the Query Designer. The Query Designer refers to the way that
Visual FoxPro allows us to get information from table. The Query Designer
is a versatile and powerful tool for retrieving information.
Working with Multiple Tables: Through Query Designer we work
with multiple pages. To add a table to the Query Designer:-
1. Right click the top pane of the designer. From the menu that
appears, select Add Table… In the Add Table or View dialog box
that appears select the table which we want to use and click Add
button.
2. A join condition dialog box will appear. Select the matching fields
from the two tables and specify whether we want Inner, Left, Right
or Full join.
3. Now the top pane of Query Designer displays two tables. The
designer shows join condition by displaying lines that connect the
matching fields between the two tables. We can drag a field from
one table to matching field in the other table to create further join
conditions.
To insert a join condition, click the Insert button and to
remove a selected join condition click the Remove button.
Directing output: The default output for the query designer is the
browse window. We can specify other output destinations using the query
destination button in the query designer toolbar.
The browse button will direct the output to the browse window.
The cursor button will direct the output to a cursor. Cursor is a
temporary file which can hold the results of a query temporarily and the
contents of which can be passed to another SQL statement.
The table button will direct the output to a dbf file.
The graph button will connect the output with Microsoft graph
wizard. It will guide us through the process of creating a rudiment graph.
The screen button will connect the output to an input/edit screen.
The report button will direct the output into a visual FoxPro report
or will create a quick report immediately.
The label button will direct the output to a label form.
After selecting the output destination, click OK.
Join: In this tab we have to specify join type, field name and criteria.
For specifying the criteria, we can use relational operators = (like), ==
(exactly like), > (more than) and < (less than). These relational operators
can be used with NOT operator to negate the logic.
To insert a join condition click the Insert button and to remove a
selected join condition clicks the Remove button.
Viewing SQL code: We can view the actual SQL code, using the SQL
button in the query designer toolbar.
Alternatively, one can also view the SQL code by selecting View SQL
from the query menu.
Maximizing the Table view: To maximize the top pane of the query
designer window, click the maximize the table view button in the query
designer toolbar.
Running the query: To run our query, choose run query from the
query menu. This will execute the SQL query we have built and send the
results to the output destination which we have specified. Alternatively
press ctrl + q keys to run the query.
A grouped query: Suppose we need to group the records on the basis
of department, select the field to be grouped from the available fields list
and click add button. Similarly, fields can be removing from the grouped
fields list by clicking the remove button.
We can also specify the selection criteria for groupings. This can be
done by clicking the having button.
SQL select command: SQL SELECT command retrieves data from a
table. Syntax: SELECT * FROM <Table name>
From: From is the keyword in SQL, which must be present in every
SELECT query. Syntax: SELECT <Column name> FROM <Table name>
Where: Suppose we want to see only a certain number of rows that
contain specific values. For this, we would need to place WHERE clause
along with the SELECT statement. Syntax: SELECT <Column name>
FROM <Table name> WHERE <Column name> <Operator> <Value>
Aggregate functions: SQL Aggregate functions produce a single
value for the entire group of table entries. These functions are SUM, AVG,
MAX, MIN and COUNT.
SUM: This function calculates the arithmetic sum of all selected values
of a given column. Syntax: SELECT SUM <Column name>
FROM<Table name>
AVG: This function calculates the average of all selected values of a
given field. Syntax: SELECT AVG <Field name> FROM<Table name>
MAX: This function calculates the largest of all selected values of a
given column. Syntax: SELECT MAX <Column name> FROM<Table
name>
Min: This function calculates the smallest of all selected values of a
given column. Syntax: SELECT MIN <Column name> FROM<Table
name>
Count: This function counts the number of rows in the output table.
Syntax: SELECT COUNT <Distinct/*> FROM < Table name>
ORDER BY: This command is used to sort in ascending or descending
order in a particular field. This is done by adding ORDER BY to the SELECT
command. Syntax: SELECT <Column name> FROM <Table name>
WHERE <Condition> ORDER BY<Column to order> [<ASC/DESC>]
GROUP BY: The GROUP BY clause allows us to form groups based on
the specified condition. Syntax: SELECT <Column>, function
(<Column>), functions (<Column>) FROM <Table> GROUP BY <Column>
HAVING: The HAVING clause defines criteria used to eliminate certain
groups from the output, just as the WHERE clause does for individual
rows. Syntax: SELECT <Column>, function (<Column>) FROM
<Table> HAVING <Column>
OTHER SQL Commands: Other than retrieving data, SQL provides
commands to update the data in a table. Updating data refers to inserting
and deleting rows in tables and changing values in columns. This invokes
command such as Insert, Update, Delete etc.
Also, SQL can be used to define security mechanism or scheme for
protecting data in a system from unauthorized access. This involves
commands such as Grant, Revoke etc.
INSERT INTO Command: INSERT INTO command adds new rows to
a table. Syntax: INSERT INTO <Table name> VALUES (<value>,
<value> …)
UPDATE Command: UPDATE is the SQL verb that changes or modifies
data values in a table. Syntax: UPDATE <table name> SET <column name
= new column value> [WHERE <condition>]
DELETE command: We can remove rows from a table with the
command DELETE. This command removes specific rows meeting the
condition and not the individual field values. Syntax: DELETE FROM
<table name> WHERE <condition>
GRANT command: This command is used to permit users access to
the database. Syntax: GRANT <privilege name> | ALL ON <object> TO
<user | PUBLIC> [WITH GRANT OPTION]
REVOKE command: This command is used to cancel database
privileges from user. Syntax: REVOKE<privilege name> | ALL ON
<object> TO <user | PUBLIC>
Using Visual FoxPro Database: In a typical database, one needs to
perform several operations, such as adding new information, modifying
existing information, arranging information in a desired order, removing
unwanted or outdated information. Visual FoxPro is such a database tool
for managing relational databases.
Opening a Database: Opening and existing Visual FoxPro database is
as same as opening other files.
To open a database, we must be:-
(i) Go to File menu and choose open
(ii) After appearing the open dialog box, drop down list of
“files of type” select “database (*.dbc)”
(iii) Now we select the location in “Look in: drop down list”.
(iv) Now double click the database, listed in the blank
rectangular area.
(v) Alternatively, we can also open a database, by entering
open database <database name> in the command
window.
Closing a Database: To close all open databases we give following
command in command window:
Close databases: For close all open databases and tables.
Close all: For close all open databases, tables, form designer,
report designer, query designer and label designer.
Deleting a Database: We can easily delete a database by giving the
following command. Syntax: delete database <database_name>
[delete tables]