How to associate two tables without association table? - mysql

It's currently i am using .In that i want to reduces the association table .And attain the same objective.Kindly some one help me to find solution.
CREATE TABLE sys_student_t (
student_id MEDIUMINT NOT NULL AUTO_INCREMENT,
student_name VARCHAR(255),
PRIMARY KEY (student_id)
);
CREATE TABLE sys_classrooms_t (
classroom_id MEDIUMINT NOT NULL AUTO_INCREMENT,
classroom_name VARCHAR(255),
PRIMARY KEY (classroom_id)
);
CREATE TABLE student_classroom_association_t (
association_id MEDIUMINT NOT NULL AUTO_INCREMENT,
classroom_id MEDIUMINT NOT NULL,
student_id MEDIUMINT NOT NULL,
FOREIGN KEY (student_id) REFERENCES sys_student_t (student_id),
FOREIGN KEY (classroom_id) REFERENCES sys_classrooms_t (classroom_id),
PRIMARY KEY (association_id)
);
Here is fiddle link :- http://www.sqlfiddle.com/#!9/ada4fd/2
thanks

A "Many-To-Many" relationship will always require an additional table. The only thing you can do is changing the primary key of the association table to the pair student_id and classroom_id.
CREATE TABLE student_classroom_association_t (
classroom_id MEDIUMINT NOT NULL,
student_id MEDIUMINT NOT NULL,
FOREIGN KEY (student_id) REFERENCES sys_student_t (student_id),
FOREIGN KEY (classroom_id) REFERENCES sys_classrooms_t (classroom_id),
PRIMARY KEY (student_id, classroom_id)
);

Related

Why does it fail to open the referenced table if it already exists?

Somehow I can't create a join table for two entities I already have.
Here is what I run on MySQL just fine, the last one is the one I'm struggling with.
create table instructor_detail_table(
instructor_detail_id int not null primary key auto_increment,
instructor_class varchar(40) not null
);
create table instructor_table(
instructor_id int not null primary key auto_increment,
instructor_name varchar(40) not null,
instructor_detail_id int not null,
foreign key (instructor_detail_id) references instructor_detail_table (instructor_detail_id)
);
create table course_table(
course_id int not null primary key auto_increment,
course_class varchar(40) not null,
instructor_id int not null,
foreign key (instructor_id) references instructor_table (instructor_id)
);
create table student_table(
student_id int not null primary key auto_increment,
student_name varchar(40) not null,
student_age int not null
);
create table student_course_table(
student_id int not null,
course_id int not null,
foreign key (student_id) references student_table (student_id),
foreign key (course_id) references course_table (course_id),
primary key (student_id, course_id)
);
When I try to create the student_course_table MySQL complaints it can't open the referenced table student_table
Thanks in advance
You can have this problem if the storage engine is not the good one.
For example 'MyISAM' instead of 'InnoDB' for the table 'student_table'.
Because MyISAM doesn't support foreign key.

i am trying to create foreign keys but i got error 1822 .. please see my code below

CREATE TABLE employee(
empid int auto_increment primary key,
empfirstname varchar(200) not null,
emplastname varchar(200) not null,
email varchar(200) not null,
officenumber int not null
);
CREATE TABLE customer(
custid int auto_increment primary key,
firstname varchar(200) not null,
lastname varchar(200) not null,
address varchar(200) not null,
contact varchar(200)
);
CREATE TABLE product(
productid int auto_increment primary key,
productdesc varchar(500) not null,
weight int not null,
unit_cost int not null
);
CREATE TABLE productorder(
productid int,
orderid int,
primary key(productid,orderid),
constraint fk3 foreign key (productid) references product(productid),
constraint fk4 foreign key (orderid) references productorder(orderid)
);
CREATE TABLE salesorder(
salesorderid int auto_increment primary key,
empid int not null,
custid int not null,
orderdate date not null,
shippingmethod varchar (200) not null,
constraint a_fk1 foreign key (empid) references employee(empid),
constraint a_fk2 foreign key (custid) references customer(custid)
);
What is this meant to do?:
constraint fk4 foreign key (orderid) references productorder(orderid)
It's not uncommon for a table to have a foreign key back to its own primary key, such as for records which have a parent/child relationship. But that doesn't seem to be the case here.
More to the point of the error though, this isn't referencing the entire primary key for the target table. That key has two fields:
primary key(productid,orderid)
So the DBMS can't create the foreign key because its structure doesn't match the target primary key.
If you want to create that foreign key, it would need to match. Probably something like this:
constraint fk4 foreign key (productid,orderid) references productorder(productid,orderid)
But it doesn't appear that you need that foreign key at all, because it doesn't seem to make sense in your data model. Instead I suspect orderid might need to be autoincrement and just use the productid foreign key. Something like this:
CREATE TABLE productorder(
orderid int auto_increment primary key,
productid int,
constraint fk3 foreign key (productid) references product(productid)
);
(Note that there could be more changes you'd want to make to your data model. This answer doesn't purport to provide you with a complete production-ready data model, just to correct the error. Your data model is likely to change/evolve as you develop your system.)
Its normal,
The foreign key in table **productorder**
is refereing to the table itself:
constraint fk4 foreign key (orderid) references **productorder**(orderid)
In order to achieve the self-referencing constraint on the table productorder, you need to add another column with the same type and make typical referencing.
For instance :
Create table productorder (
productid int,
orderid int,
orderid_parent int,
primary key(productid,orderid),
constraint fk_self foreign key(orderid) references productorder(orderid_parent)
)

How to define Many to Many linking table

I have a schema with a store and product table. A store record, may have many products, and a product may be available to many stores (many to many) To link these, a third table has the PRIMARY KEY of the store, and product records. Should this linking table, have a compound PRIMARY KEY? Or, should this simply be two FOREIGN KEYS, each with an INDEX()?
CREATE TABLE store(
id INT NOT NULL AUTO_INCREMENT,
name VARCHAR(50) NOT NULL,
PRIMARY KEY(id)
) ENGINE=INNODB;
CREATE TABLE product(
id INT NOT NULL AUTO_INCREMENT,
name VARCHAR(50) NOT NULL,
PRIMARY KEY(id)
) ENGINE=INNODB;
So, should the linking table be like..
CREATE TABLE store_product_link(
store_id INT NOT NULL,
product_id INT NOT NULL,
INDEX(store_id),
INDEX(product_id),
FOREIGN KEY (store_id) REFERENCES store(id) ON DELETE CASCADE,
FOREIGN KEY (product_id) REFERENCES product(id) ON DELETE CASCADE
) ENGINE=INNODB;
Or should this be
CREATE TABLE store_product_link(
store_id INT NOT NULL,
product_id INT NOT NULL,
PRIMARY KEY(store_id, product_id)
) ENGINE=INNODB;
Both, it should have a primary key and both foreign keys.
CREATE TABLE store_product_link(store_id INT NOT NULL, product_id INT NOT NULL,
PRIMARY KEY(store_id, product_id),
FOREIGN KEY (store_id) REFERENCES store(id) ON DELETE CASCADE,
FOREIGN KEY (product_id) REFERENCES product(id) ON DELETE CASCADE) ENGINE=INNODB;

Error Code: 1822 when data types are matching, with composite key

Getting an
Error Code: 1822. Failed to add the foreign key constraint. Missing
index for constraint 'subject_ibfk_1' in the referenced table
'enrolment'
when attempting to create the subject table. The problem is, the error does not occur on the previous table student. The data types are the same, and the primary keys are defined.
This error occurs for both the enrolment and grade tables.
create table enrolment(
stud_id char(9) not null,
subj_code char(8) not null,
semester tinyint unsigned not null,
year smallint unsigned not null,
comment text,
primary key (stud_id, subj_code, semester, year)
);
create table grade(
stud_id char(9) not null,
subj_code char(8) not null,
semester tinyint unsigned not null,
year smallint unsigned not null,
grade tinyint unsigned,
primary key (stud_id, subj_code, semester, year)
);
create table student(
stud_id char(9) not null,
stud_name char(30),
stud_phone char(12),
stud_date_of_birth date,
stud_city char(26),
stud_address char(30),
stud_postcode char(4),
primary key (stud_id),
foreign key (stud_id)
references grade(stud_id),
foreign key (stud_id)
references enrolment(stud_id)
);
create table subject(
subj_code char(8) not null,
subj_title char(40),
primary key (subj_code),
foreign key (subj_code)
references enrolment(subj_code),
foreign key (subj_code)
references grade(subj_code)
);
The problem is due to the fact that the foreign key, subj_code, is part of a multi-column primary key (PK) in the referenced table enrolment:
primary key (stud_id, subj_code, semester, year)
where this column (subj_code) is not the leftmost one.
Table student does not have this problem because its foreign key column stud_id is the leftmost column of the PK in the referenced table.
To resolve this you can create a new index for the referened column:
ALTER TABLE enrolment ADD INDEX subj_code_idx (subj_code);
Note: You have to do the same for referenced table grade in the other foreign key.
Demo here
FOREIGN KEY of one table is a PRIMARY key of another table. its a mapping key or column between two table.
First of all: you don't make subject_id as primary key in CLO table.
subject_id is act as a primary key in subject table and foreign key in clo table
ALTER TABLE `clo` DROP PRIMARY KEY, ADD PRIMARY KEY(`clo_id`);
ALTER TABLE `subject` DROP PRIMARY KEY, ADD PRIMARY KEY(`subject_id`);
ALTER TABLE clo ADD FOREIGN KEY (subject_id) REFERENCES subject(subject_id)

Can not Add External Index Constraints

This is the exact error that WAMP returns when I run the child code from an external file called entries.txt
ERROR 1215 (HY000): Can not Add External Index Constraints
I need to be able to connect the parent tables to the child table so that links can be made between the tables easily.
The question states:
Create and populate a third table called entries, again using query scripts.
This table should contain foreign keys to allow sensible links to be
made with the other two tables, together with the dates of each exam.
Parent Tables:
CREATE TABLE IF NOT EXISTS students(
student_id INT UNSIGNED NOT NULL AUTO_INCREMENT,
first_name VARCHAR(20) NOT NULL,
middle_name VARCHAR(20),
last_name VARCHAR(40) NOT NULL,
email VARCHAR(60) NOT NULL,
password CHAR(40) NOT NULL,
reg_date DATETIME NOT NULL,
PRIMARY KEY (student_id),
UNIQUE (email));
CREATE TABLE IF NOT EXISTS subjects(
subject_id INT UNSIGNED NOT NULL AUTO_INCREMENT,
subject_name CHAR(30) NOT NULL,
level_of_entry VARCHAR(10) NOT NULL,
exam_board CHAR(20) NOT NULL,
PRIMARY KEY (subject_id));
Child Table:
CREATE TABLE IF NOT EXISTS entries(
date_of_exam DATETIME NOT NULL,
first_name VARCHAR,
middle_name VARCHAR,
last_name VARCHAR,
subject_name CHAR,
level_of_entry VARCHAR,
exam_board CHAR,
INDEX idx_first_name(first_name),
INDEX idx_middle_name(middle_name),
INDEX idx_last_name(last_name),
INDEX idx_subject_name(subject_name),
INDEX idx_level_of_entry(level_of_entry),
INDEX idx_exam_board(exam_board),
PRIMARY KEY (date_of_exam),
CONSTRAINT fk_first_name FOREIGN KEY (first_name) REFERENCES students(first_name),
CONSTRAINT fk_middle_name FOREIGN KEY (middle_name) REFERENCES students(middle_name),
CONSTRAINT fk_last_name FOREIGN KEY (last_name) REFERENCES students(last_name),
CONSTRAINT fk_subject_name FOREIGN KEY (subject_name) REFERENCES subjects(subject_name),
CONSTRAINT fk_level_of_entry FOREIGN KEY (level_of_entry) REFERENCES subjects(level_of_entry),
CONSTRAINT fk_exam_board FOREIGN KEY (exam_board) REFERENCES subjects(exam_board)
)ENGINE=InnoDB;
A foreign key in the child table must reference the primary key of the parent table only. You don't need to create clones of every attribute column of the parent tables, just the primary key column. If you need to read the other attributes, you'd write a query with a JOIN.
CREATE TABLE IF NOT EXISTS entries(
student_id INT UNSIGNED NOT NULL,
subject_id INT UNSIGNED NOT NULL,
date_of_exam DATETIME NOT NULL,
PRIMARY KEY (student_id, subject_id, date_of_exam),
CONSTRAINT fk_student FOREIGN KEY (student_id) REFERENCES students(student_id),
CONSTRAINT fk_subject FOREIGN KEY (subject_id) REFERENCES subjects(subject_id),
)ENGINE=InnoDB;