📄 ug_ch10.htm
字号:
<pre><font color="#0000FF"> field name = 1;</font></pre><p><font size="2">The <b>name</b> field in <b>author</b> record isset to "Knuth, D".</font></p><p><b>Statement:</b></p><pre><font color="#0000FF"> record book { field title = 3; field pub_date = 2; }</font></pre><p><font size="2">Unconditionally create <b>book</b> record([1:1]). No entry created in CRI. Save CRT:</font></p><pre><font color="#0000FF"> author: [0:1] book: [1:1]</font></pre><p><font size="2">Field <b>title</b> set to "FundamentalAlgorithms". Field <b>pub_date</b> set to <b>1968</b>.</font></p><p><b>Statement:</b></p><pre><font color="#0000FF"> connect published;</font></pre><p><font size="2">Determine that <b>author</b> is owner,<b>book</b> is member. Make current book, [1:1], the currentrecord. Make current author, [0:1], the current owner of set<b>published</b>. Call <b>d_connect(PUBLISHED).</b></font></p><p>Statement:</p><pre><font color="#0000FF"> foreach "books.asc" {</font></pre><p><font size="2">Read the second line of <b>books.asc</b> intomemory:</font></p><pre><font color="#0000FF"> "Ullman, J.",1982,"Principles of Database Systems"</font></pre><p><b><font size="2">Statement:</font></b></p><pre><font color="#0000FF"> record author { create on 1; field name = 1; }</font></pre><p><font size="2">Search CRI for match (no match). Create<b>author</b> record (database address [0:2]), and add it to theCRI:</font></p><pre><font color="#0000FF"> key={author,"Knuth, D."}, data=[0:1] key={author,"Ullman, J."}, data=[0:2]</font></pre><p><font size="2">Update CRT:</font></p><pre><font color="#0000FF"> author: [0:2] book: [1:1]</font></pre><p><font size="2">Store "Ullman, J." in name field.</font></p><p><b>Statement:</b></p><pre><font color="#0000FF"> record book { field title = 3; field pub_date = 2; }</font></pre><p><font size="2">Unconditionally create <b>book</b> record([1:2]). No entry is created in the CRI. Save CRT:</font></p><pre><font color="#0000FF"> author: [0:2]<br><br><br> book: [1:2]</font></pre><p><font size="2">Set field title to "Principles of DatabaseSystems". Set field <b>pub_date</b> to <b>1982</b>.</font></p><p><b>Statement:</b></p><pre><font color="#0000FF"> connect published;</font></pre><p><font size="2">Determine that <b>author</b> is owner,<b>book</b> is member. Make current <b>book</b>, [1:2], the currentrecord. Make current <b>author</b>, [0:2], the current owner of set<b>published</b>. Call <b>d_connect</b>(PUBLISHED).</font></p><p><b>Statement:</b></p><pre><font color="#0000FF"> foreach "books.asc" {</font></pre><p><font size="2">Read the third line of <b>books.asc</b> intomemory:</font></p><pre><font color="#0000FF"> "Knuth, D.",1969,"Seminumerical Algorithms"</font></pre><p><b><font size="2">Statement:</font></b></p><pre><font color="#0000FF"> record author { create on 1;</font></pre><p><font size="2">Search CRI for match on: {author,"Knuth, D."}.Match is found; set CRT:</font></p><pre><font color="#0000FF"> author: [0:1] book: [1:1]</font></pre><p><b><font size="2">Statement:</font></b></p><pre><font color="#0000FF"> field name = 1;</font></pre><p><font size="2">Statement skipped.</font></p><p><b>Statement:</b></p><pre><font color="#0000FF"> record book { field title = 3; field pub_date = 2; }</font></pre><p><font size="2">Unconditionally create <b>book</b> record([1:3]). No entry is created in the CRI. Save CRT:</font></p><pre><font color="#0000FF"> author: [0:1] book: [1:3]</font></pre><p><font size="2">Field <b>title</b> is set to "SeminumericalAlgorithms". Field <b>pub_date</b> is set to 1969.</font></p><p><b>Statement:</b></p><pre><font color="#0000FF"> connect published;</font></pre><p><font size="2">Determine that <b>author</b> is owner,<b>book</b> is member. Make current <b>book</b>, [1:3], the currentrecord. Make current <b>author</b>, [0:1], the current owner of set<b>published</b>. Call <b>d_connect</b>(PUBLISHED).</font></p><p>The last iteration of the <b>foreach</b> loop will have the sameeffect as the third, where a third book record is connected to thefirst author record.</p><h4>Example Three: Full family tree database</h4><p><font size="2">Our final example demonstrates the use of theremaining two import specification language statement types. Thefirst is called <b>update on</b>, and is used to find an existingCRI entry and update the existing record with new field values.This statement will create the record for which there is notalready a CRI entry. The last statement is called <b>find on</b>,and is used to locate a record that has already been created with a<b>create on</b> or <b>update on</b> statement, and make it thecurrent record of type. It is used to locate records that will beinvolved in <b>connect</b> statements. Note that <b>update on</b>and <b>find on</b> cannot detect the presence of a record thatexisted in the database prior to this import!</font></p><p>This example is an expansion of Example One above, whichrepresents a family tree. Figure 10-3 shows the schema for buildinga family tree database.</p><p align="center"><b><img alt="Fig. 10-3. Family Tree Schema" src="dbstar_10-3.gif"><br><br>Fig. 10-3. Family Tree Schema</b></p><p>The data definition language for this schema follows:</p><pre><font color="#0000FF">database tree { data file "tree.dat" contains person, marriage; key file "tree.key" contains p_num, p_name;<br> record person { unique key int p_num; char p_last[20]; char p_first[30]; long p_birth; long p_death; compound key p_name { p_last; p_first; } } record marriage { long m_date; long m_term; } set husband { order ascending; owner person; member marriage by m_date; } set wife { order ascending; owner person; member marriage by m_date; } set offspring { order ascending; owner marriage; member person by p_birth; }}</font></pre><p><font size="2">The input data contains field-value relationshipsthat will be converted into set relationships during the import.There are two data files: one for definitions of people in the treeand a second for the definitions of marriages in the tree. Oneperson may be either a husband or wife in a marriage, and a personmay be the offspring of a marriage. The definitions of the personfields in file <b>person.asc</b> are as follows:</font></p><p>1. Numerical reference to the marriage from which this person isthe offspring.<br>2. Unique numerical identifier of this person.<br>3. Person's last name.<br>4. Person's first and middle name.<br>5. Person's birth date, if known.<br>6. Person's death date, if applicable and known.</p><p>The <b>marriage.asc</b> file contains fields defined asfollows:</p><p>1. Unique numerical identifier of this marriage.<br>2. Reference to the person record of husband.<br>3. Reference to the person record of wife.<br>4. Marriage date.<br>5. Marriage termination date, if applicable and known.</p><p>See the sample data below:</p><p><b>File: person.asc</b></p><pre><font color="#0000FF">2,1,"Warner","Micah Wayne",9761101,02,3,"Warner","Jesse David",9800706,05,4,"Warner","Wayne Lawrence",9540530,05,6,"Warner","Walter Bartlett",9560226,05,7,"Warner","Laurel Ann",9590520,05,8,"Warner","Roger James",9601025,010,9,"Warner","Lawrence James",9270603,012,11,"Warner","Clarence R.",9010313,927051614,13,"James","Pauline May",9040723,016,15,"Morgan","Leslie Bartlet",8971128,018,17,"Carte","Dorothy Lois",8890802,020,19,"Morgan","Elizabeth Jane",9330412,022,21,"Wood","Jennifer Ann",9531214,022,23,"Kepler","Michael",0,022,24,"Martin","Dinah",0,022,25,"Pools","Janice",0,022,26,"Adams","Judy",0,02,27,"Warner","Thomas James",9850404,02,28,"Warner","Paul Russell",9860904,030,29,"Kepler","Patrick Michael",9870509,0</font></pre><p><b><font size="2">File: marriage.asc</font></b></p><pre><font color="#0000FF">2,4,21,9731215,05,9,19,9511013,010,11,13,9260313,927056120,15,17,9290324,030,23,7,9841013,031,8,24,9840512,032,9,25,9740000,033,9,26,9820000,0</font></pre><p><font size="2">As these data are imported, we want to be able toconnect each <b>person</b> record to the <b>marriage</b> record(s)it is related to in two ways. A person may be a spouse in onemarriage, and may be the offspring of another marriage. In eachline of ASCII <b>person</b> data is a unique identifier of theperson, plus the identifier of the marriage of which this person isthe offspring. Hence it is possible to create up to two records foreach line of <b>person</b> data. As the <b>person</b> and<b>marriage</b> records are created or found, the <b>offspring</b>set can be connected.</font></p><p>The loop through <b>person.asc</b> will cause all <b>person</b>and <b>marriage</b> records to be created, and all <b>offspring</b>connections to be made. A loop through <b>marriage.asc</b> willallow each <b>marriage</b> record to be found and updated with fullinformation about the marriage. It will also allow the husband andwife set connections to be made.</p><p>The following import specification demonstrates the <b>updateon</b> and <b>find on</b> statements, as it updates marriageinformation and creates husband and wife connections, both in thesecond <b>foreach</b> loop.</p><pre><font color="#0000FF">database tree;foreach "person.asc" { record person { create on 2; field p_num = 2; field p_last = 3; field p_first = 4; field p_birth = 5; field p_death = 6; } record marriage { create on 1; } connect offspring;}foreach "marriage.asc" { record marriage { update on 1; field m_date = 4; field m_term = 5; } record person { find on 2; } connect husband; record person { find on 3; } connect wife;}end;</font></pre><h4>Import Specification Language Definition</h4><p><b><font size="2">Import Specification</font></b></p><pre><font color="#0000FF">database <i>dbname</i> ; foreach_loop ...end ;</font></pre><p><font size="2">The database named <i>dbname</i> is opened, andall record, field, and set type names are read into memory forverification against the names included in this specification. Thedatabase is located with the DBDPATH and DBFPATH environmentvariables, if defined. If there is an <b>db.star.ini</b> file inthe current directory, it is used. Otherwise the databasedictionary and files are expected to be in the current directory.Then each <b>foreach_loop</b> is executed in order.</font></p><p><b>Foreach Loop</b></p><pre><font color="#0000FF">foreach <i>input_file</i> { import_statement ...}</font>
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -