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.
Dieser Beitrag wurde unter Databases
veröffentlicht. Setze ein Lesezeichen auf den Permalink