How virtual views are not stored on database - sql

AS we know major difference between view and materialized view is- views are not stored on database and materialized views are stored physically on database.
But it will give output in below example.
e.g I have base table emp having 5 fields and I created v_emp view based on this table with only 3 required fields. and I run below query it gives correct output
desc v_emp;
My question is: if views are not stored anywhere on database from where it fetches data.
Note: I am using Oracle database

AFAIK, view is nothing but stored/saved query. So it's doesn't store any data and every time you run the view saying select * from v_emp behind the scene it run the underline select query saying select fld1, fld2, fld3 from employee;

Views ARE stored in the database. There are two main components of a view:
1) The definitions of the view... as in the select statement(for both types of views).
2) The physical storage of actual rows in the database (materialized views only).

Related

How to view all tables that are created by me in oracle 11g?

In Oracle 11g, how do I extract the tables created by me from user_tables (not displaying 100s of tables that are there by default)?
I wrote the following query to get the list of all user tables,
SELECT table_name FROM user_tables;
However, this gives me a very long list of tables, which are not relevant to me. I only want a list of tables that were manually created by me. I know there are only 4-5 tables created by me in the database as of now.
I guess that you are not the only one using the user/schema in the database. You can run a query using the user_objects view. Sort it by CREATED descending like the query below.
select *
from user_objects
where object_type = 'TABLE'
--and created BETWEEN :P_DATE1 AND :P_DATE2
order by created desc
And if you will create another table again, add some hint/mark in the table name. It could be your name initials or prefixes that let you know that it is your table.
Sample table name: ABC_TABLE1
Then you can just use below query to get the table you created.
SELECT *
FROM user_tables
WHERE table_name LIKE 'ABC%';
Created by me on an oracle user?
If any other user has a permission to create a table on a schema you cannot find it using user_tables.
If you remember the table names you can filter it out from user_tables.
I hope it helps.
Thanks to #Littlefoot I learned that I was using "System" (default) schema, instead of creating my own. So in that system schema there are tons of default tables that are available. So by querying the "users_tables", I was also getting all the System default tables, along with my created tables.

Is there a difference between selecting from a view or from a table?

I have some C# code which is trying to pull in 1000+ different entries through an SQL query (think, select col1 from table1 where id = x)
at each iteration of the loop x changes to present a new entry.
Is there a difference between running this query on a view or on a table?
or is there any other way to optimize this procedure?
A table contains data, a view is just a SELECT statement which has been saved in the database (more or less, depending on your database).
The advantage of a view is that it can join data from several tables thus creating a new view of it. Say you have a database with salaries and you need to do some complex statistical queries on it.
Instead of sending the complex query to the database all the time, you can save the query as a view and then SELECT * FROM view

create view over 2 databases into a third one

I have rwo Databases, let's say DB1 and DB2 which DB2 is a copy of DB1 and exactly the same.I also have an empty third Database named Main.How can i create a view within the Main database from the tables of DB1 and DB2.For example if [person].[person] is a table in DB1 and DB2,something like this:
CREATE VIEW v1 AS
SELECT * FROM [DB1].[person].[person]
UNION
SELECT * FROM [DB2].[person].[person];
P.S. All 3 databases are on the same server!
For creating view, you are not needed to have 3rd database as view is nothing but virtual table which is the combination of 2 or more different tables in same or different database or server.
For better understanding of SQL views, please refer SQL Views.

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.

Query to Access Sharded Tables

I am querying a vendors database that has data that is sharded between multiple tables. The tables are named Events_1, Events_2, Events_3, Events_4 etc.
It appears that a new table is automatically created when the table hits 10,000 records. I am looking to write a query that would be able union the results from all the tables (without having to manually add the tables as they are created) as well as a query to only look at the 2 newest tables.
Any suggestions on the best way to go about this?
The database is Microsoft SQL Server 2008
You can do this with dynamic SQL such that you query sysobjects for table name Events_* and build a SELECT UNION from the results.
Better, though perhaps more complicated, would be a DDL trigger that would dynamically update you UNION VIEW to append the new table whenever a table Events_* was created.