SQL-语法错误(PK和FK问题)

时间:2018-11-09 21:00:06

标签: sql foreign-keys syntax-error primary-key derby

我想用SQL填充数据库,但是出现语法错误。我认为问题出在PK和FK。您能否看一下,我的方法在做什么错。

非常感谢!

SQL代码

CREATE TABLE TheOrder
(
    ordereditem VARCHAR(50) NOT NULL PRIMARY KEY,
    productid INTEGER NOT NULL,
    productwholesaler VARCHAR (50) NOT NULL
);

INSERT INTO TheOrder VALUES
('overwatch', 1, 'blizzard'),
('deathadder', 2, 'razer'),
('xboxcontroller', 3, 'microsoft'),
('kraken', 4, 'razer'),
('diablo3', 5, 'blizzard'),
('warcraft3remastered', 6, 'blizzard'),
('fallout3', 7, 'bethesda'),
('pixel', 8, 'google'),
('aspire5', 9, 'acer'),
('destiny2', 10 ,'activision');




CREATE TABLE TheStock
(
    counteditems INTEGER NOT NULL, 
    isInStock BOOLEAN NOT NULL,
    stockID INTEGER NOT NULL PRIMARY KEY,
    fkordereditem VARCHAR,
    CONSTRAINT fkordereditem FOREIGN KEY (fkordereditem) REFERENCES TheOrder(ordereditem)
);

INSERT INTO TheStock VALUES
('overwatch', 5, true, 1),
('deathadder', 2, true, 2),
('xboxcontroller', 0, false, 3),
('kraken', 0, false, 4),
('diablo3', 5, true, 5),
('warcraft3remastered', 0, false, 6),
('fallout3', 7, true, 7),
('pixel', 8, true, 8),
('aspire5', 0, false, 9),
('destiny2', 10 , true, 10);



CREATE TABLE TheReorder
(
    fkproductid VARCHAR,
    fkproductwholesaler VARCHAR,
    howmanydays INTEGER NOT NULL, 
    mobilenumber INTEGER PRIMARY KEY,
    postcode INTEGER
    CONSTRAINT fkproductid FOREIGN KEY (fkproductid) REFERENCES TheOrder(productid),
    CONSTRAINT fkproductwholesaler FOREIGN KEY (fkproductwholesaler) REFERENCES TheOrder (productwholesaler)
);

INSERT INTO TheReorder VALUES
('overwatch', 'blizzard', 0, 649494, 1020),
('deathadder', 'razer', 0, 498494, 1150),
('xboxcontroller', 'microsoft', 3, 948849, 71000),
('kraken', 'razer', 5, 249489, 32009),
('diablo3', 'blizzard', 0, 194984, 29000),
('warcraft3remastered', 'blizzard', 12, 398484, 11000),
('fallout3', 'bethesda', 0, 694895, 42132),
('pixel', 'google', 0, 873243, 9201),
('aspire5', 'acer', 15, 988564, 10020),
('destiny2', 'activision', 0, 745637, 10090);


CREATE TABLE TheCourier
(
    couriername VARCHAR(50) NOT NULL PRIMARY KEY,
    couriercompany VARCHAR (50) NOT NULL,
    courieravailable BOOLEAN NOT NULL
);

INSERT INTO TheCourier VALUES
('jon snow', 'the wall', false),
('ned stark', 'winterfell', false),
('elon musk', 'space x', true),
('yusaku maezawa', 'zozo', false),
('hokusai katsushika', 'the great wave', false),
('ragnar lothbrok', 'vikings', false),
('jax teller', 'sons of anarchy', true),
('harvey specter', 'post', true),
('michael corleone', 'ups', false),
('tommy shelby', 'fedex', false);


CREATE TABLE ThePenaltyInfo
(
    fkhowmanydays INTEGER,
    fkproductidpen INTEGER,
    feetopay INTEGER NOT NULL,
    currencie VARCHAR(50) NOT NULL,
    penaltyid INTEGER NOT NULL PRIMARY KEY,
    CONSTRAINT fkhowmanydays FOREIGN KEY (fkhowmanydays) REFERENCES TheReorder(howmanydays),
    CONSTRAINT fkproductidpen FOREIGN KEY (fkproductidpen) REFERENCES TheOrder(productid)
);

INSERT INTO ThePenaltyInfo VALUES
(0, 1, 0, 'euro', 1),
(0, 2, 0, 'euro', 2),
(3, 3, 50, 'usd', 3),
(5, 4, 90, 'usd', 4),
(0, 5, 0, 'ron', 5),
(12, 6, 30, 'ron', 6),
(0, 7, 0, 'euro', 7),
(0, 8, 0, 'euro', 8),
(15, 9, 800, 'euro', 9),
(0, 10, 0, 'euro', 10);



CREATE TABLE TheShipementList 
(
    fkcouriercompany VARCHAR,
    fkproductidship VARCHAR,
    shipementid INTEGER NOT NULL PRIMARY KEY,
    shipementaddress VARCHAR(50) NOT NULL,
    customernumber INTEGER NOT NULL,
    CONSTRAINT fkcouriercompany FOREIGN KEY (fkcouriercompany) REFERENCES TheCourier(couriercompany),
    CONSTRAINT fkproductidship FOREIGN KEY (fkproductidship) REFERENCES TheOrder(productid)
);

INSERT INTO TheShipementList VALUES
('space x', 1, 1, 'Palma de Mallorca', 1994007),
('space x', 2, 1, 'Palma de Mallorca', 1994007),
('space x', 3, 1, 'Palma de Mallorca', 1994007),
('space x', 4, 1, 'Palma de Mallorca', 1994007),
('space x', 5, 1, 'Palma de Mallorca', 1994007),
('space x', 6, 1, 'Palma de Mallorca', 1994007),
('space x', 7, 1, 'Palma de Mallorca', 1994007),
('space x', 8, 1, 'Palma de Mallorca', 1994007),
('space x', 9, 1, 'Palma de Mallorca', 1994007),
('space x', 10, 1, 'Palma de Mallorca',1994007);

错误消息

  

您的SQL语法有错误;检查手册   对应于您的MySQL服务器版本以使用正确的语法   '附近       约束FK外键(fkordereditem)参考TheOrder(ordereditem)'位于第6行

2 个答案:

答案 0 :(得分:2)

您有多个问题:

  1. 您在<Page x:Class="WpfApp1.Index" xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" xmlns:d="http://schemas.microsoft.com/expression/blend/2008" xmlns:local="clr-namespace:WpfApp1" mc:Ignorable="d" d:DesignHeight="450" d:DesignWidth="800" Title="Index Tester"> <Grid> <TextBlock Text="Hallo"></TextBlock> </Grid> </Page> 之后忘记了逗号
  2. 您忘记为某些postcode INTEGER列定义指定长度(这是强制性的,如果您不想使用VARCHAR指定长度)。
  3. 某些VARCHAR(MAX)语句中的列顺序与表定义不匹配(您需要遵守顺序,或在INSERT语句中指定列名)
  4. 您正在尝试设置引用非主键的外键,如果要执行此操作,则需要将引用的列定义为INSERT
  5. 似乎您要为某些主键值插入重复项
  6. 您所引用的外键具有与列不同的数据类型

答案 1 :(得分:0)

我发现了错误。对于FK和所引用的属性,分配的类型不同。

CREATE TABLE TheOrder
(
    productid INTEGER NOT NULL PRIMARY KEY,
    ordereditem VARCHAR(50) NOT NULL,
    productwholesaler VARCHAR (50) NOT NULL
);

CREATE TABLE TheStock
(
    counteditems INTEGER NOT NULL, 
    isInStock BOOLEAN NOT NULL,
    stockID INTEGER NOT NULL PRIMARY KEY,
    testid INTEGER,
    CONSTRAINT testid FOREIGN KEY (testid) REFERENCES TheOrder(productid)
);