什么是执行sql语法的正确语法?

时间:2015-05-28 06:31:03

标签: mysql

String ag ="ALTER TABLE QUESTION"+
            "(ADD  FOREIGN KEY (a_status) REFERENCES ANSWER(a_status))";

                      stmt.executeUpdate(ag);
  

错误讯息:   com.mysql.jdbc.exceptions.jdbc4.MySQLSyntaxErrorException:你有一个   SQL语法错误;查看与您的手册相对应的手册   MySQL服务器版本,用于在' FOREIGN KEY附近使用正确的语法   (a_status)REFERENCES ANSWER(a_status))'在第1行

3 个答案:

答案 0 :(得分:0)

这应该是正确的。请参阅offical documentation

String ag ="ALTER TABLE QUESTION ADD FOREIGN KEY (a_status) REFERENCES ANSWER(a_status)";

答案 1 :(得分:0)

String ag ="ALTER TABLE QUESTION ADD CONSTRAINT (question_answer_ibfk1) FOREIGN KEY (a_status) REFERENCES ANSWER(a_status)";

一般

ALTER TABLE Child_tblName
ADD CONSTRAINT 'give any name to the foreign key generally I do Child_tblName_Parent_tblName_ibfk_number' FOREIGN KEY ('Child_tblName's Column') REFERENCES Parent_tblName('Parent_tblName's Column')

答案 2 :(得分:0)

String ag ="ALTER TABLE QUESTION ADD CONSTRAINT (question_answer_fk1) FOREIGN KEY (a_status) REFERENCES ANSWER(a_status)";

此外,您还可以添加更多特定条件:

  • ON DELETE SET NULL / CASCADE
  • ON UPDATE SET NULL