Creating a view using subquery,with alias for columns in ORACLE 9i! - oracle

This is my query,
CREATE VIEW employee_vu AS(
SELECT employee_id,last_name "employee",department_id
FROM employees);
I am giving alias of columns in lower case ,and in it is stored in lower case
after doing desc i have confirmed.
But when i am trying to select this column employee :error occurs
EMPLOYEE: invalid identifier
Since all column name is stored in upper case ,is this the problem,Please explain what is the concept behind!

You would need to select it using double quotes and matching case:
select employee_id, "employee", department_id from employees;
That's why creating columns with double quoted identifiers is considered bad practice in Oracle.

Related

Invalid column ORA- 00904 error while inserting into table from view

In my main table having table structure column without double quotes but when in View column with double quotes. While inserting data from view into table then getting error.
Here is the table structure of tbl
create table tbl (ID number(10),
name varchar2(50),
addr varchar2(200));
While View is-
create or replace view t_view as
select "ID", "name", "addr" from tbl;
While inserting data into tbl from t_view -
insert into tbl
select * from t_view;
Then getting error ORA- 00904: "addr": Invalid identifier.
So how to resolve this issue, can i remove the double quotes from creation of view.
Remove all double quotes from everywhere in your code.
If you use them while creating objects, you'll have to use them always, specifying exactly same letter case.
Get rid of those, Oracle is - by default - case insensitive and treats all names as uppercase (but you can reference them any way you want, just don't use double quotes!).
Oracle stores the name of any object in UPPERCASE by default. If you have provided double quotes then only Oracle stores the name of the object as it is.
Double quotes - Case sensitive
No quotes - Case insensitive - Stores the name in UPPERCASE
In your case, While writing the DDL of the table, you have not provided the name of the column in double quotes so your table name and column names are stored in UPPERCASE in the metadata.
You can see the same using the following query
select table_name, column_name from user_tab_columns where table_name = 'TBL';
For giving your answer, create the view either of the following syntaxes:
create or replace view t_view as
select ID, name, addr from tbl; -- no double quotes
create or replace view t_view as
select "ID", "NAME", "ADDR" from tbl; -- UPPERCASE column names in double quotes
See the demo here
Cheers!!
What is it you are trying to accomplish. Your request just doesn't make sense. Your intended statement
insert into tbl select * from t_view;
accomplishes exactly the same thing as
insert into tbl select * from tbl;
It's not that you cann't do this (you can), but it can only cause massive duplicate data issues or (hopefully) unique constraint violations.

prevent sqldeveloper from expanding asterisc wildcard in view

I have two views, the second one depends on the data from the first and adds some columns from a different table.
In sql developer I define the views as follows:
view1:
select col11, col12, col13, col14, col15, col15, col16, col17
from table1
view2:
select view1.*, col22, col23
from view1 join table2 on view1.col11 = table2.col21
But when after saving, sqldeveloper expands the "view1.*" part of the second view to the explicit list of columns, so view2 ends up being rewritten as:
select view1.col11, view1.col12, view1.col13, view1.col14, view1.col15, view1.col15, view1.col16, view1.col17, col22, col23
from view1 join table2 on view1.col11 = table2.col21
which is harder to read and to mantain.
Is there a way to prevent this behavior?
Thanks
That's not SQL Developer doing that, it's the database.
You can create the view using the * syntax, but the database will always translate that to a fully qualified SELECT list.
CREATE VIEW LOCS
AS select * from locations;
Now ask the DB for the DDL, and you get
CREATE OR REPLACE FORCE EDITIONABLE VIEW "HR"."LOCS" (
"LOCATION_ID",
"STREET_ADDRESS",
"POSTAL_CODE",
"CITY",
"STATE_PROVINCE",
"COUNTRY_ID"
) AS
SELECT
"LOCATION_ID",
"STREET_ADDRESS",
"POSTAL_CODE",
"CITY",
"STATE_PROVINCE",
"COUNTRY_ID"
FROM
locations;
The view is defined at run-time, it'll take the existing column list and assume you want the same column names in your view as you want in the underlying objects where those columns are being pulled from.
From the Docs -
Expansion of Defining Queries at View Creation Time When a view is
created, Oracle Database expands any wildcard (*) in a top-level view
query into a column list. The resulting query is stored in the data
dictionary; any subqueries are left intact. The column names in an
expanded column list are enclosed in quote marks to account for the
possibility that the columns of the base object were originally
entered with quotes and require them for the query to be syntactically
correct.

How to create a blank/empty column with SELECT query in oracle?

I want to generate an output with blank/empty column with a "Select" query in oracle. I'm able to achieve this with below sql query:
SELECT CustomerName AS Customer, "" AS Contact
FROM Customers;
So when I run above sql query it returns a table with two columns "Customer" column with content in it and "Contact" column with no content or blank column.
I want to achieve same with oracle query. Any help will be highly appreciated.
I think you should use null
SELECT CustomerName AS Customer, null AS Contact
FROM Customers;
And Remember that Oracle
treats a character value with a length of zero as null.
In DB2, using single quotes instead of your double quotes will work. So that could translate the same in Oracle..
SELECT CustomerName AS Customer, '' AS Contact
FROM Customers;
I guess you will get ORA-01741: illegal zero-length identifier if you use the following
SELECT "" AS Contact FROM Customers;
And if you use the following 2 statements, you will be getting the same null value populated in the column.
SELECT '' AS Contact FROM Customers; OR SELECT null AS Contact FROM Customers;

Oracle: access to column with name "all"

everybody. I have table, where one of the column names is "All" and I want to get this column. I trying to use next simple query:
SELECT All
FROM TableName
, but I have ORA-00936. When I use just:
SELECT *
FROM TableName
I see required column with "All" name. In table specification I see next SQL script:
create table TableName
(
houseid NUMBER not null,
id NUMBER not null,
note VARCHAR2(255),
all NUMBER
)
How I can get access to "All" column?
SELECT "ALL" from TABLENAME
this is a good reason not to use oracle reserved keywords as table or column names but by using the doublequote " " you can access them.
Keep in mind that when you use " " you must be consistent in your case in the statement. Not "All" and "all" in the same statement
Put it in quotes.
SELECT "all"
FROM TableName

Something unknown happened with a simple CREATE statement in Oracle 9i

One day, I accidently issued a CREATE statement on SQL prompt in Oracle 9i with all columns enclosing within double quotation marks as below.
CREATE TABLE emp("emp_id" VARCHAR2(6) primary key,
"emp_name" VARCHAR2(30) not null, "salary" NUMBER);
instead of issuing it as the one mentioned below without enclosing the column names within quotation marks as usual.
CREATE TABLE emp(emp_id VARCHAR2(6) primary key,
emp_name VARCHAR2(30) not null, salary NUMBER);
This simple query (first mentioned) worked just fine with no problem at all and the emp table was created successfully just then I have created three rows into this table with the following INSERT command.
INSERT INTO emp VALUES("E0001", "Henery", 50000);
INSERT INTO emp VALUES("E0002", "Alex", 65000);
INSERT INTO emp VALUES("E0003", "Peter", 70000);
Three rows created successfully into the emp table. I then executed a SELECT statement to verify whether they were created or not and found that they were indeed created.
SELECT * FROM emp;
but when I executed the SELECT statement like the one below
SELECT emp_id, emp_name, salary FROM emp;
I made sure that though, I had used the same column names as they were actually in the emp table, Oracle issued an error indicating that "Such columns don't exist."
From the SQL Language Reference:
"A quoted identifier begins and ends with double quotation marks (").
If you name a schema object using a quoted identifier, then you must
use the double quotation marks whenever you refer to that object."

Resources