使用或不使用CONSTRAINT

时间:2016-03-26 22:30:59

标签: mysql oracle syntax

以下语法之间有什么区别:

CONSTRAINT user_roles_id_pk PRIMARY KEY(role_id),
CONSTRAINT students_role_id_fk FOREIGN KEY(student_id) REFERENCES students(student_id),
CONSTRAINT ratings_institute_id_fk FOREIGN KEY(institute_id) REFERENCES institutes(institute_id) ON DELETE CASCADE,
CONSTRAINT ratings_ip_uq UNIQUE(ip) 

PRIMARY KEY (role_id),
FOREIGN KEY (student_id) REFERENCES students(student_id)
UNIQUE(ip)
FOREIGN KEY (institute_id) REFERENCES institutes(institutes_id) ON DELETE CASCADE,

我在oracle sql developer中编写create语句时使用了第一组代码。不幸的是,我不得不将oracle代码转换为mysql以在xampp上运行。

3 个答案:

答案 0 :(得分:0)

除了第二个索引,外键引用不同的字段,4个约束是相同的。但是,即使在mysql中,您也可以遵循语法等约束,请参阅create table上的mysql文档:

 create_definition:
    col_name column_definition
  | [CONSTRAINT [symbol]] PRIMARY KEY [index_type] (index_col_name,...)
      [index_option] ...
  | {INDEX|KEY} [index_name] [index_type] (index_col_name,...)
      [index_option] ...
  | [CONSTRAINT [symbol]] UNIQUE [INDEX|KEY]
      [index_name] [index_type] (index_col_name,...)
      [index_option] ...
  | {FULLTEXT|SPATIAL} [INDEX|KEY] [index_name] (index_col_name,...)
      [index_option] ...
  | [CONSTRAINT [symbol]] FOREIGN KEY
      [index_name] (index_col_name,...) reference_definition
  | CHECK (expr)

此外,您可以将oracle与apache和php一起使用,因此您不必仅仅因为预先打包的安装程序没有默认配置oracle而替换oracle。

答案 1 :(得分:0)

CONSTRAINT 关键字在MySQL中是可选的,并且(如果内存服务)在Oracle中。其目的是允许您明确命名约束,这有助于提供自我文档,如果您以后需要禁用约束或删除或重建关联索引,这也很有用。

答案 2 :(得分:0)

在Oracle中,您可以使用CONSTRAINT contraint_name ...语法指定名称,也可以将其保留,Oracle会将约束命名为SYS_C123456。自己命名约束使调试代码变得容易得多,如:

ORA-02291: integrity constraint (TEST.SYS_C009329) violated - parent key not found

并没有告诉你更多关于你错误的列 - 但是:

ORA-02291: integrity constraint (TEST.ROLES__INSTITUTE_ID__FK) violated - parent key not found

准确地告诉您发生错误的表和列(如果您使用足够的详细信息命名约束)。

您可以在此处查看约束名称的差异:

Oracle安装程序

CREATE TABLE students ( id INT CONSTRAINT students__id__pk PRIMARY KEY );
CREATE TABLE institutes( id INT CONSTRAINT institutes__id__pk PRIMARY KEY );

CREATE TABLE roles(
  role_id      INT,
  student_id   INT,
  institute_id INT,
  ip           VARCHAR2(15),
  CONSTRAINT roles__role_id__pk PRIMARY KEY(role_id),
  CONSTRAINT roles__student_id__fk
    FOREIGN KEY(student_id) REFERENCES students(id),
  CONSTRAINT roles__institute_id__fk
    FOREIGN KEY(institute_id) REFERENCES institutes(id),
  CONSTRAINT roles__ip__u UNIQUE(ip) 
);


CREATE TABLE roles1(
  role_id      INT,
  student_id   INT,
  institute_id INT,
  ip           VARCHAR2(15),
  PRIMARY KEY(role_id),
  FOREIGN KEY(student_id) REFERENCES students(id),
  FOREIGN KEY(institute_id) REFERENCES institutes(id),
  UNIQUE(ip) 
);

查询 - 命名约束

SELECT constraint_name,
       constraint_type,
       index_name,
       r_constraint_name
FROM   USER_CONSTRAINTS
WHERE  table_name = 'ROLES';

<强>输出

CONSTRAINT_NAME                CONSTRAINT_TYPE INDEX_NAME                     R_CONSTRAINT_NAME            
------------------------------ --------------- ------------------------------ ------------------------------
ROLES__ROLE_ID__PK             P               ROLES__ROLE_ID__PK                                            
ROLES__IP__U                   U               ROLES__IP__U                                                  
ROLES__STUDENT_ID__FK          R                                              STUDENTS__ID__PK               
ROLES__INSTITUTE_ID__FK        R                                              INSTITUTES__ID__PK             

查询2 - 未命名的约束

SELECT constraint_name,
       constraint_type,
       index_name,
       r_constraint_name
FROM   USER_CONSTRAINTS
WHERE  table_name = 'ROLES1';

<强>输出

CONSTRAINT_NAME                CONSTRAINT_TYPE INDEX_NAME                     R_CONSTRAINT_NAME            
------------------------------ --------------- ------------------------------ ------------------------------
SYS_C009326                    P               SYS_C009326                                                   
SYS_C009327                    U               SYS_C009327                                                   
SYS_C009328                    R                                              STUDENTS__ID__PK               
SYS_C009329                    R                                              INSTITUTES__ID__PK             

MySQL is similar but gives the constraints slightly better default names - but not by much.