Go Back   CodingForums.com > :: Server side development > MySQL

Before you post, read our: Rules & Posting Guidelines

Reply
 
Thread Tools Rate Thread
Enjoy an ad free experience by logging in. Not a member yet? Register.
Old 11-30-2012, 03:54 AM   PM User | #1
hiya1992
New to the CF scene

 
Join Date: Nov 2012
Posts: 2
Thanks: 1
Thanked 0 Times in 0 Posts
hiya1992 is an unknown quantity at this point
MySQL syntax help

Hiya all, i keep getting a
"Token line error=7, Token line offset=1, Token in error=CONSTRAINT" error message.

Using this code
Code:
Create Table Orders(
orderNumber bigint Not Null,
orderDate datetime Not Null,
completed bit Not Null,
discount numeric(2,1),
PRIMARY KEY(orderNumber)
CONSTRAINT FK_Orders_Store
FOREIGN KEY(StoreID) REFERENCES Store(StoreID)
CONSTRAINT FK_Orders_Pizza
FOREIGN KEY(itemCode) REFERENCES Pizza(itemCode)
)
(the below codes are seperate tables)

Code:
Create Table Store(
StoreID int IDENTITY,
town nvarchar(25) NOT NULL,
address nvarchar(50) NOT NULL,
PRIMARY KEY(StoreID)
)
Code:
Create Table Pizza(
itemCode int IDENTITY, 
pizzaName nvarchar(25) NOT NULL,
baseType nvarchar(20) NOT NULL,
PRIMARY KEY(itemCode)
)
Any help?

Last edited by hiya1992; 11-30-2012 at 06:40 PM..
hiya1992 is offline   Reply With Quote
Old 11-30-2012, 08:45 AM   PM User | #2
zergi
New to the CF scene

 
Join Date: Nov 2012
Posts: 9
Thanks: 1
Thanked 2 Times in 2 Posts
zergi is an unknown quantity at this point
Put the comma before the CONSTRAINT keyword as it starts a new part of the CREATE TABLE query.

Also, make sure in
Code:
CONSTRAINT FK_Orders_Store
FK_Orders_Store - is unique across the whole database.
zergi is offline   Reply With Quote
Old 11-30-2012, 03:32 PM   PM User | #3
guelphdad
Super Moderator


 
guelphdad's Avatar
 
Join Date: Mar 2006
Location: St. Catharines, Ontario Canada
Posts: 2,629
Thanks: 4
Thanked 147 Times in 138 Posts
guelphdad will become famous soon enoughguelphdad will become famous soon enough
You are missing the columns storeid and itemcode in the Orders table.
You actually have to create them in order to make a foreign key on them.

there is also no data type of IDENTITY in mysql. I believe that is Oracle or SQL Server that supports that type.
guelphdad is offline   Reply With Quote
The Following 2 Users Say Thank You to guelphdad For This Useful Post:
hiya1992 (11-30-2012), zergi (11-30-2012)
Old 11-30-2012, 06:42 PM   PM User | #4
hiya1992
New to the CF scene

 
Join Date: Nov 2012
Posts: 2
Thanks: 1
Thanked 0 Times in 0 Posts
hiya1992 is an unknown quantity at this point
hey thanks that works. I'm getting another CONSTRAINT error message though 0_o on the new codes below. I'm using webmatrix to design these by the way.


Code:
Create table Bases(
baseType nvarchar(25) NOT NULL,
basePrice numeric(6,3) NOT NULL,
calories bigint NOT NULL,
fat nvarchar(10) NOT NULL,
salt nvarchar(10) NOT NULL, 
PRIMARY KEY(basetype)
)

-
(Token line number =6, token line offset=1, token in error=constraint)

Code:
Create Table Pizza(
itemCode int IDENTITY, 
pizzaName nvarchar(25) NOT NULL,
baseType nvarchar(25),
PRIMARY KEY(itemCode)
CONSTRAINT FK_Pizza_Bases
FOREIGN KEY(baseType) REFERENCES Pizza(baseType)
)
hiya1992 is offline   Reply With Quote
Old 11-30-2012, 08:28 PM   PM User | #5
Old Pedant
Supreme Master coder!

 
Old Pedant's Avatar
 
Join Date: Feb 2009
Posts: 23,248
Thanks: 59
Thanked 3,999 Times in 3,968 Posts
Old Pedant is a name known to allOld Pedant is a name known to allOld Pedant is a name known to allOld Pedant is a name known to allOld Pedant is a name known to allOld Pedant is a name known to all
AGAIN, you are missing the COMMA!

You must have a COMMA after every declaration in the CREATE TABLE.

By the by, this is true in MySQL, SQL Server, Access, and most other DBs.

It would help if you would realize *WHERE* the declarations start and begin.

Thus:
Code:
Create Table Pizza(
    itemCode int IDENTITY, 
    pizzaName nvarchar(25) NOT NULL,
    baseType nvarchar(25),
    PRIMARY KEY(itemCode),
    CONSTRAINT FK_Pizza_Bases FOREIGN KEY(baseType) REFERENCES Pizza(baseType)
)
The lines in red and magenta show you the separate element declarations in that statement.

And by the by, this *IS* the MySQL forum. Your question is not about MySQL.
__________________
An optimist sees the glass as half full.
A pessimist sees the glass as half empty.
A realist drinks it no matter how much there is.
Old Pedant is offline   Reply With Quote
Reply

Bookmarks

Jump To Top of Thread


Thread Tools
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump


All times are GMT +1. The time now is 11:28 PM.


Advertisement
Log in to turn off these ads.