SQL - Syntax Error (Problems with PK and FK)
I a want to fill a DB with SQL, but I get syntax errors. I think the problem is in PK and FK. Can you please take a look, what am I doing wrong in my approach.
Thank you very much!
SQL Code
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);
ERROR MESSAGE
You have an error in your SQL syntax; check the manual that
corresponds to your MySQL server version for the right syntax to use
near '
CONSTRAINT FK FOREIGN KEY (fkordereditem) REFERENCES TheOrder(ordereditem) ' at line 6
sql foreign-keys syntax-error primary-key derby
|
show 1 more comment
I a want to fill a DB with SQL, but I get syntax errors. I think the problem is in PK and FK. Can you please take a look, what am I doing wrong in my approach.
Thank you very much!
SQL Code
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);
ERROR MESSAGE
You have an error in your SQL syntax; check the manual that
corresponds to your MySQL server version for the right syntax to use
near '
CONSTRAINT FK FOREIGN KEY (fkordereditem) REFERENCES TheOrder(ordereditem) ' at line 6
sql foreign-keys syntax-error primary-key derby
1
Did you check the manual that corresponds to your MySQL server version for the right syntax? You can find the documentation on the web with the search phrase MySQL documentation, and then when you go to the pages for the documentation searching for CREATE TABLE.
– Ken White
Nov 9 at 21:03
I tried it out with the code of a friend. He did the same thing as I can see, and his code works... :S I don't get it. He did the same thing with the FK and PK
– TeslaX
Nov 9 at 21:04
1
I tried it out with the code of a friend isn't reading the documentaiton for your MySQL version. Perhaps you have a typo that your friend didn't. Perhaps you're using a different version. Perhans reading the documentation will help you figure out what's wrong.
– Ken White
Nov 9 at 21:05
Oke, thanks. I am looking now.
– TeslaX
Nov 9 at 21:06
2
You forgot a comma afterpostcode INTEGER
– Yann39
Nov 9 at 21:09
|
show 1 more comment
I a want to fill a DB with SQL, but I get syntax errors. I think the problem is in PK and FK. Can you please take a look, what am I doing wrong in my approach.
Thank you very much!
SQL Code
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);
ERROR MESSAGE
You have an error in your SQL syntax; check the manual that
corresponds to your MySQL server version for the right syntax to use
near '
CONSTRAINT FK FOREIGN KEY (fkordereditem) REFERENCES TheOrder(ordereditem) ' at line 6
sql foreign-keys syntax-error primary-key derby
I a want to fill a DB with SQL, but I get syntax errors. I think the problem is in PK and FK. Can you please take a look, what am I doing wrong in my approach.
Thank you very much!
SQL Code
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);
ERROR MESSAGE
You have an error in your SQL syntax; check the manual that
corresponds to your MySQL server version for the right syntax to use
near '
CONSTRAINT FK FOREIGN KEY (fkordereditem) REFERENCES TheOrder(ordereditem) ' at line 6
sql foreign-keys syntax-error primary-key derby
sql foreign-keys syntax-error primary-key derby
asked Nov 9 at 21:00
TeslaX
449
449
1
Did you check the manual that corresponds to your MySQL server version for the right syntax? You can find the documentation on the web with the search phrase MySQL documentation, and then when you go to the pages for the documentation searching for CREATE TABLE.
– Ken White
Nov 9 at 21:03
I tried it out with the code of a friend. He did the same thing as I can see, and his code works... :S I don't get it. He did the same thing with the FK and PK
– TeslaX
Nov 9 at 21:04
1
I tried it out with the code of a friend isn't reading the documentaiton for your MySQL version. Perhaps you have a typo that your friend didn't. Perhaps you're using a different version. Perhans reading the documentation will help you figure out what's wrong.
– Ken White
Nov 9 at 21:05
Oke, thanks. I am looking now.
– TeslaX
Nov 9 at 21:06
2
You forgot a comma afterpostcode INTEGER
– Yann39
Nov 9 at 21:09
|
show 1 more comment
1
Did you check the manual that corresponds to your MySQL server version for the right syntax? You can find the documentation on the web with the search phrase MySQL documentation, and then when you go to the pages for the documentation searching for CREATE TABLE.
– Ken White
Nov 9 at 21:03
I tried it out with the code of a friend. He did the same thing as I can see, and his code works... :S I don't get it. He did the same thing with the FK and PK
– TeslaX
Nov 9 at 21:04
1
I tried it out with the code of a friend isn't reading the documentaiton for your MySQL version. Perhaps you have a typo that your friend didn't. Perhaps you're using a different version. Perhans reading the documentation will help you figure out what's wrong.
– Ken White
Nov 9 at 21:05
Oke, thanks. I am looking now.
– TeslaX
Nov 9 at 21:06
2
You forgot a comma afterpostcode INTEGER
– Yann39
Nov 9 at 21:09
1
1
Did you check the manual that corresponds to your MySQL server version for the right syntax? You can find the documentation on the web with the search phrase MySQL documentation, and then when you go to the pages for the documentation searching for CREATE TABLE.
– Ken White
Nov 9 at 21:03
Did you check the manual that corresponds to your MySQL server version for the right syntax? You can find the documentation on the web with the search phrase MySQL documentation, and then when you go to the pages for the documentation searching for CREATE TABLE.
– Ken White
Nov 9 at 21:03
I tried it out with the code of a friend. He did the same thing as I can see, and his code works... :S I don't get it. He did the same thing with the FK and PK
– TeslaX
Nov 9 at 21:04
I tried it out with the code of a friend. He did the same thing as I can see, and his code works... :S I don't get it. He did the same thing with the FK and PK
– TeslaX
Nov 9 at 21:04
1
1
I tried it out with the code of a friend isn't reading the documentaiton for your MySQL version. Perhaps you have a typo that your friend didn't. Perhaps you're using a different version. Perhans reading the documentation will help you figure out what's wrong.
– Ken White
Nov 9 at 21:05
I tried it out with the code of a friend isn't reading the documentaiton for your MySQL version. Perhaps you have a typo that your friend didn't. Perhaps you're using a different version. Perhans reading the documentation will help you figure out what's wrong.
– Ken White
Nov 9 at 21:05
Oke, thanks. I am looking now.
– TeslaX
Nov 9 at 21:06
Oke, thanks. I am looking now.
– TeslaX
Nov 9 at 21:06
2
2
You forgot a comma after
postcode INTEGER– Yann39
Nov 9 at 21:09
You forgot a comma after
postcode INTEGER– Yann39
Nov 9 at 21:09
|
show 1 more comment
2 Answers
2
active
oldest
votes
You have multiple problems :
- you forgot a comma after
postcode INTEGER - You forgot to specify the length for some
VARCHARcolumn definitions (this is mandatory, if you don't want to specify a length useVARCHAR(MAX). - column order in some
INSERTstatements does not match the table definition (you need to respect the order, or specify the column names in theINSERTstatement) - you are trying to set foreign keys that references non primary key, if you want to do this you need to define referenced columns as
UNIQUE - it seems you are trying to insert duplicates for some primary key values
- you are referencing foreign keys with different data types then your column
add a comment |
I found the error. The assigned types have not been the same for the FK and for the referenced attribute.
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)
);
2
No, any type can be a PK or FK, but they have to agree. Which the manual will tell you. There are many errors. See the other answers.
– philipxy
Nov 9 at 23:58
add a comment |
Your Answer
StackExchange.ifUsing("editor", function ()
StackExchange.using("externalEditor", function ()
StackExchange.using("snippets", function ()
StackExchange.snippets.init();
);
);
, "code-snippets");
StackExchange.ready(function()
var channelOptions =
tags: "".split(" "),
id: "1"
;
initTagRenderer("".split(" "), "".split(" "), channelOptions);
StackExchange.using("externalEditor", function()
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled)
StackExchange.using("snippets", function()
createEditor();
);
else
createEditor();
);
function createEditor()
StackExchange.prepareEditor(
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader:
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
,
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
);
);
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53233214%2fsql-syntax-error-problems-with-pk-and-fk%23new-answer', 'question_page');
);
Post as a guest
Required, but never shown
2 Answers
2
active
oldest
votes
2 Answers
2
active
oldest
votes
active
oldest
votes
active
oldest
votes
You have multiple problems :
- you forgot a comma after
postcode INTEGER - You forgot to specify the length for some
VARCHARcolumn definitions (this is mandatory, if you don't want to specify a length useVARCHAR(MAX). - column order in some
INSERTstatements does not match the table definition (you need to respect the order, or specify the column names in theINSERTstatement) - you are trying to set foreign keys that references non primary key, if you want to do this you need to define referenced columns as
UNIQUE - it seems you are trying to insert duplicates for some primary key values
- you are referencing foreign keys with different data types then your column
add a comment |
You have multiple problems :
- you forgot a comma after
postcode INTEGER - You forgot to specify the length for some
VARCHARcolumn definitions (this is mandatory, if you don't want to specify a length useVARCHAR(MAX). - column order in some
INSERTstatements does not match the table definition (you need to respect the order, or specify the column names in theINSERTstatement) - you are trying to set foreign keys that references non primary key, if you want to do this you need to define referenced columns as
UNIQUE - it seems you are trying to insert duplicates for some primary key values
- you are referencing foreign keys with different data types then your column
add a comment |
You have multiple problems :
- you forgot a comma after
postcode INTEGER - You forgot to specify the length for some
VARCHARcolumn definitions (this is mandatory, if you don't want to specify a length useVARCHAR(MAX). - column order in some
INSERTstatements does not match the table definition (you need to respect the order, or specify the column names in theINSERTstatement) - you are trying to set foreign keys that references non primary key, if you want to do this you need to define referenced columns as
UNIQUE - it seems you are trying to insert duplicates for some primary key values
- you are referencing foreign keys with different data types then your column
You have multiple problems :
- you forgot a comma after
postcode INTEGER - You forgot to specify the length for some
VARCHARcolumn definitions (this is mandatory, if you don't want to specify a length useVARCHAR(MAX). - column order in some
INSERTstatements does not match the table definition (you need to respect the order, or specify the column names in theINSERTstatement) - you are trying to set foreign keys that references non primary key, if you want to do this you need to define referenced columns as
UNIQUE - it seems you are trying to insert duplicates for some primary key values
- you are referencing foreign keys with different data types then your column
edited Nov 9 at 22:06
answered Nov 9 at 21:52
Yann39
2,07632232
2,07632232
add a comment |
add a comment |
I found the error. The assigned types have not been the same for the FK and for the referenced attribute.
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)
);
2
No, any type can be a PK or FK, but they have to agree. Which the manual will tell you. There are many errors. See the other answers.
– philipxy
Nov 9 at 23:58
add a comment |
I found the error. The assigned types have not been the same for the FK and for the referenced attribute.
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)
);
2
No, any type can be a PK or FK, but they have to agree. Which the manual will tell you. There are many errors. See the other answers.
– philipxy
Nov 9 at 23:58
add a comment |
I found the error. The assigned types have not been the same for the FK and for the referenced attribute.
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)
);
I found the error. The assigned types have not been the same for the FK and for the referenced attribute.
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)
);
edited Nov 16 at 12:58
answered Nov 9 at 21:46
TeslaX
449
449
2
No, any type can be a PK or FK, but they have to agree. Which the manual will tell you. There are many errors. See the other answers.
– philipxy
Nov 9 at 23:58
add a comment |
2
No, any type can be a PK or FK, but they have to agree. Which the manual will tell you. There are many errors. See the other answers.
– philipxy
Nov 9 at 23:58
2
2
No, any type can be a PK or FK, but they have to agree. Which the manual will tell you. There are many errors. See the other answers.
– philipxy
Nov 9 at 23:58
No, any type can be a PK or FK, but they have to agree. Which the manual will tell you. There are many errors. See the other answers.
– philipxy
Nov 9 at 23:58
add a comment |
Thanks for contributing an answer to Stack Overflow!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Some of your past answers have not been well-received, and you're in danger of being blocked from answering.
Please pay close attention to the following guidance:
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53233214%2fsql-syntax-error-problems-with-pk-and-fk%23new-answer', 'question_page');
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
1
Did you check the manual that corresponds to your MySQL server version for the right syntax? You can find the documentation on the web with the search phrase MySQL documentation, and then when you go to the pages for the documentation searching for CREATE TABLE.
– Ken White
Nov 9 at 21:03
I tried it out with the code of a friend. He did the same thing as I can see, and his code works... :S I don't get it. He did the same thing with the FK and PK
– TeslaX
Nov 9 at 21:04
1
I tried it out with the code of a friend isn't reading the documentaiton for your MySQL version. Perhaps you have a typo that your friend didn't. Perhaps you're using a different version. Perhans reading the documentation will help you figure out what's wrong.
– Ken White
Nov 9 at 21:05
Oke, thanks. I am looking now.
– TeslaX
Nov 9 at 21:06
2
You forgot a comma after
postcode INTEGER– Yann39
Nov 9 at 21:09