How can I arrange a database table like another table? - sql

I have two database tables: TABLE 1 and TABLE 2, both have the same number of columns and have the same columns, the problem is that TABLE 2 has a different order of it columns, So I want to arrange the columns of TABLE 2 like they are arranged in TABL1, but i Dont know how to do that.
I'll be very grateful if you can help me.

Re-create Table2 as create table Table2_Temp as select <sequence of fields like Table1> from Table2. After that re-name tables: Table2 -> Table2_Old (or just drop this one), Table2_Temp -> Table2. This is the simplest way and can be realized in any version of DB.
Change order of columns using non-standard SQL language facilities but if it's possible in using a type of DB.

Related

Dynamic table select based on another table value

I have a work table "sheet1" with columns "Article, article_1(1,2,3,4), database_id1". I have ten (or more) tables with database_id (table1, table2, table3...) with four columns :
table1: article_1, database_id1, Information, another_information
Table2: article_2, database_id2, information, another information
I need to make a query like select article, information, another information WHERE article1=database_id1 FROM database_id(NUMBER) some kind of dynamic table.

Joining Different Database Tables

I have two tables in Access pulling from databases. There is no primary key linking the two tables, because the databases pull from different programs.
Using SQL, I need all of the information from both tables to pull into a query, and this is where I have problems. The two tables are pulling the same data, but they column titles might not necessarily be the same. For now, I'm assuming they are. How can I get it so that the data from both tables pull into the correct column together?
Here's an example of code (I can't post the real code for certain reasons):
SELECT system1_vehiclecolor, system1_vehicleweight, system1_licenseplate, system2_vehiclecolor, system2_vehicleweight, system2_licenseplate
FROM system1, system2
To further explain this, I want the table to have a column for vehiclecolor, vehicleweight, and licenseplate that combines all of the information. Currently, the way I have it, it is making a column for each of the names in each table, which isn't what I want.
You can use 2 queries to get this done
Select col1as c1 ,col2 col as c2 into resulttable from table1
Insert into resulttable (c1,c2) select colX as c1, colY as c2 from table2
Hope this will help you

Retrive unique records from an access table which does not have a fixed structure

I want only the unique records in same or new table. And I want to do this with different tables (having duplicate records) in access database through same code.
The flow should be like:
input table ------VBA MODULE------> table with unique records
I am able to do this group by function but for that i have to use field names in query. But field names will differ from table to table.
Please help!
Just use query
SELECT DISTINCT * FROM MyAnyTable

Oracle SQL merge tables without specifying columns

I have a table people with less than 100,000 records and I have taken a backup of this table using the following:
create table people_backup as select * from people
I add some new records to my people table over time, but eventually I want to merge the records from my backup table into people. Unfortunately I cannot simply DROP my table as my new records will be lost!
So I want to update the records in my people table using the records from people_backup, based on their primary key id and I have found 2 ways to do this:
MERGE the tables together
use some sort of fancy correlated update
Great! However, both of these methods use SET and make me specify what columns I want to update. Unfortunately I am lazy and the structure of people may change over time and while my CTAS statement doesn't need to be updated, my update/merge script will need changes, which feels like unnecessary work for me.
Is there a way merge entire rows without having to specify columns? I see here that not specifying columns during an INSERT will direct SQL to insert values by order, can the same methodology be applied here, is this safe?
NB: The structure of the table will not change between backups
Given that your table is small, you could simply
DELETE FROM table t
WHERE EXISTS( SELECT 1
FROM backup b
WHERE t.key = b.key );
INSERT INTO table
SELECT *
FROM backup;
That is slow and not particularly elegant (particularly if most of the data from the backup hasn't changed) but assuming the columns in the two tables match, it does allow you to not list out the columns. Personally, I'd much prefer writing out the column names (presumably those don't change all that often) so that I could do an update.

execute trigger when data is export from csv

We have seven tables in postgres database.t1,t2,t3,t4,t5,t,t7 Each table contains various columns with duplicate product_id number
The product_id number is exists is each table.That means
t1 --> 123(product_id)
t1 --> 123(with various other column data)
t2 --> 123 upto t7
this "123" product id will be existing in each table upto t7.And also,the table will have more than one same product_ids.
Current requirement is to process all product_id's in my server, I need to create intermediate table with unique product ids.
whenever i am updating the tables(t1..t7) the intermediate table has to be triggered to update.
Edit1:
The Intermediate view has to be generated by making all seven tables together.
When I am again importing few more rows from csv/(copy tablename from csvpath...) to these seven tables.The intermediate view also need to be computed and updated by the trigger method
Because this is the frequent operation.Updating the tables from csv and again computing and updating the intermediate view.
So ,How it supposed to write the trigger when updating the seven tables by importing from csv?
Don't create a table, create a view that selects from those tables.
create or replace view all_product_ids
as
select product_id
from t1
union
select product_id
from t2
union
... you get the picture ...
Once you have done that, re-think your database model. By the little information you have provided it sure sounds like your model is not ideal.
Take a look at the PostgreSQL documentation on PL/pgSQL triggers, and especially on this example, your activities looks similar.