按过程将外键插入表中不受影响

时间:2018-10-18 15:21:28

标签: mysql mysql-workbench procedure create-table

下面是我用来向表中插入数据的过程,但是当我试图向表中插入数据时,tdd_tv_ZvrstOddaje始终为0。这是因为约束吗?我该如何处理?

创建语句:

CREATE TABLE IF NOT EXISTS `pb2_pb`.`Zvrst_oddaje` (
  `ID_Zvrst` INT NOT NULL,
  `vrstaOddaje` VARCHAR(45) NOT NULL
)
ENGINE = InnoDB;

ALTER TABLE Zvrst_oddaje
ADD CONSTRAINT PK_zvrst_oddaje PRIMARY KEY (ID_Zvrst);

ALTER TABLE Zvrst_oddaje MODIFY COLUMN ID_Zvrst INT AUTO_INCREMENT;

-- -----------------------------------------------------
-- Table `pb2_pb`.`Spored`
-- -----------------------------------------------------
CREATE TABLE IF NOT EXISTS `pb2_pb`.`Spored` (
  `ID_Spored` INT NOT NULL,
  `datumPredvajanja` DATE NOT NULL,
  `casPredvajanja` TIME NOT NULL,
  `aktualno` VARCHAR(45) NOT NULL
)
ENGINE = InnoDB;

ALTER TABLE Spored
ADD CONSTRAINT PK_Spored PRIMARY KEY (ID_Spored);

ALTER TABLE Spored MODIFY COLUMN ID_Spored INT AUTO_INCREMENT;

-- -----------------------------------------------------
-- Table `pb2_pb`.`Oddaja`
-- -----------------------------------------------------
CREATE TABLE IF NOT EXISTS `pb2_pb`.`Oddaja` (
  `ID_Oddaja` INT NOT NULL,
  `imeOddaje` VARCHAR(45) NOT NULL,
  `dolzinaTrajanja` DATETIME NOT NULL,
  `frekvencaOddaje` VARCHAR(45) NOT NULL,
  `TK_ZvrstOddaje` INT NOT NULL,
  `TK_Spored` INT NOT NULL
  )
ENGINE = InnoDB;

ALTER TABLE Oddaja
ADD CONSTRAINT PK_Oddaja PRIMARY KEY (ID_Oddaja);

ALTER TABLE Oddaja
ADD CONSTRAINT FK_Zvrst_oddaje FOREIGN KEY (TK_ZvrstOddaje) REFERENCES Zvrst_oddaje(ID_Zvrst);

ALTER TABLE Oddaja
ADD CONSTRAINT FK_Spored FOREIGN KEY (TK_Spored) REFERENCES Spored(ID_Spored);

ALTER TABLE Oddaja MODIFY COLUMN ID_Oddaja INT AUTO_INCREMENT;

程序:

   DELIMITER //
CREATE PROCEDURE polni_zvrst_oddaje (st_zvrst_oddaje INT)
    BEGIN

        DECLARE vrsta_oddaje VARCHAR(45);
        DECLARE stevec INT;
        DECLARE konecVal INT;

        SET stevec = 0;
        SET konecVal = st_zvrst_oddaje;

        WHILE (stevec < konecVal) DO
            SET vrsta_oddaje = "Vrsta";

            INSERT INTO `pb2_pb`.`Zvrst_oddaje` (`vrstaOddaje`) VALUES (CONCAT(vrsta_oddaje, (stevec + 1)));

            SET stevec = stevec + 1;
        END WHILE;
    END //

DELIMITER ;

-- -----------------------------------------------------
-- Table `pb2_pb`.`Spored`
-- -----------------------------------------------------
DELIMITER //
CREATE PROCEDURE polni_spored (st_sporeda INT)
    BEGIN
        DECLARE datum_predvajanja DATETIME;
        DECLARE cas_predvajanja TIME;
        DECLARE aktualno_ne VARCHAR(45);
        DECLARE aktualno_da VARCHAR(45);
        DECLARE stevec INT;
        DECLARE konecVal INT;

        SET stevec = 0;
        SET konecVal = st_sporeda;

        WHILE (stevec < konecVal) DO
            SET datum_predvajanja = (SELECT DATE_FORMAT(FROM_UNIXTIME(UNIX_TIMESTAMP(NOW()) + FLOOR(0 + (RAND() * 63072000))), '%Y-%m-%d'));
            SET cas_predvajanja = (SELECT DATE_FORMAT(FROM_UNIXTIME(UNIX_TIMESTAMP(NOW()) + FLOOR(0 + (RAND() * 63072000))), ' %H:%i:%s'));
            SET aktualno_da = 'DA';
            SET aktualno_ne = 'NE';

            IF stevec%3=0 THEN
                INSERT INTO `pb2_pb`.`Spored` (`datumPredvajanja`, `casPredvajanja`, `aktualno`) VALUES (datum_predvajanja, cas_predvajanja, aktualno_ne);
            ELSE
                INSERT INTO `pb2_pb`.`Spored` (`datumPredvajanja`, `casPredvajanja`, `aktualno`) VALUES (datum_predvajanja, cas_predvajanja, aktualno_da);
            END IF;

            SET stevec = stevec + 1;
        END WHILE;
    END //

DELIMITER ;
-- -----------------------------------------------------
-- Table `pb2_pb`.`Oddaja`
-- -----------------------------------------------------
DELIMITER //
CREATE PROCEDURE polni_oddaja (st_oddaj INT)
    BEGIN
        DECLARE ime_oddaje VARCHAR(45);
        DECLARE dolzina_trajanja DATETIME;
        DECLARE frekvenca VARCHAR(45);
        DECLARE tk_zvrstOddaja INT;
        DECLARE tk_spored INT;
        DECLARE stevec INT;
        DECLARE konecVal INT;

        DECLARE stevec_2 INT;
        DECLARE st_zv_oddaj INT;


        DECLARE fertik INT DEFAULT FALSE;
        DECLARE kurzor_1 CURSOR FOR SELECT zo.ID_Zvrst FROM `pb2_pb`.`Zvrst_oddaje` zo;
        DECLARE CONTINUE HANDLER FOR NOT FOUND SET fertik = TRUE;


        SET stevec_2 = 0;

        SET st_zv_oddaj = (SELECT COUNT(*) FROM `pb2_pb`.`Zvrst_oddaje`);

        OPEN kurzor_1;

        WHILE stevec_2 < st_zv_oddaj DO
            FETCH kurzor_1 INTO tk_zvrstOddaja;

            SET stevec = 0;
            SET konecVal = st_oddaj;

            WHILE (stevec < konecVal) DO
                SET dolzina_trajanja = NOW() - INTERVAL FLOOR(RAND() * 1000) DAY;
                SET ime_oddaje = "Oddaja";
                SET frekvenca = "45";

                SET tk_spored = FLOOR(RAND() *(SELECT COUNT(*) FROM `pb2_pb`.`Spored`)) + 1;

                INSERT INTO `pb2_pb`.`Oddaja` (`imeOddaje`, `dolzinaTrajanja`, `frekvencaOddaje`, `TK_ZvrstOddaje`, `TK_Spored`) VALUES (ime_oddaje, dolzina_trajanja, CONCAT(frekvenca, (stevec + 1)), tk_zvrstOddaje, tk_spored);

                SET stevec = stevec + 1;
            END WHILE;

        SET stevec_2 = stevec_2 + 1;
        END WHILE;

        CLOSE kurzor_1;
    END //

DELIMITER ;

我正在尝试致电polni_zvrst_oddaje(3),polni_spored(10),polni_oddaja(10)

The result shown in table Oddaja

0 个答案:

没有答案