java中如何声明外键约束,外键约束不正确 - java-mysql
what is the mysql problem ??
I do not know what the error is since I had not used foreign keys before
String clientes = "CREATE TABLE clientes("+
"ID INTEGER NOT NULL AUTO_INCREMENT,"+
"CUENTA INTEGER,"+
"NOMBRE VARCHAR(255),"+
"EDAD INTEGER,"+
"ADICIONAL INTEGER,"+
"DOMICILIO VARCHAR(255),"+
"PRIMARY KEY(ID))";
String cuenta = "CREATE TABLE cuenta("+
"CUENTA INTEGER,"+
"SALDOAFAVOR DOUBLE(14,2),"+
"SALDOENCONTRA DOUBLE(14,2),"+
"FECHACORTE DATE,"+
"LIMITECREDITO DOUBLE(14,2),"+
"FOREIGN KEY(CUENTA) REFERENCES clientes(CUENTA))";
解決方案
The correct reference and definition would use the primary key:
CREATE TABLE cuenta (
CUENTA INTEGER AUTO_INCREMENT PRIMARY KEY,
CLIENTE_ID INT,
SALDOAFAVOR NUMERIC(14, 2),
SALDOENCONTRA NUMERIC(14, 2),
FECHACORTE DATE,
LIMITECREDITO NUMERIC(14,2),
FOREIGN KEY(CLIENT_ID) REFERENCES clientes(ID)
);
Notes:
CUENTA seems like it should be the primary key of this table.
The foreign key reference should use the id on CLIENTES.
You don't need CUENTA on CLIENTES.
DOUBLE(14, 2) is not the right data type. You want DECIMAL(14, 2)/NUMERIC(14, 2).
總結
以上是生活随笔為你收集整理的java中如何声明外键约束,外键约束不正确 - java-mysql的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: yaml parse python_py
- 下一篇: php根据分类生成网址,PHP实现无限极