Create table from select in Oracle - sql

I would like to do something like this:
create table NEW_TABLE (select * from OLD_TABLE where id=3582)
And have a new table created from the resulting columns of that select statement.
I bet its possible in mySQL, but how to do it in Oracle?

Try this:
CREATE TABLE new_table AS select * from OLD_TABLE where id=3582

Related

Does Create Table as Select preserves columns order in Oracle?

I'm using following statement to create a duplicate table with data. But i'm curious to know whether this statement preserves the column order of the old_table in the new_table as it is. Since i need to compare some column data of 2 tables later using MINUS statement.
CREATE TABLE new_table
AS
SELECT *
FROM old_table
Below is the minus statement used to compare. In the following statement cols are queried from ALL_TAB_COLUMNS meta data table.
SELECT COUNT(*) FROM (SELECT cols FROM old_table MINUS (SELECT cols FROM new_table))
Below is the meta data query to get column list in the order.
SELECT COLUMN_NAME FROM ALL_TAB_COLUMNS WHERE TABLE_NAME = 'new_table'
SELECT COLUMN_NAME FROM ALL_TAB_COLUMNS WHERE TABLE_NAME = 'old_table'
My question, Is column order preserved in both new_table and old_table when copied using CREATE TABLE new_table AS SELECT statement mentioned above?
Yes, The New table will have columns exactly in same order as your select query.

Insert into table using select plus value?

I created a table modeled on an existing table:
CREATE TABLE new_table AS
(SELECT * FROM old_table WHERE record_status = 'xyz' ) WITH DATA;
I then added a column:
ALTER TABLE new_table ADD new_column VARCHAR(100);
I now want to select more rows from the old table into the new table, but add a fixed value (eg. 'blabla') to the new column for each row selected. I tried a bunch of things, such as:
INSERT INTO new_table
SELECT *, 'blabla'
FROM old_table)
WHERE is_blabla = 'Y'
Tried every permutation I could think of but keep getting syntax errors. Any suggestions? I'm using Teradata.
Dave
INSERT INTO new_table
SELECT old_table.*,'your_fixed_value'
FROM old_table
WHERE is_blabla='Y'
You just need to alias your table
INSERT INTO new_table
SELECT a.*,'your_fixed_value'
FROM old_table a
WHERE is_blabla='Y'

Create additional columns when using "Create Table As"

I am creating a table using the CREATE TABLE AS Statement. Script looks like this :
CREATE TABLE table2
AS
SELECT column1, column2, ..., columnN
FROM table1
WHERE ROWNUM <= 50;
My question is, can I create additional columns on table2 that don't exist in table1 inside the CREATE TABLE AS Statement or do I have to resort to ALTER afterwards?
EDIT: For example table1 contains ID, FULLNAME, STATUS and I wanna add a column somewhere in between called AGE
I am using Oracle Sql
You can add whatever you'd like to the SELECT
CREATE TABLE table2
AS
SELECT column1, column2, ..., columnN,
trunc(months_between(birth_date,sysdate)/12) age,
'Some string' another_column
FROM table1
WHERE ROWNUM <= 50;

duplicate rows using insert into

I have a table that contains data and I want to use insert into statement
here is my query
INSERT INTO [table]
SELECT Distinct * FROM [DataSource]
how to to ignore a row from being inserted if it's already found in the table?
Assuming your target table and your data source table have identical structure, you could do something like this:
INSERT INTO [table]
SELECT Distinct *
FROM [DataSource]
EXCEPT
SELECT *
FROM [table]
Alternatively, you could use MERGE statement or SELECT ... WHERE NOT EXISTS.

sql select into columns to table and columns

I have a source table that is NewID|Fruit|Apples and I need to insert those rows into target table OldID|Fruit|Apples
Is there a way to select into a new table while changing columns?
Thanks!
Try:
CREATE TABLE new_table SELECT NewID as OldID, Fruit, Apples FROM old_table
Assuming target table already exists and you want to copy all the columns
INSERT INTO target_table(OldID,Fruit,Apples)
SELECT NewID,Fruit,Apples FROM source_table