Oracle 创建表

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/18428061/
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-19 01:52:58  来源:igfitidea点击:

Oracle Create TABLE

oracleddl

提问by user2714726

I am trying to create a table with foreign key using oracle. My syntax is as follows

我正在尝试使用 oracle 创建一个带有外键的表。我的语法如下

CREATE TABLE product (
   product_id       INT(7) NOT NULL,
   supplier_id      INT(7) NOT NULL,
   product_name     VARCHAR2(30),
   product_price    DOUBLE(4),
   product_category VARCHAR2(30),
   product_brand    VARCHAR2(20),
   product_expire   DATE,
   PRIMARY KEY (product_id),
   FOREIGN KEY (supplier_id)
)

I got a error, saying

我有一个错误,说

Error at Command Line:2 Column:14 Error report: SQL Error: ORA-00907: missing right parenthesis 00907. 00000 - "missing right parenthesis" *Cause:
*Action:

命令行中的错误:2 列:14 错误报告:SQL 错误:ORA-00907:缺少右括号 00907。00000 - “缺少右括号” *原因:
*操作:

Please help!

请帮忙!

回答by Radu Gheorghiu

Your foreign key should refference another column on another table.

您的外键应该引用另一个表上的另一列。

  1. Here is the documentation you need to fix your issue(how to write the query with the correct syntax for foreign key)

  2. Also, change your data type for column product_price from DOULBE(4)to NUMBER(12,4).

  1. 这是解决问题所需的文档(如何使用正确的外键语法编写查询)

  2. 此外,将列 product_price 的数据类型从DOULBE(4)更改为NUMBER(12,4)

回答by Sagar Joon

You should not use limit for int type...oracle will take default length for int type . Instead of int you can use Number type to make it run. And DOUBLE PRECISION is a data type in oracle but Double is not there. Also , syntax for foreign key is wrong. so this query will work for sure :

您不应该对 int type 使用 limit...oracle 将采用 int type 的默认长度。您可以使用 Number 类型代替 int 使其运行。DOUBLE PRECISION 是 oracle 中的一种数据类型,但 Double 不存在。此外,外键的语法是错误的。所以这个查询肯定会起作用:

CREATE TABLE product(
   product_id       number(7) NOT NULL,
   supplier_id      number(7) NOT NULL,
   product_name     VARCHAR2(30),
   product_price    DOUBLE PRECISION,
   product_category VARCHAR2(30),
   product_brand    VARCHAR2(20),
   product_expire   DATE,
   PRIMARY KEY (product_id),
   FOREIGN KEY (supplier_id)
    REFERENCES parent_table (supplier_id)
);