Duplicating a table in dashDB - sql

I want to create a duplicate table in dashDB (with or without the original data, it does not really matter, the table structure is what is important).
I tried:
SELECT * INTO new_table
FROM old_table;
but I get this error:
"new_table" is not valid in the context where it is used.. SQLCODE=-206, SQLSTATE=42703, DRIVER=3.69.56
I also tried:
CREATE TABLE new_table AS
(SELECT * FROM old_table);
but I get this error:
An unexpected token "END-OF-STATEMENT" was found following "AS (SELECT * FROM old_table". Expected tokens may include: "WITH DATA, WITH NO DATA".. SQLCODE=-104, SQLSTATE=42601, DRIVER=3.69.56

I found an answer here:
Create table (structure) from existing table
seems this did the trick
CREATE TABLE new_table LIKE old_table;

Just do this. This will work and create an exact copy of the table including data:
CREATE TABLE NEW_TAB AS (SELECT * FROM EXISTING_TAB) WITH DATA;

Related

CREATE TABLE ... LIKE not working

Need help trying to create a table using CREATE TABLE ... LIKE, code is below, error given is
Incorrect syntax near the keyword 'LIKE'
CREATE TABLE jobserve_reports LIKE Jobserve
I've tried putting the LIKE in quotations with no luck, tried making it a temporary table, using curly braces and nothing has worked. Am at my wits end.
In SQL Server, we can create using:
SELECT *
INTO newtable
FROM oldtable
WHERE 1 = 0;
If LIKE is not available in your db you could use create select
CREATE TABLE jobserve_reports AS
select * from Jobserve
or equivalent
eventuallly using
CREATE TABLE jobserve_reports AS
select * from Jobserve
where 1 = 2
for get no result
For Postgres, MS sql-server and Oracle use the AS keyword:
CREATE TABLE new_table
AS SELECT * FROM old_table;
In MySQL you can use the AS keyword as well, but it is optional:
CREATE TABLE new_table
AS SELECT * FROM old_table;
or
CREATE TABLE new_table
SELECT * FROM old_table;

Create table (structure and data) from existing table in oracle

I want to create (and fill) a table similar to an existing table in Oracle (PL-SQL).
I used this code (extracted from this question):
Select * into NewTable from OldTable
but this error occurred:
ORA-00905: missing keyword
I think this code works only in SQL-Server. so how can I handle it in Oracle?
CREATE TABLE NewTable AS
SELECT * FROM OldTable

CREATE TABLE AS SELECT

I am trying a simple CREATE TABLE AS SELECT in Impala 2.3.0 and it doesn't seem to work.
CREATE TABLE clone_of_t1 AS SELECT * FROM t1;
Query: create TABLE clone_of_t1 AS SELECT * FROM t1 ERROR:
AnalysisException: testdb CAUSED BY: NoSuchObjectException: testdb
Are there any special options required for this to work?
Thanks.
It looks like the database testdb doesn't exist. You could look your JDBC url to check if the database name is correct, it should be similar to this:
jdbc:impala://{host}:{port}/{database}

How to copy a table schema with sql based on existance of the table?

I want to duplicate a table schema, but only if the target table does not yet exist:
CREATE TABLE IF NOT EXISTS my_table_secondary as select * from my_table_primary
Problem: running this if the my_secondary_table exists results in:
ERROR: Syntaxerror at "as".
What could I do to make this sql statement work?
(postgres 9.4)
To duplicate the schema of a table, you need a completely different syntax.
create table if not exists my_table_secondary (
like my_table_primary including all
);
INCLUDING ALL is an abbreviated form of INCLUDING DEFAULTS INCLUDING
CONSTRAINTS INCLUDING INDEXES INCLUDING STORAGE INCLUDING COMMENTS.
It doesn't duplicate foreign key constraints. See CREATE TABLE. Search for "The LIKE clause".
Try this approach:
CREATE OR REPLACE VIEW my_view AS SELECT * FROM my_table_primary;
CREATE TABLE IF NOT EXISTS my_table_secondary LIKE my_view;
INSERT INTO my_table_secondary
SELECT * FROM my_view
WHERE NOT EXISTS ( SELECT * FROM my_table_secondary );
More on CREATE TABLE ... LIKE: http://www.postgresql.org/docs/9.1/static/sql-createtable.html

Oracle - Zombie Table

I'm having this odd problem since yesterday. I've tried several options and I actually reinstalled ORACLE and the DB itself.
Here's the problem: I have this table that is somekind of zombie. Here are the symptoms:
SELECT TABLE_NAME FROM USER_TABLES WHERE TABLE_NAME='MYTABLE'
Returns a record, meaning that the table exists.
SELECT COLUMN_NAME FROM USER_TAB_COLUMNS WHERE TABLE_NAME = 'MYTABLE'
Returns all the columns of MYTABLE. So far so good, the table exists.
SELECT * FROM MYTABLE
Returns ORA-00942: table or view does not exist.
At this point I'm quite confused: the table seems to exist on the USERTABLES but I cannot SELECT over it?
CREATE TABLE MYTABLE (Foo NUMBER) TABLESPACE MYTABLESPACE
Returns:
ORA-00604: error occurred at recursive SQL level 1
ORA-00001: unique constraint (SYS.I_OBJ2) violated
I do not understand this error. But the best is yet to come.
SELECT * FROM MYTABLE
Surprisingly, the above query (an exact copy of the 3rd query) returns several records now!
Moreover, I noticed that the column Foo is not present: the table I now see is my initial table that had other columns.
DROP TABLE MYTABLE
I now try to drop the table and I get the following errors:
ORA-00604: error occurred at recursive SQL level 1
ORA-00942: table or view does not exist
ORA-06512: at line 19
SELECT * FROM MYTABLE
More confused than ever, I try the above query and, surprise surprise, the table no longer exists.
I don't undestand this: the table is on USERTABLES but I cannot SELECT over it, however, if I create a new table with the same name, I get an error but now I can SELECT over the previous version of that table with several records.
Any thoughts ? I really need your help :(
EDIT - I checked now: I'm unable to drop ANY table. This might just be a new symptom.
Solution
The problem was that MDSYS.SDO_GEOR_SYSDATA_TABLE table was missing and a drop event trigger was trying to access it, generating the error. The solution was restoring that table.
If have privileges, try this query:
SELECT *
FROM dba_objects
WHERE object_name = 'MYTABLE';
And see what objects exist with that name. It might point you in the right direction.
You didn't qualify the schema names when trying to select and drop. The CURRENT_SCHEMA of your session may be different form the log-on user. Check by trying
select SYS_CONTEXT('USERENV', 'CURRENT_SCHEMA') from dual;
Instead of describing what the output was, could you please copy/paste the complete output for us?
Lastly, can you exclude that someone messed up the dictionary? You know, SYSDBA can do anything....