-1

这是我的架构:

CREATE TABLE item (
    id integer NOT NULL PRIMARY KEY AUTO_INCREMENT,
    title varchar(60) NOT NULL,
    description varchar(900) NOT NULL,
    company_id integer NOT NULL REFERENCES company (id),
    date datetime NOT NULL,
    source_id integer NOT NULL REFERENCES source (id),
    link varchar(255) NOT NULL,
    location_id integer NOT NULL REFERENCES location (id)
);

CREATE TABLE location (
    id integer NOT NULL PRIMARY KEY AUTO_INCREMENT,
    name varchar(255) NOT NULL,
    coordinate varchar(255) NOT NULL,
    location_id integer NOT NULL REFERENCES country (id)
);

CREATE TABLE country (
    id integer NOT NULL PRIMARY KEY AUTO_INCREMENT,
    name varchar(255) NOT NULL
);

CREATE TABLE company (
    id integer NOT NULL PRIMARY KEY AUTO_INCREMENT,
    name varchar(60) NOT NULL,
);

CREATE TABLE source (
    id integer NOT NULL PRIMARY KEY AUTO_INCREMENT,
    name varchar(60) NOT NULL,
);

当我将其放入并单击构建模式时,它告诉我http://sqlfiddle.com上的第四行存在无效语法。我看不出有什么错误,有人能解释一下吗?

另外,如果我做的不好或做出了错误的决定,请告诉我。

4

4 回答 4

4

您有 2 个错误的逗号。一个在“CREATE TABLE company”、“CREATE TABLE source”的末尾。并在位置之前创建国家。

CREATE TABLE company (
    id SERIAL PRIMARY KEY,
    name varchar(60) NOT NULL
);

CREATE TABLE country (
    id SERIAL PRIMARY KEY,
    name varchar(255) NOT NULL
);

CREATE TABLE location (
    id SERIAL PRIMARY KEY,
    name varchar(255) NOT NULL,
    coordinate varchar(255) NOT NULL,
    location_id integer NOT NULL REFERENCES country (id)
);

CREATE TABLE source (
    id SERIAL PRIMARY KEY,
    name varchar(60) NOT NULL
);

CREATE TABLE item (
    id SERIAL PRIMARY KEY,
    title varchar(60) NOT NULL,
    description varchar(900) NOT NULL,
    company_id integer NOT NULL REFERENCES company (id),
    date timestamp NOT NULL,
    source_id integer NOT NULL REFERENCES source (id),
    link varchar(255) NOT NULL,
    location_id integer NOT NULL REFERENCES location (id)
);
于 2012-12-03T15:59:15.810 回答
2

您必须先创建表,然后才能在其他地方引用它们。按以下顺序创建表格:来源、公司、国家/地区、位置、项目

请注意,您可以按任意顺序执行前三个操作,但 Country 必须在 Location 之前,并且 Location、Source 和 Company 必须在 Item 之前。

在 Source 和 Company 的定义末尾还有两个额外的逗号。你必须删除那些。

于 2012-12-03T15:59:34.387 回答
1
  1. country在引用它之前创建location表。
  2. 删除 和,的定义中的悬空。companysource
于 2012-12-03T15:57:18.010 回答
1

首先,您需要创建表,country然后locationcompanysource最后是item

于 2012-12-03T15:59:44.433 回答