SQL - 缺少右括号

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/13350419/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-01 12:04:31  来源:igfitidea点击:

SQL - Missing right parenthesis

sqloracleoracle10goracle11g

提问by mdanishs

I am trying to execute this script in Oracle 11g and getting the following error, I dont know where I am missing the paranthesis or what is the mistake kindly help me figure this out.

我正在尝试在 Oracle 11g 中执行此脚本并收到以下错误,我不知道我在哪里缺少括号或错误是什么,请帮助我解决这个问题。

Script:

脚本:

CREATE TABLE User_Role ( 
  user_role_id INT  NOT NULL  , 
  Users_user_id INT  FOREIGN KEY REFERENCES Users(user_id), 
  User_Types_user_type VARCHAR(20) FOREIGN KEY REFERENCES User_Types(user_type),  
  PRIMARY KEY(user_role_id) 
)

Error:

错误:

ORA-00907: missing right parenthesi

ORA-00907: 缺少右括号

回答by Nick Krasnov

Delete FOREIGN KEYclause. Rewrite your CREATE TABLEstatement as follows:

删除FOREIGN KEY条款。将你的CREATE TABLE陈述改写如下:

CREATE TABLE User_Role ( 
      user_role_id         INT  NOT NULL  , 
      Users_user_id        INT  REFERENCES Users(user_id), 
      User_Types_user_type VARCHAR(20) REFERENCES User_Types(user_type),  
      PRIMARY KEY(user_role_id) 
    )

In this case constraint names will be generated by Oracle. If you want to give them more meaningful names you could write your create tablestatement as follows:

在这种情况下,约束名称将由 Oracle 生成。如果你想给他们更有意义的名字,你可以写你的create table陈述如下:

  CREATE TABLE User_Role1 ( 
      user_role_id         INT  NOT NULL  , 
      Users_user_id        INT  , 
      User_Types_user_type VARCHAR(20) ,  
      constraint PK_YourTable PRIMARY KEY(user_role_id), 
      constraint FK_Table_1 foreign key(Users_user_id) REFERENCES Users(user_id),
      constraint FK_Table_2 foreign key(User_Types_user_type) REFERENCES User_Types(user_type)
    )

回答by Yogendra Singh

I think you need to define the columns first and then add the FOREIGN KEYconstraint in the very same manner as you are adding PRIMARY KEYconstraint as below:

我认为您需要先定义列,然后FOREIGN KEY以与添加PRIMARY KEY约束相同的方式添加约束,如下所示:

    CREATE TABLE User_Role ( 
       user_role_id INT  NOT NULL  , 
       Users_user_id INT, 
       User_Types_user_type VARCHAR(20),
       FOREIGN KEY (Users_user_id) REFERENCES Users(user_id),
       FOREIGN KEY (User_Types_user_type) REFERENCES User_Types(user_type), 
       PRIMARY KEY(user_role_id) 
     )

回答by SAGAR

remove the size of int and recompile

删除 int 的大小并重新编译

Example :-

例子 :-

integer (20) not null
integer not null
User_Types_user_type VARCHAR(20),
User_Types_user_type VARCHAR,

回答by a_horse_with_no_name

You canspecify foreign keys inline, you just need to remove the foreign keykeyword:

可以内联指定外键,您只需要删除foreign key关键字:

CREATE TABLE User_Role 
(
  user_role_id           INT NOT NULL  , 
  Users_user_id          INT         REFERENCES Users, 
  User_Types_user_type   VARCHAR(20) REFERENCES User_Types,  
  PRIMARY KEY(user_role_id) 
);

SQLFiddle example: http://sqlfiddle.com/#!4/4ca9f/1

SQLFiddle 示例:http://sqlfiddle.com/#!4/4ca9f/1

Listing the primary key columns in the "references" part is optional. If you prefer, you can also write REFERENCES Users(user_id)

在“引用”部分列出主键列是可选的。如果你愿意,你也可以写REFERENCES Users(user_id)

This format also has the disadvantage that the constraint names will be generated by Oracle (with a meaningless name). In order to be able to properly specify a constraint name for the foreign key, you need to use the syntax in the accepted answer.

这种格式还有一个缺点,即约束名称将由 Oracle 生成(具有无意义的名称)。为了能够为外键正确指定约束名称,您需要使用已接受答案中的语法。