Sub query a "bit slow" how to improve? - mysql

I've been working for a while with MySQL, and right now I have a problem with some sub query, that takes 60 sec to execute! :(
I should change this because it seems a "bit slow", and I'm no SQL expert, so I figure I'd ask if there's a smarter way of doing this. This is the current query:
SELECT VAR_ID, VAR_REF, DET_VAR_NOMB, DET_VAR_IMG, DET_VAR_MEDIDA, DET_VAR_DETALLE, SEN_FECH, SEN_VALOR
FROM variable, detalle_variable, senal
WHERE equipo_EQO_ID =6
AND detalle_variable_DET_VAR_ID = DET_VAR_ID
AND variable_VAR_ID = VAR_ID
AND SEN_ID
IN (
SELECT MAX( SEN_ID )
FROM senal
GROUP BY variable_VAR_ID
)
The query result is: (and thats all I wish to select)
VAR_ID VAR_REF DET_VAR_NOMB DET_VAR_IMG DET_VAR_MEDIDA DET_VAR_DETALLE SEN_FECH SEN_VALOR
8 101 vsth ../../img/foto/variable/play_blue.png something something 2012-05-30 03:14:17 16
The tables look like this:
-- -----------------------------------------------------
-- Table `pgssgc_stap2`.`detalle_variable`
-- -----------------------------------------------------
DROP TABLE IF EXISTS `pgssgc_stap2`.`detalle_variable` ;
CREATE TABLE IF NOT EXISTS `pgssgc_stap2`.`detalle_variable` (
`DET_VAR_ID` INT(10) UNSIGNED NOT NULL AUTO_INCREMENT ,
`DET_VAR_NOMB` VARCHAR(45) NOT NULL ,
`DET_VAR_MEDIDA` VARCHAR(45) NULL DEFAULT NULL ,
`DET_VAR_DETALLE` VARCHAR(255) NULL DEFAULT NULL ,
`DET_VAR_IMG` VARCHAR(255) NULL DEFAULT NULL ,
PRIMARY KEY (`DET_VAR_ID`) )
ENGINE = InnoDB
AUTO_INCREMENT = 101
DEFAULT CHARACTER SET = utf8
COLLATE = utf8_general_ci;
-- -----------------------------------------------------
-- Table `pgssgc_stap2`.`variable`
-- -----------------------------------------------------
DROP TABLE IF EXISTS `pgssgc_stap2`.`variable` ;
CREATE TABLE IF NOT EXISTS `pgssgc_stap2`.`variable` (
`VAR_ID` INT(10) UNSIGNED NOT NULL AUTO_INCREMENT ,
`VAR_REF` VARCHAR(45) NOT NULL ,
`detalle_variable_DET_VAR_ID` INT(10) UNSIGNED NOT NULL ,
`equipo_EQO_ID` INT(10) UNSIGNED NOT NULL ,
PRIMARY KEY (`VAR_ID`) ,
INDEX `fk_variable_detalle_variable1` (`detalle_variable_DET_VAR_ID` ASC) ,
INDEX `fk_variable_equipo1` (`equipo_EQO_ID` ASC) ,
CONSTRAINT `fk_variable_detalle_variable1`
FOREIGN KEY (`detalle_variable_DET_VAR_ID` )
REFERENCES `pgssgc_stap2`.`detalle_variable` (`DET_VAR_ID` )
ON DELETE NO ACTION
ON UPDATE NO ACTION,
CONSTRAINT `fk_variable_equipo1`
FOREIGN KEY (`equipo_EQO_ID` )
REFERENCES `pgssgc_stap2`.`equipo` (`EQO_ID` )
ON DELETE NO ACTION
ON UPDATE NO ACTION)
ENGINE = InnoDB
AUTO_INCREMENT = 11
DEFAULT CHARACTER SET = utf8
COLLATE = utf8_general_ci;
-- -----------------------------------------------------
-- Table `pgssgc_stap2`.`senal`
-- -----------------------------------------------------
DROP TABLE IF EXISTS `pgssgc_stap2`.`senal` ;
CREATE TABLE IF NOT EXISTS `pgssgc_stap2`.`senal` (
`SEN_ID` INT(11) NOT NULL AUTO_INCREMENT ,
`variable_VAR_ID` INT(10) UNSIGNED NOT NULL ,
`SEN_FECH` DATETIME NULL DEFAULT NULL ,
`SEN_VALOR` VARCHAR(45) NOT NULL ,
PRIMARY KEY (`SEN_ID`) ,
INDEX `fk_senal_variable1` (`variable_VAR_ID` ASC) ,
CONSTRAINT `fk_senal_variable1`
FOREIGN KEY (`variable_VAR_ID` )
REFERENCES `pgssgc_stap2`.`variable` (`VAR_ID` )
ON DELETE NO ACTION
ON UPDATE NO ACTION)
ENGINE = InnoDB
AUTO_INCREMENT = 2086
DEFAULT CHARACTER SET = utf8
COLLATE = utf8_general_ci;
Thank you for your reply.

Assuming that you have problem with jut the sub query.
Make sure that the column variable_VAR_ID of table senal is INDEXED.

Related

MySQL Referencing Same Foreign key twice

Trying to do some data modelling. I'm trying to track two bots in my Games, Matches, and Turns tables. These bots are listed in the Bots table, and in the three mentioned tables the foriegn key needs to appear twice (once for each bot).
This database is intended to record the results of two AI's competing against each other
Not sure if this is good practice for the model, but when trying to implement this I get errorno: 150. Not understanding how to resolve this issue. Any help and advice would be appreciated. SQL code listed below.
SET #OLD_UNIQUE_CHECKS=##UNIQUE_CHECKS, UNIQUE_CHECKS=0;
SET #OLD_FOREIGN_KEY_CHECKS=##FOREIGN_KEY_CHECKS, FOREIGN_KEY_CHECKS=0;
SET #OLD_SQL_MODE=##SQL_MODE, SQL_MODE='TRADITIONAL,ALLOW_INVALID_DATES';
CREATE SCHEMA IF NOT EXISTS `battleship` DEFAULT CHARACTER SET utf8 COLLATE utf8_general_ci ;
USE `battleship` ;
-- -----------------------------------------------------
-- Table `battleship`.`Security`
-- -----------------------------------------------------
DROP TABLE IF EXISTS `battleship`.`Security` ;
CREATE TABLE IF NOT EXISTS `battleship`.`Security` (
`SecurityID` INT NOT NULL ,
`Level` VARCHAR(45) NULL ,
`Description` VARCHAR(200) NULL ,
PRIMARY KEY (`SecurityID`) )
ENGINE = InnoDB;
-- -----------------------------------------------------
-- Table `battleship`.`Users`
-- -----------------------------------------------------
DROP TABLE IF EXISTS `battleship`.`Users` ;
CREATE TABLE IF NOT EXISTS `battleship`.`Users` (
`UserID` INT NOT NULL ,
`Username` VARCHAR(45) NULL ,
`First_name` VARCHAR(45) NULL ,
`Last_name` VARCHAR(45) NULL ,
`Email` VARCHAR(45) NULL ,
`Student Number` INT NULL ,
`Enabled` BINARY NULL ,
`SecurityID` INT NOT NULL ,
PRIMARY KEY (`UserID`) ,
INDEX `fk_Users_Security_idx` (`SecurityID` ASC) ,
CONSTRAINT `fk_Users_Security`
FOREIGN KEY (`SecurityID` )
REFERENCES `battleship`.`Security` (`SecurityID` )
ON DELETE NO ACTION
ON UPDATE NO ACTION)
ENGINE = InnoDB;
-- -----------------------------------------------------
-- Table `battleship`.`News`
-- -----------------------------------------------------
DROP TABLE IF EXISTS `battleship`.`News` ;
CREATE TABLE IF NOT EXISTS `battleship`.`News` (
`EventID` INT NOT NULL ,
`Event_Name` VARCHAR(45) NULL ,
`Event_Date` DATETIME NULL ,
`Event_Description` VARCHAR(45) NULL ,
`UserID` INT NOT NULL ,
PRIMARY KEY (`EventID`, `UserID`) ,
INDEX `fk_News_Users1_idx` (`UserID` ASC) ,
CONSTRAINT `fk_News_Users1`
FOREIGN KEY (`UserID` )
REFERENCES `battleship`.`Users` (`UserID` )
ON DELETE NO ACTION
ON UPDATE NO ACTION)
ENGINE = InnoDB;
-- -----------------------------------------------------
-- Table `battleship`.`Bots`
-- -----------------------------------------------------
DROP TABLE IF EXISTS `battleship`.`Bots` ;
CREATE TABLE IF NOT EXISTS `battleship`.`Bots` (
`BotID` INT NOT NULL ,
`Name` VARCHAR(45) NULL ,
`UserID` INT NOT NULL ,
`Revision` INT NULL ,
`SubmissionDate` DATETIME NULL ,
`Approved` BINARY NULL ,
PRIMARY KEY (`BotID`, `UserID`) ,
INDEX `fk_Bots_Users1_idx` (`UserID` ASC) ,
CONSTRAINT `fk_Bots_Users1`
FOREIGN KEY (`UserID` )
REFERENCES `battleship`.`Users` (`UserID` )
ON DELETE NO ACTION
ON UPDATE NO ACTION)
ENGINE = InnoDB;
-- -----------------------------------------------------
-- Table `battleship`.`Competitions`
-- -----------------------------------------------------
DROP TABLE IF EXISTS `battleship`.`Competitions` ;
CREATE TABLE IF NOT EXISTS `battleship`.`Competitions` (
`CompetitionsID` INT NOT NULL ,
`CompetitionName` VARCHAR(45) NULL ,
`Description` VARCHAR(45) NULL ,
`Date` VARCHAR(45) NULL ,
PRIMARY KEY (`CompetitionsID`) )
ENGINE = InnoDB;
-- -----------------------------------------------------
-- Table `battleship`.`Matches`
-- -----------------------------------------------------
DROP TABLE IF EXISTS `battleship`.`Matches` ;
CREATE TABLE IF NOT EXISTS `battleship`.`Matches` (
`MatchID` INT NOT NULL ,
`Winner` VARCHAR(45) NULL ,
`Bots_BotID1` INT NOT NULL ,
`Bots_BotID2` INT NOT NULL ,
`CompetitionsID` INT NOT NULL ,
PRIMARY KEY (`MatchID`) ,
INDEX `fk_Matches_Bots1_idx` (`Bots_BotID1` ASC, `Bots_BotID2` ASC) ,
INDEX `fk_Matches_Competitions1_idx` (`CompetitionsID` ASC) ,
CONSTRAINT `fk_Matches_Bots1`
FOREIGN KEY (`Bots_BotID1` , `Bots_BotID2` )
REFERENCES `battleship`.`Bots` (`BotID` , `BotID` )
ON DELETE NO ACTION
ON UPDATE NO ACTION,
CONSTRAINT `fk_Matches_Competitions1`
FOREIGN KEY (`CompetitionsID` )
REFERENCES `battleship`.`Competitions` (`CompetitionsID` )
ON DELETE NO ACTION
ON UPDATE NO ACTION)
ENGINE = InnoDB;
-- -----------------------------------------------------
-- Table `battleship`.`Entrants`
-- -----------------------------------------------------
DROP TABLE IF EXISTS `battleship`.`Entrants` ;
CREATE TABLE IF NOT EXISTS `battleship`.`Entrants` (
`EntryID` INT NOT NULL ,
`Bots_BotID` INT NOT NULL ,
`Competitions_CompetitionsID` INT NOT NULL ,
PRIMARY KEY (`EntryID`) ,
INDEX `fk_Entrants_Bots1_idx` (`Bots_BotID` ASC) ,
INDEX `fk_Entrants_Competitions1_idx` (`Competitions_CompetitionsID` ASC) ,
CONSTRAINT `fk_Entrants_Bots1`
FOREIGN KEY (`Bots_BotID` )
REFERENCES `battleship`.`Bots` (`BotID` )
ON DELETE NO ACTION
ON UPDATE NO ACTION,
CONSTRAINT `fk_Entrants_Competitions1`
FOREIGN KEY (`Competitions_CompetitionsID` )
REFERENCES `battleship`.`Competitions` (`CompetitionsID` )
ON DELETE NO ACTION
ON UPDATE NO ACTION)
ENGINE = InnoDB;
-- -----------------------------------------------------
-- Table `battleship`.`Games`
-- -----------------------------------------------------
DROP TABLE IF EXISTS `battleship`.`Games` ;
CREATE TABLE IF NOT EXISTS `battleship`.`Games` (
`GameID` INT NOT NULL ,
`Matches_MatchID` INT NOT NULL ,
`Bots_BotID1` INT NOT NULL ,
`Bots_BotID2` INT NOT NULL ,
`Winner` VARCHAR(45) NULL ,
PRIMARY KEY (`GameID`) ,
INDEX `fk_Games_Matches1_idx` (`Matches_MatchID` ASC) ,
INDEX `fk_Games_Bots1_idx` (`Bots_BotID1` ASC, `Bots_BotID2` ASC) ,
CONSTRAINT `fk_Games_Matches1`
FOREIGN KEY (`Matches_MatchID` )
REFERENCES `battleship`.`Matches` (`MatchID` )
ON DELETE NO ACTION
ON UPDATE NO ACTION,
CONSTRAINT `fk_Games_Bots1`
FOREIGN KEY (`Bots_BotID1` , `Bots_BotID2` )
REFERENCES `battleship`.`Bots` (`BotID` , `BotID` )
ON DELETE NO ACTION
ON UPDATE NO ACTION)
ENGINE = InnoDB;
-- -----------------------------------------------------
-- Table `battleship`.`Turns`
-- -----------------------------------------------------
DROP TABLE IF EXISTS `battleship`.`Turns` ;
CREATE TABLE IF NOT EXISTS `battleship`.`Turns` (
`TurnID` INT NOT NULL ,
`Bots_BotID1` INT NOT NULL ,
`Bots_BotID2` INT NOT NULL ,
`Bot1Move` VARCHAR(45) NULL ,
`Bot2Move` VARCHAR(45) NULL ,
`ThinkingTime` VARCHAR(45) NULL ,
`Turnscol` VARCHAR(45) NULL ,
`Games_GameID` INT NOT NULL ,
PRIMARY KEY (`TurnID`) ,
INDEX `fk_Turns_Bots1_idx` (`Bots_BotID1` ASC, `Bots_BotID2` ASC) ,
INDEX `fk_Turns_Games1_idx` (`Games_GameID` ASC) ,
CONSTRAINT `fk_Turns_Bots1`
FOREIGN KEY (`Bots_BotID1` , `Bots_BotID2` )
REFERENCES `battleship`.`Bots` (`BotID` , `BotID` )
ON DELETE NO ACTION
ON UPDATE NO ACTION,
CONSTRAINT `fk_Turns_Games1`
FOREIGN KEY (`Games_GameID` )
REFERENCES `battleship`.`Games` (`GameID` )
ON DELETE NO ACTION
ON UPDATE NO ACTION)
ENGINE = InnoDB;
USE `battleship` ;
SET SQL_MODE=#OLD_SQL_MODE;
SET FOREIGN_KEY_CHECKS=#OLD_FOREIGN_KEY_CHECKS;
SET UNIQUE_CHECKS=#OLD_UNIQUE_CHECKS;
Think I found a viable solution here with modifications to my model.
I defined many to many relationships and ended up creating 3 more tables to associate bots against turns, games, and matches. Below is an updated screen shot of the model to illustrate.
Ran this against mysql and it accepted it without error. Cheers

cant create table in Mysql database

I am trying to create table in Mysql database with the below sql
CREATE TABLE IF NOT EXISTS `dev_dict`.`TAMILWORD_SECONDARY` (
`ID` INT NOT NULL ,
`TAMILWORD_ID` INT NOT NULL ,
`WORDS` VARCHAR(50) NOT NULL ,
`CREATED_DTTM` TIMESTAMP NOT NULL ,
`MODIFIED_DTTM` TIMESTAMP NOT NULL ,
PRIMARY KEY (`ID`) ,
UNIQUE INDEX `WORDS_UNIQUE` (`WORDS` ASC, `TAMILWORD_ID` ASC) ,
INDEX `FK_TAMILWORD_ID_idx` (`TAMILWORD_ID` ASC) ,
CONSTRAINT `FK_TAMILWORD_ID`
FOREIGN KEY (`TAMILWORD_ID` )
REFERENCES `dev_dict`.`TAMILWORD` (`ID` )
ON DELETE NO ACTION
ON UPDATE NO ACTION)
ENGINE = InnoDB
but getting Error Code: 1022. Can't write; duplicate key in table 'tamilword_secondary'.
thanks for helping me.. i have the table tamilword already , and getting same error
CREATE TABLE IF NOT EXISTS `dev_dict`.`TAMILWORD` (
`ID` INT NOT NULL ,
`WORD` VARCHAR(50) CHARACTER SET 'utf8' COLLATE 'utf8_general_ci' NOT NULL ,
`SENTENCE` VARCHAR(100) NULL ,
`CREATED_DTTM` TIMESTAMP NOT NULL ,
`MODIFIED_DTTM` TIMESTAMP NOT NULL ,
PRIMARY KEY (`ID`) ,
INDEX `SECONDARY` (`WORD` ASC, `SENTENCE` ASC) )
ENGINE = InnoDB
Create dev_dict.TAMILWORD table first.
Other than that your statement works.
See SQL Fiddle example

Should this SELECT use a join or a subquery?

SELECT u.users_username, u.givenname, u.familyname, u.studentassent, u.parentconsent, u.birthdate, u.gender
FROM users AS u
JOIN classes_users as c
ON c.users_username = u.users_username
JOIN classes_users as x
ON c.classes_id = x.classes_id
WHERE x.users_username = "johnny" AND x.role = "teacher"
Or
SELECT u.users_username, u.givenname, u.familyname, u.studentassent, u.parentconsent, u.birthdate, u.gender
FROM users AS u
WHERE u.users_username
IN (
SELECT c.users_username
FROM classes_users as c
JOIN classes_users as x
ON c.classes_id = x.classes_id
WHERE x.users_username = "johnny" AND x.role = "teacher"
)
I'm thinking the first one is better, but I'm still learning how to write better SQL statements am not clear on all the internals of what happens that makes one statement better than the other in this case.
If there is a better way to write them than the two ways I've written, please let me know. Thanks.
EDIT:
There are teachers and students. Their position as student or teacher in any given class is found by looking at the classes_users table. What I want to do is when given a user, find the classes in which he is a teacher, then return all students in those classes.
Here is my DB schema:
-- -----------------------------------------------------
-- Table `kcptech`.`users`
-- -----------------------------------------------------
CREATE TABLE IF NOT EXISTS `kcptech`.`users` (
`users_username` VARCHAR(63) NOT NULL ,
`password` VARCHAR(255) NULL DEFAULT NULL ,
`salt` VARCHAR(127) NULL DEFAULT NULL ,
`givenname` VARCHAR(96) NULL DEFAULT NULL ,
`familyname` VARCHAR(128) NULL DEFAULT NULL ,
`privileges` TINYINT NULL DEFAULT NULL ,
`studentassent` TINYINT(1) UNSIGNED NULL DEFAULT NULL ,
`parentconsent` TINYINT(1) UNSIGNED NULL DEFAULT NULL ,
`birthdate` DATE NULL DEFAULT NULL ,
`gender` VARCHAR(1) NULL DEFAULT NULL ,
`registration` TIMESTAMP NULL DEFAULT NULL ,
PRIMARY KEY (`users_username`) ,
UNIQUE INDEX `uname_UNIQUE` (`users_username` ASC) )
ENGINE = InnoDB;
-- -----------------------------------------------------
-- Table `kcptech`.`classes`
-- -----------------------------------------------------
CREATE TABLE IF NOT EXISTS `kcptech`.`classes` (
`classes_id` INT UNSIGNED NOT NULL AUTO_INCREMENT ,
`course` VARCHAR(127) NULL ,
`period` VARCHAR(31) NULL DEFAULT '' ,
`organization` VARCHAR(127) NULL DEFAULT '' ,
PRIMARY KEY (`classes_id`) ,
UNIQUE INDEX `id_UNIQUE` (`classes_id` ASC) )
ENGINE = InnoDB;
-- -----------------------------------------------------
-- Table `kcptech`.`classes_users`
-- -----------------------------------------------------
CREATE TABLE IF NOT EXISTS `kcptech`.`classes_users` (
`classes_id` INT UNSIGNED NOT NULL ,
`users_username` VARCHAR(64) NOT NULL ,
`role` VARCHAR(12) NOT NULL ,
PRIMARY KEY (`classes_id`, `users_username`) ,
INDEX `fk_class_users__users_users_username` (`users_username` ASC) ,
INDEX `fk_class_users__class_class_id` (`classes_id` ASC) ,
CONSTRAINT `fk_class_users__users_users_username`
FOREIGN KEY (`users_username` )
REFERENCES `kcptech`.`users` (`users_username` )
ON DELETE NO ACTION
ON UPDATE NO ACTION,
CONSTRAINT `fk_class_users__class_class_id`
FOREIGN KEY (`classes_id` )
REFERENCES `kcptech`.`classes` (`classes_id` )
ON DELETE NO ACTION
ON UPDATE NO ACTION)
ENGINE = InnoDB;
The first one is better, assuming that (classes_id, users_username) is unique.
MySQL cannot do semi-joins (IN constructs) with the inner query leading. So the IN query will always use users as a leading table, while for the JOIN query, the optimizer can choose the leading table.
If (classes_id, users_username) is not unique, the queries are semantically not equivalent. You would need to add DISTINCT to the join query.

MySQL 1005 Error - What's wrong with the query?

I'm unable to see the issue with this CREATE TABLE sentences. I've double checked but can't find the error.
-- -----------------------------------------------------
-- Table `agentes`
-- -----------------------------------------------------
CREATE TABLE IF NOT EXISTS `agentes` (
`id` INT(11) NOT NULL AUTO_INCREMENT ,
`codAgente` VARCHAR(8) NOT NULL ,
`clave` VARCHAR(8) NOT NULL ,
`nombre` VARCHAR(50) NOT NULL ,
`apellido1` VARCHAR(50) NOT NULL ,
`apellido2` VARCHAR(50) NOT NULL ,
`email` VARCHAR(100) NOT NULL ,
`usuarioDA` VARCHAR(45) NULL ,
`passDA` VARCHAR(100) NULL ,
PRIMARY KEY (`id`) )
ENGINE = InnoDB
COLLATE = latin1_spanish_ci;
-- -----------------------------------------------------
-- Table `cola`
-- -----------------------------------------------------
CREATE TABLE IF NOT EXISTS `colas` (
`idCola` INT NOT NULL AUTO_INCREMENT ,
`cliente` INT NOT NULL ,
`nombre` VARCHAR(45) NOT NULL ,
PRIMARY KEY (`idCola`) ,
INDEX `fk_colas_clientes1` (`cliente` ASC) ,
CONSTRAINT `fk_colas_clientes1`
FOREIGN KEY (`cliente` )
REFERENCES `clientes` (`idCliente` )
ON DELETE NO ACTION
ON UPDATE NO ACTION)
ENGINE = InnoDB;
-- -----------------------------------------------------
-- Table `relColaExt`
-- -----------------------------------------------------
CREATE TABLE IF NOT EXISTS `relColaExt` (
`id` INT NOT NULL AUTO_INCREMENT ,
`codAgente` VARCHAR(8) NOT NULL ,
`cola` VARCHAR(45) NOT NULL ,
`prioridad` INT NOT NULL ,
PRIMARY KEY (`id`) ,
UNIQUE INDEX `id` (`id` ASC) ,
INDEX `fk_relColaExt_agentes1` (`codAgente` ASC) ,
INDEX `fk_relColaExt_colas1` (`cola` ASC) ,
CONSTRAINT `fk_relColaExt_agentes1`
FOREIGN KEY (`codAgente` )
REFERENCES `agentes` (`codAgente` )
ON DELETE NO ACTION
ON UPDATE NO ACTION,
CONSTRAINT `fk_relColaExt_colas1`
FOREIGN KEY (`cola` )
REFERENCES `colas` (`nombre` )
ON DELETE NO ACTION
ON UPDATE NO ACTION)
ENGINE = InnoDB;
It throws #1005 - Can't create table './centralita/relColaExt.frm' (errno: 150) creating the relColaExt table.
It's a Foreign Key issue but I'm not able to see what's wrong. Can someone help me, please?
According to this comment on MySQL manual page on Foreign keys, it may be due to different encoding of the VARCHAR columns. In table agentes you defined a collation but in relColaExt you did not.
Try using the same collation on every table (by defining it or by letting the default kick in).

sql error errno: 121

CREATE TABLE `users` (
`UID` INT UNSIGNED NOT NULL AUTO_INCREMENT ,
`username` VARCHAR(45) NOT NULL ,
`password` VARCHAR(100) NULL ,
`name` VARCHAR(100) NULL ,
`gender` BIT NULL ,
`email` VARCHAR(255) NULL ,
`phone` VARCHAR(30) NOT NULL ,
`verified` BIT NOT NULL DEFAULT 0 ,
`time_zone` INT NULL ,
`time_register` DATETIME NULL ,
`time_active` DATETIME NULL ,
PRIMARY KEY (`UID`) ,
UNIQUE INDEX `username_UNIQUE` (`username` ASC) ,
INDEX `verified_INDEX` (`verified` ASC) )
ENGINE = InnoDB
DEFAULT CHARACTER SET = utf8;
-- -----------------------------------------------------
-- Table `companies`
-- -----------------------------------------------------
CREATE TABLE `companies` (
`CID` INT UNSIGNED NOT NULL AUTO_INCREMENT ,
`name` VARCHAR(45) NOT NULL ,
`address` VARCHAR(100) NULL ,
`email` VARCHAR(255) NULL ,
`phone` VARCHAR(30) NULL ,
`link` TEXT NULL ,
`image_small` TEXT NULL ,
`image_large` TEXT NULL ,
`yahoo` VARCHAR(100) NULL ,
`linkin` VARCHAR(100) NULL ,
`twitter` VARCHAR(20) NULL ,
`description` TEXT NULL ,
`shoutout` VARCHAR(140) NULL ,
`verified` BIT NOT NULL DEFAULT 0 ,
PRIMARY KEY (`CID`) ,
INDEX `name_INDEX` (`name` ASC) ,
INDEX `verified_INDEX` (`verified` ASC) )
ENGINE = InnoDB
DEFAULT CHARACTER SET = utf8;
-- -----------------------------------------------------
-- Table `products`
-- -----------------------------------------------------
CREATE TABLE `products` (
`PID` INT UNSIGNED NOT NULL AUTO_INCREMENT ,
`CID` INT UNSIGNED NOT NULL ,
`name` VARCHAR(100) NULL ,
`description` TEXT NULL ,
`image_small` TEXT NULL ,
`image_large` TEXT NULL ,
`tag` VARCHAR(45) NULL ,
`price` DECIMAL(11,2) NULL ,
PRIMARY KEY (`PID`) ,
INDEX `tag_INDEX` (`tag` ASC) ,
INDEX `company.cid_FK` (`CID` ASC) ,
CONSTRAINT `company.cid_FK`
FOREIGN KEY (`CID` )
REFERENCES `companies` (`CID` )
ON DELETE NO ACTION
ON UPDATE NO ACTION)
ENGINE = InnoDB
DEFAULT CHARACTER SET = utf8;
-- -----------------------------------------------------
-- Table `users_companies`
-- -----------------------------------------------------
CREATE TABLE `users_companies` (
`UID` INT UNSIGNED NOT NULL ,
`CID` INT UNSIGNED NOT NULL ,
`role` INT UNSIGNED NULL ,
PRIMARY KEY (`CID`, `UID`) ,
INDEX `users.uid_FK` (`UID` ASC) ,
INDEX `company.cid_FK` (`CID` ASC) ,
CONSTRAINT `users.uid_FK`
FOREIGN KEY (`UID` )
REFERENCES `users` (`UID` )
ON DELETE NO ACTION
ON UPDATE NO ACTION,
CONSTRAINT `company.cid_FK`
FOREIGN KEY (`CID` )
REFERENCES `companies` (`CID` )
ON DELETE NO ACTION
ON UPDATE NO ACTION)
ENGINE = InnoDB
DEFAULT CHARACTER SET = utf8;
i got error
#1005 - Can't create table 'wew.users_companies' (errno: 121) (Details...)
can anyone tell me which one is the problem ?
You have two constraints called company.cid_FK. Rename one.
For errors like this, you can find more information with 'perror'. i.e.
shell $ perror 121
MySQL error code 121: Duplicate key on write or update
Win32 error code 121: The semaphore timeout period has expired.
I got the same error MySQL error code 121: Duplicate key on write or update.
although I deleted the table from my database to recreated again but I could not so I recreated the table yet with different constrain.
I found that the old constrain is still there.