如何使用创建或替换?
我对 CREATE OR REPLACE 的理解是否正确?基本上意味着“如果对象存在,则删除它,然后以任何一种方式创建它?”
如果是这样,我做错了什么? 这有效:
CREATE TABLE foo (id NUMBER,
title VARCHAR2(4000) DEFAULT 'Default Title')
而这不起作用(ORA-00922:缺少或无效选项):
CREATE OR REPLACE TABLE foo (id NUMBER,
title VARCHAR2(4000) DEFAULT 'Default Title')
我在做一些愚蠢的事情吗? 我似乎无法找到有关此语法的太多文档。
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.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(14)
这适用于函数、过程、包、类型、同义词、触发器和视图。
更新:
第三次更新帖子后,我将重新表述:
是的,有 关于此语法的文档,并且
CREATE TABLE
没有REPLACE
选项。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
.该语法的好处之一是,您可以确保
CREATE OR REPLACE
永远不会导致您丢失数据(您丢失的最多的是代码,希望您将其存储在源代码控制某处)。表的等效语法是 ALTER,这意味着您必须显式枚举所需的确切更改。
编辑:
顺便说一句,如果您需要在脚本中执行 DROP + CREATE,并且您不关心虚假的“对象不存在”错误(当 DROP 找不到表时),您可以这样做:
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:
Oracle 中没有创建或替换表。
你必须:
There is no create or replace table in Oracle.
You must:
CREATE OR REPLACE
只能用于函数、过程、类型、视图或包 - 它不适用于表。CREATE OR REPLACE
can only be used on functions, procedures, types, views, or packages - it will not work on tables.以下脚本应该可以在 Oracle 上实现这一目的:
Following script should do the trick on Oracle:
不适用于表格,仅适用于函数等。
这是一个包含一些示例的网站。
Does not work with Tables, only functions etc.
Here is a site with some examples.
对于 Oracle 数据库来说,一个有用的过程,无需使用例外(在某些情况下,您必须将 user_tables 替换为 dba_tables 和/或限制查询中的表空间):
A usefull procedure for oracle databases without using exeptions (under circumstances you have to replace user_tables with dba_tables and/or constrain the tablespace in the query):
如果您在代码中执行操作,请首先检查数据库中的表
通过使用查询
选择表名
来自用户表
WHERE table_name = 'XYZ'
如果找到记录,则截断表,否则创建表
工作,如创建或替换。
If you are doing in code then first check for table in database
by using query
SELECT table_name
FROM user_tables
WHERE table_name = 'XYZ'
if record found then truncate table otherwise create Table
Work like Create or Replace.
您可以使用 CORT (www.softcraftltd.co.uk/cort)。 该工具允许在 Oracle 中创建或替换表。
它看起来像:
它保存数据。
You can use CORT (www.softcraftltd.co.uk/cort). This tool allows to CREATE OR REPLACE table in Oracle.
It looks like:
It preserves data.
所以我一直在使用它并且它运行得很好: - 它的工作方式更像是 DROP IF EXISTS 但完成了工作
希望这会有所帮助
另参考:
PL/SQL Developer 中出现 PLS-00103 错误
So I've been using this and it has worked very well: - it works more like a DROP IF EXISTS but gets the job done
Hope this helps
Also reference:
PLS-00103 Error in PL/SQL Developer
“创建或替换表”是不可能的。 正如其他人所说,您可以编写一个过程和/或使用立即开始执行(...)。 因为我没有看到如何(重新)创建表的答案,所以我添加了一个脚本作为答案。
PS:正如 jeffrey-kemp 提到的那样:下面的脚本不会保存您要删除的表中已存在的数据。 由于存在丢失数据的风险,我们公司只允许更改生产环境上现有的表,不允许删除表。 通过使用 drop table 语句,迟早你会让公司警察站在你的办公桌前。
'Create or replace table' is not possible. As others stated, you can write a procedure and/or use begin execute immediately (...). Because I don't see an answer with how to (re)create the table, I putted a script as an answer.
PS: in line of what jeffrey-kemp mentioned: this beneath script will NOT save data that is already present in the table you are going to drop. Because of the risk of loosing data, at our company it is only allowed to alter existing tables on the production environment, and it is not allowed to drop tables. By using the drop table statement, sooner or later you will get the company police standing at your desk.
我会做这样的事情
I would do something like this
如果这是针对 MS SQL.. 无论表是否存在,以下代码都将始终运行。
If this is for MS SQL.. The following code will always run no matter what if the table exist already or not.