I have a project in java where i want that certain data from one table (that is in Sql management studio) is selected and that is inserted in other table. So that i can access data on a jsp page from second table. How to do this?
One method would be to iterate through the table while writing the values into an array. Once the data has been stored into the array you can re-iterate through the array but this time inserting the values into a new table.
This may not be the most efficient method, I am sure someone else will chime in if so.
Another method which does not require Java would be to use the Select As statement in SQL, see example.
CREATE TABLE suppliers
AS (SELECT *
FROM companies
WHERE id > 1000);
Or if you already have a table created you can do the following,
INSERT INTO suppliers
(supplier_id, supplier_name)
SELECT account_no, name
FROM customers
WHERE city = 'Newark';
If you use SQL, you can use SELECT INTO statements to achieve this easily:
SELECT Column1,Column2
INTO SecondTable
FROM FirstTable
WHERE Column3='Whatever'
This will copy the data from FirstTable into SecondTable.
See This Link for more examples
Related
In my project, there are 2 tables, orders and order_payments.
The latter is a join table with 2 columns, order_id and payment_id.
I didn't use FK for efficiency sake.
When I delete some orders from orders table, I need to delete the related records from payments table based on the order_id field.
So, after the orders are deleted using the DELETE statement, I want the order_ids of the deleted orders to be returned, so that I can use them to retrieve the order_payments table and delete related records.
Could someone teach me how to do that?
I tried
private List<OrdersRecord> deleteOrders(OrderQuery orderQuery, DSLContext dsl) {
DeleteQuery<OrdersRecord> deleteQuery = dsl.deleteQuery(ORDERS);
deleteQuery.addConditions(orderQueryConditions(orderQuery));
deleteQuery.setReturning();
deleteQuery.execute();
return deleteQuery.getReturnedRecords();
}
but an empty List was returned.
Where I did wrong?
Thanks in advance!
I don't think this is possible in MySQL 8.0.21 yet:
https://bugs.mysql.com/bug.php?id=83139
If you know a way to do this with native MySQL (whatever version), or emulate it somehow, please document it here: https://github.com/jOOQ/jOOQ/issues/6865
You can use 'on delete cascade'. So you can resolve your problem. And you don't must getting ids from first table. You just must execute delete query from first table and records from second table will deleted too.
For example:
https://www.mysqltutorial.org/mysql-on-delete-cascade/
I'm relatively new to working with JDBC and SQL. I have two tables, CustomerDetails and Cakes. I want to create a third table, called Transactions, which uses the 'Names' column from CustomerDetails, 'Description' column from Cakes, as well as two new columns of 'Cost' and 'Price'. I'm aware this is achievable through the use of relational databases, but I'm not exactly sure about how to go about it. One website I saw said this can be done using ResultSet, and another said using the metadata of the column. However, I have no idea how to go about either.
What you're probably looking to do is to create a 'SQL View' (to simplify - a virtual table), see this documentation
CREATE VIEW view_transactions AS
SELECT Name from customerdetails, Description from cakes... etc.
FROM customerdetails;
Or something along those lines
That way you can then query the View view_transactions for example as if it was a proper table.
Also why have you tagged this as mysql when you are using sqlite.
You should create the new table manually, i.e. outside of your program. Use the commandline 'client' sqlite3 for example.
If you need to, you can use the command .schema CustomerDetails in that tool to show the DDL ("metadata" if you want) of the table.
Then you can write your new CREATE TABLE Transactions (...) defining your new columns, plus those from the old tables as they're shown by the .schema command before.
Note that the .schema is only used here to show you the exact column definitions of the existing tables, so you can create matching columns in your new table. If you already know the present column definitions, because you created those tables yourself, you can of course skip that step.
Also note that SELECT Name from CUSTOMERDETAILS will always return the data from that table, but never the structure, i.e. the column definition. That data is useless when trying to derive a column definition from it.
If you really want/have to access the DB's metadata programatically, the documented way is to do so by querying the sqlite_master system table. See also SQLite Schema Information Metadata for example.
You should read up on the concept of data modelling and how relational databases can help you with it, then your transaction table might look just like this:
CREATE TABLE transactions (
id int not null primary key
, customer_id int not null references customerdetails( id )
, cake_id int not null references cakes( id )
, price numeric( 8, 2 ) not null
, quantity int not null
);
This way, you can ensure, that for each transaction (which is in this case would be just a single position of an invoice), the cake and customer exist.
And I agree with #hanno-binder, that it's not the best idea to create all this in plain JDBC.
I have a table named "preference" which includes more than 100 columns in oracle,I wrote a little bit complicated SQL which need use keyword UNION/INTERSECT/MINUS to do a query.
Take a simple example:
select a.* from preference a where a.id = ? union
select a.* from preference a where a.id = ?
The business care have been changed due to unlimited length string storage on demand. one column need to be re-defined to Clob type. Oracle don't allow union on the clob type, so ideally the a.* cannot be used here.
I changed SQL to like below:
select a.a,a.b,a.c... from preference a where a.id = ? union
select a.a,a.b,a.c... from preference a where a.id = ?
It lists all columns except clob and then I have to do another selection to append the Clob value together. Is that a good idea?
The Another issue brought from above case is that: as I mentioned this table has large columns, list all columns in sql it make SQL much longer. Is there expression I can select all columns but getting rid of specific one?
Oracle when delaing with log does not allow union/minus but allows union all, may be you can rewrite your query using union all and use a select . in the select clause you can issue a select a. or list every column.
After reading your question my main concern is memory usage on Java, are you using an orm to load the data? or are you using the jdbc api?
If you are loading all the clobs into some strings you could end with an OutOfMemoryError. My advice is to load the clob only for rows you need to show to the user (or for the rows where the clob filed has to be processed).
Can you give more insight about your application (the numer fo rows it has to process) and your data (epsecially the clob size)?
In my project are two matrices using Microsoft Access database, sourced from two different tables (for example TABLE_A and TABLE_B). I need to retrieve a row and a column from that database.
Selecting COL1 from one table means selecting a corresponding row from the other.
I would like to retrieve COL1 and the corresponding row at the same time.
How is this possible using SQL and a JDBC call?
Thank you!
In order to work with two separate tables of the same database you will need one Connection and a Statement and a ResultSet for each database table.
Since you do not clarify how you have stored your matrix data in the database i can not give you more details here. But, whatever you do, in order to access the contents of the first matrix from the database table you will have to replicate for the second. Just change the table name in your SQL select.
However it will not happen "at the same time", one query will follow the other.
If I have a table like:
CREATE TABLE FRED
(
recordId number(18) primary key,
firstName varchar2(50)
);
Is there an easy way to clone it's structure (not it's data) into another table of a given name. Basically I want to create table with exactly the same structure, but a different name, so that I can perform some functionality on it. I want to do this in code obviously. Java preferably, but most other languages should be similar.
If you're looking a way to find the exact DDL to recreate the table, including the storage clause, you can use
select dbms_metadata.get_ddl('TABLE', 'TABLE_NAME', 'SCHEMA_NAME') from dual
as described here.
CREATE TABLE tablename AS SELECT * FROM orginaltable WHERE 1=2;
Edit: The WHERE clause prohibits any rows from qualifying.
SELECT INTO TARGET_TABLE FROM SOURCE_TABLE;
OR
CREATE TABLE TARGET_TABLE_NAME AS SELECT * FROM SOURCE_TABLE;
if you want to copy only the structure then add the Where Clause WHERE 1=2.
I hope it will be helpful.