Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/mysql/64.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/database/8.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Mysql 完全是幸福问题_Mysql_Database_Triggers_Sql Insert_Cascade - Fatal编程技术网

Mysql 完全是幸福问题

Mysql 完全是幸福问题,mysql,database,triggers,sql-insert,cascade,Mysql,Database,Triggers,Sql Insert,Cascade,所以我创建了我的lil数据库,然后使用它。我添加了两个表,现在我想做的就是:当一个用户被插入到USERS表中时,还要将一些默认信息插入到user\u ACCOUNT表中,该表对应于新插入的用户。很明显我做错了什么,但我不知道。。。提前感谢。=) 在没有看到错误消息的情况下,我只是在猜测,但我怀疑您没有满足FK约束这一事实是罪魁祸首。你的触发代码应该是 DELIMITER // CREATE TRIGGER makeDefaultUserAccount AFTER INSERT ON

所以我创建了我的lil数据库,然后使用它。我添加了两个表,现在我想做的就是:当一个用户被插入到USERS表中时,还要将一些默认信息插入到user\u ACCOUNT表中,该表对应于新插入的用户。很明显我做错了什么,但我不知道。。。提前感谢。=)


在没有看到错误消息的情况下,我只是在猜测,但我怀疑您没有满足FK约束这一事实是罪魁祸首。你的触发代码应该是

DELIMITER //
  CREATE TRIGGER makeDefaultUserAccount
    AFTER INSERT ON USERS
    For each row
    BEGIN
      INSERT INTO USER_ACCOUNT 
          (OwnerUserID, UserAccountName, UserAccountType, UserAccountBalence) 
        values (NEW.UserID, 'Default Account','default', 100);
    END; //
DELIMITER ;
它将
USER\u ACCOUNT.OwnerUserID
设置为刚刚插入的
USERS.UserID
的值

CREATE DATABASE REST_PROJECT;
USE REST_PROJECT;

CREATE TABLE `USERS` (
    UserID              BIGINT(20) UNSIGNED PRIMARY KEY AUTO_INCREMENT NOT NULL,
    UserEmail           VARCHAR(50),
    UserPassword        VARCHAR(30)
);

CREATE TABLE `USER_ACCOUNT` (
    UserAccountID         BIGINT(20) UNSIGNED PRIMARY KEY AUTO_INCREMENT NOT NULL,
    UserID                BIGINT(20) UNSIGNED NOT NULL,
    UserAccountName       VARCHAR(30),
    UserAccountType       VARCHAR(10) NOT NULL,
    UserAccountBalance    DECIMAL(19,6),
    CONSTRAINT `fk_USER_ACCOUNT_UserID` FOREIGN KEY (`UserID`)
        REFERENCES `USERS`.`UserID` (`UserID`)
        ON DELETE CASCADE
        ON UPDATE CASCADE
);

DELIMITER //CREATE TRIGGER `makeDefaultUserAccount`
AFTER INSERT ON `USERS`
FOR EACH ROW BEGIN
    INSERT INTO USER_ACCOUNT (OwnerUserID, UserAccountName, UserAccountType, UserAccountBalance) 
    VALUES (NEW.UserID, 'Default Account','default', 100);
END//
DELIMITER ;  
注释

  • 请注意
    主键的
    数据类型
    中的更改,是的,如果您希望系统中的
    大规模数据将是适合您的
    初始设置
  • 您的
    用户帐户
    表的
    外键
    部分的更改也应符合
    参考表列
    的相关键,并且对于任何更改也应
    级联
  • UserAccountBalance
    更改为
    UserAccountBalance
    ,以及
    数据类型
    ,以适应将保存在所述列中的
    货币值
  • 关于
    USERS
    表中的
    UserEmail
    列,从
    30
    更改为
    50
    ,您需要给应用程序一些喘息的空间,以防验证突然失败
  • 更新了触发区域,以保存将在
    外键中使用的

那么当你运行这个时实际会发生什么呢?我对创建表
USER\u ACCOUNT
语句做了一个小小的更改:不是引用
USERS
UserID
UserID
)而是引用
USERS
),一切都正常了。
CREATE DATABASE REST_PROJECT;
USE REST_PROJECT;

CREATE TABLE `USERS` (
    UserID              BIGINT(20) UNSIGNED PRIMARY KEY AUTO_INCREMENT NOT NULL,
    UserEmail           VARCHAR(50),
    UserPassword        VARCHAR(30)
);

CREATE TABLE `USER_ACCOUNT` (
    UserAccountID         BIGINT(20) UNSIGNED PRIMARY KEY AUTO_INCREMENT NOT NULL,
    UserID                BIGINT(20) UNSIGNED NOT NULL,
    UserAccountName       VARCHAR(30),
    UserAccountType       VARCHAR(10) NOT NULL,
    UserAccountBalance    DECIMAL(19,6),
    CONSTRAINT `fk_USER_ACCOUNT_UserID` FOREIGN KEY (`UserID`)
        REFERENCES `USERS`.`UserID` (`UserID`)
        ON DELETE CASCADE
        ON UPDATE CASCADE
);

DELIMITER //CREATE TRIGGER `makeDefaultUserAccount`
AFTER INSERT ON `USERS`
FOR EACH ROW BEGIN
    INSERT INTO USER_ACCOUNT (OwnerUserID, UserAccountName, UserAccountType, UserAccountBalance) 
    VALUES (NEW.UserID, 'Default Account','default', 100);
END//
DELIMITER ;