简体   繁体   English

为什么在表创建期间不添加DB约束

[英]Why DB constraints are not added during table creation

What is the difference between these to ways of table creation. 这些与表创建方式有什么区别。

CREATE TABLE TABLENAME(
field1....
field2...

add constraint constraint1;
add constraint constraint2;
)

AND

CREATE TABLE TABLENAME(
    field1....
    field2...
    )

ALTER TABLE TABLENAME add constaint1
ALTER TABLE TABLENAME add constaint2

Moreover the first scripts fails on the SQL+ but they pass on sqldeveloper Thanks! 此外,第一个脚本在SQL +上失败但是它们传递给sqldeveloper谢谢! Pratik PRATIK

The difference seems to be that the first method is a single statement whereas the second one uses three distinct statements. 差异似乎是第一种方法是单个语句,而第二种方法使用三种不同的语句。 IF the statements succeed, the overall result will be the same. 如果陈述成功,总体结果将是相同的。

You might want to check your synthax though (especially the use of ";"): 您可能想检查您的synthax(特别是使用“;”):

SQL> CREATE TABLE table1 (
  2     field1 NUMBER,
  3     field2 NUMBER,
  4     CONSTRAINT pk_table1 PRIMARY KEY (field1),
  5     CONSTRAINT chk_table1 CHECK (field2 > 0)
  6  );     
Table created

SQL> CREATE TABLE table2 (
  2     field1 NUMBER,
  3     field2 NUMBER);     
Table created

SQL> ALTER TABLE table2 ADD CONSTRAINT pk_table2 PRIMARY KEY (field1);     
Table altered

SQL> ALTER TABLE table2 ADD CONSTRAINT chk_table2 CHECK (field2 > 0);     
Table altered

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM