Create table - Mysql - mysql

I'm looking for a little help with this table. First: The table isn't being accepted by mysql. I'm getting a 150 error, which doesn't tell me much. Second, I'm very certain that I'm not using the best data types & way to go about things.... please give me a little input!
CREATE TABLE IF NOT EXISTS `axis`.`Employee` (
`idEmployee` INT(10) UNSIGNED NOT NULL AUTO_INCREMENT ,
`idAccount` INT(10) UNSIGNED NOT NULL ,
`user` VARCHAR(45) NULL DEFAULT NULL ,
`pass` VARCHAR(45) NULL DEFAULT NULL ,
`firstName` VARCHAR(45) NULL DEFAULT NULL ,
`lastName` VARCHAR(45) NULL DEFAULT NULL ,
`middleName` VARCHAR(45) NULL DEFAULT NULL ,
`idGrp` INT(10) UNSIGNED NULL DEFAULT NULL ,
`idCompany` INT(10) UNSIGNED NULL DEFAULT NULL ,
`idLocation` INT(10) UNSIGNED NULL DEFAULT NULL ,
`idUnit` INT(10) UNSIGNED NULL DEFAULT NULL ,
`idCrew` INT(10) UNSIGNED NULL DEFAULT NULL ,
`idPosition` INT(10) UNSIGNED NULL DEFAULT NULL ,
`officeLoc` VARCHAR(45) NULL DEFAULT NULL ,
`wPhone` VARCHAR(12) NULL DEFAULT NULL ,
`wCell` VARCHAR(12) NULL DEFAULT NULL ,
`wFax` VARCHAR(12) NULL DEFAULT NULL ,
`wEmail` VARCHAR(45) NULL DEFAULT NULL ,
`radio` VARCHAR(12) NULL DEFAULT NULL ,
`hPhone` VARCHAR(12) NULL DEFAULT NULL ,
`hCell` VARCHAR(12) NULL DEFAULT NULL ,
`hEmail` VARCHAR(45) NULL DEFAULT NULL ,
`addrOne` VARCHAR(45) NULL DEFAULT NULL ,
`addrTwo` VARCHAR(45) NULL DEFAULT NULL ,
`city` VARCHAR(45) NULL DEFAULT NULL ,
`state` VARCHAR(45) NULL DEFAULT NULL ,
`zip` VARCHAR(10) NULL DEFAULT NULL ,
`emergContact` VARCHAR(45) NULL DEFAULT NULL ,
`emergPhone` VARCHAR(12) NULL DEFAULT NULL ,
`gender` ENUM('male','female') NULL DEFAULT NULL ,
`birthDate` DATE NULL DEFAULT NULL ,
`ssn` VARCHAR(5) NULL DEFAULT NULL ,
`hireDate` DATE NULL DEFAULT NULL ,
`sepDate` DATE NULL DEFAULT NULL ,
`comment` VARCHAR(255) NULL DEFAULT NULL ,
`photo` MEDIUMBLOB NULL DEFAULT NULL ,
`isActive` BIT(1) NOT NULL DEFAULT 1 ,
`allowLogin` BIT(1) NOT NULL DEFAULT 0 ,
`trackTravel` BIT(1) NOT NULL DEFAULT 1 ,
`trackTimesheet` BIT(1) NOT NULL DEFAULT 1 ,
`defFltArr` VARCHAR(10) NULL DEFAULT NULL ,
`defFltDep` VARCHAR(10) NULL DEFAULT NULL ,
`defDayTimeStart` TIME NOT NULL DEFAULT '06:00:00' ,
`defHoursPerDay` DECIMAL(4,2) NOT NULL DEFAULT '11.50' ,
`userType` ENUM('root','admin','view','acl','report','employee') NOT NULL DEFAULT 'employee',
`created` DATETIME NOT NULL ,
`modified` TIMESTAMP NOT NULL ,
`modifiedBy` INT(10) UNSIGNED NULL DEFAULT NULL ,
PRIMARY KEY (`idEmployee`, `idAccount`, `idGrp`, `idCompany`, `idLocation`, `idUnit`, `idCrew`, `idPosition`, `modifiedBy`) ,
UNIQUE INDEX `UNIQUE` (`idEmployee` ASC) ,
INDEX `fk_Employee_Grp` (`idGrp` ASC) ,
INDEX `fk_Employee_Company` (`idCompany` ASC) ,
INDEX `fk_Employee_Unit` (`idUnit` ASC) ,
INDEX `fk_Employee_Location` (`idLocation` ASC) ,
INDEX `fk_Employee_Crew` (`idCrew` ASC) ,
INDEX `fk_Employee_Position` (`idPosition` ASC) ,
INDEX `fk_Employee_Employee` (`modifiedBy` ASC) ,
CONSTRAINT `fk_Employee_Grp`
FOREIGN KEY (`idGrp` )
REFERENCES `axis`.`Grp` (`idGrp` )
ON DELETE SET NULL
ON UPDATE CASCADE,
CONSTRAINT `fk_Employee_Company`
FOREIGN KEY (`idCompany` )
REFERENCES `axis`.`Company` (`idCompany` )
ON DELETE SET NULL
ON UPDATE CASCADE,
CONSTRAINT `fk_Employee_Unit`
FOREIGN KEY (`idUnit` )
REFERENCES `axis`.`Unit` (`idUnit` )
ON DELETE SET NULL
ON UPDATE CASCADE,
CONSTRAINT `fk_Employee_Location`
FOREIGN KEY (`idLocation` )
REFERENCES `axis`.`Location` (`idLocation` )
ON DELETE SET NULL
ON UPDATE CASCADE,
CONSTRAINT `fk_Employee_Crew`
FOREIGN KEY (`idCrew` )
REFERENCES `axis`.`Crew` (`idCrew` )
ON DELETE SET NULL
ON UPDATE CASCADE,
CONSTRAINT `fk_Employee_Position`
FOREIGN KEY (`idPosition` )
REFERENCES `axis`.`Position` (`idPosition` )
ON DELETE SET NULL
ON UPDATE CASCADE,
CONSTRAINT `fk_Employee_Employee`
FOREIGN KEY (`modifiedBy` )
REFERENCES `axis`.`Employee` (`idEmployee` )
ON DELETE SET NULL
ON UPDATE CASCADE)
ENGINE = InnoDB
DEFAULT CHARACTER SET = utf8
COLLATE = utf8_general_ci

One of many mistakes :
`idGrp` INT(10) UNSIGNED NULL DEFAULT NULL`
then
PRIMARY KEY (`idEmployee`, `idAccount`, `idGrp`, ... )
(Column that is part of PRIMARY KEY must be NOT NULL - mysql silently makes it not null,
finally
CONSTRAINT `fk_Employee_Grp` FOREIGN KEY (`idGrp` )
REFERENCES `axis`.`Grp` (`idGrp` ) ON DELETE SET NULL ON UPDATE CASCADE
(which is impossible - ON DELETE SET NULL - column is not null.
And why do you create composite key at all? auto_increment column uniquely identifies your record. All other stuff is redundant. It might possible make sense in some cases for MyIsam tables (I would not recommend to use it anyway), because a value for auto_increment column will be generated differently if it's a part of composite index , but not for INNODB engine.

Uhm, I may be very wrong, but are you allowed to have more than one Primary Key? I thought the whole purpose was that it was indeed a Primary (Unique) Key.

Adding to a1ex07's reply, see this thread:
http://forums.mysql.com/read.php?22,19755,259716#msg-259716
It might also be due to signed/unsigned integer-related problems.
Also, your primary key (a unique, not null (set of) field(s)) is, de facto, your employee ID, based on the table's definition.

Related

error 1215 MYSQL Cannot add foreign key constraint,

I have this code:
CREATE TABLE IF NOT EXISTS `biblioteca`.`ejemplar` (
`idejemplar` INT(11) NOT NULL AUTO_INCREMENT ,
`estado` VARCHAR(45) NOT NULL ,
`comentario` VARCHAR(45) NULL ,
`isbn` VARCHAR(45) NOT NULL ,
PRIMARY KEY (`idejemplar`) ,
INDEX `fk_ejemplar_libro1_idx` (`isbn` ASC) ,
CONSTRAINT `fk_ejemplar_libro1`
FOREIGN KEY (`isbn` )
REFERENCES `biblioteca`.`libro` (`isbn` )
ON DELETE NO ACTION
ON UPDATE NO ACTION)
ENGINE = InnoDB
DEFAULT CHARACTER SET = utf8
and the table that's makes reference is:
CREATE TABLE IF NOT EXISTS `biblioteca`.`libro` (
`isbn` VARCHAR(25) NOT NULL ,
`idcategoria` INT(11) NOT NULL ,
`ideditorial` INT(11) NOT NULL ,
`titulo` VARCHAR(45) NOT NULL ,
`autor` VARCHAR(45) NOT NULL ,
`reseña` VARCHAR(45) NULL ,
PRIMARY KEY (`isbn`) ,
INDEX `fk_libro2_idx` (`idcategoria` ASC) ,
INDEX `fk_libro3_idx` (`ideditorial` ASC) ,
CONSTRAINT `fk_libro2`
FOREIGN KEY (`idcategoria` )
REFERENCES `biblioteca`.`categoria` (`idcategoria` )
ON DELETE NO ACTION
ON UPDATE NO ACTION,
CONSTRAINT `fk_libro3`
FOREIGN KEY (`ideditorial` )
REFERENCES `biblioteca`.`editorial` (`ideditorial` )
ON DELETE CASCADE
ON UPDATE CASCADE)
ENGINE = InnoDB
DEFAULT CHARACTER SET = utf8;
and i think thats foreign keys are good.
If these are fresh tables with no values, It seems like an error with the columns
one has a varchar(25) the other a var_char(45) make them the same
edit:
CREATE TABLE IF NOT EXISTS `biblioteca`.`libro` (
`isbn` VARCHAR(45) NOT NULL ,
`idcategoria` INT(11) NOT NULL ,
`ideditorial` INT(11) NOT NULL ,
`titulo` VARCHAR(45) NOT NULL ,
`autor` VARCHAR(45) NOT NULL ,
`reseña` VARCHAR(45) NULL ,
PRIMARY KEY (`isbn`) ,
INDEX `fk_libro2_idx` (`idcategoria` ASC) ,
INDEX `fk_libro3_idx` (`ideditorial` ASC) )
ENGINE = INNODB
DEFAULT CHARACTER SET = utf8;
CREATE TABLE IF NOT EXISTS `biblioteca`.`ejemplar` (
`idejemplar` INT(11) NOT NULL AUTO_INCREMENT ,
`estado` VARCHAR(45) NOT NULL ,
`comentario` VARCHAR(45) NULL ,
`isbn` VARCHAR(45) NOT NULL ,
PRIMARY KEY (`idejemplar`) ,
INDEX `fk_ejemplar_libro1_idx` (`isbn` ASC) ,
CONSTRAINT `fk_ejemplar_libro1`
FOREIGN KEY (`isbn` )
REFERENCES `biblioteca`.`libro` (`isbn` )
ON DELETE NO ACTION
ON UPDATE NO ACTION)
ENGINE = INNODB
DEFAULT CHARACTER SET = utf8;
works no problem for me. Ive taken away the other 2 keys which i dont have schemas for though

MYSQL error in creating foreign key constraint

I am using MySQL to create a small database. and facing some issues in foreign keys and primary key. I really don't understand as it seems a simple problem.
CREATE TABLE IF NOT EXISTS `db_trimms`.`urban_area` (
`area_id` INT(10) NOT NULL ,
`city` VARCHAR(60) NOT NULL ,
`state` VARCHAR(60) NOT NULL ,
`urban_area` VARCHAR(60) NOT NULL ,
`census_region` VARCHAR(60) NOT NULL ,
`area_no` VARCHAR(60) NOT NULL ,
`freeway_speed` VARCHAR(60) NOT NULL ,
`arterial_speed` VARCHAR(60) NOT NULL ,
PRIMARY KEY (`area_id`) )
ENGINE = InnoDB
DEFAULT CHARACTER SET = latin1;
its running successfully. But while create another table and creating foreign key referring to the area_id of the above table...its creating issues...
#1005 - Can't create table 'db_trimms.emiss_others_offpeak' (errno: 150) (Details...)
and here is the query
CREATE TABLE IF NOT EXISTS `db_trimms`.`emiss_others_offpeak` (
`area_id` INT(10) UNSIGNED NOT NULL ,
`ammonia` VARCHAR(60) NOT NULL ,
`atm_carbon_dio` VARCHAR(60) NOT NULL ,
`carbon_dio_equiv` VARCHAR(60) NOT NULL ,
`carbon_mono` VARCHAR(60) NOT NULL ,
`methane` VARCHAR(60) NOT NULL ,
`nitrogen_dio` VARCHAR(60) NOT NULL ,
`nitrogen_oxide` VARCHAR(60) NOT NULL ,
`nitrous_oxide` VARCHAR(60) NOT NULL ,
`non_meth_hydrocarbs` VARCHAR(60) NOT NULL ,
`oxides_of_nitrogen` VARCHAR(60) NOT NULL ,
`particulate_matter_pm10` VARCHAR(60) NOT NULL ,
`particulate_matter_pm2_5` VARCHAR(60) NOT NULL ,
`sulfate` VARCHAR(60) NOT NULL ,
` sulfur_dioxide` VARCHAR(60) NOT NULL ,
`total_hydrocarbon` VARCHAR(60) NOT NULL ,
`vol_org_comp` VARCHAR(60) NOT NULL ,
PRIMARY KEY (`area_id`) ,
CONSTRAINT `fk_others_offpeak_urban`
FOREIGN KEY (`area_id` )
REFERENCES `db_trimms`.`urban_area` (`area_id` )
ON DELETE CASCADE
ON UPDATE CASCADE)
ENGINE = InnoDB
DEFAULT CHARACTER SET = latin1;
The data types are incompatible with each other. Make column area_id from table urban_area also UNSIGNED.
`area_id` INT(10) UNSIGNED NOT NULL ,
SQLFiddle Demo

CASCADE DELETE how to delete child rows

I have 3 tables
player, goal, card
how should i build my database so it automatically deletes goal and card row containing player id?
my declarations of tables i suppose i should add on delete cascade but i don't understand it very well so can any of you help me?
CREATE TABLE IF NOT EXISTS `#__footsal_players` (
`id` int(11) UNSIGNED NOT NULL AUTO_INCREMENT,
`ordering` INT(11) NOT NULL ,
`state` TINYINT(1) NOT NULL DEFAULT '1',
`checked_out` INT(11) NOT NULL ,
`checked_out_time` DATETIME NOT NULL DEFAULT '0000-00-00 00:00:00',
`created_by` INT(11) NOT NULL ,
`id_team` INT(11) NOT NULL ,
`first_name` varchar(255) NOT NULL ,
`last_name` varchar(255) NOT NULL ,
`birth_date` DATE NOT NULL DEFAULT '0000-00-00',
`email` varchar(255) NOT NULL ,
PRIMARY KEY (`id`)
) ENGINE=InnoDB DEFAULT COLLATE=utf8_polish_ci
CREATE TABLE IF NOT EXISTS `#__footsal_goals` (
`id` int(11) UNSIGNED NOT NULL AUTO_INCREMENT,
`state` TINYINT(1) NOT NULL DEFAULT '1',
`checked_out` INT(11) NOT NULL ,
`checked_out_time` DATETIME NOT NULL DEFAULT '0000-00-00 00:00:00',
`created_by` INT(11) NOT NULL ,
`id_player` INT(11) NOT NULL ,
`id_resault` INT(11) NOT NULL ,
`id_game` INT(11) NOT NULL ,
`goals_number` VARCHAR(255) NOT NULL ,
`id_session` VARCHAR(11) NOT NULL ,
PRIMARY KEY (`id`)
) ENGINE=InnoDB DEFAULT COLLATE=utf8_polish_ci;
CREATE TABLE IF NOT EXISTS `#__footsal_yellow_cards` (
`id` int(11) UNSIGNED NOT NULL AUTO_INCREMENT,
`state` TINYINT(1) NOT NULL DEFAULT '1',
`checked_out` INT(11) NOT NULL ,
`checked_out_time` DATETIME NOT NULL DEFAULT '0000-00-00 00:00:00',
`created_by` INT(11) NOT NULL ,
`id_player` INT(11) NOT NULL ,
`id_game` INT(11) NOT NULL ,
`id_resault` INT(11) NOT NULL ,
PRIMARY KEY (`id`)
) ENGINE=InnoDB DEFAULT COLLATE=utf8_polish_ci;
I don't have MySQL running atm, but code below should work for goals. The idea is that when you define the foreign key, you assign delete and update rules. These apply when the primary key (id in player) is modified. So when the primary key in player is deleted (i.e. the player is deleted), the rows with corresponding foreign keys, behave according to their rule. Cascade means 'follow', so in this case delete it too.
CREATE TABLE IF NOT EXISTS `#__footsal_goals` (
`id` int(11) UNSIGNED NOT NULL AUTO_INCREMENT,
`state` TINYINT(1) NOT NULL DEFAULT '1',
`checked_out` INT(11) NOT NULL ,
`checked_out_time` DATETIME NOT NULL DEFAULT '0000-00-00 00:00:00',
`created_by` INT(11) NOT NULL ,
`id_player` INT(11) NOT NULL ,
`id_resault` INT(11) NOT NULL ,
`id_game` INT(11) NOT NULL ,
`goals_number` VARCHAR(255) NOT NULL ,
`id_session` VARCHAR(11) NOT NULL ,
PRIMARY KEY (`id`),
FOREIGN KEY goals_player_fk (`id_player`)
REFERENCES `#__footsal_players` (`id`)
ON DELETE CASCADE
ON UPDATE CASCADE
) ENGINE=InnoDB DEFAULT COLLATE=utf8_polish_ci;

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

I am getting a
"Cannot add or update a child row: a foreign key constraint fails
(smarturbia.pois, CONSTRAINT fk_pois_cities1 FOREIGN KEY
(city) REFERENCES cities (id) ON DELETE NO ACTION ON UPDATE NO
ACTION) (1509)"
and i have no idea why.
My tables are generated through Mysql workbench 5.2, if you need the rest of the tables let me know :
-- -----------------------------------------------------
-- Table `smarturbia`.`cities`
-- -----------------------------------------------------
DROP TABLE IF EXISTS `smarturbia`.`cities` ;
CREATE TABLE IF NOT EXISTS `smarturbia`.`cities` (
`id` BIGINT(11) NOT NULL AUTO_INCREMENT ,
`published` VARCHAR(1) NOT NULL DEFAULT '0' ,
`open` VARCHAR(1) NOT NULL DEFAULT '0' ,
`path` VARCHAR(25) NOT NULL DEFAULT 'taxi' ,
`key` VARCHAR(50) NOT NULL ,
`world` VARCHAR(10) NOT NULL DEFAULT 'earth' ,
`name` VARCHAR(50) NOT NULL ,
`description` VARCHAR(250) NULL DEFAULT NULL ,
`logo` VARCHAR(250) NULL DEFAULT NULL ,
`footer` VARCHAR(250) NOT NULL DEFAULT '/taxi/assets/img/taxi_smarturbia_image_default.png' ,
`footer_large` VARCHAR(250) NOT NULL ,
`leftpub` VARCHAR(50) NOT NULL ,
`rightpub` VARCHAR(50) NOT NULL ,
`model` VARCHAR(250) NOT NULL ,
`modelxscale` FLOAT NULL DEFAULT '1' ,
`modelyscale` FLOAT NULL DEFAULT '1' ,
`modelzscale` FLOAT NULL DEFAULT '1' ,
`wheelmodelxscale` FLOAT NULL DEFAULT '1' ,
`wheelmodelyscale` FLOAT NULL DEFAULT '1' ,
`wheelmodelzscale` FLOAT NULL DEFAULT '1' ,
`allwheels` VARCHAR(250) NULL DEFAULT NULL ,
`frontleftwheel` VARCHAR(250) NULL DEFAULT NULL ,
`frontrightwheel` VARCHAR(250) NULL DEFAULT NULL ,
`rearleftwheel` VARCHAR(250) NULL DEFAULT NULL ,
`rearrightwheel` VARCHAR(250) NULL DEFAULT NULL ,
`axisdistance` FLOAT NOT NULL DEFAULT '2.5' ,
`wheelsdistance` FLOAT NULL DEFAULT '1' ,
`wheelsheight` FLOAT NULL DEFAULT '1' ,
`kms` FLOAT NULL DEFAULT '0' ,
`maxspeed` FLOAT NULL DEFAULT '160' ,
`accel` FLOAT NULL DEFAULT '25' ,
`accelstep` FLOAT NULL DEFAULT '25' ,
`minaccelstep` FLOAT NULL DEFAULT '5' ,
`maxrevspeed` FLOAT NULL DEFAULT '30' ,
`decel` FLOAT NULL DEFAULT '90' ,
`gravity` FLOAT NULL DEFAULT '70' ,
`camheight` FLOAT NULL DEFAULT '5' ,
`camtilt` FLOAT NULL DEFAULT '90' ,
`traildistance` FLOAT NULL DEFAULT '15' ,
`mass` FLOAT NULL DEFAULT '3000' ,
`vehicleagility` FLOAT NULL DEFAULT '0.0005' ,
`suspensionstiffness` FLOAT NULL DEFAULT '0.5' ,
`suspensionrestlength` FLOAT NULL DEFAULT '0.5' ,
`suspensiondamping` FLOAT NULL DEFAULT '-0.15' ,
`suspensiondeltatime` FLOAT NULL DEFAULT '0.25' ,
`turnspeedmin` FLOAT NULL DEFAULT '20' ,
`turnspeedmax` FLOAT NULL DEFAULT '60' ,
`speedmaxturn` FLOAT NULL DEFAULT '5' ,
`speedminturn` FLOAT NULL DEFAULT '50' ,
`steerroll` FLOAT NULL DEFAULT '-1' ,
`rollspring` FLOAT NULL DEFAULT '0.5' ,
`rollclamp` FLOAT NULL DEFAULT '50' ,
`mapiconurl` VARCHAR(250) NULL DEFAULT NULL ,
`vehicleshadow` VARCHAR(250) NULL DEFAULT NULL ,
`vehiclesound` VARCHAR(250) NULL DEFAULT NULL ,
`vehiclesoundtime` FLOAT NULL DEFAULT '150' ,
`vehiclefastsound` VARCHAR(250) NULL DEFAULT NULL ,
`vehiclefastsoundtime` FLOAT NULL DEFAULT '150' ,
`backgroundsound` VARCHAR(250) NULL DEFAULT NULL ,
`backgroundsoundtime` FLOAT NULL DEFAULT '150' ,
`crashsound` VARCHAR(250) NULL DEFAULT NULL ,
`crashsoundtime` FLOAT NULL DEFAULT '150' ,
`vehicletype` VARCHAR(50) NULL DEFAULT 'car' ,
`date` TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP ,
PRIMARY KEY (`id`) ,
INDEX `key` (`key` ASC) )
ENGINE = InnoDB
AUTO_INCREMENT = 153
DEFAULT CHARACTER SET = latin1;
-- -----------------------------------------------------
-- Table `smarturbia`.`pois`
-- -----------------------------------------------------
DROP TABLE IF EXISTS `smarturbia`.`pois` ;
CREATE TABLE IF NOT EXISTS `smarturbia`.`pois` (
`id` BIGINT(20) NOT NULL AUTO_INCREMENT ,
`city` BIGINT(11) NOT NULL ,
`name` VARCHAR(50) NOT NULL ,
`description` VARCHAR(250) NOT NULL ,
`lat` DOUBLE NOT NULL ,
`lon` DOUBLE NOT NULL ,
`heading` FLOAT NULL DEFAULT '0' ,
PRIMARY KEY (`id`) ,
INDEX `fk_pois_cities1_idx` (`city` ASC) ,
CONSTRAINT `fk_pois_cities1`
FOREIGN KEY (`city` )
REFERENCES `smarturbia`.`cities` (`id` )
ON DELETE NO ACTION
ON UPDATE NO ACTION)
ENGINE = InnoDB
AUTO_INCREMENT = 408
DEFAULT CHARACTER SET = latin1;
Insert the values in the table in which your primary key is defined..Then try to insert in the table in which you having the foreign key...
I think you are inserting the values in the table of foreign key without inserting in the table having primary key..
It is not recommended but you can try this
set foreign_key_checks=0;
To enable foreign key checking
set foreign_key_checks=1;

How to fetch data from row that is indexed to another table row?

Hi I have mysql table like that:
.poll_users
(id int(11) NOT NULL AUTO_INCREMENT
name VARCHAR(255) NOT NULL
PRIMARY KEY (id))
.poll_referendum
(id int(11) NOT NULL AUTO_INCREMENT
name varchar(255) DEFAULT NULL
PRIMARY KEY (id))
.poll_questions
id int(11) NOT NULL AUTO_INCREMENT
referendum_id int(11) DEFAULT NULL
body varchar(255) DEFAULT NULL
created_at datetime DEFAULT NULL
updated_at datetime DEFAULT NULL
PRIMARY KEY (`id`)
KEY `referendum_id` (`referendum_id`))
.poll_answers
`id` int(11) NOT NULL AUTO_INCREMENT
`vote_id` int(11) DEFAULT '0'
`question_id` int(11) NOT NULL
`created_at` datetime DEFAULT NULL
`updated_at` datetime DEFAULT NULL
PRIMARY KEY (`id`)
KEY `vote_id` (`vote_id`)
KEY `question_id` (`question_id`))
.poll_voting
`id` int(11) NOT NULL AUTO_INCREMENT
`question_id` int(11) NOT NULL DEFAULT '0'
`answer_id` int(11) NOT NULL DEFAULT '0'
`user_id` int(11) NOT NULL DEFAULT '0'
`created_at` datetime DEFAULT NULL
`updated_at` datetime DEFAULT NULL
PRIMARY KEY (`id`)
KEY `question_id` (`question_id`)
KEY `answer_id` (`answer_id`)
KEY `user_id` (`user_id`))
.vote_types
`id` int(11) NOT NULL AUTO_INCREMENT
`type` varchar(255) DEFAULT NULL
PRIMARY KEY (`id`))
How can I fetch vote types that are in vote_types table and correspond to proper question and survey?
I have tried: SELECT vote_id FROM surveys.poll_answers WHERE question_id = 1;
but this gives me:
and 1,2 are indexes to the vote type text SELECT *
FROMsurveys.vote_types
WHEREid=1
In poll_answers table I keep indexes what vote types are assigned to questions and questions are assigned to proper referendums in poll_referendum table
so how do I fetch those vote types corresponing to proper question and survey
like
SELECT vote_id indexes from poll_answers table and they index to vote_types where are stored vote types in text and those vote_types corresponds to proper questions that are stored in poll_questions and referendums that are stored in poll_referendum ?
Thank you for help
I'm not sure. Perhaps you are talking about table joins.
SELECT
v.*
FROM poll_answers AS a
INNER JOIN poll_voting AS v ON (v.id=a.vote_id)
WHERE a.question_id = 1;