-1
zone_id(PK)   ZoneName
-----------   --------
1             east
2             west
3             north
4             south

tbluser

usrId(PK) userzoneId(FK to tblzone)   username
--------  -------------------------   ------------
1         1                           manish
2         3                           rahul
3         2                           ankit
4         4                           amir
5         2                           rashmi
6         1                           akash
tbldoctor

docId(PK)    usrId(Fk to tbluser)      docname
--------     --------------------      ------------
1             2                        hemant
2             2                        chintu
3             3                        rahim
4             1                        salman
5             3                        kishor
6             3                        saurabh
7             2                        banti

tblmedAssign 

transId(Pk)  doctorId(FK to tbldoctor)    medId(FK)    dateInsert
----------   -------------------------    ------   -----------
1            2                            2        20/12/2012
2            3                            3        21/12/2012
3            2                            3        23/12/2012
4            4                            1        24/12/2012


tblunnormalize
 transId(Pk)       docname    username  ZoneName  dateInsert
-------------      -------    --------  --------  ----------

我想要的是有一个非规范化表,其中包含与所有这些表相关的数据。是否可以编写在运行时插入数据的过程意味着当数据插入 tblmedAssign 表时,数据应该在非规范化表中的特定时间插入?

4

1 回答 1

2

干得好 :

DELIMITER $$

DROP PROCEDURE IF EXISTS `medAssignProcedure`$$

CREATE

    PROCEDURE medAssignProcedure(IN transId INT, IN doctorId INT, IN medId INT )

    BEGIN
    DECLARE doctorname VARCHAR(100);
    DECLARE user_Name VARCHAR(100);
    DECLARE zone_name VARCHAR(100);
    DECLARE userId INT;
    DECLARE datetimenow DATETIME;
    SET datetimenow = NOW();
    INSERT INTO tblmedassign VALUES (transId,doctorId,medId,datetimenow);
    SET doctorname = (SELECT docname FROM tbldoctor WHERE docid = doctorId);
    SET userId = (SELECT usrid FROM tbldoctor WHERE docid = doctorId);
    SET user_Name = (SELECT username FROM tbluser WHERE usrid = userId);
    SET zone_name = (SELECT zonename FROM tblzone WHERE zone_id = (SELECT userzoneId FROM tbluser WHERE usrid = userId));   
    INSERT INTO tblunnormalize VALUES (transId, doctorname, user_Name, zone_name, datetimenow);

    END$$

DELIMITER ;
于 2012-09-28T05:42:40.663 回答