Trouble trying to make these tables - mysql

Hey guys I'm running into an issue while trying to create these 5 tables. From what I can tell the issue has to deal with the champ. Every time I try to import my sql file I get an errno 150 stating that the champion table cannot be created.
To clarify a champion can only have one faction but a faction can be composed of many champions. The same relationship for roles and affinity. Thank you for any help in advance.
DROP TABLE IF EXISTS `champion`;
DROP TABLE IF EXISTS `role`;
DROP TABLE IF EXISTS `build`;
DROP TABLE IF EXISTS `faction`;
DROP TABLE IF EXISTS `build_type`;
DROP TABLE IF EXISTS `affinity`;
-- roles table
-- Table consisting of the roles various champions play
CREATE TABLE `role` (
`role_id` int(11) NOT NULL AUTO_INCREMENT,
`role_name` varchar(255) NOT NULL,
PRIMARY KEY (`role_id`)
)ENGINE=InnoDB;
-- build table
CREATE TABLE `build` (
`build_id` int(11) NOT NULL AUTO_INCREMENT,
`build_name` varchar(255) NOT NULL,
PRIMARY KEY (`build_id`),
UNIQUE KEY (`build_name`)
)ENGINE=InnoDB;
-- faction table
-- Table consisting of the faction each champion belongs to
CREATE TABLE `faction`(
`faction_id` int(11) NOT NULL AUTO_INCREMENT,
`faction_name` varchar(255) NOT NULL,
PRIMARY KEY (`faction_id`)
)ENGINE=InnoDB;
-- Champions table
-- Table consisting of various champions in League of Legends
CREATE TABLE `champion`(
`champion_id` int(11) NOT NULL AUTO_INCREMENT,
`champion_name` varchar(255) NOT NULL,
`f_id` int(11) NOT NULL,
`r_id` int(11) NOT NULL,
`a_id` int(11) NOT NULL,
PRIMARY KEY (`champion_id`),
FOREIGN KEY (`r_id`) REFERENCES `role` (`role_id`)
ON DELETE CASCADE
ON UPDATE CASCADE,
FOREIGN KEY (`f_id`) REFERENCES `faction` (`faction_id`)
ON DELETE CASCADE
ON UPDATE CASCADE,
FOREIGN KEY (`a_id`) REFERENCES `affinity` (`affinity_id`)
ON DELETE CASCADE
ON UPDATE CASCADE,
UNIQUE KEY (`champion_name`)
)ENGINE=InnoDB;
-- build_type table
CREATE TABLE `build_type`(
`cid` int(11) NOT NULL,
`bid` int(11) NOT NULL,
PRIMARY KEY (`cid`, `bid`),
FOREIGN KEY (`cid`) REFERENCES `champion` (`champion_id`)
ON DELETE CASCADE
ON UPDATE CASCADE,
FOREIGN KEY (`bid`) REFERENCES `build` (`build_id`)
ON DELETE CASCADE
ON UPDATE CASCADE
)ENGINE=InnoDB;
-- affinity table
-- Table consisting of the affinity a certain champion synergizes
-- with
CREATE TABLE `affinity`(
`affinity_id` int(11) NOT NULL AUTO_INCREMENT,
`affinity_name` varchar(255) NOT NULL,
PRIMARY KEY (`affinity_id`)
)ENGINE=InnoDB;

That's because champion table has a column a_id referring to a table affinity that does not exist yet.To solve this problem you must first create the affinity table before champion table
This
CREATE TABLE `affinity`(
`affinity_id` int(11) NOT NULL AUTO_INCREMENT,
`affinity_name` varchar(255) NOT NULL,
PRIMARY KEY (`affinity_id`)
)ENGINE=InnoDB;
must come before
CREATE TABLE `champion`(
`champion_id` int(11) NOT NULL AUTO_INCREMENT,
`champion_name` varchar(255) NOT NULL,
`f_id` int(11) NOT NULL,
`r_id` int(11) NOT NULL,
`a_id` int(11) NOT NULL,
PRIMARY KEY (`champion_id`),
FOREIGN KEY (`r_id`) REFERENCES `role` (`role_id`)
ON DELETE CASCADE
ON UPDATE CASCADE,
FOREIGN KEY (`f_id`) REFERENCES `faction` (`faction_id`)
ON DELETE CASCADE
ON UPDATE CASCADE,
FOREIGN KEY (`a_id`) REFERENCES `affinity` (`affinity_id`)
ON DELETE CASCADE
ON UPDATE CASCADE,
UNIQUE KEY (`champion_name`)
)ENGINE=InnoDB;

Related

How to link one to many relationship with multiple keys pointing on the same table id?

I have a table files and a table users and staff and client.
The file table contain a row made_by a row published_byand a row about_client pointing to a staff, a staff and a client respectively.
One user must be either a staff or a client.
One file can have multiple author (made_by row), but only one user can publish it (published_by), and it can only be about one client (about_client).
First, I put 3 foreign key on files:
CONSTRAINT `made_by_fk` FOREIGN KEY (`made_by`) REFERENCES `users` (`id`) ON DELETE CASCADE ON UPDATE CASCADE,
CONSTRAINT `published_by_fk` FOREIGN KEY (`published_by`) REFERENCES `users` (`id`) ON DELETE CASCADE ON UPDATE CASCADE,
CONSTRAINT `about_client_fk` FOREIGN KEY (`about_client`) REFERENCES `users` (`id`) ON DELETE CASCADE ON UPDATE CASCADE
but after a little research, I found out that I should put the foreign key on staff / client, and here come the problem, the fk are OPTIONAL. A staff is not obligatory an author of a file, or the client maybe don't have any file attached to it.
I am a bit lost.
DROP TABLE IF EXISTS `files`;
CREATE TABLE IF NOT EXISTS `files` (
`id` INT NOT NULL AUTO_INCREMENT
PRIMARY KEY,
`made_by` INT NOT NULL,
`published_by` INT NOT NULL,
`about_client` INT NOT NULL,
`creation_date` DATETIME NOT NULL,
`modification_date` DATETIME NULL
ON UPDATE CURRENT_TIMESTAMP,
`path` VARCHAR(255) NOT NULL,
`title` VARCHAR(100) NOT NULL UNIQUE,
`category` INT NOT NULL,
`type` VARCHAR(30) NOT NULL,
`size` INT NOT NULL,
CONSTRAINT `made_by_fk` FOREIGN KEY (`made_by`) REFERENCES `users` (`id`) ON DELETE CASCADE ON UPDATE CASCADE,
CONSTRAINT `published_by_fk` FOREIGN KEY (`published_by`) REFERENCES `users` (`id`) ON DELETE CASCADE ON UPDATE CASCADE,
CONSTRAINT `about_client_fk` FOREIGN KEY (`about_client`) REFERENCES `users` (`id`) ON DELETE CASCADE ON UPDATE CASCADE
)
ENGINE = InnoDB
COLLATE = utf8_unicode_ci;
DROP TABLE IF EXISTS `staff`;
CREATE TABLE IF NOT EXISTS `staff` (
`id` INT NOT NULL AUTO_INCREMENT
PRIMARY KEY,
`job` INT NOT NULL,
`password` VARCHAR(255) NOT NULL
)
ENGINE = InnoDB
COLLATE = utf8_unicode_ci;
DROP TABLE IF EXISTS `users`;
CREATE TABLE IF NOT EXISTS `users` (
`id` INT NOT NULL AUTO_INCREMENT
PRIMARY KEY,
`name` VARCHAR(100) NOT NULL,
`surname` VARCHAR(100) NOT NULL,
`email` VARCHAR(50) NOT NULL UNIQUE
)
ENGINE = InnoDB
COLLATE = utf8_unicode_ci;
DROP TABLE IF EXISTS `clients`;
CREATE TABLE IF NOT EXISTS `clients` (
`id` INT NOT NULL AUTO_INCREMENT
PRIMARY KEY,
`phone` VARCHAR(30) NOT NULL,
`age` INT NOT NULL,
`date_of_birth` DATE NOT NULL,
`security_number` VARCHAR(99) NOT NULL UNIQUE
)
ENGINE = InnoDB
COLLATE = utf8_unicode_ci;
If there can be multiple made_by, it can't be a column in the file table, since that can only contain one value.
You need another table, file_made_by that represents the many-to-many relationship between files and authors.
CREATE TABLE file_made_by (
file_id INT NOT NULL,
author_id INT NOT NULL,
PRIMARY KEY (file_id, author_id),
FOREIGN KEY (file_id) REFERENCES files (id) ON DELETE CASCADE ON UPDATE CASCADE,
FOREIGN KEY (author_id) REFERENCES users (id) ON DELETE CASCADE ON UPDATE CASCADE
);
The clients and staff tables should also include a user_id column that's a foreign key to users.id.
If about_client has to be about a client, not staff, then it should be a foreign key to clients.id rather than users.id.
And if only staff are allowed to be authors, file_made_by.author_id should be a FK to staff.id rather than users.id. The same with files.published_by.

1072 - Key column 'trax_zone_id' doesn't exist in table

this is the parent table
CREATE TABLE trax_zone(
trax_zone_id INT(11) NOT NULL AUTO_INCREMENT PRIMARY KEY,
trax_zone_name VARCHAR(255) NOT NULL,
)ENGINE=InnoDB;
this is the child table code
CREATE TABLE city(
city_id INT(11) NOT NULL AUTO_INCREMENT PRIMARY KEY,
city_name VARCHAR(255) NOT NULL,
FOREIGN KEY fk_trax(trax_zone_id) REFERENCES trax_zone(trax_zone_id) ON UPDATE CASCADE ON DELETE RESTRICT,
FOREIGN KEY fk_zoop(zoop_zone_id) REFERENCES zoop_zone(zoop_zone_id) ON UPDATE CASCADE ON DELETE RESTRICT
)ENGINE=InnoDB;
i am new to mysql , kindly guide me

MySQL ON DELETE CASCADE not work

i have two table (type innoDb) why when i deleting main table row like
+note(im using arch os so my database server type is maria db)
DELETE FROM buildings
WHERE
building_no = 2;
relation table rows dont delete ???
CREATE TABLE buildings (
building_no INT PRIMARY KEY AUTO_INCREMENT,
building_name VARCHAR(255) NOT NULL,
address VARCHAR(255) NOT NULL
);
CREATE TABLE rooms (
room_no INT PRIMARY KEY AUTO_INCREMENT,
room_name VARCHAR(255) NOT NULL,
building_no INT NOT NULL,
FOREIGN KEY (building_no)
REFERENCES buildings (building_no)
ON DELETE CASCADE
);
This syntax works for me on MySQL database but i can't see issues in your query too
CREATE TABLE `rooms` (
room_no INT PRIMARY KEY AUTO_INCREMENT,
room_name VARCHAR(255) NOT NULL,
building_no INT NOT NULL,
CONSTRAINT `FK_rooms_1` FOREIGN KEY (`building_no`) REFERENCES `buildings`
(`building_no`) ON DELETE CASCADE
) ENGINE=InnoDB;
or add the constraint after table creation
ALTER TABLE `rooms`
ADD CONSTRAINT `FK_rooms_1` FOREIGN KEY (`building_no`) REFERENCES `buildings` (`building_no`) ON DELETE CASCADE ON UPDATE CASCADE;

Cannot add or update a child row: a foreign key constraint fails mysql

CREATE TABLE `class` (
`class_id` int(11) NOT NULL AUTO_INCREMENT,
`section_name` varchar(50) NOT NULL,
`class_alias` varchar(200) NOT NULL,
`grading_scheme` int(11) NOT NULL DEFAULT '0',
`year` year(4) NOT NULL,
`grade_calc_method_id` varchar(20) DEFAULT NULL,
PRIMARY KEY (`class_id`)
) ENGINE=InnoDB AUTO_INCREMENT=48819 DEFAULT CHARSET=latin1;
CREATE TABLE `teachers` (
`teacher_id` int(11) NOT NULL AUTO_INCREMENT,
`user_id` int(11) NOT NULL,
`teacher_subject` varchar(20) NOT NULL DEFAULT 'None',
PRIMARY KEY (`teacher_id`),
KEY `user_id` (`user_id`,`school_id`)
) ENGINE=InnoDB AUTO_INCREMENT=48606 DEFAULT CHARSET=latin1;
CREATE TABLE `teacher_classes` (
`teacher_class_id` int(11) NOT NULL AUTO_INCREMENT,
`teacher_id` int(11) NOT NULL,
`class_id` int(11) NOT NULL,
PRIMARY KEY (`teacher_class_id`),
UNIQUE KEY `teacher_id_class_id` (`teacher_id`,`class_id`),
KEY `teacher_id` (`teacher_id`,`class_id`)
) ENGINE=InnoDB AUTO_INCREMENT=46707 DEFAULT CHARSET=latin1;
Trying to insure data consistency between the tables by using foreign key so that the DBMS can check for errors.I have another junction table teacher_classes
Here is my query to add foreign keys constraint
ALTER TABLE teacher_classes
ADD CONSTRAINT `tc_fk_class_id` FOREIGN KEY (`class_id`)
REFERENCES class (`class_id`) ON UPDATE NO ACTION ON DELETE NO ACTION,
ADD CONSTRAINT `tc_fk_teacher_id` FOREIGN KEY (`teacher_id`)
REFERENCES teachers (`teacher_id`) ON UPDATE NO ACTION ON DELETE NO ACTION;
've seen the other posts on this topic, but no luck, getting following error.
Cannot add or update a child row: a foreign key constraint fails
(DB_NAME.#sql-403_12, CONSTRAINT
tc_fk_teacher_id FOREIGN KEY (teacher_id) REFERENCES teachers
(teacher_id) ON DELETE NO ACTION ON UPDATE NO ACTION)
Too late to Answer. I just had the same problem the solution is easy.
You're getting this error because you're trying to or UPDATE a row to teacher_classes doesn't match the id in table teachers.
A simple solution is disable foreign key checks before performing any operation on the table.
SET FOREIGN_KEY_CHECKS = 0;
After you are done with the table enable it again.
SET FOREIGN_KEY_CHECKS = 1;
Or you can remove not null constraint and insert a NULL value in it.
That's most probably the column definition doesn't match properly. For table teachers the PK column definition is as below.
`teacher_id` int(11) NOT NULL AUTO_INCREMENT
Make sure you have the same definition in your child table teacher_classes

mysql creating table foreign key

I've created a table :
CREATE TABLE users (
id INT(6) UNSIGNED AUTO_INCREMENT PRIMARY KEY
,uName VARCHAR(50)
,uSecondName VARCHAR(50)
,eMail VARCHAR(50)
)
After this I even insert some data without any problems. But when I've tried to create new table with FOREIGN KEY referenced to users.id I've got an error:
CREATE TABLE posts(
id INT(6) AUTO_INCREMENT NOT NULL
,pTitle VARCHAR(155) NOT NULL DEFAULT 'not_set'
,pText TEXT
,pAuthor INT(6)
,PRIMARY KEY(id)
,CONSTRAINT fk_PerAuthor FOREIGN KEY (pAuthor)
REFERENCES users(id) ON DELETE CASCADE ON UPDATE CASCADE
);
Did I miss something?