Now that your server has been started, you can go ahead and create a database.
To create a new database, right click on Java DB again. From the menu that
appears, select Create Database:
When you click on Create Database, you’ll see a dialogue box appear:
Type a name for your database in the first box. Call it Employees. Type
any User Name and Password (something a bit harder to crack than ours below!):
Click OK to create your database. It should then appear on the list:
Creating a Table in the Database
Now that the database has been created, you need to create a table in the database.
To do so, right click on your database. From the menu that appears select Connect:
When a connection is made, you’ll see some default folders for Tables, Views,
and Procedures (see further down if your screen is not like this):
To create a new table in your database, right click the Tables folder.
From the menu that appears, select Create Table:
When you click on Create Table, a dialogue box appears:
From here, you not only type a name for your table, but you also set up the
columns for the table.
In the Table Name at the top, delete the default name of Untitled. Type a new
name for your table. Call it Workers. You’ll then have a table called
Workers, which is in the Employees database.
But you can’t click OK just yet as the table has no columns in it. We want
to create columns with the following names:
ID
First_Name
Last_Name
Job_Title
The ID column will hold a unique identifying number. This will identify a row
in the table. A column with unique data in it is known as a Primary Key.
Because it’s the Primary Key, the column has to hold data: It can’t hold a null
value. (A null value just means there’s no information there.)
Click the button on the right, Add column. A new dialogue box pops up:
Set the following:
Name: ID
Type: Integer
Constraints: Primary Key, Unique
Your dialogue box should look like this:
The NAME is the name of the column in the table, like ID, First_Name,
etc. The TYPE is the DATA TYPE, Integer, VARCHAR, etc. Click the dropdown
list to see more. Then check or uncheck the CONSTRAINTS boxes as indicated
(Primary, Unique).
Click OK to return to the Create Table dialogue box. A new row is added:
We now have enough for the ID column in the table. Click the Add Column
button on the right to add a new column to the table. Enter the following
values for this column (VARCHAR means a variable number of characters):
Key: Unchecked
Index: Unchecked
Null: Unchecked
Unique: Unchecked
Column Name: First_Name
Data Type: VARCHAR
Size: 20
For the third column in your table, enter the following values:
Key: Unchecked
Index: Unchecked
Null: Unchecked
Unique: Unchecked
Column Name: Last_Name
Data Type: VARCHAR
Size: 20
For the final column, here are the values to enter:
Key: Unchecked
Index: Unchecked
Null: Unchecked
Unique: Unchecked
Column Name: Job_Title
Data Type: VARCHAR
Size: 40
When you’re finished, your Create Table dialogue box should look like this:
Click OK when you’ve entered all the information. Your table and table columns
will then be created:
The next thing to do is to add some records to the database table. We’ll do
that next.
|
Kaynak : https://www.homeandlearn.co.uk/java/creating_a_database_with_java.html ‘sitesinden alıntı