Do you need the same table structure as an existing table in your database, but empty of rows? Oracle SQL’s CREATE TABLE AS statement achieves this easily. Continue reading and learn how with a simple example…
If you enjoy the content written here, by all means, share this blog and your favorite post(s) with others who may benefit from or like it as well. Since coffee is my favorite drink, you can even buy me one if you would like!
I have this FRIENDS table that I want to duplicate:
However, the FRIENDS table has 7 rows of data and I want the clone table to be empty.
The Newsletter for PHP and MySQL Developers
Receive a copy of my ebook, “10 MySQL Tips For Everyone”, absolutely free when you subscribe to the OpenLampTech newsletter.
CREATE TABLE AS
Using SELECT * – or explicitly listing the individual columns you need – in a CREATE TABLE AS command with a WHERE clause predicate that evaluates to FALSE, creates a table based on the FROM clause table of the SELECT statement, but will not fill it with rows:
(SELECT * FROM FRIENDS
WHERE 1 = 2);
Viewing the newly created SAME_FRIENDS table description, we can see it has the same structure as the FRIENDS table it is based on:
Are you a Medium member? If so, receive an email notification each time I publish a blog post if you prefer the Medium platform. Not a member? No worries! Use my sign-up link (I will get a commission at no extra cost to you) and join. I really enjoy reading all the great content there and I know you will too!!!
Querying the SAME_FRIENDS table shows there are no rows present in the table:
Consider making a donation as I continue to provide valuable content here on my blog. Thank you so much!!! Every bit is much appreciated and helps tremendously!
This is a quick and simple way you can essentially duplicate a table’s exact structure, without populating it with any rows.
As always, if you have any questions or see any mistakes in the code, please let me know via the comments. Constructive comments help me provide accurate blog posts and are much appreciated.
Like what you have read? See anything incorrect? Please comment below and thank you for reading!!!
A Call To Action!
Thank you for taking the time to read this post. I truly hope you discovered something interesting and enlightening. Please share your findings here, with someone else you know who would get the same value out of it as well.
Visit the Portfolio-Projects page to see blog posts/technical writing I have completed for clients.
Be sure and visit the “Best Of” page for a collection of my best blog posts.
Josh Otwell has a passion to study and grow as a SQL Developer and blogger. Other favorite activities find him with his nose buried in a good book, article, or the Linux command line. Among those, he shares a love of tabletop RPG games, reading fantasy novels, and spending time with his wife and two daughters.
Disclaimer: The examples presented in this post are hypothetical ideas of how to achieve similar types of results. They are not the utmost best solution(s). The majority, if not all, of the examples provided, are performed on a personal development/learning workstation environment and should not be considered production quality or ready. Your particular goals and needs may vary. Use those practices that best benefit your needs and goals. Opinions are my own.