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

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

Related

what is the problem with my tables in mysql?

I'm going to create 3 table. table "post" and "taxonomy" are connected to "taxonomy_relationship" table with primary key and foreign key but i don't know why do i get this error:
1005 - Can't create table taxonomy_relationship (errno: 150 "Foreign key constraint is incorrectly formed")
CREATE TABLE `post`(
id int not null AUTO_INCREMENT,
title varchar(255) not null,
content TEXT not null,
PRIMARY KEY(id)
);
CREATE TABLE `taxonomy`(
id int not null AUTO_INCREMENT,
tax_title varchar(255) not null,
type varchar(255) not null,
PRIMARY KEY(id, tax_title)
);
CREATE TABLE taxonomy_relationship(
id INT AUTO_INCREMENT,
post_id int not null,
title varchar(255) not null,
PRIMARY KEY(id),
FOREIGN KEY (post_id)
REFERENCES post(id)
ON DELETE CASCADE
ON UPDATE CASCADE
FOREIGN KEY (title)
REFERENCES taxonomy(tax_title)
ON DELETE CASCADE
ON UPDATE CASCADE
);
when i use "SHOW ENGINE INNODB STATUS" to show error details it returns:
>
2020-01-19 16:08:31 0x2fb8 Error in foreign key constraint of table blog.taxonomy_relationship:
FOREIGN KEY (title)
REFERENCES taxonomy(tax_title)
ON DELETE CASCADE
ON UPDATE CASCADE
):
Cannot find an index in the referenced table where the
referenced columns appear as the first columns, or column types
in the table and the referenced table do not match for constraint.
Note that the internal storage type of ENUM and SET changed in
tables created with >= InnoDB-4.1.12, and such columns in old tables
cannot be referenced by such columns in new tables.
Please refer to https://mariadb.com/kb/en/library/foreign-keys/ for correct foreign key definition.
Create table blog.taxonomy_relationship with foreign key constraint failed. There is no index in the referenced table where the referenced columns appear as the first columns near 'FOREIGN KEY (title)
REFERENCES taxonomy(tax_title)
ON DELETE CASCADE
ON UPDATE CASCADE
)'.
You should to add index KEY title_idx (tax_title) on taxonomy table :
CREATE TABLE `taxonomy` (
`id` int NOT NULL AUTO_INCREMENT,
`tax_title` varchar(255) NOT NULL,
`type` varchar(255) NOT NULL,
PRIMARY KEY (`id`),
KEY `title_idx` (`tax_title`)
);
After you can create foreign key:
CREATE TABLE taxonomy_relationship(
id INT AUTO_INCREMENT,
post_id int not null,
title varchar(255) not null,
PRIMARY KEY(id),
FOREIGN KEY (post_id)
REFERENCES post(id)
ON DELETE CASCADE
ON UPDATE CASCADE,
FOREIGN KEY (title)
REFERENCES taxonomy(tax_title)
ON DELETE CASCADE
ON UPDATE CASCADE
);
Another way you can duplicate title in taxonomy_relationship but link it by id field:
CREATE TABLE taxonomy_relationship(
id INT AUTO_INCREMENT,
post_id int not null,
taxonomy_id int not null,
PRIMARY KEY(id),
FOREIGN KEY (post_id)
REFERENCES post(id)
ON DELETE CASCADE
ON UPDATE CASCADE,
FOREIGN KEY (taxonomy_id)
REFERENCES taxonomy(id)
ON DELETE CASCADE
ON UPDATE CASCADE
);

Trouble trying to make these tables

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;

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?

Trouble constraining one database table to another

I'm new to SQL and trying to learn how to reference on table to another. This is what I have:
CREATE TABLE IF NOT EXISTS itemData (
id int(11) AUTO_INCREMENT PRIMARY KEY,
title varchar(64) NOT NULL,
sector_id int(11) DEFAULT NULL,
status_id int(11) DEFAULT NULL,
locations_id int(11) DEFAULT NULL,
payments_id int(11) DEFAULT NULL,
type_id int(11) DEFAULT NULL,
CONSTRAINT `fk_sector_id` FOREIGN KEY (sector_id) REFERENCES `sector` (`sector_id`),
CONSTRAINT `fk_status_id` FOREIGN KEY (`status_id`) REFERENCES `status` (`status_id`),
CONSTRAINT `fk_locations_id` FOREIGN KEY (`locations_id`) REFERENCES `location` (`locations_id`),
CONSTRAINT `fk_payments_id` FOREIGN KEY (`payments_id`) REFERENCES `payments` (`payments_id`),
CONSTRAINT `fk_type_id` FOREIGN KEY (`type_id`) REFERENCES `type` (`type_id`)
);
Then I have my reference table for example:
CREATE TABLE IF NOT EXISTS itemStatus (
id int(11) AUTO_INCREMENT PRIMARY KEY,
name varchar(64) NOT NULL UNIQUE KEY
);
This doesn't seem to validate, can someone tell me where I have gone wrong please?
You probably need to change your table definition like this:
CREATE TABLE IF NOT EXISTS itemStatus (
status_id int(11) AUTO_INCREMENT PRIMARY KEY,
name varchar(64) NOT NULL UNIQUE KEY
);
as the constraint definition in your table is like this:
CONSTRAINT `fk_status_id` FOREIGN KEY (`status_id`) REFERENCES `itemstatus` (`status_id`),
You need to update the correct table in the constraint.
Also if you dont want to change the table definition then change the constraint like this:
CONSTRAINT `fk_id` FOREIGN KEY (`id`) REFERENCES `itemstatus` (`id`)
Here's your constraint:
CONSTRAINT `fk_status_id` FOREIGN KEY (`status_id`) REFERENCES `status` (`status_id`)
Here's the table definition:
CREATE TABLE IF NOT EXISTS itemStatus (
id int(11) AUTO_INCREMENT PRIMARY KEY,
name varchar(64) NOT NULL UNIQUE KEY
);
There's no column named status_id in that table.
Either rename the primary key column to status_id or change the constraint to point to id.

Mysql create table with multiple foreign key on delete set null

I am trying to create a database with multiple foreign keys with delete/ update constraints, but I got a error code 1005 with following sql scripts:
CREATE TABLE Worker (
WorkerID smallint auto_increment,
WorkerType varchar(45) NOT NULL,
WorkerName varchar(45) NOT NULL,
Position varchar(45) NOT NULL,
TaxFileNumber int NOT NULL,
Address varchar(100) ,
Phone varchar(20) ,
SupervisorID smallint ,
PRIMARY KEY (WorkerID),
FOREIGN KEY (SupervisorID) REFERENCES Worker(WorkerID)
ON DELETE SET NULL
ON UPDATE CASCADE
)Engine=InnoDB;
CREATE TABLE Grape (
GrapeID smallint NOT NULL,
GrapeType varchar(45) NOT NULL,
JuiceConversionRatio int,
StorageContainer ENUM('Stainless Steel Tank','Oak Barrel'),
AgingRequirement int,
PRIMARY KEY (GrapeID)
)Engine=InnoDB;
CREATE TABLE Vineyard (
VineyardID smallint auto_increment,
VineyardName VARCHAR(45) NOT NULL,
FarmerID smallint NOT NULL,
GrapeID smallint NOT NULL,
ComeFrom varchar(45) NOT NULL,
HarvestedAmount int,
RipenessPercent int,
PRIMARY KEY (VineyardID),
FOREIGN KEY (FarmerID) REFERENCES Worker(WorkerID)
ON DELETE SET NULL
ON UPDATE CASCADE,
FOREIGN KEY (GrapeID) REFERENCES Grape(GrapeID)
ON DELETE SET NULL
ON UPDATE CASCADE
)Engine=InnoDB;
The error code says that fail to create the Vineyard table, I just want to know the proper format for creating multiple foreign keys with delete/update control.
Your foreign key rule is ON DELETE SET NULL but your column definition is NOT NULL.
Either change your column definition and remove the NOT NULL part or overthink your foreign key rule. That works:
CREATE TABLE Vineyard (
VineyardID smallint auto_increment,
VineyardName VARCHAR(45) NOT NULL,
FarmerID smallint,
GrapeID smallint,
ComeFrom varchar(45) NOT NULL,
HarvestedAmount int,
RipenessPercent int,
PRIMARY KEY (VineyardID),
FOREIGN KEY (FarmerID) REFERENCES Worker(WorkerID)
ON DELETE SET NULL
ON UPDATE CASCADE,
FOREIGN KEY (GrapeID) REFERENCES Grape(GrapeID)
ON DELETE SET NULL
ON UPDATE CASCADE
)Engine=InnoDB;
SQLFiddle demo
Try with create table(innoDB enginer) without foreign key and use the update table with constraint syntax, for example:
ALTER TABLE `vineyard`
ADD CONSTRAINT `relation_farmer_has_many_vineyards`
FOREIGN KEY (`farmer_id`)
REFERENCES `worker` (`worker_id`)
ON DELETE SET NULL
ON UPDATE CASCADE;
Reference:
http://dev.mysql.com/doc/refman/5.6/en/innodb-foreign-key-constraints.html
Trick: is not recommended to use capital letters(or camel case) in the names of the tables that the behavior differs from the operating system being used:
http://dev.mysql.com/doc/refman/5.1/en/identifier-case-sensitivity.html
Visit :
https://developer.android.com/training/basics/data-storage/databases.html#DefineContract
Cursor c = db.query(
FeedEntry.TABLE_NAME, // The table to query
projection, // The columns to return
selection, // The columns for the WHERE clause
selectionArgs, // The values for the WHERE clause
null, // don't group the rows
null, // don't filter by row groups
sortOrder // The sort order
);
Visit :
http://dev.mysql.com/doc/refman/5.5/en/innodb-foreign-key-constraints.html
CREATE TABLE `ffxi_characterJob` (
`serverID` int(11) NOT NULL,
`userid` int(10)unsigned NOT NULL,
`characterName` varchar(255) NOT NULL,
`jobAbbr` char(4) NOT NULL,
`jobLevel` int(11) default '0',
PRIMARY KEY (`serverID`,`userid`,`characterName`,`jobAbbr`),
INDEX (`jobAbbr`),
CONSTRAINT FOREIGN KEY (`serverID`,`userid`,`characterName`) REFERENCES `ffxi_characters` (`serverID`,`userid`,`characterName`)
ON DELETE CASCADE ON UPDATE CASCADE,
CONSTRAINT FOREIGN KEY (`jobAbbr`) REFERENCES `ffxi_jobType` (`jobAbbr`) ON DELETE CASCADE ON UPDATE CASCADE
) TYPE=InnoDB;