SQL - Missing right parenthesis

吃可爱长大的小学妹 提交于 2019-12-01 06:54:58

Delete FOREIGN KEY clause. Rewrite your CREATE TABLE statement as follows:

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:

  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)
    )

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:

    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) 
     )

You can specify foreign keys inline, you just need to remove the foreign key keyword:

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

Listing the primary key columns in the "references" part is optional. If you prefer, you can also write 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.

SAGAR

remove the size of int and recompile

Example :-

integer (20) not null
integer not null
User_Types_user_type VARCHAR(20),
User_Types_user_type VARCHAR,
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!