//从MySQL导出表
CREATE TABLE `pairs` (
`parent` varchar(12) NOT NULL,
`child` varchar(12) NOT NULL
) ENGINE=InnoDB DEFAULT CHARSET=utf8;
ALTER TABLE `pairs` ADD PRIMARY KEY (`parent`,`child`);
/ ---------------------------------------------- -------------------------------------------------- ----------------
//从MySQL导出表
CREATE TABLE `dailyreports` (
`SquareNo` varchar(5) NOT NULL,
`id` varchar(12) NOT NULL,
`notes` longtext,
`dateReport` date DEFAULT NULL,
`signed1` varchar(20) DEFAULT NULL,
`signed2` varchar(20) NOT NULL
) ENGINE=InnoDB DEFAULT CHARSET=utf8;
DELIMITER $$
CREATE TRIGGER `dailyreport_BEFORE_INSERT` BEFORE INSERT ON `dailyreports` FOR EACH ROW BEGIN
declare SqNo varchar(5);
declare CUser varchar(12);
declare serNo INT;
Set SqNo = new.SquareNo;
Set CUser = new.id;
Select Serial Into serNo from squareserials where Category ='DR' and SquareNo = SqNo;
SET NEW.id = CONCAT(SqNo,'-DR',SUBSTRING('0000',1,4-LENGTH(CAST(serNo AS CHAR))),CAST(serNo AS CHAR));
update squareserials set Serial = Serial +1 where Category ='DR' and SquareNo = SqNo;
insert into activities (username,activity) values (CUser,CONCAT('Added Daily Report ',NEW.id));
END
$$
DELIMITER ;
ALTER TABLE `dailyreports` ADD PRIMARY KEY (`SquareNo`,`id`);
ALTER TABLE `dailyreports` ADD CONSTRAINT `FK_DR_SquareNo` FOREIGN KEY (`SquareNo`) REFERENCES `squareserials` (`SquareNo`) ON UPDATE CASCADE;
/ ---------------------------------------------- -------------------------------------------------- ----------------
以下语句不起作用,给出错误“#1215 - 无法添加外键约束”
ALTER TABLE pairs ADD CONSTRAINT FK_DR_Parent FOREIGN KEY (parent) REFERENCES dailyreports(id) ON UPDATE CASCADE ON DELETE RESTRICT;
任何想法都将不胜感激。
答案 0 :(得分:0)
问题在于:
ALTER TABLE pairs ADD CONSTRAINT FK_DR_Parent FOREIGN KEY (parent) REFERENCES dailyreports(id) ON UPDATE CASCADE ON DELETE RESTRICT;
但是
id varchar(12) NOT NULL
,
dailyreports
中的既不是主要的也不是唯一的。您只能在基表中引用primary
或unique
的列来创建外键。因此,将列设为关键列或唯一,然后重试。
答案 1 :(得分:0)
使C:\OpenGTS_2.6.4\bin>initdb.bat
"C:\Program Files\Java\jdk1.8.0_45\bin\java" "-Dfile.encoding=UTF-8" -classpath
"C:\OpenGTS_2.6.4\build\lib"\gtsdb.jar;"C:\OpenGTS_2.6.4\build\lib"\gtsutils.jar;"C:\OpenGTS_2.6.4\build\lib"\optdb.jar;"C:\OpenGTS_2.6.4\build\lib"\ruledb.jar;"C:\OpenGTS_2.6.4\build\lib"\bcrossdb.jar;"C:\OpenGTS_2.6.4\build\lib"\custom.jar;"C:\OpenGTS_2.6.4\build\lib"\dmtpserv.jar;"C:\OpenGTS_2.6.4\build\lib"\gtsdmtp.jar; org.opengts.db.DBConfig -conf:"C:\OpenGTS_2.6.4\default.conf"
-log.file.enable:false -initTables
Version: 2.6.4
[ERROR|DBAdmin.execCommands:1006] SQLException message: Access denied for user 'root'@'localhost' (using password: NO)
[ERROR|DBAdmin.execCommands:1007] ==> SQLException: DB create error [jdbc:mysql://localhost:3306/]
[ERROR|DBAdmin.execCommands:1007] Message: Access denied for user 'root'@'localhost' (using password: NO)
[ERROR|DBAdmin.execCommands:1007] SQLState: 28000
[ERROR|DBAdmin.execCommands:1007] ErrorCode: 1045
[DBAdmin.execCommands:1007] java.sql.SQLException: Access denied for user 'root'@'localhost' (using password: NO)
java.sql.SQLException: Access denied for user 'root'@'localhost' (using password: NO)
at com.mysql.jdbc.SQLError.createSQLException(SQLError.java:998)
at com.mysql.jdbc.MysqlIO.checkErrorPacket(MysqlIO.java:3835)
at com.mysql.jdbc.MysqlIO.checkErrorPacket(MysqlIO.java:3771)
at com.mysql.jdbc.MysqlIO.checkErrorPacket(MysqlIO.java:870)
at com.mysql.jdbc.MysqlIO.proceedHandshakeWithPluggableAuthentication(MysqlIO.java:1659)
at com.mysql.jdbc.MysqlIO.doHandshake(MysqlIO.java:1206)
at com.mysql.jdbc.ConnectionImpl.coreConnect(ConnectionImpl.java:2239)
at com.mysql.jdbc.ConnectionImpl.connectOneTryOnly(ConnectionImpl.java:2270)
at com.mysql.jdbc.ConnectionImpl.createNewIO(ConnectionImpl.java:2069)
at com.mysql.jdbc.ConnectionImpl.<init>(ConnectionImpl.java:794)
at com.mysql.jdbc.JDBC4Connection.<init>(JDBC4Connection.java:44)
at sun.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method)
at sun.reflect.NativeConstructorAccessorImpl.newInstance(NativeConstructorAccessorImpl.java:62)
at sun.reflect.DelegatingConstructorAccessorImpl.newInstance(DelegatingConstructorAccessorImpl.java:45)
at java.lang.reflect.Constructor.newInstance(Constructor.java:422)
at com.mysql.jdbc.Util.handleNewInstance(Util.java:389)
at com.mysql.jdbc.ConnectionImpl.getInstance(ConnectionImpl.java:399)
at com.mysql.jdbc.NonRegisteringDriver.connect(NonRegisteringDriver.java:325)
at java.sql.DriverManager.getConnection(DriverManager.java:664)
at java.sql.DriverManager.getConnection(DriverManager.java:247)
at org.opengts.dbtools.DBConnection.getConnection(DBConnection.java:809)
at org.opengts.dbtools.DBConnection.createStatement(DBConnection.java:945)
at org.opengts.dbtools.DBConnection.createStatement(DBConnection.java:933)
at org.opengts.dbtools.DBConnection._executeUpdate(DBConnection.java:1188)
at org.opengts.dbtools.DBConnection.executeUpdate(DBConnection.java:1143)
at org.opengts.dbtools.DBConnection.executeUpdate(DBConnection.java:1124)
at org.opengts.dbtools.DBProvider.createDatabase(DBProvider.java:1699)
at org.opengts.dbtools.DBAdmin.execCommands(DBAdmin.java:995)
at org.opengts.db.DBConfig._main(DBConfig.java:2036)
at org.opengts.db.DBConfig.main(DBConfig.java:2180)
与Not Null一起成为唯一。
或将其设为主键