The different between TABLE_NAME vs "TABLE_NAME" in Oracle? - oracle

I'm a very new Oracle SQL user and I got a problem.
If I create a table with code such as
create table TBL_NAME...
after that I can get data by
select * from TBL_NAME;
However, when I create table with Navicat (just click on button new table), then I have to add "" to table name to access my table, such as:
select * from "TBL_NAME";
So, is there have other type of table? And if I use Navicat to create table, what type of it?

When you specify the name of a table in Oracle without double quotes then Oracle converts that table name to uppercase. But if you specify the name of the table within double-quotes then Oracle will respect the lower-case letters that you may have.
So, as in your example the name of your table is already all upper-case, then there is no difference in specifying or not the double-quotes.
But for example if you create a table like this:
CREATE TABLE "my_table" ....
Then you cannot access it like this:
SELECT * FROM my_table;
as Oracle will convert that select to this:
SELECT * FROM MY_TABLE;
And there is no such table in your system.
In your case with Navicat, it just needs you to specify the name of the table as-is, but don't worry, just put the double-quotes and stick to all upper-case names and you will be fine.

Related

Oracle does not allow to create "USER" table

Oracle is not allowing to create USER table.
Can anyone guide me to create USER table in Oracle.?
TIA.
Can you use a different name like my_user or something else. If you are insistent about using the table name user then you will have to provide the table name in quotes.
CREATE TABLE "USER"
(
col1 NUMBER(10)
)
You, will have to use quotes and maintain the upper case when doing any operations on this table.
The following will give you an error.
select * from USER;
ORA-00903: invalid table name
However, the following will work.
select * from "USER";
That said I don't recommend this option and it would be good if you can change your table name.
USER is a reserved keyword in oracle. Thus it can't be used directly.
Here is the list of restricted keywords a.k.a reserved words.
e.g. you can't either create a table called TABLE...

Oracle DatabaseMetadate get columns for table named USER

We are using JDBC DatabaseMetadata to discover objects in the database. DatabaseMetadata.getColumns returns more verbose information -that we really need- than those from select * from table where 1 = 2.
Now when I pass the table name -that is a reserved word in Oracle- to getColumns, it fails. If I escape the table name, it does not get results -as it doesn't see a table with that name (i.e. pass the table name "USER" instead of USER).
Is there a possible way to pass such table -named after reserved words to getColumns for Oracle database.

SQLDeveloper Trigger Error report - ORA-00942: table or view does not exist

I put this code into SQL Developer's Worksheet:
CREATE TRIGGER T_testDSNa
before INSERT
on testDSNa
referencing new as new
for each ROW
BEGIN
SELECT S_testDSN.nextval INTO :NEW.SYSID FROM dual;
END;
I get this:
Error report -
ORA-00942: table or view does not exist
00942. 00000 - "table or view does not exist"
*Cause:
*Action:
Would anyone know why? This has worked for 3 previous tables until I tried to run the DDL to create a 4th. Alternatively, is there a better way to set up an autoincrementing PK?
The problem was lack of schema. Oracle Definition of a schema :
Collection of database objects, including logical structures such as
tables, views, sequences, stored procedures, synonyms, indexes,
clusters, and database links. A schema has the name of the user who
controls it.
If you want to know the objects accessible without alias. You have to look on [USER_OBJECTS]. Which describes the relational objects owned by the current user :
SELECT
OBJECT_NAME
, OBJECT_TYPE
, LAST_DDL_TIME
FROM USER_OBJECTS;
If you want to know the objects accessible to the current user :
SELECT
OWNER
, OBJECT_NAME
, OBJECT_TYPE
, LAST_DDL_TIME
FROM ALL_OBJECTS;
In your case to see your objects in the list of available tables you need:
SELECT * FROM ALL_OBJECTS WHERE OWNER = 'USER';
You can also alter the session to avoid alias :
ALTER SESSION SET current_schema = User;
For priviliges/ roles views you can look at :
SELECT * FROM USER_SYS_PRIVS;
SELECT * FROM USER_ROLE_PRIVS;
The last method but not the most secure to avoid alias. Is to log on with a user that has the same name as the schema.
Hoping that it can help
I was getting the same issue.
Solution: What I observed that my table which I created was surrounded by double quotes, which made it case sensitive.
So for each time I refer to my table, I need to surround it by double quotes.
CREATE TRIGGER T_testDSNa
before INSERT
on "testDSNa"
referencing new as new
for each ROW
BEGIN
SELECT S_testDSN.nextval INTO :NEW.SYSID FROM dual;
END;
refer this link: What exactly do quotation marks around the table name do?

How to create a table identical to other table in structure and constraints in Oracle?

I want to create a table (lets say table_copy) which has same columns as other table (lets call it table_original) in Oracle database, so the query will be like this :
create table table_copy as (select * from table_original where 1=0);
This will create a table, but the constraints of table_original are not copied to table_copy, so what should be done in this case?
Only NOT NULL constraints are copied using Create Table As Syntax (CTAS). Others should be created manually.
You might however query data dictionary view to see the definitions of constraints and implement them on your new table using PL/SQL.
The other tool that might be helpful is Oracle Data Pump. You could import the table using REMAP_TABLE option specifying the name for the new table.
Use a database tool to extract the DDL needed for the constraints (SQL Developer does the job). Edit the resulting script to match the name of the new class.
Execute the script.
If you need to do this programmatically you can use a statement like this:
DBMS_METADATA.GET_DDL('TABLE','PERSON') from DUAL;

How to swap table name in oracle

I have 2 oracle tables that are identical in definition just different in partition definition. I want to test one table vs another table design. Is there a way to swap the table name? I don't want to drop a table because they are big and takes a long time to load data into them.
Use a synonym that points to the real tables.
For example,
CREATE OR REPLACE SYNONYM partition_test FOR partition_table1;
Test partition_table1, e.g. select pt.* from partition_test pt;
CREATE OR REPLACE SYNONYM partition_test FOR partition_table2;
Test partition_table2, e.g. select pt.* from partition_test pt;
Notice the test code is then same each time.
When you're done testing, drop the synonym.
DROP SYNONYM partition_test;
Just rename them. For example, if you have TABLE_A, rename it to TABLE_A_TEMP. Then rename TABLE_B to TABLE_A. Then rename TABLE_A_TEMP to TABLE_B.
To rename, you'll have to issue
alter table table_name rename to new_table_name;
A third method would be to use a view. Let's say your "real" table names are TABLE_A and TABLE_B. Create a view, MY_DATA_VIEW (or whatever), and have it point to whichever table you want it to point to:
CREATE OR REPLACE VIEW TEST_VIEW AS SELECT * FROM TABLE_A;
or
CREATE OR REPLACE VIEW TEST_VIEW AS SELECT * FROM TABLE_B;
Share and enjoy.

Resources