Lets consider a simple table say products
in Oracle (I tried on Oracle 9i). I'm creating this table with the following CREATE statement.
CREATE TABLE products
("prod_id" varchar2(7) primary key, "product_name" varchar2(30) NOT NULL);
It is to be noted specially that I'm enclosing the column names within double quotation marks ""
as not usually we do. It would obviously work and the products
table would be created with those two columns with the specified CONSTRAINTS.
Now, lets insert some rows into this table using the following INSERT INTO command.
INSERT INTO products VALUES('P0001', 'Nokia-N97');
INSERT INTO products VALUES('P0002', 'Nokia-1208');
INSERT INTO products VALUES('P0003', 'Nokia-1115');
Would insert three rows into the products
table.
To make sure that these rows have indeed inserted or not, we can issue a SELECT statement as follows.
SELECT * FROM products;
Would work just fine and display three rows we inserted.
Now, the actual question here. When we issue the following SELECT statement,
SELECT prod_id, product_name FROM products;
would not work even though we didn't make any mistake in this SQL. Oracle would report instead that such columns don't exist. Why does this happen? There must be very specific reason behind it, I think.
I'm sure that enclosing column names unnecessarily within double quotation marks as I have just done may not be the best practice but just a question occurred to me.
Double quotes in Oracle Double-quotes are used to enclose identifiers like table name/table alias or column name/column alias. They are rarely used when the name doesn't need to conform to Oracle Database Object Naming Rules. Using double-quotes to represent identifiers is not recommended.
Double quotes are used to indicate identifiers within the database, which are objects like tables, column names, and roles. In contrast, single quotes are used to indicate string literals.
If you want a single quote to appear in the middle of a string add another single quote to it. If you want a single quote to appear at the beginning or end of a string add 2 single quotes to it. If you want a single quote to appear on its own add 3 single quotes to it.
Against common believe, Oracle is case sensitive in column and table names. It just converts everything to upper case by default.
But if you use names in double quotes, you tell Oracle to create the column in the exact spelling you provided (lower case in the CREATE
statement).
Since in the SELECT
statement, you don't use quotes, the names are converted to upper case and are therefore not equal to the lower case names in the CREATE
statement.
So this should work:
SELECT "prod_id", "product_name" FROM products;
If you don't know how column or table names are specified, you can look it up in the data dictionary. You will find lower case column names for your product
table, but upper case table name, since it wasn't quoted.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With