How to replicate a table 100 times and add it back to the original table - sql

I just started using SQL and need to perform the following task.
1) First, I need to dupicate a table 100 times. For all these duplicated tables, I want to keep its content unchanged. But I also want to update the primary key by one.
2) Secend, I want to concatenate all these duplicated tables together by row, and also concatenate them to the original table by row.
Example: The original table looks like:
ID CATEGORY
1 A
2 B
… …
26 Z
And I want to duplicate this table, and concatenate it to the original one. I want to maintain the colums other than the primary key (ID here) unchanged, and update the primary key by one each time. I want to get:
ID CATEGORY
1 A
2 B
...
26 Z
27 A
...
52 Z
How to do this? Thanks!

Related

Selecting multiple values from another table on SQL (phpMyAdmin)

I've been trying to select multiple values from another table (i.e. texts in multiple rows) and display it on main table so that multiple values can be on one cell.
(also, I don't speak English, so some terms might sound different, sorry)
Example:
1st table:
ID
Name
Value1
1
Cell 2
A; B
2
Cell 4
B;C;D
2nd table (column Value1 is connected to the previous table's Value1 column):
ID
Value1
1
A
2
B
3
C
4
D
5
E
So in the first table I want to put both values in one cell from the 2nd table
Thank you!
At the moment, I am able to select only one value for Value1 column in the first table that is referenced to the second table.
On phpMyAdmin, I tried to add several columns on one constraint, but error occurred.

Conditional Column on sql

I'm trying to create a new table on my DB, the table has 2 important columns
id_brands (This is an FK from the table brands)
id_veiculo
What I would like to have is something like this:
id_brands
id_veiculo
1
1
1
2
2
1
2
2
3
1
1
3
3
2
I create the table but I'm trying to find a way to make this condition with a trigger but without success, I don't know if it's possible or if a trigger is the best way to do that.
What you are probably trying to do, by the pattern of the example table, is setting up an auxiliary N to N relationship table.
In this case, by having another table, for id_veiculo and its properties, you will be able to have both ids as FKs. As for the primary key in this auxiliary table, it would be both id_brands and id_veiculo:
PRIMARY KEY (id_veiculo, id_brands);
Here's another Stackoverflow question about NxM/NxN relationships.
Also, it isn't very clear what you're trying to do with the table, but if it's the population/seeding of data, then yes, a Trigger is an viable solution.

Oracle Enforce Uniqueness

I need to enforce uniqueness on specific data in a table (~10 million rows). This example data illustrates the rule -
For code=X the part# cannot be duplicate. For any other code there can be duplicate part#. e.g ID 8 row can't be there but ID 6 row is fine. There are several different codes in the table and part# but uniqueness is desired only for one code=X.
ID CODE PART#
1 A R0P98
2 X R9P01
3 A R0P98
4 A R0P44
5 X R0P44
6 A R0P98
7 X T0P66
8 X T0P66
The only way I see is to create a trigger on the table and check for PART# for code=X before insert or update. However, I fear this solution may slow down inserts and updates on this table.
Appreciate your help!
In Oracle, you can create a unique index on an expression for this:
create unique index myidx
on mytable (case when code = 'X' then part# end);

Database lookup in Talend

In my talned job I am copying data from Excel to SQL table.
For this job to maintain the foreign key constraint I had to do a look up before copying the data.
The task goes like this.
I have to copy data in Table2 (id keys value).
My excel sheet has data for id and keys column. Table 1 has two columns id and value.
For value column's data I want to look at Table1's corresponding entry with the id of the current record in Table2. I have to copy the data from Table1's value column to Table2's value column.
Excel (id 1 2 3, keys a b c)
Table_1 (id 1 2 3, value 123 456 789)
desired output: Table_2 (id 1 2 3, keys a b c, value 123 456 789)
current output: Table_2 (id 1 2 3, keys a b c, value null null null)
How do I properly map this?
You've set the job up exactly as needs to be done really so it's not your job layout that's the problem here.
If you're expecting every record in your Excel document to have a matching record in your database table then you should use an inner join condition in your tMap join like so:
And this then allows you to have an output that grabs everything that isn't joining (which is your issue here):
This should show you everything in your source (not lookup) file that isn't matching. I suspect that everything is failing to match on your join condition (essentially WHERE ExcelDoc.id = Table.Id) even if it looks like it should. This could be down to a mismatch of datatypes as they are read into Talend (so the Java/Talend datatypes such as int/Integer or String rather than the DB types) or because one of your id columns has extraneous whitespace padding.
If you find that your id column in one of your sources does in fact have any padding then you should be able to convert it in a previous step before your join or even just make the transformation in the join:
The only other thing I'd recommend is that you name your flows between components (just click on them to change the name), especially when you are joining anything in a tMap.

Automatic id assign

i'm a student and im having problems using the automatic increment because when i delete a row it will continue to increment. explaining:
i want to increment id automaticly
so:
id name age
1 michael 18
2 katy 17
3 jack 20
now i delete row 3 and when i click in the button new it'll go to the id 4 instead of id 3
i'v tried rows.count and refresh the textbox but nothing
some adicional info
ds= dataset
maxrows = ds.Tables("virtualtable").Rows.Count
idcliTextBox.Text = maxrows
how do i make it set id to the real last row?
It is the correct behavior and it is not a problem. Usually the autoincrement columns in a database are never reset to accomodate for empty holes caused by deletion of previous inserted records.
The autoincrement column is usually used as primary key to uniquely identify a single record in your table.
Suppose that your table represents students where the ID field value is used as foreign key for another table examresults. In this table you store the exam result of your students. Your student Katy (2) has two records in the examresults table for the graduation in math and geography.
If you delete the record with ID=2 from the table students and the related records from examresults changing the record for Jack from 3 to 2 means that you need to change also the related records for examresults of Jack. This is very impractical and useless if you think about it.