我要创建一个具有两个表的数据库。我想在两个表之间创建更新级联关系。如果在EMPLOYEE中更改了SNAME,则WORKREPORT也将更改其SNAME。

EMPLOYEE:
SNO      SNAME           SPASSWORD     SEX      BDATE           HEIGHT    BTITLE
2014      boss            12345        male      1987-06-02       180       Manager
2015      Tom             1234567      male      1987-06-05       180       Employee


WORKREPORT
   SNO       SNAME             SDATA         SCHECKLIST       SIMAGE
   2014      boss            1987-06-02      abc               afafafaf
   2015      Tom             1987-06-05      affafa            afafafaf


我的代码效果很好。我的问题是:假设有很多员工,我不知道如何仅从“ WORKREPORT”查询员工的信息(而不是经理)。我该怎么办?我对“ WORKREPORT”的设计是否正确?
这是我的代码:

CREATE database  if not exists cm360_cm360_1;
use cm360_cm360_1;
CREATE TABLE IF NOT EXISTS EMPLOYEE(SNO VARCHAR(7) NOT NULL,  SNAME VARCHAR(8) NOT NULL,  SPASSWORD VARCHAR(11) NOT NULL,SEX VARCHAR(8) NOT NULL,  BDATE DATETIME NOT NULL,  HEIGHT DEC(5,2) DEFAULT 000.00,BTitle VARCHAR(15) NOT NULL,  PRIMARY KEY(SNO), UNIQUE KEY (SNAME))ENGINE=InnoDB ;

SET SQL_SAFE_UPDATES=0;
INSERT INTO EMPLOYEE VALUES (2014,'boss','12345','male','2014-6-10 11:00:00',160.00,'Manager');
INSERT INTO EMPLOYEE VALUES (2015,'Tom','1234567','male','2014-6-10 12:00:00',160.00,'Employee');
SELECT * FROM EMPLOYEE;


CREATE TABLE IF NOT EXISTS WORKREPORT(SNO VARCHAR(7) NOT NULL,  SNAME VARCHAR(8) NOT NULL,SDATA DATETIME ,  SCHECKLIST VARCHAR(150),SIMAGE VARCHAR(20),FOREIGN KEY (SNO) REFERENCES EMPLOYEE (SNO) ON UPDATE CASCADE,FOREIGN KEY(SNAME) REFERENCES EMPLOYEE (SNAME) ON UPDATE CASCADE ) ENGINE=InnoDB;


INSERT INTO WORKREPORT VALUES (2014,'boss',' 2014-6-10 14:38:59','abc','afdsfdfds');
INSERT INTO WORKREPORT VALUES (2015,'Tom',' 2014-6-10 15:38:59','abc','afdsfdfds');
SELECT * FROM  WORKREPORT order by SDATA ASC;

UPDATE WORKREPORT SET SCHECKLIST='elevator;floor' WHERE SNAME='hanlu2';
delete from workreport WHERE  SIMAGE='N/A' AND SNAME='enlan';

最佳答案

如果要获取Employee WORKREPORT信息,则下面的查询应该有效。

SELECT WORKREPORT.*
FROM WORKREPORT, EMPLOYEE
WHERE EMPLOYEE.SNO = WORKREPORT.SNO
AND EMPLOYEE.BTITLE = "Employee"

10-07 12:31