CREATE TABLE ... LIKE not working - sql

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;

Related

`CREATE TABLE AS SELECT FROM` in Oracle Cloud doesn't create a new table

I was trying to create a series of tables in a single SQL query in Oracle Cloud under the ADMIN account. In the minimum script below, RAW_TABLE refers to an existing table.
CREATE TABLE BASE1 AS SELECT * FROM RAW_TABLE;
CREATE TABLE BASE2 AS SELECT * FROM BASE1;
CREATE TABLE BASE3 AS SELECT * FROM BASE2;
SELECT * FROM BASE3
This returns a view of the first 100 rows in BASE3, but it doesn't create the three tables along the way. Did I miss something or is there something peculiar about create table statements in Oracle SQL?
EDIT: The environment is Oracle Database Actions in Oracle Cloud. The three tables would not be available in the list of tables in the database, and doing something like select * from BASE3 in a subsequent query would fail.
CREATE TABLE BASE1 AS SELECT * FROM RAW_TABLE;
CREATE TABLE BASE2 AS SELECT * FROM BASE1;
CREATE TABLE BASE3 AS SELECT * FROM BASE2;
SELECT * FROM BASE3
Above is a valid query sequence for Oracle database. It should have been created three new tables in database. Since it's not happening please do the work in few steps to find out what's wrong.
First please check whether RAW_TABLE is available in database or not. Then try to select data from RAW_TABLE
select * from RAW_TABLE;
If all those are successful then try to create single table with below query:
CREATE TABLE BASE1 AS SELECT * FROM RAW_TABLE;
Hope you would find the problem by then.
DB-Fiddle:
Creating RAW_TABLE and populating data
create table RAW_TABLE (id int, name varchar(50));
insert into RAW_TABLE values (1,'A');
Query to create three more tables ans selecting from the last table:
CREATE TABLE BASE1 AS SELECT * FROM RAW_TABLE;
CREATE TABLE BASE2 AS SELECT * FROM BASE1;
CREATE TABLE BASE3 AS SELECT * FROM BASE2;
SELECT * FROM BASE3
Output:
ID
NAME
1
A
db<>fiddle here
your query fails because you are executing the whole script as one batch and each line is depends on another one , the transactional DBMS's work with blocks of code as one transaction , and that block of code doesn't commit until sql engine can parse and validate the whole block, and since in your block, BASE1 and BASE2 tables doesn't exists just yet , It fails.
so you need to run each statement as a separate batch. either by executing them one by one or in Oracle you can use / as batch separator, like in sql server you can use GO. these commands are not SQL or Oracle commands and are not sent to the database server , they are just break block of code in batches on your client ( like SQL*Plus or shell or SSMS (for Microsoft sql server), so It would look like this:
CREATE TABLE BASE1 AS SELECT * FROM RAW_TABLE;
/
CREATE TABLE BASE2 AS SELECT * FROM BASE1;
/
CREATE TABLE BASE3 AS SELECT * FROM BASE2;
/
SELECT * FROM BASE3
if your client doesn't support that then you only have to run them one by one in separate batches.

SQLite Select data where the column name contains a string and create a table with those headers only

Following from the answer to this question:
SQLite Select data where the column name contains a string?
I would like to know how to create a table with these new columns obtained from the previous table.
I wrote something like this (Please ignore the WHERE statement):
CREATE TABLE new_table
AS(
SELECT name
FROM pragma_table_info("old_table")
WHERE name NOT LIKE '%\%%' ESCAPE '\'
FROM old_table
);
however, it is not working.
Any help in this direction would be much appreciated.
Many Thanks
If you want all the columns of old_table then you can do it like this:
create table new_table as
select * from old_table
where 0;
If you want specific columns you must specify them on the select list:
create table new_table as
select col1, col2, ... from old_table
where 0;

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

dynamically create table statement in sql

how to dynamically create a table with same columns as that of previous table. in sql
select * into new_table from table where 1 = 0
select * into new_table from table
Thats works in SQL2005
I believe that both of the above answers will work, but since you don't need the data and you just need the format, I would do the following:
select * into new_table from table
TRUNCATE new_table; -- I'm sure you know this, but just in case someone is new and doesn't, truncate leaves the table structure and removes all of the data.

How can I create a table as a select from another database in Oracle?

Is it possible to create a table (in my dev db) using a SELECT from a different database?
I want something like:
create tmp_table as select * from prod_db.prod_schema.table
Is there syntax to do this, or do I need to create a database link first?
You have to create a datalink first.
Oracle cannot query other databases unless a DB link is created. If a DB link exists, as you remarked, you have to do :
create tmp_table as select * from prod_schema.table#prod_db
#Steve is correct that there has to be a DB Link, but the syntax is:
create tmp_table as select * from table#dblink
Don't forget to create your indexes. You can get this for all the tables in your schema with a query like this:
SELECT DBMS_METADATA.GET_DDL('INDEX',u.index_name)
FROM USER_INDEXES u;
CREATE TABLE table_name
AS SELECT * FROM schema_name.table_name;