我的问题是:我应该如何编辑下表:
CREATE TABLE users(
id int UNSIGNED AUTO_INCREMENT PRIMARY KEY,
name varchar(20) NOT NULL,
surname varchar(20) NOT NULL,
faculty varchar(35) NOT NULL,
username varchar(20) NOT NULL UNIQUE,
password varchar(20) NOT NULL,
is_admin boolean NOT NULL DEFAULT 0,
account_limit int UNSIGNED,
created_at datetime
) CHARACTER SET utf8 COLLATE utf8_general_ci;
CREATE TABLE test_results(
id int UNSIGNED AUTO_INCREMENT PRIMARY KEY,
test_id int UNSIGNED NOT NULL,
test_person_id int UNSIGNED NOT NULL,
score float UNSIGNED NOT NULL,
standard_deviation float UNSIGNED NOT NULL,
average_answer_time float UNSIGNED NOT NULL,
removed boolean NOT NULL DEFAULT 0,
created_at datetime,
FOREIGN KEY (test_id) REFERENCES test_tasks(id) ON UPDATE CASCADE, FOREIGN KEY (test_person_id) REFERENCES test_persons(id) ON UPDATE CASCADE) CHARACTER SET utf8 COLLATE utf8_general_ci;
因此,如果我从用户表中删除一行,如果 test_results 中的一行具有引用用户表行的外键,我不会在 test_results 表中得到任何约束错误等?
更新:对不起,我在提出问题时犹豫了:
这是我所有的桌子:
CREATE TABLE users(
id int UNSIGNED AUTO_INCREMENT PRIMARY KEY,
name varchar(20) NOT NULL,
surname varchar(20) NOT NULL,
faculty varchar(35) NOT NULL,
username varchar(20) NOT NULL UNIQUE,
password varchar(20) NOT NULL,
is_admin boolean NOT NULL DEFAULT 0,
account_limit int UNSIGNED,
created_at datetime
) CHARACTER SET utf8 COLLATE utf8_general_ci;
CREATE TABLE test_groups(
id int UNSIGNED AUTO_INCREMENT PRIMARY KEY,
user_id int UNSIGNED NOT NULL,
name varchar(60) NOT NULL,
password varchar(20) NOT NULL,
tests varchar(60),
created_at datetime,
FOREIGN KEY (user_id) REFERENCES users(id) ON DELETE CASCADE ON UPDATE CASCADE
) CHARACTER SET utf8 COLLATE utf8_general_ci;
CREATE TABLE test_persons(
id int UNSIGNED AUTO_INCREMENT PRIMARY KEY,
group_id int UNSIGNED NOT NULL,
username varchar(45) NOT NULL UNIQUE,
tries int UNSIGNED NOT NULL,
created_at datetime,
FOREIGN KEY (group_id) REFERENCES test_groups(id) ON DELETE CASCADE ON UPDATE CASCADE
) CHARACTER SET utf8 COLLATE utf8_general_ci;
CREATE TABLE test_tasks(
id int UNSIGNED AUTO_INCREMENT PRIMARY KEY,
user_id int UNSIGNED NOT NULL,
name varchar(45) NOT NULL,
type varchar(20) NOT NULL,
description text NOT NULL,
question_time int UNSIGNED DEFAULT 500,
answer_time int UNSIGNED DEFAULT 3000,
random_questions boolean NOT NULL DEFAULT 1,
questions varchar(150),
created_at datetime,
FOREIGN KEY (user_id) REFERENCES users(id) ON DELETE CASCADE ON UPDATE CASCADE) CHARACTER SET utf8 COLLATE utf8_general_ci;
CREATE TABLE test_results(
id int UNSIGNED AUTO_INCREMENT PRIMARY KEY,
test_id int UNSIGNED NOT NULL,
test_person_id int UNSIGNED NOT NULL,
score float UNSIGNED NOT NULL,
standard_deviation float UNSIGNED NOT NULL,
average_answer_time float UNSIGNED NOT NULL,
removed boolean NOT NULL DEFAULT 0,
created_at datetime,
FOREIGN KEY (test_id) REFERENCES test_tasks(id) ON UPDATE CASCADE, FOREIGN KEY (test_person_id) REFERENCES test_persons(id) ON UPDATE CASCADE) CHARACTER SET utf8 COLLATE utf8_general_ci;
现在如果我尝试从用户表中删除一行我得到约束错误,为什么?我是 mysql 新手