I'm trying to create an access table (with a Primary key) that loads in the data from a Union SQL view.
Im hoping there is a way of achieving this? Ive tried an append and create table queries which copies the data but still no primary key.
I've created a blank table with the fields i want but cant think how to import the fields from the view into it?
Am i better off loading the view into excel?
Any tips would be great!
thanks
I assume you are trying to turn this query into a table because the query takes a long time to run and the data don't change often. In which case this is fine. Otherwise you should really be using your query. And if having a PK is a problem (which it shouldn't be because you can't edit a union query) then you should explain more about what you are trying to do.
Anyway, to turn your query into a table you need to make a table with the columns in your query and add one more column which is an AutoNumber column. This is your PK. Then create your append query like normal. Don't append anything to the AutoNumber column, it will automatically populate itself when a new row is added.
Related
I'm very new to database/server management. I'm working with a database that I can't add any columns to since it interfaces directly with another piece of software and therefore must stay in a very specific format. However, I'd like to be able to add DateCreated, and CreatedBy columns to the tables in this database to setup some automatic email updates when new entries are made. To do this, I thought I might be able to keep a copy of the original database that automatically updates when changes are made to the original and simply add the additional columns to the copy. I'm working in Microsoft SQL 2017. If anyone could provide any guidance on the best way to accomplish this, your help would be much appreciated.
Create a table extension that consists of the additional columns + the key value from the original table. Each row in Table 1 should have 1 or 0 rows in Table 2. Use a trigger on Table 1 to insert a row in Table 2 on Insert or Update.
Table will be getting new data everyday from source system and i want the duplicates to be deleted automatically as soon as new data gets loaded to table.
Is it possible in bigquery?
I tried to create a view named sites_view in bigquery with below query
SELECT DISTINCT * FROM prd.sites
but duplicates not getting deleted automatically.
Below is for BigQuery:
Duplicates will not be deleted automatically - there is no such functionality in BigQuery
You should have some process to make this happen as frequently as you need or use views
Bigquery is based on append-only kind of a design. So, it accepts all the data.
This is one of the reasons there are no Primary/Unique key constraints on it, so you can't prevent duplicates from entering in the table.
So, you have to have a process like:
1.) Create a new table without duplicates from your original table.
(You can use DISTINCT/ROW_NUMBER() for doing this.)
2.) Drop original table.
3.) Rename new table with original table name.
Let me know if this information helps.
I add a new column called "key" by doing an update on my existing table.
I insert some dummy data with "key" value and run the following query:
SELECT key, name, id ...
FROM table
GROUP BY key, name, id ...
The query run correctly with following results:
existing data has key = null and existing data intact
dummy data contains valid key but no real data
However when I try to save this query as a View I get the following error:
Failed to create view. Field 'key' not found in table 'foo.table'.
This doesn't make sense because the column exists in the table. Does anyone come across this? My current work around is to just run the query without saving it as a View. However when I have more complicated queries involving this new "key" column it would be handy to be able to create Views containing the new column.
This issue has been acknowledge by Google and fixed via Enterprise Support ticket #04171323
Creating views should work now when a table has been altered with a new column.
I need to add a new column to at table. I wonder if it is faster to run an alter table query to add the new column and then an update query to insert data in the column. In compare to creating at new table.
I suppose I could just try both to see witch is faster, but maybe someone could explain why?
Point of view speed:
It's more faster create only one column instead of re-creating a table
Point of view data consistence:
A table probabily has a lot of relation with other DB table (it can be a foreign table for others), so if you re-creating a table you must value a script about update other tables reference to your.
I hope, I've answered completely to your question. Have a nice day
I have one staging table and want to insert data to Main table, so i want to check while inserting data from staging to Main table, if exists then update the records else insert as new records. Here the issue is both the staging as well as Main table does not have any key column based on which i can compare values.
Is it possible to do without having key columns i.e. primary key on both the tables? if yes, please, suggest me how.
Thanks in advance.
If there is no unique key or set of data within a row to define uniqueness, then no.
The set of data can be a combination of the data in each column, creating a sum of parts which will provide uniqueness; however without exposure to your data you would need to make that decision.
You write the WHERE-clause to include all the fields that make your record unique (ie. the fields that decide whether the record is new or should be updated.)
Take a look at this article (http://blogs.msdn.com/b/miah/archive/2008/02/17/sql-if-exists-update-else-insert.aspx) for hints on how to construct it.
If you are using SQL Server 2008r2, you could also use the MERGE statement - I haven't tried it on tables without keys, so I don't know whether it would work for you.