开发者

How to add three foreign key for same child table with three master tables?

开发者 https://www.devze.com 2023-04-11 22:21 出处:网络
Here I have four tables in my database named test_center. Those are: test_user --> PK = u_id test_metadata --> PK = test_id

Here I have four tables in my database named test_center. Those are:

  1. test_user --> PK = u_id
  2. test_metadata --> PK = test_id
  3. student_detail --> PK = Student_id
  4. test_records --> PK = test_record_id (Child table)

Table 4 is child table and 1, 2, 3 are masters.

I am trying for test_records table to have three foreign keys from each of these master tables, but it i开发者_开发问答s showing some errors as follows. (I am using MySQL Workbench 5.0 and MySQL server)

ERROR 1005: Can't create table 'test_center.#sql-aa4_12' (errno: 121)

SQL Statement:

ALTER TABLE `test_center`.`test_records` 
  ADD CONSTRAINT `Student_id`
  FOREIGN KEY (`Student_id` )
  REFERENCES `test_center`.`student_detail` (`Student_id` )
  ON DELETE CASCADE
  ON UPDATE CASCADE, 
  ADD CONSTRAINT `test_id`
  FOREIGN KEY (`test_id` )
  REFERENCES `test_center`.`test_metadata` (`test_id` )
  ON DELETE CASCADE
  ON UPDATE CASCADE, 
  ADD CONSTRAINT `u_id`
  FOREIGN KEY (`u_id` )
  REFERENCES `test_center`.`test_user` (`u_id` )
  ON DELETE CASCADE
  ON UPDATE CASCADE

ERROR: Error when running failback script. Details follow. ERROR 1046: No database selected

SQL Statement:

CREATE TABLE `test_records` (
  `test_record_id` int(11) NOT NULL AUTO_INCREMENT,
  `test_name` varchar(45) NOT NULL,
  `Result` float NOT NULL,
  `status` varchar(45) NOT NULL,
  `Student_id` varchar(45) NOT NULL,
  `u_id` int(11) NOT NULL,
  `test_id` int(11) NOT NULL,`enter code here`
  PRIMARY KEY (`test_record_id`)
) ENGINE=InnoDB DEFAULT CHARSET=latin1


Your query is unable to identify the database. You need to tell MySQL which database to use:

USE database_name;

before you create a table.In case the database does not exist, you need to create it as:

CREATE DATABASE database_name;

followed by

 USE database_name;
0

精彩评论

暂无评论...
验证码 换一张
取 消