Building a Database with Kexi Hypermania2, 123RF DATA FRIEND
Total Page:16
File Type:pdf, Size:1020Kb
LINUXUSER KDE's Kexi Database Building a database with Kexi hypermania2, 123RF hypermania2, 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 Linux 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 Microsoft Access, 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 spreadsheet. 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 • Thestudentstablehasfourfields:a name, enter Example first name, last name, student ID, and One, click OK, and use birth date. the browse dialog to • Theassignmentstablehasfourfields: 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. • Thegradestablehasthreefields: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 step. 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.