how to create a duplicate table with no data in it - sql

how to create a duplicate table with no data in it
old tablename--student
newtablename--student1
plz tell me the exat query
thanks,
Churchill..

SELECT TOP 0 * INTO student1 FROM student

select * into student1 from student where 1=0

First you can copy the whole table by:
select * into (your_table_name) from student
After copying the whole table, you delete the data by running:
truncate table (your_table_name);

create table student1 as select * from student;
In the above query student1 is the table which has the exact copy of the already existing table student which copies the entire data.
create table student1 as select * from student where 0=1;
The query copies only the columns present in the student table but no data will be copied
But please note, you can't copy the constraints and indexes

select *
into student1
from student
where 1=2
This will get you the columns,
but indexes and other objects
will require scripting
with a database tool of some sort.

create table newtable as select * from oldtable where clause

In Sql Server you can right click on the table name in the Object Explorer.
Select "Script Table as" > "CREATE To" and then select "New Query Editor Window"
This creates a query to create the table.
In this case table name [student] change this to [student1] in the query and run and you have an exact copy of the schema, indexes, vartypes, primary keys etc.

Related

Create clone copy of table without data in SQL Server

How can I create clone copy of table without data? Because I just want to copy a definition of table not data.
I tried below but it is copying data as well
Select *
into Clone_Supplier
from Supplier
Copy all columns from selected table
Select Top 0 * into NewTable from OldTable
Copy some columns from selected table
Select Top 0 Col1,Col2 into NewTable from OldTable
Copy all(Data and Structure)
Select * into NewTable from OldTable
You could add WHERE 1=2 to get structure only:
Select *
into Clone_Supplier
from Supplier
where 1=2;
It won't be exact copy though:
no constraints
no indexes
no triggers
It is useful table to generate and new table
Select Top 0 * into tblNew from tblOld
Solution Query :
SELECT TOP 0 * INTO newTable1 FROM oldTable1;
You just need to add one false condition. So that it wont return any data and you will create clone copy of table without data. you can use below query
Select * into Clone_Supplier from Supplier WHERE 1=2
Create Table new_table LIKE old_table
Will create an empty copy of original table with original attributes etc.

SQLite create table from table

I need to create a copy of the table in SQLite. Result table should have only one difference. One of the rows called 'Desired_row' must have different type(int -> bigint).
If I knew full source table structure I could just create a new table with identical fields.
CREATE TABLE MyTableCopy(... , )
As I have different versions of the table, I do not exactly know its full structure to create a new one with hardcoded columns. I need something which will do something like this CREATE TABLE Destination AS SELECT * FROM Source, but will not create Desired_row in the Destination table.
Any ideas? Thanks in advance!
with
SELECT sql FROM sqlite_master WHERE type='table' AND name='mytable'
you can get the the structure. This you can modify and create your new table. And finally you can
INSERT INTO 'MyTableCopy' (*) SELECT * FROM 'mytable'
In python you can clone/copy a sqlite table named "mytable" to a new table "mynewtable" using:
conn = sqlite3.connect(file)
cur = conn.cursor()
cur.execute("SELECT sql FROM sqlite_master WHERE type='table' AND name='mytable'")
txt=(cur.fetchone()[0])
txt=txt.replace("mytable","mynewtable")
cur.execute(txt)
cur.execute("INSERT INTO mynewtable SELECT * FROM mytable")
i believe this method will work similarly in other languages by: 1) getting the create mytable statement from the sqlite_master table, 2) changing only the table name, 3)execute the new create mynewtable statement and finally 4)populate the new table by inserting all data from old table

Temporary tables and AS statement usage

If I have a query, say:
SELECT *
FROM ( SELECT fields
FROM tables
WHERE condition
)
AS TEMP_TABLE
are the results of the above query saved in a temporary table called TEMP_TABLE so as I can perform another query on it later? Will the query below be executed successfully when using DB2?
SELECT fields
FROM TEMP_TABLE
WHERE condition
The answer is NO, it's just an alias for a subquery.
If you want to use it later, you have to explicitly create it.
you can create temporary table in following way.
CREATE TEMPORARY TABLE temp_table AS (
SELECT fields
FROM tables
WHERE condition
);
then you can retrieve data from the temp table as below.
SELECT * FROM temp_table

"select * into table" Will it work for inserting data into existing table

I am trying to insert data from one of my existing table into another existing table.
Is it possible to insert data into any existing table using select * into query.
I think it can be done using union but in that case i need to record all data of my existing table into temporary table, then drop that table and finally than apply union to insert all records into same table
eg.
select * into #tblExisting from tblExisting
drop table tblExisting
select * into tblExisting from #tblExisting union tblActualData
Here tblExisting is the table where I actually want to store all data
tblActualData is the table from where data is to be appended to tblExisting.
Is it right method.
Do we have some other alternative ?
You should try
INSERT INTO ExistingTable (Columns,..)
SELECT Columns,...
FROM OtherTable
Have a look at INSERT
and SQL SERVER – Insert Data From One Table to Another Table – INSERT INTO SELECT – SELECT INTO TABLE
No, you cannot use SELECT INTO to insert data into an existing table.
The documentation makes this very clear:
SELECT…INTO creates a new table in the default filegroup and inserts the resulting rows from the query into it.
You generally want to avoid using SELECT INTO in production because it gives you very little control over how the table is created, and can lead to all sorts of nasty locking and other performance problems. You should create schemas explicitly and use INSERT - even for temporary tables.
#Ryan Chase
Can you do this by selecting all columns using *?
Yes!
INSERT INTO yourtable2
SELECT * FROM yourtable1
Update from CTE? http://www.sqlservercentral.com/Forums/Topic629743-338-1.aspx

How can I create a copy of an Oracle table without copying the data?

I know the statement:
create table xyz_new as select * from xyz;
Which copies the structure and the data, but what if I just want the structure?
Just use a where clause that won't select any rows:
create table xyz_new as select * from xyz where 1=0;
Limitations
The following things will not be copied to the new table:
sequences
triggers
indexes
some constraints may not be copied
materialized view logs
This also does not handle partitions
I used the method that you accepted a lot, but as someone pointed out it doesn't duplicate constraints (except for NOT NULL, I think).
A more advanced method if you want to duplicate the full structure is:
SET LONG 5000
SELECT dbms_metadata.get_ddl( 'TABLE', 'MY_TABLE_NAME' ) FROM DUAL;
This will give you the full create statement text which you can modify as you wish for creating the new table. You would have to change the names of the table and all constraints of course.
(You could also do this in older versions using EXP/IMP, but it's much easier now.)
Edited to add
If the table you are after is in a different schema:
SELECT dbms_metadata.get_ddl( 'TABLE', 'MY_TABLE_NAME', 'OTHER_SCHEMA_NAME' ) FROM DUAL;
create table xyz_new as select * from xyz where rownum = -1;
To avoid iterate again and again and insert nothing based on the condition where 1=2
Using sql developer select the table and click on the DDL tab
You can use that code to create a new table with no data when you run it in a sql worksheet
sqldeveloper is a free to use app from oracle.
If the table has sequences or triggers the ddl will sometimes generate those for you too. You just have to be careful what order you make them in and know when to turn the triggers on or off.
You can do this
Create table New_table as select * from Old_table where 1=2 ;
but be careful
The table you create does not have any Index, PK and so on like the old_table.
DECLARE
l_ddl VARCHAR2 (32767);
BEGIN
l_ddl := REPLACE (
REPLACE (
DBMS_LOB.SUBSTR (DBMS_METADATA.get_ddl ('TABLE', 'ACTIVITY_LOG', 'OLDSCHEMA'))
, q'["OLDSCHEMA"]'
, q'["NEWSCHEMA"]'
)
, q'["OLDTABLSPACE"]'
, q'["NEWTABLESPACE"]'
);
EXECUTE IMMEDIATE l_ddl;
END;
Simply write a query like:
create table new_table as select * from old_table where 1=2;
where new_table is the name of the new table that you want to create and old_table is the name of the existing table whose structure you want to copy, this will copy only structure.
SELECT * INTO newtable
FROM oldtable
WHERE 1 = 0;
Create a new, empty table using the schema of another. Just add a WHERE clause that causes the query to return no data:
WHERE 1 = 0 or similar false conditions work, but I dislike how they look. Marginally cleaner code for Oracle 12c+ IMHO is
CREATE TABLE bar AS
SELECT *
FROM foo
FETCH FIRST 0 ROWS ONLY;
Same limitations apply: only column definitions and their nullability are copied into a new table.
If one needs to create a table (with an empty structure) just to EXCHANGE PARTITION, it is best to use the "..FOR EXCHANGE.." clause. It's available only from Oracle version 12.2 onwards though.
CREATE TABLE t1_temp FOR EXCHANGE WITH TABLE t1;
This addresses 'ORA-14097' during the 'exchange partition' seamlessly if table structures are not exactly copied by normal CTAS operation. I have seen Oracle missing some of the "DEFAULT" column and "HIDDEN" columns definitions from the original table.
ORA-14097: column type or size mismatch in ALTER TABLE EXCHANGE
PARTITION
See this for further read...
you can also do a
create table abc_new as select * from abc;
then truncate the table abc_new. Hope this will suffice your requirement.
Using pl/sql developer you can right click on the table_name either in the sql workspace or in the object explorer, than click on "view" and than click "view sql" which generates the sql script to create the table along with all the constraints, indexes, partitions etc..
Next you run the script using the new_table_name
copy without table data
create table <target_table> as select * from <source_table> where 1=2;
copy with table data
create table <target_table> as select * from <source_table>;
In other way you can get ddl of table creation from command listed below, and execute the creation.
SELECT DBMS_METADATA.GET_DDL('TYPE','OBJECT_NAME','DATA_BASE_USER') TEXT FROM DUAL
TYPE is TABLE,PROCEDURE etc.
With this command you can get majority of ddl from database objects.
Create table target_table
As
Select *
from source_table
where 1=2;
Source_table is the table u wanna copy the structure of.
create table xyz_new as select * from xyz;
-- This will create table and copy all data.
delete from xyz_new;
-- This will have same table structure but all data copied will be deleted.
If you want to overcome the limitations specified by answer:
How can I create a copy of an Oracle table without copying the data?
The task above can be completed in two simple steps.
STEP 1:
CREATE table new_table_name AS(Select * from old_table_name);
The query above creates a duplicate of a table (with contents as well).
To get the structure, delete the contents of the table using.
STEP 2:
DELETE * FROM new_table_name.
Hope this solves your problem. And thanks to the earlier posts. Gave me a lot of insight.