Visual FoxPro Ch2 By CHANG YU1 Chapter Two Database Management Part one Database definition Record...
-
Upload
bertram-norton -
Category
Documents
-
view
228 -
download
0
Transcript of Visual FoxPro Ch2 By CHANG YU1 Chapter Two Database Management Part one Database definition Record...
Visual FoxPro Ch2 By CHANG YU 1
Chapter TwoDatabase Management
Part one• Database definition
• Record and Field
• Type of columns
• Expression
Visual FoxPro Ch2 By CHANG YU
2
2.1 Database and Table
• Database : An organized collection of logically related tables. (staff.dbc )
• Table: An organized collection of related data. ( student.dbf, course.dbf)
• A record consists of one or more columns.
• A table consists of one or more records.
• A database consists of one or more tables.
Visual FoxPro Ch2 By CHANG YU
3
record
columntable
Visual FoxPro Ch2 By CHANG YU
4
2.2 Expression
• ? < Expression> ---- this command will display the result of the expression on the main screen.
? 32+5 , ? pay , ? sqrt(100)
• Go < nRecordNumber> ---- this command will move the pointer to the nth record of the table.
Use d:/basic/person.dbf
Browse
go 3
? pay
Visual FoxPro Ch2 By CHANG YU
5
2.2 Expression
• Five kinds of expression:• Numeric Expression 34 + 34
• Character Expression “ ABC” + “DEF”
• Date Expression Date( ) +20
• Logical Expression Not .T.
• Relation Expression 34 > 12
Visual FoxPro Ch2 By CHANG YU
6
2.2 Expression
< operand 1> < operator> < operand 2 > 32 + 56 • The operand 1 and operand 2 must match the data type.• The operator may be an unary operator.
• Not • “ -” -32 , not .t.
Visual FoxPro Ch2 By CHANG YU
7
2.2 Expression
• The types of operants :• Constant
• Variable » use only with letters and “_” and numbers .
» 128 letters.
» begin with letters or “_”.
» Can not use with keywords.
• Column name
pay, xname…
• Function Date(), LEFT( “abcdefg” , 3”)
Visual FoxPro Ch2 By CHANG YU
8
2.2 Expression
• The kinds of expression :
• Numeric expression: + , - ,^ , ** , * , / , %
• Character expression: + , -
• Date expression: +, -
• Logical expression: NOT, AND, OR
• Relation expression: =, #, < >, > , < ,>= , <= , $
Visual FoxPro Ch2 By CHANG YU
9
Operator Example Result
+ - 3 + 4 7
^ ** 3 ^ 4 81
* / 30 / 6 5
% 10 % 3 1
Numeric Expression:
Visual FoxPro Ch2 By CHANG YU
10
Operator Example Result
+ “ABC ”+ “DEF” “ABC DEF”
- “ABC ” – “DEF” “ABCDEF ”
Character Expression
Operator Example Result
+ {^1999.05.11} + 50 {^1999.06.30}
- {^1999.05.11} - 50 {^1999.03.22}
- {^1999.05.11} - {^1999.03.22} 50
Date Expression
Visual FoxPro Ch2 By CHANG YU
11
.T..T. OR .F.OR
.T..T. AND .T.AND
.F.NOT .T.NOT
ResultExampleOperator
Logical Expression
.T.“ABCD” = “AB”=
.T.“AB” $ “ABCD”$
.F.32 # 32#, < >
ResultExampleOperator
Relation Expression
Visual FoxPro Ch2 By CHANG YU
12
? 66 + sqrt(4)? 66 + pay? 3 ^ 4? 23 % 7? 1/3set decimals to 4? 1/3? “ABC” + Left(“defgh”, 3)?date() + 19? {^1999.06.03} -100? 3 # 4? “caa”> “bbb”
Exercise
Visual FoxPro Ch2 By CHANG YU
13
? {^1999.06.03}-{^1999.02.23}
? 100 + date( )
? {^1999.06.03} + {^1999.02.23}
? A = .F.
? .T. ANF .F.
? .T. AND A
? NOT .T.
? NOT .T. OR .F. AND .T.
? “a” > “b”
? “caa” > “bbb”
? “abcd” = “ab”
set exact on
? “abcd” = “ab”
EXERCISE
Visual FoxPro Ch2 By CHANG YU
14
Database managementPart two
• Database• Create database• Select database• modify database• Close database
• Table– Free table– Database table
– Create table– Open table and close table– Modify table– Select work Area
Visual FoxPro Ch2 By CHANG YU
15
The type of the column • Character : 1--254 letters(127 Chinese words) , for any word.
• Currency : For money.• Numeric : For numeric value.
• Float : For numeric value.• Date Time : 0001.0101 --9999.132.31. For date and time.
• Double : Double precise float number. Width 8• Integer : -214783647--2147483646. Width 4.• Logic : true or false.• Memo : 4 bytes. (*.fpt). Unlimited width.• General : music, picture, video. 4 bytes.
Visual FoxPro Ch2 By CHANG YU
16
2.3 Create Database
• Database table :
– A table which is in the database.
create database staff
create
• Free table :
– A table which is independent and is not in any
databases.
Visual FoxPro Ch2 By CHANG YU
17
2.3.1 Create new database
• Use the menu bar– file --> new --> database --> database name.
• Command: Create database [<dbname> | ? ]• this command will create a new database.
• Command: Modify database [ <dbname> ]• this command will call the database designer to modify and
display the database structure.
• Project manager
Visual FoxPro Ch2 By CHANG YU
18
2.3.2 Create new table
• Use the menu bar : – > file -> open -> table -> input table name
• Command: Create [ <table name> | ? ]
• Press the right button of mouse in the database designer :– database designer -> press the right button of the mouse -> choice
the new table -> input the table name “persoin1.dbf”
Visual FoxPro Ch2 By CHANG YU
19
2.3.3 Design the table structure
• Name : column name , 128 letters.
• Type : the type of a column.
• Width : the width of a column.
• Floating point number : the number of
decimal points.
• Caption: input the Chinese column name.
Visual FoxPro Ch2 By CHANG YU
20
2.3.3 Design the table structure
• Field Validation : • Rule : .NOT. EMPTY (NO)• Message: the staff-no must be entered in the table.• Append : insert the data to test the rule and message.
• Default value:• Field command:• Entry the data: Ctr-w to save the data. (Esc)• Table name:• Field Validation of a table:
– rule and message. – .NOT.(empty(addr).AND.empty(tel))
Visual FoxPro Ch2 By CHANG YU
21
2.3.4 Create a free table
• A free table is independent. It is not in any databases. So we do not need to open any database before we create a free table.– Use the menu bar– Command: Create < tablename>
Visual FoxPro Ch2 By CHANG YU
22
2.4 How to use databases-- open a database
• Open a database:– use the menu bar– Command:
OPEN DATABASE [<databasename> | ? ] [ Exclusive | Shared]
• Exclusive : A keyword indicates that the database cannot be opened by more than one user at a same time.
• Shared : the database can be opened by more than one user at a same time.
Visual FoxPro Ch2 By CHANG YU
23
Convert a database
• Visual FoxPro allows a user to open more
than one database at any time, but he/she
can manipulate only one database at a time.
• Use the tool bar. Using the mouse to choice.
• Command: SET DATABASE TO[<databasename>]
Visual FoxPro Ch2 By CHANG YU
24
Close a database
• Close Database – this command will close the database which is
being used now.
• Close All– this command will close all databases which be
opened by the user.
Visual FoxPro Ch2 By CHANG YU
25
Modify a database
• Modify Database [ < DatabaseName > | ? ] – Modify Database library = Open database library + modify database
• View and modify a table :
• Based on the database designer, a user can use the
right button of the mouse on the table structure to
view, delete and modify a table.
Visual FoxPro Ch2 By CHANG YU
26
Modify a database
• Add a free table in a database:– The table must be free table.
– The table must be closed.
– Cannot add any database table.
• Move the table from a database:– The database must be opened in the exclusive mode.
– The table must be closed.
Visual FoxPro Ch2 By CHANG YU
27
Open a table -- USE
• Visual FoxPro allows a user to open more than one table. But you can choice only one table to manipulate at a time.
• Work Area is a place where a table can be opened. There are 225 Work Areas. One table can be opened on only one workArea. So we can open up to 225 tables at a time.
Visual FoxPro Ch2 By CHANG YU
28
WorkArea
1
2
3
224
225
database
table
Visual FoxPro Ch2 By CHANG YU
29
USE
• Any time we can modify and edit only one database and one table, although we can open many databases and many tables.
• Open table:– use menu bar: file -> open -> *.dbf -> person.dbf
– command: Use [ < tablename> | ? ]
– [use <tablename>] can open the table in workArea one. If we open more tables, we should select other workAreas.
Visual FoxPro Ch2 By CHANG YU
30
Select a WorkArea• Select < nWorkArea > |< cTableAlias >
– close all– open database library– use book ALIAS book-data– select 2– use student ALIAS student-data– ...– select book-data ( = select 1 )– select student-data ( = select 2 )
Visual FoxPro Ch2 By CHANG YU
31
Select a WorkArea
• Use [ < tablename | ?] [Alias <aliasname>] [Exclusive | Shared ] [In <nworkarea> |<ctablealias>]
• – close all– open database library– use student in 5 – use book in 0– use course in A Minimum workarea number
select 5
use student
= use course in 1
*Workarea A—j represent workarea 1—10 respectively.
Visual FoxPro Ch2 By CHANG YU
32
Modify table Structure
• Use database designer– press the right button of mouse on the table.
• Command: Modify Structure– open database staff1– use person1– modify structure
Visual FoxPro Ch2 By CHANG YU
33
ExerciseDesign the database score.dbc and two tables: Score1.dbf
Column-name TYPE WIDTH Description
stuid character 9 Student no
Cour_num character 5 Course no
Score1 numeric 5.1 midterm
Score2 numeric 5.1 Final_exam
Flag character 1
Req_sel character 1 Major/Minor
Visual FoxPro Ch2 By CHANG YU
34
Exercise
Column-name Type Width Description
Cour_num character 5 course no
grade character 1 term no
credit numeric 3.1 grade
hour numeric 1 hour
Chin_name character 30 Course name
Eng_name character 60 Course name
Course1.dbf
Visual FoxPro Ch2 By CHANG YU
35
Command? <Expression>
Go <nRecordNumber>
Create database [ <dbname> | ? ]
Create [ <tablename> | ? ]
Open database [ <databasename> | ? ]
Set database to [ < dbname> ]
Close database
Close all
Modify database [ <dbname> | ? ]
Use [ <tablename> | ? ] [ Alias <aliasname> ]
Select <nworkarea> | <ctableAlias>
Use <tablename> IN <nworkarea> | <ctableAlias>
Create
Create table-name
Create ?
Visual FoxPro Ch2 By CHANG YU
36
Question:1. Open database library
Use student use student
Use loan use loan in 0
Use book use book in 0
2. Select 5
Use person ALIAS ppp
3. Select A
use person
select C
use course
?
Result ?
= use person in 1
= use course in 3
?