Creating related table from existing parent table with automatic update - sql

In SQL Server 2008, I need to create a child table from parent big table copying it's values in two columns and updates automatically. These values must be distinct and copy into corresponding two columns in new table. The whole idea would be making reference table with these distinct values and make relation with parent table. Thanks

Enkhzul,
You'll want to create a VIEW. A VIEW will simply run a SELECT query in the background against any tables you request in the query and display as instructed. Syntax will be:
USE [DATABASE]
GO
CREATE VIEW [VIEWNAME] AS
(
select statement here
)
After that you just need to do a SELECT query against the newly created VIEW.

Related

How to create a computed column in PrestoDB with values persisted?

I am working in PrestoDB to query and create tables to configure data in a way that I can draw upon it and work on it in Excel and PowerBI. I am trying to create a persisted calculated column that is simply the quotient of two other existing columns.
A colleague suggested
Create Table B as
Select * , Column A/Column B as Column Q
from Table A
however when I perform
Select *
from Table B
column Q is there but completely empty.
What can I run to permanently add these computed columns so that when I query this data the values are persisted?
I don't think PrestoDB supports computed columns, much less persisted computed columns.
I think what you want is view, though, not a table:
create view v_a
select a.*, ColumnA/ColumnB as ColumnQ
from A a;
Anyone who queries v_b will see ColumnQ with the latest calculation of the ratio.

How can i update a table using this view

I have 3 tables called Airlines,Destinations and PriceTable.
The Airlines table has two columns - Airline_ICAO_Code and Airline. The Destinations table has two columns - Airport_ICAO_Code and Destination. The PriceTable has these columns - ID,Airport_ICAO_Code, Airline_ICAO_Code,Departure,Price and RouteStaus.
The PK in PriceTable is ID.The PK in Airlines table is Airline_ICAO_Code. The PK in Destinations table is Airport_ICAO_Code. The columns Airport_ICAO_Code and Airline_ICAO_Code in the price table are FKs.
I created a view called InputFlightPrices which i want to use to update the PriceTable which stores the flight prices. The reason im using the view to do this is that it allows me to see clearly what airline routes need updating as its difficult to determine the airline and destination from the codes.
This is the view code:
Create View InputFlightPrices
As
Select ID,Airline,Destination,AirportName,Price,Departure,RouteStatus
From Airlines As a
Join PriceTable As p
On a.Airline_ICAO_Code = p.Airline_ICAO_Code
Join Destinations As d
On d.Airport_ICAO_Code = p.Airport_ICAO_Code;
I want the view to display all airlines a-z but I cannot use an Order By clause in the view.
I therefore ran the query below on the view to order the airlines in the view A-Z
Select * from InputFlightPrices
Order By Airline Asc
The resulting view from the above statement displays correctly but it does not allow me to edit the records in it in order to update the table.
Is there a solution.
Thanks for any help offered.
Im editing this in response to Philpxy to try and clarify what i want:
I want to update a table called PriceTable which contains flight prices.
The Airline and Destination columns within the PriceTable contain codes. It is difficult to know what airline and destination to update from these codes so I created a view called InputFlightPrices which shows the Airline and Destination names. This makes it easy to enter the prices for the correct routes.
The problem with the View is that the Airline column is not sorted. Records belonging to an Airline are scattered throughout the table. This could lead to me missing some routes that need to be updated.
Therefore I created a select statement which Ordered the View by Airline A-Z.
The problem that i have now is that I cannot update the PriceTable using this View(the result of select statement) as It does not allow me to edit it.I tried to edit by clicking it directly using the GUI.
I hope thats clear.
As you have multiple joins in view, the update may not update the records correctly. You can use triggers here to update the table.
You may be able to edit your Select * from InputFlightPrices Order By Airline Asc query per answers to How to quickly edit values in table in SQL Server Management Studio?:
In Mgmt Studio, when you are editing the top 200, you can view the SQL
pane - either by right clicking in the grid and choosing Pane->SQL or
by the button in the upper left. This will allow you to write a custom
query to drill down to the row(s) you want to edit.
Go to Tools > Options. In the tree on the left, select SQL Server
Object Explorer. Set the option "Value for Edit Top Rows command" to
0. It'll now allow you to view and edit the entire table from the context menu.
Re updating when the query result involves a view:
From SQL Server 2014 CREATE VIEW (Transact-SQL)
The SELECT clauses in a view definition cannot include the following:
An ORDER BY clause, unless there is also a TOP clause in the select list of the SELECT statement
The ORDER BY clause is used only to determine the rows that are returned by the TOP or OFFSET clause in the view definition. The ORDER
BY clause does not guarantee ordered results when the view is queried,
unless ORDER BY is also specified in the query itself.
[...]
You can modify the data of an underlying base table through a view, as
long as the following conditions are true:
Any modifications, including UPDATE, INSERT, and DELETE statements, must reference columns from only one base table.
[...]
Generally, the
Database Engine must be able to unambiguously trace modifications from
the view definition to one base table.
Per the last sentence: If you want to update a base table for a particular row then it has to be just one base table's columns, columns of some key from that base table must be in the view (UNIQUE NOT NULL or PRIMARY KEY), and you must not be updating those columns.
Otherwise when you ask to update some row in the view it's not clear what row in what base table is to be updated.
See also SQL Server 2014 Modify Data Through a View.

Adding a column to an existing table via join - Oracle SQL

I have a table that exists currently, and I want to add in a column from another table via a join. Is there a way to add it to this table and not have to create a new one? I know I could easily make another table with by joining the data in but I wasn't sure if there was a way to insert that kind of data to an existing one.

delete old values of a table and update the table with results of same query

My question is to simple, but I can't find out a way to delete old values of a table and update same table with results of same query.
UPDATE
The query is an SELECT on Table A, and the results be Table B. And nothing on Table B different of the result of last query on Table A.
I have a very big table, and I need to process the records and create a new table regularly. The old values of this table are not important, only the new ones.
I will appreciate any help.
What about a view? If you only need table B to query on. You said you have a select on table A. Lets say your select is SELECT * FROM TableA WHERE X = Y. Then your statement would be
CREATE VIEW vwTableB AS
SELECT * FROM TableA WHERE X = Y
And then instead of querying tableB you would query vwTableB. Any changes to the data in table A would be reflected in the view so you don't have to keep running a script yourself.
This was the data in vwTableB would be kept updated and you wouldn't have to keep deleting and inserting into the second table.
you can use a temporary table to store results you are working with, if you only need it for one session. it will automatically be dropped when you sign out.
you didn't say what db you are using, but try this
create temp tableB AS select * from tableA

Updating all columns of a table

I am using MS Access to connect to a linked Oracle database. I have table B that pulls only certain columns from table A (in the linked DB).
I am trying to execute a macro that runs Query1 to update table B with the data from A that is constantly changing.
The two are not necessarily related by some ID, so the UPDATE TABLE command doesn't seem logical to me. Should I be using JOIN? I just need a place to launch my query from.
Based on the discussion in the comments, it sounds like Table B is simply a "view" on Table A. I am not confident that you need an UPDATE query.
In MS Access, the syntax to create a view is thus:
CREATE VIEW my_view AS
SELECT
col1, col2
FROM
[Table A]