0

私の質問は次のとおりです。次のテーブルをどのように編集する必要がありますか。

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;

したがって、users-tableから行を削除しても、test_resultsの行にuser-table行を参照する外部キーを持つ行がある場合、test_results-tableに制約エラーなどが発生しませんか?

更新:申し訳ありませんが、質問をするときに躊躇しました:

これが私のすべてのテーブルです:

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を初めて使用します

4

1 に答える 1

2

書きます

ALTER TABLE `test_results`
ADD CONSTRAINT `tbl_userplan_ibfk_1` 
FOREIGN KEY (`test_person_id`) REFERENCES `test_persons(id)`  
ON DELETE CASCADE ON UPDATE CASCADE

および外部キーは、@David Wがコメントで説明した方法で機能しました

于 2012-10-05T13:30:54.327 に答える