You are on page 1of 5

Working from the Command Window Using the Command Window: When we made selection from the menu

system, we saw that Visual FoxPro generated the relevant commands in the Command window. It may be easier to enter commands than to make selections from the menu system. Creating a New Table: To create a table file from command window, type the word CREATE followed by the name of the table file, and press Enter key. 1. Type CREATE and give file name and press Enter key in the command window. 2. Fill the structure in appeared table and click on OK button. 3. Visual FoxPro displays a dialog box and asks if you want to input data records now. 4. Give the answer. If answer is Yes then enter the records in table. 5. Press ^ + w keys to save the record. Modify the structure of a Table: From the command window we can modify the structure by typing the MODIFY STUCTURE command. 1. Type MODIFY STRUCTURE in the command window. 2. Move the pointer on the field, where we want to insert the new field. 3. Click Insert button. Now type the field name and enter the width. 4. Now we click Ok button to save the changes. 5. Click Yes to make changes permanent. Displaying and Editing Data: Working from the command window, we can USE a table and then type one of the following: APPEND to display the table in Append mode. EDIT or CHANGE to display the table in edit mode. BROWSE to display the table in browse mode. REPLACE to change the contents in more than one record. Append command: Syntax: APPEND [BLANK] [FROM <Filename> [FOR <expression>]] This command is used to add records to the active database file. If any file will not open then using this command will asked filename first. APPEND will gives us Data Entry Screen for continues entry (may be terminated by CTRL W). APPEND BLANK will add a blank record at the bottom of the screen. APPEND FROM <Filename> add records of a closed database file to the active database file. The structure of both of the files should be same, otherwise record nos. will increases but all or partially data will not include. If expression will add with Append From <Filename> then only those records will added who fulfill the given expression. Example: Use Kanak Append Append From Kumar Append From Kumar For State="Bihar" Edit/Change command: Syntax: EDIT [FIELDS <Field List>] [condition] or CHANGE [FIELDS <Field List>] [condition] If we know the record number in advance, we can edit using EDIT command. For example: EDIT 5 We may also specify the field list, conditions and scope while giving EDIT command. For example: Use kanak EDIT FIELDS NAME, BRANCH, ROLL_NO FOR BRANCH = computer science Browse command: Syntax: - [BROWSE] [FIELDS <Field List>] [FREEZE] [WIDTH n] [Lock] This is a powerful editing command provide us facility to edit records horizontally with showing us multiple records at a time. Example: Browse Browse Fields Name, Salary Browse Width 10 Browse Freeze Salary Replace command: Syntax: [REPLACE <field1> WITH <expr1> [, <field2> WITH <expr2> [<Scope>] [FOR <expL1>] This is a powerful editing command. This is used to replace a new value in a field with or without a condition. We can replace a new value in more than one record in same command line. Example: Replace All Name with Kanak For Name=Raja Replace All Salary with Salary*1.1 for Salary < 5000 Delete and Recall commands: Delete command: - [DELETE] <SCOPE> [FOR <Expression>] [<Logical Operator> <Expression>] This command is used to mark the specified records for deletion. Delete does not remove records physically. It adds an astrick (*) sign along with those records you want to delete. Example: Delete All Delete Next 5 Delete Rest Delete Record 10 Delete All For State = "Bihar" Delete All For State = "Bihar" .And. City = "Patna" Delete All For Salary <=5000 Delete All For Male Delete All For Dob={10/10/80}

Pack command: Syntax: [PACK] This command is used to physically remove all those records which was marked for deletion. Remaining records will be copied to original file. Example: - Pack 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 thats 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 IFELSEENDIF 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 IFELSE 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. Working with Relational Databases in Visual FoxPro Working with Relational Databases in Visual FoxPro: A Relational database is defined as a database that allows us to group its data items into one or more independent tables that can be related to one another by using fields common to each related table. Table in a relational database is similar to the traditional file system with its records, fields and files. The tables rows, called tuples, contain records and columns, called attributes, contain fields. Advantage of Relational Database:(a) The relational database structure can be efficiently used even with a PC that has limited main memory and processing capability. (b) Relational database is very effective for small databases. (c) Relational database is much easier to use. (d) Relational database is only concerned with data and not with the structures, which improves the performance. (e) In Relational database record relationship is implemented using primary key or composite key field. Hence it is very simple and represents the logical relationship among the data items rather than physical relationship. (f) Relational model is very useful for representing most of the real world objects and the relationships among them. Relationships among Tables: - A keyfield establishes relationship among two or more tables. There are three types of relationships that can be established among tables. These are: One to one It means that for every record in one table there a single corresponding record in another table. One to many one to much relation ship is a relationship in which a record in one table has one or more related records in another table. Many to many in this relationship , a record n one table has many corresponding records in a second table and for every record in he second table, there are many corresponding records in the previous table. Creating a New Database in Visual FoxPro: - If we create an application that requires several tables, we will probably find it easier to store them all in one place. In Visual FoxPro, a database is a collection of tables, views and relationships between the tables stored as a file with .dbc extension. Entering and viewing data using the Form Wizard: The One-to-many wizard creates a form from related tables. One table is considered as the parent table and another as the child table. The fields of child table appear in a grid below the fields of parent table. To run the One-to-many Form wizard

1. 2.
3.

Click Tools menu, highlight Wizards and choose Form. After this Wizard selection dialog box will appear. Now we choose One-to-Many Form Wizard and then click OK. In the dialog box that appears, the databases and Tables: area will show the current database. If we want a table other than this, click on the small button to the right of Databases and Tables: area. The open dialog box will get opened and we can open the required table using the dialog box. From the Available Fields: area, select the field which we want in our form, the click click the button. button. To remove all the fields, click the button. After To remove a field from the selected fields, click the this we click on Next> button. button. To select all the fields

4. 5. 6. 7.
8.

Similarly, select the fields from the child table in the dialog box that appears. Now we click on Next> button. In the dialog box that appears, we can select the fields that determine the relationship between the tables from the field lists. Now we click on Next> button. In the dialog box that appears, from the style box, select a style for the3 form. The Wizard displays a picture in the magnifying glass as an example of the style which we highlight.

9. The button type: area specifies the forms navigation buttons. Now we click on Next> button. 10. In the next dialog box that appears, select the fields based on which we want to sort the records in the parent table.
Then click Next>.

11. In the next dialog box, we click the Preview button to view the form before we exit the One-To-Many Form Wizard.
Otherwise select any of the listed options and click Finish. Query Wizard to work with Relational Database:

1. 2. 3. 4. 5.

Click Tools menu, highlights Wizards and choose Query. In the Wizard selection dialog box that appears, select Query Wizard and Click OK. In the Query Wizard dialog box that appears, first select fields from one table. Move the required fields from the Available Fields: area to the selected fields: area. Similarly, select fields from the other table and move these also to the selected fields: area. Now we click on Next>. Now we select related fields from the fields lists to determine the relationship between the two tables. Now we click on Next>. From the dialog box that appears, we have to choose whether we want to include only related rows from both tables or all rows from either table. By default, only related records are included. Now we click on Next>.

6. 7. 8.

In the screen that appears, we can specify our criteria. Criteria are restrictions we place on a query or an advanced filter to identify the specific records we want to work with. Now we click on Next>. The next step is to determine the order in which our Query results will be stored. Select a field from the Available fields: list and click the Add> button to move it to selected fields. We can select up to three fields. Now we select Ascending to sort the results in ascending order or Descending to sort them in descending order. Now we click on Next>. In the dialog box that appears, mention whether we want all the records or only a portion of the records. We have chosen to view all the records.

9. Select an option to our choice from the dialog box that appears and click Finish button. Using One-To-Many Report Wizard: The One-to-Many Report Wizard creates a Report that groups records from a parent table with records from a child table. To work with One-To-Many Report Wizard 1. Click Tools menu, highlight Wizards and choose Report. 2. In the Wizard selection dialog box, Choose One-to-Many Report Wizard. 3. The rest of the steps are same as that of the form Wizard. In the last step Check the Wrap fields that do not fit checkbox if we do not want the fields to wrap. Wrapping occurs when the number of selected cannot fit on a single line within the width of our report. If we do not want the fields to warp, uncheck the checkbox.
Using the View Window: The view window can be used to open and display tables, to establish temporary relationships and to set work area properties. To Work with the View Window 1. Click Window menu and chose Data session. The Data Session window that appears is known as View window. 2. The Current session: drop down list displays the name of the current data session. The Aliases area displays the view name or table name without the file extension. The relations area indicates any temporary relationship established between the tables or views listed in the Aliases box. 3. Click the open button, to add a table or view to the Aliases list. 4. Click properties button to see the Work Area Properties dialog box. In this dialog box we can modify the structure of a table, select index files and fields, and define data filters. If no tables or views are listed, an open dialog box will appear from where we have to open a table or view. 5. Click the Browse button to display the table or view, currently selected, in a Browse window where we can examine, edit and/ or append data. 6. Click the Relations button to define the relations between tables or views. A set Index Order dialog box will appear if no index order has already been set between the two tables. When we click OK, the Expression builder will appear in which we can set relationship between the tables. 7. Click the One-To-many button to display the Create One-To-Many Relationships dialog box, where we can establish a one-to-many temporary relationship between child and parent tables. This button gets enabled only when we establish relation between tables by selecting them in the View dialog box and then clicking Relations. 8. Click Close button to Remove the selected table or view from the Aliases list. Using Queries and SQL: SQL (Structure Query Language) is a simple high level language that enables us to utilize a database completely and easily. It is a very simple and powerful language because it uses compact, English-like statement and performs very complex jobs to access information from very large size database. Purpose of using SQL:a. Creating, deleting, and modifying table structure. b. Defining the relationship between two or more tables. c. Inserting data into tables. d. Extracting data in meaningful ways based on the tables defined relationships that is accessing or retrieving data. e. This includes maintaining database and manipulating the stored values to keep the information current. f. This includes system security and authorization which must be controlled and maintained. The syntax for the Visual FoxPro SQL SELECT statement is following: SELECT <field list> FROM <table list> TO <output option> WHERE <join criteria> GROUP BY <group column> HAVING <filter criteria> ORDER BY <order item> Clause Function SELECT Select the fields to be output FROM Specify the tables from which to retrieve information TO Determine where should output go WHERE Specify the primary section criteria GROUP BY Specify any groups HAVING Specify any secondary selection criteria ORDER BY Specify a sort order for the final output

You might also like