简体   繁体   English

ALTER TABLE FOREIGN KEY从java到mysql

[英]ALTER TABLE FOREIGN KEY from java to mysql

Well, I want to make a modification in my database, so I need to use alter table but java seems to have problems making that. 好吧,我想在我的数据库中进行修改,所以我需要使用alter table,但java似乎有问题。

This is the sentence 这是一句话

ALTER TABLE loans ADD FOREIGN KEY (id_reader) REFERENCES readers (id);

how do you execute it? 你怎么执行它?

I was doing this: 我这样做:

rawStatement="ALTER TABLE loans ADD FOREIGN KEY (id_reader) REFERENCES readers (id);";
currentStatement = conn.createStatement();
currentStatement.execute(rawStatement); 

is the last line correct? 最后一行是否正确?

As far I know, execute must run everything. 据我所知, execute必须运行一切。

Use this : 使用这个

executeUpdate() 

instead of 代替

execute()

Also if the constraint is already present it will throw an exception 此外,如果约束已经存在,它将抛出异常

Other things you should look out for: 你应该注意的其他事项:

  • Does the user have the privileges to alter the table? 用户是否具有更改表的权限?

Try with the below code: 尝试使用以下代码:

rawStatement="ALTER TABLE loans ADD FOREIGN KEY (id_reader) REFERENCES readers (id)"; 
PreparedStatement ps = conn.prepareStatement(rawStatement);
ps.executeUpdate();

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM