Reputation: 265
I am not able to create Foreign key in phpmyadmin . I used the following queries to create 2 tables.The first table was created but when i use 2nd query i got the error as
#1064 - 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 'order ( Orderid INT NOT NULL, cost INT, CustId INT NOT NULL, PRIMARY KEY(Or' at line 1
This is my first table:
CREATE TABLE customer
(
CustId INT NOT NULL,
first_name VARCHAR(30),
PRIMARY KEY (CustId)
);
and this is my 2nd table:
CREATE TABLE order
(
Orderid INT NOT NULL,
cost INT,
CustId INT NOT NULL,
PRIMARY KEY(Orderid),
INDEX (CustId),
FOREIGN KEY (CustId) REFERENCES customer (CustId)
);
So what i am doing wrong here? how do i create this tables and also implement the foreign key in phpmyadmin.
Upvotes: 0
Views: 4664
Reputation: 5034
Also this would help you:
If you ask to me, backticks should always be used. But there are some reasons why a team may prefer not to use them.
Advantages:
Disadvantages:
Refer: http://dev.mysql.com/doc/refman/5.0/en/identifiers.html
Upvotes: 0
Reputation: 780673
order
is a MySQL reserved word (it's used in ORDER BY
clauses), you have to enclose it in backticks if you want to use it as the name of a table or column.
CREATE TABLE `order`
(
Orderid INT NOT NULL,
cost INT,
CustId INT NOT NULL,
PRIMARY KEY(Orderid),
INDEX (CustId),
FOREIGN KEY (CustId) REFERENCES customer (CustId)
);
You'll have to remember to put it in backticks whenever you reference it in a query. You'll save yourself lots of trouble if you simply choose a different name for the table.
Upvotes: 1