使用另一个 table 的属性值作为 MYSQL 中另一个 table 的属性的默认值

Use the value of an attribute from another table as a default value of an attribute from another table in MYSQL

我想在 table tbl_student 中使用 student_lastname 上的值作为 sis_password 中的默认值 table tbl_sis_account 但我不知道该怎么做。我尝试在“默认”之后放置“Select 查询”,但它不起作用,无论如何这里是 sql:

DROP TABLE IF EXISTS tbl_sis_account;
CREATE TABLE `tbl_sis_account`(
     sis_account_id INT(15) NOT NULL AUTO_INCREMENT,
     sis_username INT(15) NOT NULL,
     sis_password VARCHAR(8) DEFAULT '====>Value of attribute student_lastname<====',
     PRIMARY KEY(`sis_account_id`),
     CONSTRAINT `sis_username_student_fk` FOREIGN KEY (`sis_username`) REFERENCES `tbl_student` 
     (`student_id`) ON UPDATE CASCADE
)ENGINE=INNODB AUTO_INCREMENT=1 DEFAULT CHARSET=utf8mb4;
SELECT * FROM tbl_sis_account;
DROP TABLE IF EXISTS tbl_student;
CREATE TABLE `tbl_student` (
    `student_id` INTEGER(15) NOT NULL AUTO_INCREMENT,
    `student_firstname` VARCHAR(50) NOT NULL,
    `student_midname` VARCHAR(50) NOT NULL,
    `student_lastname` VARCHAR(50) NOT NULL,
    PRIMARY KEY(`student_id`)
)ENGINE=INNODB AUTO_INCREMENT=20201 DEFAULT CHARSET=utf8mb4;
SELECT * FROM tbl_student;

不,你不能那样做,但你可以在 tbl_sis_account table 中插入时查询 tbl_student table 以检索 student_lastname 来自 `student_id' 通过嵌套 sql 查询或触发器。

我想出了这个问题的解决方案,已经有一段时间了。不过忘了 post 答案,因为我不再使用这种方法了。但这是我所做的。

在 tbl_student 我创建了一个“插入后触发器

 BEGIN 
    INSERT INTO tbl_sis_account (student_id,sis_password) values (new.student_id, concat(new.student,new.student_lastname));
    END

所以 tbl_sis_account 上的插入结果是

student_id  |  sis_password
20200001    |  202000001Doe