The addition inquiry embeds information: new records, into the table.
Syntax
Following is the essential grammar of the INSERT proclamation −
ij>INSERT INTO table_name VALUES (column_name1, column_name2, ...);
where column1, column2 are the section esteems in the line that will be embedded.
Example
The accompanying SQL INSERT proclamation embeds another line in the Student table, where it embeds values in the sections id, age, first name and, last name.
SQL> INSERT INTO Student VALUES (101, 20, 'Zara', 'Ali');
Syntax 2
Or on the other hand, you can embed two explicit sections by referencing the segment names, as given beneath −
ij>INSERT INTO table_name VALUES (column_name1, column_name2, ...) VALUES
(value1, value2, ...);
Note − Apache Derby naturally computes values for created segments. For instance, there is no compelling reason to pass esteems for the id segment in the understudy table made before in this instructional exercise. In the event that your table has created segments, use syntax2.
Example
ij> INSERT INTO Student(Age, First_Name, Last_Name) VALUES (21, 'Sucharitha' , 'Tyagi');
1 row inserted/updated/deleted
Furthermore, you can likewise embed two columns utilizing one proclamation as follows −
ij>INSERT INTO Student(Age, First_Name, Last_Name) VALUES (20, 'Amit',
'Bhattacharya'), (22, 'Rahul', 'Desai');
2 rows inserted/updated/deleted
You can check the substance of the table utilizing the SELECT order (we will talk about this order later in this instructional exercise).
Syntax 3
You can utilize another question in the addition articulation as −
INSERT INTO table_Name Query
Example
Assume, we have a table named First_Year in the data set as demonstrated underneath with comparable segments as in Student table −
ID |AGE |FIRST_NAME |LAST_NAME
-----------------------------------------------------------------
1 |20 |Raju |Pendyala
2 |21 |Bhargav |Prayaga
3 |22 |Deepthi |Yerramilli
You can embed values in this table to the understudy table utilizing the above punctuation as −
ij> INSERT INTO Student (Age, First_Name, Last_Name)
SELECT Age, First_Name, Last_Name FROM First_Year;
> 3 rows inserted/updated/deleted
In the wake of executing all the above supplement articulations, the Student table will be as per the following −
ID |AGE |FIRST_NAME |LAST_NAME
-------------------------------------------------------------
1 |21 |Sucharitha |Tyagi
2 |20 |Amit |Bhattacharya
3 |22 |Rahul |Desai
4 |20 |Raju |Pendyala
5 |21 |Bhargav |Prayaga
6 |22 |Deepthi |Yerramill
Insert Data using JDBC program
This segment shows you how to embed information in to a table in Apache Derby data set utilizing JDBC application.
On the off chance that you need to demand the Derby network worker utilizing network customer, ensure that the worker is fully operational. 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 embed information into a table in Apache Derby −
Step 1: Register the driver
To speak with the data set, above all else, you need to enroll the driver. The forName() strategy for the class, Class acknowledges a String esteem speaking to a class name loads it in to the memory, which naturally enlists it. Register the driver utilizing this technique.
Step 2: Get the connection
All in all, 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 an information base worker. You can make an association object by conjuring the getConnection() strategy for the DriverManager class. Make an association utilizing this technique.
Step 3: Create a statement object
You need to make a Statement or PreparedStatement or, CallableStatement objects to send SQL proclamations to the data set. You can make these utilizing the strategies createStatement(), prepareStatement() and, prepareCall() separately. Make any of these items utilizing the fitting technique.
Step 4: Execute the query
In the wake of making an assertion, you need to execute it. The Statement class gives different techniques to execute an inquiry like the execute() strategy to execute an explanation that profits more than one outcome set.
The executeUpdate() strategy executes inquiries 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 already.
Example
Following JDBC model exhibits how to embed information into a table in Apache Derby utilizing JDBC program. Here, we are interfacing 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 InsertData {
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();
//Creating a table and populating
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))";
//Executing the query
String query = "INSERT INTO Employees("
+ "Name, Salary, Location) VALUES "
+ "('Amit', 30000, 'Hyderabad'), "
+ "('Kalyan', 40000, 'Vishakhapatnam'), "
+ "('Renuka', 50000, 'Delhi'), "
+ "('Archana', 15000, 'Mumbai'), "
+ "('Trupthi', 45000, 'Kochin'), "
+ "('Suchatra', 33000, 'Pune'), "
+ "('Rahul', 39000, 'Lucknow'), "
+ "('Trupti', 45000, 'Kochin')";
stmt.execute(query);
System.out.println("Values inserted");
}
}
Output
On executing the above program, you will get the accompanying yield −
Values inserted