summaryrefslogtreecommitdiffstats
path: root/doc/en/details-database.docbook
blob: 8b141e7fea2e04eb0b220b084dc1cbc5eaa23a0b (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
<?xml version="1.0" encoding="UTF-8"?>
<chapter id="details.database">
<chapterinfo>
  <authorgroup>
    <author>
      <firstname>Tony</firstname>
      <surname>Bloomfield</surname>
      <affiliation>
        <address><email></email></address>
      </affiliation>
    </author>
  </authorgroup>
  <date>2009-08-05</date>
  <releaseinfo>1.0</releaseinfo>
</chapterinfo>
<title>Database</title>

<sect1 id="details.database.usage">
<title>Caution when using the database</title>

<caution>
  <para>
    At the time of writing, the software described in this chapter is still in
    an experimental stage, and loss of data is possible. Please ensure that you
    have an alternative backup copy of your data before committing to database
    usage.
  </para>
</caution>

<sect2>
<title>Introduction</title>
<para>
  As of release 1.0, &kappname; allows you to hold your data in a relational
  database. One of the advantages of using this industry-standard format is that
  it may allow you to view your data using one of the graphic front ends such as
  OpenOffice.org<trademark class="copyright"/>, perhaps in some format that
  &kappname; currently doesn't provide. Also, a little knowledge of SQL
  (Structured Query Language, the language used world-wide to access relational
  databases) should enable you more easily to export data to an external
  program, for example, a budgeting application.</para> <para/>
</sect2>

<sect2><title>Preparation</title>

<para>
  To access the database, &kappname; uses the SQL module supplied by &TQt;
  Software as part of their &TQt; programming system. This module supports a
  number of different database systems through a collection of drivers. Among
  the more popular open-source systems for which drivers are available are
  MySQL<trademark class="registered"/>, SQLite, and PostgreSQL. The module also
  supports the 'heavier', more industrial, systems such as Oracle<trademark
  class="registered"/> and IBM DB2<trademark class="registered"/>.
</para>

<para>
  With the exception of SQLite, these systems use a client/server model, where
  the 'client' software sits on 'your' machine, while the server lives on the
  same machine as the database itself, which may be elsewhere on a network. Of
  course, in the normal scenario for a personal finance application such as
  &kappname;, 'your' machine acts as both client and server. Your first task
  therefore, having decided which database system you wish to use, is to install
  the client, and most probably server, software.
</para>
  
<para>
  In addition to the database software itself, you must also install the
  corresponding &TQt; driver module. Most distributions will include driver
  modules for the more popular databases.
</para>

<note>
  <para>
    SQLite does not operate on a client/server model; each database is held in a
    regular file, local or remote, accessed using the normal methods supplied by
    the underlying operating system. In this case, therefore, there is only one
    software package to install. Also, some of the following information,
    particularly that related to administration, may not apply to SQLite.
  </para>
</note>  
</sect2>

<sect2>
<title>Administration</title>

<para>
  Looking after databases is a little more complex than dealing with regular
  files. Each system has different methods for performing those necessary
  administrative tasks such as creating databases, assigning permissions to
  various users, producing backups, &etc;. Describing these tasks is outside the
  scope of this manual, but all of the supported products provide comprehensive
  reference documentation, and a quick search of the web will point you at many
  tutorials on the subject.
</para>

<sect3>
<title>Creating the database</title>

<para>
  Code has been included to create an initial database to hold your data if one
  doesn't exist. However, it is strongly recommended that you pre-create a
  database, because most of the products provide a number of options which may
  be relevant. One that may be of particular importance to some would be the
  designation of the character set (e.g., UTF-8) to be used for text
  fields.
</para>

<para>
  At this time, you will also need to specify permissions for various users to
  perform different operations on the database. In most systems, the user who
  creates the database will be automatically assigned all permissions, but this
  is an area in which the documentation should be consulted.</para>

<para>
  For your first use of the database, and occasionally at other times when the
  database layout changes, you will need permission (also called privileges) to
  create and alter tables and views (see next paragraph). There may be different
  names for the permission/privilege in different systems, but something like
  CREATE and ALTER should be commonplace. For normal running, you will need to
  be able to read and write records; these are normally defined in SQL as
  SELECT, INSERT, UPDATE, and DELETE permissions.
</para>
</sect3>

<sect3>
<title>Creating Tables</title>

<para>
  On your first use, &kappname; will attempt to create the necessary table
  structures. In order to achieve the maximum compatibility between various
  database types, only a subset of common data types are used. There may
  nevertheless be situations where a particular type is not supported, and in
  this case, provision has been made to generate the SQL code necessary to
  create tables. This code can then be modified as required and used to create
  the tables outside of &kappname;. Should you find yourself in this situation,
  help can usually be obtained from &devlist;.
</para>
</sect3>
</sect2>

<sect2 id="details.database.selectdatabase">
<title>Initialisation</title>

<para>
  Using &kappname;, open or import an existing data file, or create a new
  one. Then select <guimenuitem>Save as Database</guimenuitem> from the
  <guimenu>File</guimenu> menu. This will present the following dialog:
</para> 

<screenshot>
  <mediaobject>
  <imageobject>
  <imagedata fileref="select_database.png" format="PNG" />
  </imageobject>
  </mediaobject>
</screenshot>

<sect3>
<title>Database Type</title>

<para>
  This box lists all &TQt; SQL drivers installed on your system. Select the
  driver for your database type. If the one you want is not in the list, you
  need to install the appropriate driver.
</para>
</sect3>

<sect3>
<title>Database name</title>
<para>
  The default database name is KMyMoney, but you may choose some other name if
  you like. SQLite has one database per file; selecting this driver will open
  the standard 'Open File' dialog from which a file may be selected or
  created. For some database types, &kappname; may not be able to create the
  database, so it must be pre-created using the appropriate administrative
  procedure. However, &kappname; will create all table structures where
  necessary.
</para>
</sect3>

<sect3><title>Host name</title>
<para>
  For the average user, the default name of <quote>localhost</quote>, being the
  machine you are currently using, is correct. For networked databases, enter
  the connected host name.
</para>

<note>
  <para>
    NOTE: For SQLite databases, the Host name, User name, and Password fields
    are not relevant. The SQLite file must have the appropriate read/write
    permissions set by the underlying file system to enable the appropriate
    access for the currently logged-in user.
  </para>
</note>
</sect3>

<sect3>
<title>User name and password</title>
<para>
  Check the permissions set up on your database, or contact the database
  administrator, for the correct values to use here. The user name must be
  capable of selecting, inserting, updating, and deleting records. If the user
  name is the same as your login name, a password is not normally required.
</para>
</sect3>

<sect3>
<title>Generate SQL</title>
<para>
  This button will generate the CREATE TABLE commands and save them to a text
  file, which may be edited if the in-built commands do not work for your
  database system (see previous paragraph).
</para>
</sect3>
</sect2>

<sect2>
<title>Accessing your data</title>

<sect3>
<title>Table design</title>

<para>
  To access your data in &kappname;, use the <guimenuitem>Open
  Database</guimenuitem> entry in the <guimenu>File</guimenu> menu. This will
  open a dialog similar to the above.
</para>

<para>
  To access your data in other formats, you will need to know a little about how
  it is held in relational databases. By far the easiest way to get a feel for
  this is to open the database in a front-end such as OpenOffice.org. This
  provides a list of the various tables which make up the database, and
  enables you to see the layout of each of them.
</para>

<para>
  To extract data, e.g., into a spreadsheet or external file, it is almost
  invariably necessary to select linked data from more than one table. This is
  done by 'joining' the tables, using a field which is common to each. You can
  find a lot more information about how this is done from the online database
  tutorials mentioned above. The following table lists the fields used to define
  these inter-table relationships.
</para>

<informaltable>
  <tgroup cols="3">
    <colspec colname="c1" colwidth="4.5cm"/>
    <colspec colname="c2" colwidth="6.2cm"/>
    <colspec colname="c3" colwidth="6.3cm"/>
    <thead>
      <row>
        <entry valign="top">
          <para>Relationship</para>
        </entry>
        <entry valign="top">
          <para>Match</para>
        </entry>
        <entry valign="top">
          <para>With</para>
        </entry>
      </row>
    </thead>
    <tbody>
      <row>
        <entry valign="top">
          <para>Institutions and Accounts</para>
        </entry>
        <entry valign="top">
          <para>kmmInstitutions.id</para>
        </entry>
        <entry valign="top">
          <para>kmmAccounts.institutionId</para>
        </entry>
      </row>
      <row>
        <entry valign="top">
          <para>Accounts Parent/Child</para>
        </entry>
        <entry valign="top">
          <para>kmmAccounts.id</para>
        </entry>
        <entry valign="top">
          <para>kmmAccounts.parentId</para>
        </entry>
      </row>
      <row>
        <entry valign="top">
          <para>Transactions and Splits (see Note 1)</para>
        </entry>
        <entry valign="top">
          <para>kmmTransactions.id</para>
        </entry>
        <entry valign="top">
          <para>kmmSplits.transactionId</para>
        </entry>
      </row>
      <row>
        <entry valign="top">
          <para>Accounts and Splits</para>
        </entry>
        <entry valign="top">
          <para>kmmAccounts.id</para>
        </entry>
        <entry valign="top">
          <para>kmmSplits.accountId</para>
        </entry>
      </row>
      <row>
        <entry valign="top">
          <para>Payees and Splits</para>
        </entry>
        <entry valign="top">
          <para>kmmPayees.id</para>
        </entry>
        <entry valign="top">
          <para>kmmSplits.payeeId</para>
        </entry>
      </row>
      <row>
        <entry valign="top">
          <para>Schedules and Transactions</para>
        </entry>
        <entry valign="top">
          <para>kmmSchedules.id</para>
        </entry>
        <entry valign="top">
          <para>kmmTransactions.id</para>
        </entry>
      </row>
      <row>
        <entry valign="top">
          <para>Transactions and Currencies</para>
        </entry>
        <entry valign="top">
          <para>kmmTransactions.currencyId</para>
        </entry>
        <entry valign="top">
          <para>kmmCurrencies.ISOCode</para>
        </entry>
      </row>
      <row>
        <entry valign="top">
          <para>Accounts and Securities (see Note 2)</para>
        </entry>
        <entry valign="top">
          <para>kmmAccounts.currencyId</para>
        </entry>
        <entry valign="top">
          <para>kmmSecurities.id</para>
        </entry>
      </row>
      <row>
        <entry valign="top">
          <para>Securities and Prices</para>
        </entry>
        <entry valign="top">
          <para>kmmSecurities.id</para>
        </entry>
        <entry valign="top">
          <para>kmmPrices.fromId or kmmPrices.toId</para>
        </entry>
      </row>
      <row>
        <entry valign="top">
          <para>Currency Rates</para>
        </entry>
        <entry valign="top">
          <para>kmmCurrencies.ISOCode</para>
        </entry>
        <entry valign="top">
          <para>kmmPrices.fromId or kmmPrices.toId</para>
        </entry>
      </row>
    </tbody>
  </tgroup>
</informaltable>

<para>
  Notes:
</para>

<para>
  1 &#x2013; txType = &#x201C;N&#x201D; for normal transactions,
  &#x201C;S&#x201D; for scheduled transactions
</para>

<para>
2 &#x2013; if kmmAccounts.isStockAccount = &#x201C;Y&#x201D;
</para>
</sect3>

<sect3>
<title>Field formats</title>

<para>
  Several of the data fields are held in an internal format which may not be
  immediately useful to external programs. In these cases, the information has
  been duplicated in both internal and external formats.
</para>

<para>
  Monetary amounts and share values are shown both in numerator/denominator
  format, and, with a field name suffixed with 'Formatted', in the form as shown
  on your screens.
</para>
  
<para>
  Similarly, some fields, such as account type appear both as a numeric code,
  and in a field suffixed 'String' in the form and language of the application.
</para>
</sect3>

<sect3>
<title>Updating your data</title>

<para> 
  Having data in an industry standard format does give you the ability to modify
  it outside the &kappname; application. DO NOT DO IT unless you really know
  what you are doing, and always be certain to make a backup copy of your data
  first. If you get it wrong, &kappname; may not be able to access your data,
  and you could even end up losing it altogether. You have been warned!
</para>
</sect3>

<sect3>
<title>Stored queries</title>

<para>
  Most database systems allow you to store commonly used queries and procedures,
  and in some cases, these may be held as tables or other objects within your
  database itself. As you will have guessed from the above, all the tables used
  by &kappname; begin with the lowercase letters 'kmm'. This standard will be
  maintained, and only tables beginning with these letters will be
  updated. Thus, provided you avoid these in the naming of your queries etc.,
  you should not experience any problems.
</para>
</sect3>
</sect2>

<sect2>
<title>Encryption</title>

<para>
  Encryption of data in your database is not currently supported.
</para>
</sect2>
</sect1>
</chapter>