The Shed is going Social! Join us on FaceBook and Twitter and chime in on the conversation.
|
 |
|
Dev Shed Forums
> Databases
> MySQL Help
|
MySQL syntax help
Discuss MySQL syntax help in the MySQL Help forum on Dev Shed. MySQL syntax help MySQL Help forum discussing administration, SQL syntax, and other MySQL-related topics. MySQL is an open-source relational database management system (RDBMS).
|
|
 |
|
|
|
|

Dev Shed Forums Sponsor:
|
|
|

November 29th, 2012, 06:43 PM
|
|
Registered User
|
|
Join Date: Nov 2012
Posts: 2
Time spent in forums: 5 h 56 m 45 sec
Reputation Power: 0
|
|
|
MySQL syntax help
2 SQL codes below. Keep getting an error on the second one.
Code:
Original
- Code |
|
|
|
Create Table Store(
StoreID int IDENTITY,
town nvarchar(25) NOT NULL,
address nvarchar(50) NOT NULL,
PRIMARY KEY(StoreID)
)
Code:
Original
- Code |
|
|
|
Create Table Order(
orderNumber int PRIMARY KEY,
orderDate datetime Not Null,
completed bit Not Null,
discount numeric(2,1) Not Null,
CONSTRAINT FK_Order_StoreID
FOREIGN KEY(StoreID) REFERENCES (StoreID)
)
|

November 29th, 2012, 08:29 PM
|
 |
SQL Consultant
|
|
Join Date: Feb 2003
Location: Toronto Canada
|
|
|
you'll get several errors if you actually run those statements in mysql
in the first one, IDENTITY is invalid for mysql
in the second one, ORDER is a reserved word, so you cannot name your table this without escaping it with backticks, and the foreign key declaration won't work because you forgot to include the StoreID column in the table, and you also didn't say which table it references
|

November 29th, 2012, 08:44 PM
|
|
Registered User
|
|
Join Date: Nov 2012
Posts: 2
Time spent in forums: 5 h 56 m 45 sec
Reputation Power: 0
|
|
Quote: | Originally Posted by r937 you'll get several errors if you actually run those statements in mysql
in the first one, IDENTITY is invalid for mysql
in the second one, ORDER is a reserved word, so you cannot name your table this without escaping it with backticks, and the foreign key declaration won't work because you forgot to include the StoreID column in the table, and you also didn't say which table it references |
Thankyou, sorry i did notice that stupid slip up for the table referencing. I'm not sure what you mean by the IDENTITY being invalid. I hate to bother you further but is there an easy explanation for that?
I'm still getting a CONSTRAINT error as well using the below codes.
Code:
Original
- 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)
)
Code:
Original
- Code |
|
|
|
Create Table Store(
StoreID int IDENTITY,
town nvarchar(25) NOT NULL,
address nvarchar(50) NOT NULL,
PRIMARY KEY(StoreID)
)
Code:
Original
- Code |
|
|
|
Create Table Pizza(
itemCode int IDENTITY,
pizzaName nvarchar(25) NOT NULL,
baseType nvarchar(20) NOT NULL,
PRIMARY KEY(itemCode)
)
|

November 30th, 2012, 02:50 AM
|
|
Contributing User
|
|
Join Date: Jan 2003
Location: Paris Uppland
|
|
|
If you get an error, post the complete error message.
|

November 30th, 2012, 02:57 AM
|
|
Contributing User
|
|
Join Date: Oct 2003
Location: Germany
|
|
|
And please read the manual before proceeding:
http://dev.mysql.com/doc/refman/5.5/en/create-table.html
__________________
I will not read nor answer questions where the SQL code is messy and not formatted properly using [code] tags.
http://forums.devshed.com/misc.php?do=bbcode#code
Tips on how to ask better questions:
http://tkyte.blogspot.de/2005/06/how-to-ask-questions.html
http://wiki.postgresql.org/wiki/SlowQueryQuestions
http://catb.org/esr/faqs/smart-questions.html
|

November 30th, 2012, 04:26 AM
|
 |
SQL Consultant
|
|
Join Date: Feb 2003
Location: Toronto Canada
|
|
Quote: | Originally Posted by hiya1992 I'm still getting a CONSTRAINT error as well using the below codes. | because you're trying to declare a foreign key for a column that you have not included in the table
wrong --
CREATE TABLE wrong
( foo INTEGER
, bar INTEGER
, CONSTRAINT x FOREIGN KEY (fap) REFERENCES humpty ( dumpty )
)
right --
CREATE TABLE right
( foo INTEGER
, bar INTEGER
, fap INTEGER
, CONSTRAINT x FOREIGN KEY (fap) REFERENCES humpty ( dumpty )
)
|
Developer Shed Advertisers and Affiliates
| Thread Tools |
Search this Thread |
|
|
|
| Display Modes |
Rate This Thread |
Linear Mode
|
|
Posting Rules
|
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts
HTML code is Off
|
|
|
|
|