Am I correct in understanding that CREATE OR REPLACE basically means "if the object exists, drop it, then create it either way?"
If so, what am I doing wrong? This works:
CREATE TABLE foo (id NUMBER,
title VARCHAR2(4000) DEFAULT 'Default Title')
And this doesn't (ORA-00922: missing or invalid option):
CREATE OR REPLACE TABLE foo (id NUMBER,
title VARCHAR2(4000) DEFAULT 'Default Title')
Am I doing something stupid? I don't seem to be able to find much documentation about this syntax.
Following script should do the trick on Oracle:
CREATE OR REPLACE
can only be used on functions, procedures, types, views, or packages - it will not work on tables.One of the nice things about the syntax is that you can be sure that a
CREATE OR REPLACE
will never cause you to lose data (the most you will lose is code, which hopefully you'll have stored in source control somewhere).The equivalent syntax for tables is ALTER, which means you have to explicitly enumerate the exact changes that are required.
EDIT: By the way, if you need to do a DROP + CREATE in a script, and you don't care for the spurious "object does not exist" errors (when the DROP doesn't find the table), you can do this:
Does not work with Tables, only functions etc.
Here is a site with some examples.
This works on functions, procedures, packages, types, synonyms, trigger and views.
Update:
After updating the post for the third time, I'll reformulate this:
And yes, there is documentation on this syntax, and there are no
REPLACE
option forCREATE TABLE
.