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
SQL - Missing right parenthesis
提问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 KEY
clause. Rewrite your CREATE TABLE
statement 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 table
statement 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 KEY
constraint in the very same manner as you are adding PRIMARY KEY
constraint 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 key
keyword:
您可以内联指定外键,您只需要删除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 生成(具有无意义的名称)。为了能够为外键正确指定约束名称,您需要使用已接受答案中的语法。