LINUXUSER KDE's

Building a database with Kexi hypermania2, 123RF DATA FRIEND

Kexi, part of the KOffice suite, allows anyone to create, populate, and maintain a database. BY MARTIN STREICHER

he open source world offers a The Kexi database development tool knowledge of SQL. Instead, a query is number of solutions for manag- [3] is another alternative for many largely drag and drop: Choose one or Ting data. SQLite is a tiny yet pow- users. Kexi, billed as “Access for Linux,” more tables, relate one field to another erful database; MySQL is extremely pop- has been available since 2006, and the by point and click, and select the fields ular with web developers; and Postgre- latest stable release, Kexi 1.1.3, is part of you want to display. Kexi also offers SQL boasts transactional features on par the KOffice 1.6 suite [4]. Kexi 1.1.3, drag-and-drop form building to create in- with Oracle. All three solutions have which I used for this article, dates to terfaces to enter data, and like Access, command-line and programmatic inter- April 2008, but Kexi 2.0 is marching Kexi either can use its own database for- faces, and both MySQL and PostgreSQL through a series of pre-releases, with the mat or can connect to another engine for have support for browser-based manage- intention to ship by the end of the year. more oomph. In Kexi’s case, you can use ment [1] [2]. SQLite, MySQL, and Post- MySQL. If you dislike the phpMyAdmin greSQL all have strengths and weak- Introducing Kexi interface, try Kexi to browse an existing nesses, and each is capable, but sadly, Kexi, like , offers a MySQL database. none of the three is suitable for the new friendly user interface to create, popu- Kexi is modal. The Design View cre- Linux netbook owner or casual desktop late, and maintain a database. Many ates and edits tables, queries, and forms, user. Indeed, Linux users have had few tasks in Kexi are like filling out a spread- whereas the Data View shows the end options for a friendly, graphical, and sheet. For example, to create a table, you result. Building an application in Kexi is lightweight database solution akin to Mi- fill out a row for each field in the table. typified by switching from one mode to crosoft Access. Then you name each field, assign a type the other. First I’ll use Kexi to build an A Windows user can build tables, (such as date, integer, or text), and per- application, exploring its features along enter data, formulate queries, and craft haps link the new field to an existing the way. reports in Access with little more than a field in another table, thereby relating mouse. The OpenOffice.org Base tool at- the two tables (hence the name “rela- Installing Kexi tempts a similar mission within Linux tional database”). Once you define a The easiest and fastest way to install environments, but some popular distros table, you can populate it with data as Kexi is via your package manager. On don't include Base with the default desk- you would a . Debian and Ubuntu variants, use apt-get top, and many Linux users are still Crafting queries in Kexi is a little more or one of the equivalent graphical front searching for other options. involved but does not require any ends:

86 ISSUE 105 AUGUST 2009 KDE's Kexi Database LINUXUSER

choose New Project table for scores, which ties a student to a Stored in File. When grade for a particular assignment. Kexi asks for a database •฀ The฀students฀table฀has฀four฀fields:฀a฀ name, enter Example first name, last name, student ID, and One, click OK, and use birth date. the browse dialog to •฀ The฀assignments฀table฀has฀four฀fields:฀ choose a location for the a description, a maximum possible file on your disk. score, a unique ID, and a date to re- The main Kexi win- cord when the work was assigned. dow resembles Figure 1. •฀ The฀grades฀table฀has฀three฀fields:฀a฀ The pane on the left col- student ID, an assignment ID, and a lects your tables, que- score. ries, forms, and scripts To create a table, right-click on Tables in (the latter is an experi- the leftmost pane and choose Create Ob- mental feature and is ig- ject: Table. To begin, I’ll show you how nored here). Kexi calls to create the students table. each of those entities an When the blank table appears in cen- Figure 1: The main Kexi window. object. To create a new ter pane, click in the first row of the table, for example, right- table, type id in the Field Caption col- $ sudo apt-get update click on Tables and choose Create Object: umn and select Integer Number as the $ sudo apt-get install kexi Table. (From the menubar: Choose Insert Data Type. In the rightmost pane, choose | Table.) The tool palette is immediately the Properties tab, click on Primary Key Kexi depends on KDE [5]. If you do not below the menubar; the list of available and choose Yes. (Whenever you desig- use KDE or any of its components, the tools changes depending on the task at nate a field as a primary key, Kexi auto- addition of Kexi, KDE, and the latter hand. matically sets the other properties to en- package’s attendant dependencies could The central area of the Kexi window is sure the field is unique.) You will refer to add some 100 packages to your system, the heart of the application, in that most the student ID from the grades table. including the KOffice suite. Otherwise, of your work is performed here. The Now choose Save from the File menu. the process is very quick. rightmost panel, which is empty in Fig- When prompted, type students in the After the installation finishes, run ure 1 but shown in subsequent figures, Caption field and click OK. Your Kexi Kexi: is used to edit the attributes of fields. window should now look something like Figure 2. $ kexi Building Your First To continue, create the fields for the Database first and last names, using both the table When the Choose Project window pops To demonstrate, I’ll show you how to editor and the properties editor. The two up, choose Blank Database and click construct a database to maintain student fields should be Text, and make sure OK. The next prompt asks where you grades. The structure of the database, or both are required, are indexed (to speed would like to create the database. The schema, includes three tables: a table for two choices are in a file or in a MySQL students, a table for the class curriculum database server. To keep things simple, (homework, quizzes, and tests), and a

Figure 3: Choose a table and row as a foreign Figure 2: The students table with one field. key.

AUGUST 2009 ISSUE 105 87 LINUXUSER KDE's Kexi Database

tioned at the outset, it’s typical to write queries in SQL, but that’s not particu- larly natural for the ordinary user. Kexi, again like Access, provides a graphical interface in which you can construct Figure 5: Correlate fields between two tables to form a queries. Figure 4: A collection of (very) capable students. query. Now I’ll show you how to build a query searches), and have a maximum length student ID, use the tabs in the rightmost that will summarize all of the scores that of 100 characters. Neither field can be pane, but the field should refer to the as- have been recorded. To produce the blank, so set the appropriate property to signment ID. summary, the student’s name is drawn make the fields mandatory. Furthermore, If Kexi prompts you to create a pri- from the students table, the assignment create an additional field to record each mary key for the grades table when you details are drawn from the assignments student’s birth date. Its type should be save your work, simply click No. This table, and the score is extracted from the Date/ Time, and specify that the field is table does not require a primary key. grades table. The ID fields scattered required. When the students table is Now you have enough structure in throughout the three tables match the complete, save your work. place to record student scores and run corresponding records to one another. Next, create an assignments table for queries. To create the query, choose Insert | classwork using much the same process So far, you’ve been working in the De- Query. In the panel that opens at center, you did in the last . Create an assign- sign View. To enter data, choose any of choose a table name from the drop- ment field (Text, required, not blank) to the tables and switch to Data View by down menu, and click Add. Do the same describe the assignment, a maximum_ choosing View | Data View or by press- for the other two tables until all three are score field (Integer Number, required) to ing the F6 key. Again, the interface shown in the middle pane. record the maximum score possible, and should remind you of a spreadsheet with Next, click on the id field in the as- a date field (Date/ Time, required, not a header at top to clarify the order of the signments table and drag the field to the blank) to indicate when the assignment fields. Here, you can create entries for activity_id field in the grades table. This was given. five or six students and entries for a correlates the fields. Do the same to cor- Finally, create the grades table. It ties a handful of assignments before you cre- relate the id field in the students table student to an assignment and records ate grades, combine a student record with the student_id of the grades table. WANT TO KNOW the student’s score. First, create a field and assignment, and tally a score. Figure Your view should now resemble that in named student_id and set its type to In- 4 shows some possible student records. Figure 5. teger Number. In the Properties tab at Now that you have some data, the The next step is to choose the fields right, make the field required and make next step is to put the information to you want to display. If you double-click sure it cannot be blank. Next, click on work. on first_name, last_name, assignment, WHAT‘S UP NEXT? the Associations tab in the rightmost and score, these fields drop into the pane (the icon is a little drop-down Probing Data with Queries Query Columns list at the bottom. Figure menu) to define the field as a foreign In addition to persisting data, a database 6 shows the center panel after adding key, or a key from another table that es- derives information via a query. As men- the four fields. tablishes a connection between one re- If you switch to Data View, cord and another. you should see something like Here, the foreign key must be the ID of Figure 7. It displays the results a student. In the dialog, set Row source of the query. SUBSCRIBE TO LINUX to students and choose id as the Bound By assigning values to the column. (The ID is now the foreign key Criteria field of a query col- MAGAZINE PREVIEW, that binds two records together.) In addi- umn, you can limit results just tion to this, you can set the Visible col- to those in which you are inter- OUR FREE MONTHLY umn to display a value from the student ested. Also, you can sort re- record that is more meaningful than the sults by setting the Sorting EMAIL NEWSLETTER! ID, such as the student’s last_name. Fig- field. With a combination of ta- ure 3 shows how the foreign key is de- bles, correlations, and query fined. columns, you can tailor reports Next, create a field named score as a to your specific needs. By the required Integer Number, then create a way, if you prefer to write SQL field named activity_id. To mirror the Figure 6: Choose the fields to display. code by hand, the query editor

88 ISSUE 105 AUGUST 2009 WWW.LINUX-MAGAZINE.COM/NEWSLETTER KDE's Kexi Database LINUXUSER

also offers a Text View in which you can version of Kexi has some quirks, but the and feature integration with the entire author and validate your code. Kexi team promises a novel interface in KOffice 2 suite. For instance, Staniek v2.0, in addition to a report designer to foresees ODF document generation, Forms and More produce professional-looking output. Ac- without programming, and data sharing Kexi also offers a form editor in which cording to Kexi developer Jarosław Stan- across the desktop. you can create data entry interfaces. iek, the forthcoming version of Kexi will Stay tuned. A future column will re- Other tools import and export data and connect to more – including visit Kexi 2 once the software is in wide- import database schemas. The current SQL Server, Oracle, Sybase, and dBase – spread beta testing. n INFO [1] phpMyAdmin: http:// www. phpmyadmin. net/ [2] PremiumSoft Navicat: http:// www. navicat. com/ [3] Kexi: http:// kexi-project. org/ about. html [4] KOffice: http:// www. . org/ [5] KDE: http:// www.kde. org/

Martin Streicher is the former Editor in Chief of Linux Magazine and a free- lance Ruby on Rails developer. When not writing prose or code, he collects art and toys and dreams of becoming a famous comic book author. You can reach Martin at martin. streicher@ THE AUTHOR THE gmail. com. Figure 7: The results of a query. WANT TO KNOW WHAT‘S UP NEXT?

SUBSCRIBE TO LINUX MAGAZINE PREVIEW, OUR FREE MONTHLY EMAIL NEWSLETTER!

WWW.LINUX-MAGAZINE.COM/NEWSLETTER