I need to copy data from one data table to another as mentioned below
When I enter company manager details of:
|NAME |VALUE |
|title |$randomList |
|firstName |$random |
|initial |$random |
|surname |$random |
When I enter company employee details of
|NAME |VALUE |
|title |$randomList |
|firstName | |
|initial | |
|surname | |
I am new to jbehave. In the above tables when i execute this scenario random characters will be generated and assigned to firstname,initial,surname in the 1st table I somehow need the same data when I execute the second table. I know we should parametrize by using examples table but in this case I don't want to use it. so how to copy the data from one table to another.
Related
Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 1 year ago.
Improve this question
I want to insert 5 million data into a 500 million table named t_phone_numbers without duplicated phones, so I select exist phones all and compare them in memory, but I got Out Of Memory ERROR because the MySQL tables have a large data.
How can I solve it?
Restriction: Mysql table t_phone_numbers cannot change, and the column phone_number is not unique. Create a new table is acceptable.
AS an example.
CREATE TABLE existing (phone VARCHAR(255))
SELECT '123456789' phone UNION ALL
SELECT '789456123' UNION ALL
SELECT '456456456' UNION ALL
SELECT '654654645' UNION ALL
SELECT '123321123' ;
SELECT * FROM existing;
| phone |
| :-------- |
| 123456789 |
| 789456123 |
| 456456456 |
| 654654645 |
| 123321123 |
CREATE TABLE new (phone VARCHAR(255) UNIQUE)
SELECT '123456789' phone UNION ALL
SELECT '464646464' UNION ALL
SELECT '123321123' ;
SELECT * FROM new;
| phone |
| :-------- |
| 123321123 |
| 123456789 |
| 464646464 |
INSERT
INTO existing (phone)
SELECT phone
FROM new
WHERE NOT EXISTS ( SELECT NULL
FROM existing
WHERE new.phone = existing.phone );
SELECT * FROM existing;
| phone |
| :-------- |
| 123456789 |
| 789456123 |
| 456456456 |
| 654654645 |
| 123321123 |
| 464646464 |
db<>fiddle here
The most problem is SELECT part in INSERT. If neither existing nor new table's phone column is indexed then the process may be infinite..
So the recommendations - index this column in new table and (if possible) in existing table. Maybe even create a copy of existing table (phone column only), index it and use in SELECT part.
Anycase this SELECT optimization is the most problem (indexing, using LEFT JOIN, etc.), the insertion itself will be relatively fast.
So let's say I have the following table for a student:
+----+------------+-----------+---------------------+----------+------------+
| first_name | last_name | ID | Birthday | Grade | Periods |
+----+------------+-----------+---------------------+----------+------------+
| John | Doe | 123456 | 1/1/2004 | 11 | 7 |
+----+------------+-----------+---------------------+----------+------------+
How could I get the ResultSet for just that one student by just using their ID for example?
Could the query be SELECT ID(123456) FROM Students" or something along those lines? I found some answers online as to how I could loop through all the Students for example and print out that data or do something else with it, but not how to get the data for a singular element in the table based on one attribute.
In short, I just want to be able to get all the data on a student from just their ID number.
I am wondering is it possible to select only the default value of empty column?
I have absolutely empty table and I want just to select one of the columns default value - it is important for my JAVA app which is filling the table.
Thanks.
You can get the default from the INFORMATION_SCHEMA.COLUMNS
select COLUMN_DEFAULT
from INFORMATION_SCHEMA.COLUMNS
where TABLE_SCHEMA='your_db' and TABLE_NAME='your_table' and COLUMN_NAME='your_column'
You can define a default value for a column when you create a table, if you just want MySQL to insert it automatically:
create table my_table (i INT DEFAULT 1);
But if you mean you want the default value which is stored in the DB dictionary, you can get it by this query:
SELECT Column_Default
FROM Information_Schema.Columns
WHERE Table_Schema = 'yourSchema'
AND Table_Name = 'yourTableName'
AND Column_Name = 'yourColumnName'
I can only think of two ways:
Inserting a row
Insert a row without specifying a value for that column
Select the column from that row; it will have the default value of the column
Delete the row
...probably all in a transaction so nothing else sees it.
Using describe (explain)
The describe command (aka explain) describes objects in the system, including tables. So if you do explain YourTable, you'll get back information about the table, including its default values.
Here's an example from that linked documentation:
mysql> DESCRIBE City;
+−−−−−−−−−−−−+−−−−−−−−−−+−−−−−−+−−−−−+−−−−−−−−−+−−−−−−−−−−−−−−−−+
| Field | Type | Null | Key | Default | Extra |
+−−−−−−−−−−−−+−−−−−−−−−−+−−−−−−+−−−−−+−−−−−−−−−+−−−−−−−−−−−−−−−−+
| Id | int(11) | NO | PRI | NULL | auto_increment |
| Name | char(35) | NO | | | |
| Country | char(3) | NO | UNI | | |
| District | char(20) | YES | MUL | | |
| Population | int(11) | NO | | 0 | |
+−−−−−−−−−−−−+−−−−−−−−−−+−−−−−−+−−−−−+−−−−−−−−−+−−−−−−−−−−−−−−−−+
So you can extract the default from the Default column in the returned rows.
Ah, of course, there's a third way, see slaakso's answer for it.
Suppose I have the following tables, in an Oracle DB
Foo:
+--------+---------+---------+
| id_foo | string1 | string2 |
+--------+---------+---------+
| 1 | foo | bar |
| 2 | baz | bat |
+--------+---------+---------+
Bar:
+--------+-----------+--------+
| id_bar | id_foo_fk | string |
+--------+-----------+--------+
| 1 | 1 | boo |
| 2 | 1 | bum |
+--------+-----------+--------+
When I insert into Foo, by using a Dataset and JDBC, such as
Dataset<Row> fooDataset = //Dataset is initialized
fooDataset.write().mode(SaveMode.Append).jdbc(url, table, properties)
an ID is auto-generated by the database. Now when I need to save Bar, using the same strategy, I want to be able to link it to Foo, via id_foo_fk.
I looked into some possibilities, such as using monotonically_increasing_id() as suggested in this question, but it won't solve the issue, as I need the ID generated by the database. I tried what was suggested in this question, but it leads to the same issue, of unique non-database IDs
It's also not possible to select from the JDBC again, as string1 and string2 may not be unique. Nor is it possible to change the database. For instance, I can't change it to be UUID, and I can't add a trigger for it. It's a legacy database that we can only use
How can I achieve this? Is this possible with Apache Spark?
I'm not a Java specialist so you will have to look into the database layer on how to proceed exactly but there are 3 ways you can do this:
You can create a store procedure if the database server you are using is capable of (most do) and call it from your code.
Create a trigger that returns the id number on the first insertion and use it in your next DB insertion.
Use UUID and use this as the key instead of the database auto generated key.
I have a xlsx file, that has some tabs with different data. I want to be able to save each row of a tab in a list. The first thing that comes to mind is a list of lists, but I was wondering if there is another way. I'd like to save that information in a object, with all its benefits, but can't think of a way to generate/create such diverse objects on the fly. The data in the xlsx is diverse and ideally the program is agnostic of any content.
So instead of e.g. create a list for each row, than put that list in another list for each tab and each tab in another list, I'd like to store the information that each row represents in a single object and just have a list of different objects.
A small graphic to visualize the problem :
+--------------------------------------------------------------------+
|LIST |
| |
| +------------------+ +------------------+ +-----------------+ |
| | Class1 | | Class2 | | Class3 | |
| |------------------| |------------------| |-----------------| |
| | var1 | | var1 | | var5 | |
| | var2 | | var2 | | var6 | |
|... | var3 | | | | var7 |...|
| | | | | | | |
| | | | | | | |
| | | | | | | |
| | | | | | | |
| +------------------+ +------------------+ +-----------------+ |
| |
+--------------------------------------------------------------------+
How about a generic class Row which will contain all the information in a row from your file. Then you simply create a list of Rows. Methods for the Row can allow you to get each column.
Without knowing more about the data, you will not be able to write classes to encapsulate it. You could "dynamically" create classes to create new source code. But then the question is, how would you use the new classes?
Well since you want to avoid a "list of lists" kind of solution there would be another way.
This might not be very efficient or fast but I don't have any experience with it, so maybe it isn't too bad. Here's the idea:
For each Row:
Use javassist to create as many fields as needed dynamically that contain each cell's information. Then create an instance of this class and store it in your list of rows. You could also add a field with information about this particular row (e.g. how many fields there are or their names or types or whatever you might need).
The number of fields or methods could also be determined using Reflection.
To get started with javassist there's a tutorial here.
Besides that I don't think there's much to do that does not involve some sort of List<List<SomeType>>