0

你们好吗?

基本上我已经编写了这段 SQL 代码来创建一个表,但是我不断收到标题中所述的错误,知道为什么吗?

这是代码:

    CREATE TABLE staff(
    staffID INT NOT NULL PRIMARY KEY,
    firstName VARCHAR2(20),
    lastName VARCHAR2(20),
    addressLine_1 VARCHAR2(30),
    city VARCHAR2(15),
    postcode VARCHAR2(7),
    telephone VARCHAR2(15),
    salary DECIMAL (19,4),
    branchID INT FOREIGN KEY REFERENCES branches(branchID)
    );

这也是我的“分支”表的代码

    CREATE TABLE branches
    (branchID int NOT NULL PRIMARY KEY,
    addressLine_1 VARCHAR2(30),
    city VARCHAR2(15),
    postcode VARCHAR2(7),
    telephone VARCHAR2(15),
    manager VARCHAR2(20));

任何帮助,将不胜感激!

谢谢!

4

3 回答 3

3

几点建议:

首先确保branches表已经创建。

其次,我会将创建表代码更改为以下内容:

CREATE TABLE staff(
    staffID INT NOT NULL PRIMARY KEY,
    firstName VARCHAR(20),
    lastName VARCHAR(20),
    addressLine_1 VARCHAR2(30),
    city VARCHAR2(15),
    postcode VARCHAR2(7),
    telephone VARCHAR2(15),
    salary DECIMAL (19,4),
    branchID INT,
    constraint fk_branchId FOREIGN KEY (branchID) REFERENCES branches(branchID)
    );

请参阅SQL Fiddle with Demo在创建表期间创建FOREIGN KEY的语法是:

CREATE TABLE table_name
(
  column1 datatype null/not null,
  column2 datatype null/not null,
  ...

  CONSTRAINT fk_column
    FOREIGN KEY (column1, column2, ... column_n)
    REFERENCES parent_table (column1, column2, ... column_n)
);
于 2013-04-04T23:40:14.793 回答
0

这是表staff1的创建

CREATE TABLE staff
(
staffID INT NOT NULL PRIMARY KEY,
firstName VARCHAR2(20),
lastName VARCHAR2(20),
addressLine_1 VARCHAR2(30),
city VARCHAR2(15),
postcode VARCHAR2(7),
telephone VARCHAR2(15),
branchID int,
salary DECIMAL (19,4),
CONSTRAINT BRANCH_fk FOREIGN KEY(branchID) REFERENCES branches(branchID)
)
SQL> /
Table created.

请使用约束名称,以便发现错误变得容易。

于 2013-04-06T19:47:52.027 回答
0
create table medication (
id int not null primary key, 
name varchar(20),
mudslig price number (10),
protect date not null default (getdate()),
finish date not null default (getdate()),
company proect varchre2 (20),
shelf id int,
chemistid int,
constraint shelf_fk foreign key (shelf id) refences shelf (shelf id),
constraint chemist_fk foreign key (chemistid) refences chemist (chemistid)
);

请使用约束名称,以便发现错误变得容易。

于 2014-02-11T22:56:37.620 回答