Monday, November 12, 2007

How To Rename Colums in Derby DB and NetBeans 6 Beta 2

Yesterday, I showed you how to create foreign keys in Derby DB within NetBeans 6 Beta 2 database tools. After I thought I had finished this tutorial I noticed a little mistake in my 'PRUEFEN' table of my database.

The primary key or better the foreign key 'PERNR' had the wrong name. The right name should have been 'PERSNR' which is a reference to the primary key 'PERSNR' of my 'PROFESSOREN' table. So, how can I refactore this little (I thought it was little but it wasn't) mistake.

The first step I have done was to search for a solution in the Derby documentation and it's SQL implementation and feature support. Today I've finally realised that something like 'ALTER TABLE RENAME COLUMN OLDNAME TO NEWNAME' isn't supported by Derby so I tried to figure out an alternative way to find a solution for my problem. Here's what I did:

I've created a backup of my table and with this backup I'm able to recreate my table again. The idea was to delete the old 'PRUEFEN' table and create a new one by recreating the old table.

This will open a dialog with a SQL command which will set up my new table (=> simple CREATE TABLE statement). So, the advantage of this workaround is, I just have to rename my column in the SQL-statement and I would have found a solution for my problem... BUT... (keep on reading ;o)

First of all you'll be able to backup your table by right clicking it and execute the 'Grab Structure...' command. A save-dialog will pop up and there you can choose a place to save your table structure.





No you will be able to grab this structure of your old table and recreate a new one by right clicking the Table folder und select the 'Recreate Table...' button. Of course you have to rename your table if you haven't deleted the old 'PRUEFEN' table to something like 'PRUFENNEW'.



This will open up a new diaolog with the CREATE TABLE statement. Now by clicking the 'Edit table script' button you will be able to change your column name. If you have already forgotten... that's why I've started to write this tutorial ;-)





Have you recognized the table script in the above picture? Where have all the foreign keys gone? Hmmmmmm.... So now I have the same work as yesterday. Hopefully in the final release this problem will be solved. I changed my table script so all my requirements are implemented and that is what it looks like:



Finished! No.... I've forgotton the PERSNR foreign key. Damn it! But I have to do the whole work again anyway because I don't want to have the '...NEW' in my table's name. Ahhhh ;o)

Finally: My Script and my Table view




So, what do you think...? I think that's not a quite easy and effective workaround. Hopefully we will see some improvements in the final release of NetBeans 6.

Cheers

Sunday, November 11, 2007

Foreign Keys in Derby with NetBeans 6 Beta 2

Although NetBeans 6 Beta 2 provides several convenient tools for working with databases it was not possible to create a table and set foreign keys without using SQL statements.

So how did I set those foreign keys in my tables using the integrated Derby database which comes out fo the box with Netbeans. So first of all I've created my tables using the 'Create Table' tool of NetBeans by right clicking my 'Tables' folder of a connected database and execute the 'Create Table...' command.




Then you will be able configure you table by editing your table scheme. In my case I have a 'PRUEFEN' table which represents a relation between students (N), lectures (N) and teachers (1) so I have three foreign keys: MATRNR (students), VORLNR (lectures) and PERNR (teachers) but you won't be able to set those foreign keys with this database tool.



So, after clicking the 'OK' button your table will be created but no foreign keys will be set. If you click on your foreign keys folder in your table structure you won't find any. So, you have to additionally add them by executing the corresponding SQL ALTER statements.



Right click your foreign keys folder and press the 'Execute Statement...' command.



Then type the following command: ALTER TABLE PRUEFEN ADD FOREIGN KEY(MATRNR) REFERENCE STUDENTEN; as you can see in the next picture and run the command by clicking the little run-button which you can find over the sql statement.



So, if there are no errors you should see something like the following screen shows in your output window.



Then you have to refresh your table by right clicking it and press the 'Refresh' button. Now you should be able to see the new foreign key reference. You have to repeat this approach for every foreign key in your table.







Finally you should have successfully configured all foreign keys but hold on I've found a little mistake in my table: PERNR should be renamed to PERSNR. But this is the topic of my next post tomorrow: How to rename columns in Derby within NetBeans.

Goold night and I hope this tutorial can help you to design your database tables.

Cheers

C over a Terminal in Gutsy Gibbon

Today I'd like to show you how to write an 'Hello World' application in C language under Ubuntu 7.10 Codename Gutsy Gibbon over a terminal without using an IDE.

If you have the same version of Ubuntu (VMWare Image) as I have then you have the newest version of the GNU C Compiler collection (4.2.1) out of the box and you don't have to lose time for preconfiguration stuff to develope C-applications. The first step is to open a new terminal under Ubuntu.



Then you have to create a new C-file with an editor of your choice. I prefer nano, so I typed nano test.c and the editor opens in the terminal with your file.



The next step is to write your 'Hello World' C-code and save it by pressing Strg+X (in nano), 'J' and 'Enter'. After those steps nano will save your code to your test.c file and you'll be able to compile it.









By typing gcc test.c the gcc-compiler will create an executable (if there are no errors in your application) and it's default name will be a.out. You will be able to execute it by typing ./a.out and press 'Enter'. Then you should be able to see your 'Hello World'-output.







Hope you liked this short tutorial about how to write a simple 'Hello World' application in C over a terminal in Ubuntu 7.10.

Cheers