The CREATE TABLE articulation is utilized for making another table in Derby information base.
Syntax
Following is the punctuation of the CREATE explanation.
CREATE TABLE table_name (
column_name1 column_data_type1 constraint (optional),
column_name2 column_data_type2 constraint (optional),
column_name3 column_data_type3 constraint (optional)
);
Another approach to make a table in Apache Derby is that you can determine the segment names and information types utilizing an inquiry. The language structure for this is given beneath −
CREATE TABLE table_name AS SELECT * FROM desired_table WITH NO DATA;
Example
The accompanying SQL articulation makes a table named Student with four segments, where id is the essential key and it is auto produced.
ij> CREATE TABLE Student (
Id INT NOT NULL GENERATED ALWAYS AS IDENTITY,
Age INT NOT NULL,
First_Name VARCHAR(255),
last_name VARCHAR(255),
PRIMARY KEY (Id)
);
> > > > > > > 0 rows inserted/updated/deleted
The DESCRIBE order portrays indicated table by posting the sections and their subtleties, if the table exists. You can utilize this order to confirm if the table is made.
ij> DESCRIBE Student;
COLUMN_NAME |TYPE_NAME |DEC&|NUM&|COLUM&|COLUMN_DEF|CHAR_OCTE&|IS_NULL&
------------------------------------------------------------------------------
ID |INTEGER |0 |10 |10 |AUTOINCRE&|NULL |NO
AGE |INTEGER |0 |10 |10 |NULL |NULL |NO
FIRST_NAME |VARCHAR |NULL|NULL|255 |NULL |510 |YES
LAST_NAME |VARCHAR |NULL|NULL|255 |NULL |510 |YES
4 rows selected
Create a Table using JDBC Program
This part shows you how to make a table in Apache Derby information base utilizing JDBC application.
In the event that you need to demand the Derby network worker utilizing network customer, ensure that the worker is going. The class name for the Network customer driver is org.apache.derby.jdbc.ClientDriver and the URL is jdbc:derby://localhost:1527/DATABASE_NAME;create=true;user=USER_NAME;passw ord=PASSWORD".
Follow the means offered beneath to make a table in Apache Derby −
Step 1: Register the driver
To speak with the information base, above all else, you need to enroll the driver. The forName() technique for the class, Class acknowledges a String esteem speaking to a class name loads it in to the memory, which naturally enrolls it. Register the driver utilizing this strategy.
Step 2: Get the connection
By and large, the initial step we do to impart to the data set is to interface with it. The Connection class speaks to the actual association with a data set worker. You can make an association object by summoning the getConnection() strategy for the DriverManager class. Make an association utilizing this strategy.
Step 3: Create a statement object
You need to make a Statement or PreparedStatement or, CallableStatement objects to send SQL explanations to the information base. You can make these utilizing the strategies createStatement(), prepareStatement() and, prepareCall() separately. Make both of these items utilizing the suitable strategy.
Step 4: Execute the query
Subsequent to making an assertion, you need to execute it. The Statement class gives different strategies to execute a question like the execute() technique to execute an explanation that profits more than one outcome set. The executeUpdate() technique executes questions like INSERT, UPDATE, DELETE. The executeQuery() strategy to results that profits information and so forth Use both of these strategies and execute the assertion made beforehand.
Example
Following JDBC model exhibits how to make a table in Apache Derby utilizing JDBC program. Here, we are associating with an information base named sampleDB (will make in the event that it doesn't exist) utilizing the inserted driver.
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;
import java.sql.Statement;
public class CreateTable {
public static void main(String args[]) throws Exception {
//Registering the driver
Class.forName("org.apache.derby.jdbc.EmbeddedDriver");
//Getting the Connection object
String URL = "jdbc:derby:sampleDB;create=true";
Connection conn = DriverManager.getConnection(URL);
//Creating the Statement object
Statement stmt = conn.createStatement();
//Executing the query
String query = "CREATE TABLE Employees( "
+ "Id INT NOT NULL GENERATED ALWAYS AS IDENTITY, "
+ "Name VARCHAR(255), "
+ "Salary INT NOT NULL, "
+ "Location VARCHAR(255), "
+ "PRIMARY KEY (Id))";
stmt.execute(query);
System.out.println("Table created");
}
}
Output
On executing the above program, you will get the accompanying yield
Table created