Should this SELECT use a join or a subquery? - mysql

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.

Related

Illegal mix of collations for operation 'UNION' Nodejs Mysql

I´m trying to get all the posts that a user wrote using this sql:
'SELECT * FROM users WHERE username=? UNION ALL SELECT * FROM posts WHERE writer_name=?'
And i´m getting this error:
"Illegal mix of collations for operation 'UNION'"
tables
CREATE TABLE `claudioBlog`.`users` (
`id` INT UNSIGNED NOT NULL AUTO_INCREMENT ,
`username` VARCHAR(100) NOT NULL ,
`email` VARCHAR(100) NOT NULL ,
`age` VARCHAR(100),
`discription` VARCHAR (255),
`ocupation` VARCHAR (100),
`profile_image` VARCHAR (100)
`password` VARCHAR(255) NOT NULL ,
PRIMARY KEY (`id`), UNIQUE (`email`)
) ENGINE = InnoDB
CREATE TABLE posts (
id int(11) NOT NULL AUTO_INCREMENT,
title VARCHAR(50) NOT NULL,
category VARCHAR(50) NOT NULL,
body VARCHAR(2000) NOT NULL,
writer_id VARCHAR(50) NOT NULL,
writer_name VARCHAR(50) NOT NULL,
post_img VARCHAR(100),
created_at DATETIME NOT NULL DEFAULT CURRENT_TIMESTAMP,
PRIMARY KEY (id)
) ENGINE=InnoDB AUTO_INCREMENT=4 DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci;
What do i have to chage?
You want a join, not union. Presumably, writer_id is a foreign key reference to users(id)
SELECT p.*
FROM users u JOIN
posts p
ON p.writer_id = u.id
WHERE u.username = ? ;
Note: If write_id does indeed refer to users(id), it is inappropriate to store write_name in posts, unless -- for some strange reason -- it can vary by post and is not the users real name.

Correct structure and index for a Weekly table

I need a table to store text every week for each user.
So I thought two alternatives:
1) Using composite primary key:
CREATE TABLE `WeeklyTxt` (
`Year` YEAR(4) NOT NULL ,
`Week` ENUM('1','2','3','4', ... ,'51','52','53') NOT NULL ,
`UserId` BIGINT NOT NULL ,
`WeekTxt` TEXT NOT NULL,
PRIMARY KEY (`Year`, `Week`, `UserId`)
) ENGINE = InnoDB;
2) Using autoincrement primary key
CREATE TABLE `WeeklyTxt_2` (
`WeekTxtId` BIGINT UNSIGNED NOT NULL AUTO_INCREMENT PRIMARY KEY,
`Year` YEAR(4) NOT NULL ,
`Week` ENUM('1','2','3','4', ... ,'51','52','53') NOT NULL ,
`UserId` BIGINT NOT NULL ,
`WeekTxt` TEXT NOT NULL
) ENGINE = InnoDB;
I can't figure out what could be the better choice (and why)
It depends of the search in the table that you will usually do!
Tipically I would use a Simple PRIMARY KEY, and I will add another KEY like your KEY: (Year, Week, UserId)

Sub query a "bit slow" how to improve?

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.

MySQL code schema generating code that does nothing?

I'm using MySQL to design a database, I'm exporting the code for generating the database by using the 'SQL Create Script' option on the workbench. However, when I run the code, and use SHOW TABLES I get null, which probably means no table was created in the database. This is the code that was generated (its long but since it was auto generated the error is probably from a setting I had when exporting it.)
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';
CREATE SCHEMA IF NOT EXISTS `travel_agency` DEFAULT CHARACTER SET latin1 COLLATE latin1_swedish_ci ;
USE `travel_agency` ;
-- -----------------------------------------------------
-- Table `travel_agency`.`Region`
-- -----------------------------------------------------
CREATE TABLE IF NOT EXISTS `travel_agency`.`Region` (
`region_name` VARCHAR(45) NOT NULL ,
`languages` VARCHAR(100) NOT NULL ,
PRIMARY KEY (`region_name`) )
ENGINE = InnoDB;
-- -----------------------------------------------------
-- Table `travel_agency`.`Country`
-- -----------------------------------------------------
CREATE TABLE IF NOT EXISTS `travel_agency`.`Country` (
`country_name` VARCHAR(15) NOT NULL ,
`Region_region_name` VARCHAR(45) NOT NULL ,
`currency` CHAR(20) NOT NULL ,
PRIMARY KEY (`country_name`, `Region_region_name`) ,
INDEX `fk_Country_Region1` (`Region_region_name` ASC) ,
CONSTRAINT `fk_Country_Region1`
FOREIGN KEY (`Region_region_name` )
REFERENCES `travel_agency`.`Region` (`region_name` )
ON DELETE NO ACTION
ON UPDATE NO ACTION)
ENGINE = InnoDB;
-- -----------------------------------------------------
-- Table `travel_agency`.`Transport`
-- -----------------------------------------------------
CREATE TABLE IF NOT EXISTS `travel_agency`.`Transport` (
`Country_country_name` VARCHAR(15) NOT NULL ,
`cost_estimate` DECIMAL(8,2) NOT NULL ,
`transport_deals` BLOB NOT NULL ,
`transport_rating` CHAR(1) NOT NULL ,
PRIMARY KEY (`Country_country_name`) ,
CONSTRAINT `fk_Transport_Country`
FOREIGN KEY (`Country_country_name` )
REFERENCES `travel_agency`.`Country` (`country_name` )
ON DELETE NO ACTION
ON UPDATE NO ACTION)
ENGINE = InnoDB;
-- -----------------------------------------------------
-- Table `travel_agency`.`Resort`
-- -----------------------------------------------------
CREATE TABLE IF NOT EXISTS `travel_agency`.`Resort` (
`resort_name` VARCHAR(50) NOT NULL ,
`city` VARCHAR(50) NOT NULL ,
`Country_country_name` VARCHAR(15) NOT NULL ,
`resort_type` CHAR(20) NOT NULL ,
INDEX `fk_Resort_Country1` (`Country_country_name` ASC) ,
PRIMARY KEY (`resort_name`, `city`) ,
CONSTRAINT `fk_Resort_Country1`
FOREIGN KEY (`Country_country_name` )
REFERENCES `travel_agency`.`Country` (`country_name` )
ON DELETE NO ACTION
ON UPDATE NO ACTION)
ENGINE = InnoDB;
-- -----------------------------------------------------
-- Table `travel_agency`.`Hotel`
-- -----------------------------------------------------
CREATE TABLE IF NOT EXISTS `travel_agency`.`Hotel` (
`hotel_name` CHAR NOT NULL ,
`rating` CHAR(1) NOT NULL ,
`address1` CHAR(50) NOT NULL ,
`address2` CHAR(50) NOT NULL ,
`postcode` CHAR(10) NOT NULL ,
`telephone` CHAR(20) NOT NULL ,
`Resort_resort_name` VARCHAR(50) NOT NULL ,
PRIMARY KEY (`hotel_name`) ,
INDEX `fk_Hotel_Resort1` (`Resort_resort_name` ASC) ,
CONSTRAINT `fk_Hotel_Resort1`
FOREIGN KEY (`Resort_resort_name` )
REFERENCES `travel_agency`.`Resort` (`resort_name` )
ON DELETE NO ACTION
ON UPDATE NO ACTION)
ENGINE = InnoDB;
-- -----------------------------------------------------
-- Table `travel_agency`.`Room`
-- -----------------------------------------------------
CREATE TABLE IF NOT EXISTS `travel_agency`.`Room` (
`room_num` INT NOT NULL ,
`room_type` CHAR(10) NOT NULL ,
`Hotel_hotel_name` CHAR NOT NULL ,
`minibar` BIT NOT NULL ,
`tv` BIT NOT NULL ,
`smoking_permitted` BIT NOT NULL ,
INDEX `fk_Room_Hotel1` (`Hotel_hotel_name` ASC) ,
PRIMARY KEY (`room_num`, `room_type`) ,
CONSTRAINT `fk_Room_Hotel1`
FOREIGN KEY (`Hotel_hotel_name` )
REFERENCES `travel_agency`.`Hotel` (`hotel_name` )
ON DELETE NO ACTION
ON UPDATE NO ACTION)
ENGINE = InnoDB;
-- -----------------------------------------------------
-- Table `travel_agency`.`Guest`
-- -----------------------------------------------------
CREATE TABLE IF NOT EXISTS `travel_agency`.`Guest` (
`guest_id` INT NOT NULL ,
`firstname` VARCHAR(20) NOT NULL ,
`surname` VARCHAR(20) NOT NULL ,
`mobilephone` CHAR(20) NOT NULL ,
PRIMARY KEY (`guest_id`) )
ENGINE = InnoDB;
-- -----------------------------------------------------
-- Table `travel_agency`.`Bookings`
-- -----------------------------------------------------
CREATE TABLE IF NOT EXISTS `travel_agency`.`Bookings` (
`Guest_guest_id` INT NOT NULL ,
`Hotel_hotel_name` CHAR NOT NULL ,
`guest_count` INT NOT NULL ,
PRIMARY KEY (`Guest_guest_id`, `Hotel_hotel_name`) ,
INDEX `fk_Booking_Guest1` (`Guest_guest_id` ASC) ,
INDEX `fk_Booking_Hotel1` (`Hotel_hotel_name` ASC) ,
CONSTRAINT `fk_Booking_Guest1`
FOREIGN KEY (`Guest_guest_id` )
REFERENCES `travel_agency`.`Guest` (`guest_id` )
ON DELETE NO ACTION
ON UPDATE NO ACTION,
CONSTRAINT `fk_Booking_Hotel1`
FOREIGN KEY (`Hotel_hotel_name` )
REFERENCES `travel_agency`.`Hotel` (`hotel_name` )
ON DELETE NO ACTION
ON UPDATE NO ACTION)
ENGINE = InnoDB;
-- -----------------------------------------------------
-- Table `travel_agency`.`HotelFacilities`
-- -----------------------------------------------------
CREATE TABLE IF NOT EXISTS `travel_agency`.`HotelFacilities` (
`Hotel_hotel_name` CHAR NOT NULL ,
`internet_access` BIT NOT NULL ,
`hotel_restaurant` BIT NOT NULL ,
`games_room` BIT NOT NULL ,
`bar` BIT NOT NULL ,
`evening_shows` BIT NOT NULL ,
`massage_parlour` BIT NOT NULL ,
`misc_details` BLOB NOT NULL ,
PRIMARY KEY (`Hotel_hotel_name`) ,
CONSTRAINT `fk_HotelFacilities_Hotel1`
FOREIGN KEY (`Hotel_hotel_name` )
REFERENCES `travel_agency`.`Hotel` (`hotel_name` )
ON DELETE NO ACTION
ON UPDATE NO ACTION)
ENGINE = InnoDB;
-- -----------------------------------------------------
-- Table `travel_agency`.`Attractions`
-- -----------------------------------------------------
CREATE TABLE IF NOT EXISTS `travel_agency`.`Attractions` (
`Region_region_name` VARCHAR(45) NOT NULL ,
`dirt_mountains` BIT NOT NULL ,
`beaches` BIT NOT NULL ,
`casinos` BIT NOT NULL ,
`safari` BIT NOT NULL ,
`snow_mountains` BIT NOT NULL ,
`misc_details` BLOB NOT NULL ,
PRIMARY KEY (`Region_region_name`) ,
CONSTRAINT `fk_Attractions_Region1`
FOREIGN KEY (`Region_region_name` )
REFERENCES `travel_agency`.`Region` (`region_name` )
ON DELETE NO ACTION
ON UPDATE NO ACTION)
ENGINE = InnoDB;
-- -----------------------------------------------------
-- Table `travel_agency`.`Rate`
-- -----------------------------------------------------
CREATE TABLE IF NOT EXISTS `travel_agency`.`Rate` (
`Hotel_hotel_name` CHAR NOT NULL ,
`Room_room_num` INT NOT NULL ,
`Room_room_type` CHAR(10) NOT NULL ,
`first_quarter` DECIMAL(20,2) NOT NULL ,
`second_quarter` DECIMAL(20,2) NOT NULL ,
`third_quarter` DECIMAL(20,2) NOT NULL ,
`fourth_quarter` DECIMAL(20,2) NOT NULL ,
`discount_information` BLOB NOT NULL ,
PRIMARY KEY (`Hotel_hotel_name`, `Room_room_num`, `Room_room_type`) ,
INDEX `fk_Rate_Room1` (`Room_room_num` ASC, `Room_room_type` ASC) ,
CONSTRAINT `fk_Rate_Hotel1`
FOREIGN KEY (`Hotel_hotel_name` )
REFERENCES `travel_agency`.`Hotel` (`hotel_name` )
ON DELETE NO ACTION
ON UPDATE NO ACTION,
CONSTRAINT `fk_Rate_Room1`
FOREIGN KEY (`Room_room_num` , `Room_room_type` )
REFERENCES `travel_agency`.`Room` (`room_num` , `room_type` )
ON DELETE NO ACTION
ON UPDATE NO ACTION)
ENGINE = InnoDB;
SET SQL_MODE=#OLD_SQL_MODE;
SET FOREIGN_KEY_CHECKS=#OLD_FOREIGN_KEY_CHECKS;
SET UNIQUE_CHECKS=#OLD_UNIQUE_CHECKS;
I'm using sql command line client and putting the code but it doesn't work.
Its because you're using command line client as your database. That just makes your pc function as a database but doesn't exactly have all features a professional database would have. And MySQL workbench is designed to export code for databases more professional.
Since you're using command line client I'm assuming this is either practice or homework of some sort? Either way, this is the way the code would work in Command line client. Try this.
CREATE TABLE Region
(
region_name VARCHAR(45) NOT NULL ,
languages VARCHAR(100) NOT NULL ,
PRIMARY KEY (region_name)
);
CREATE TABLE Country
(
country_name VARCHAR(15) NOT NULL ,
Region_region_name VARCHAR(45) NOT NULL ,
currency CHAR(20) NOT NULL ,
PRIMARY KEY (country_name, Region_region_name) ,
CONSTRAINT fk_Country_Region1
FOREIGN KEY (Region_region_name )
REFERENCES Region (region_name )
);
CREATE TABLE Transport (
Country_country_name VARCHAR(15) NOT NULL ,
cost_estimate DECIMAL(8,2) NOT NULL ,
transport_deals CHAR(100) NULL ,
transport_rating CHAR(20) NOT NULL ,
PRIMARY KEY (Country_country_name) ,
CONSTRAINT fk_Transport_Country
FOREIGN KEY (Country_country_name)
REFERENCES Country (country_name )
);
CREATE TABLE Resort (
resort_name VARCHAR(50) NOT NULL ,
Country_country_name VARCHAR(15) NOT NULL ,
resort_type CHAR(20) NOT NULL ,
PRIMARY KEY (resort_name) ,
CONSTRAINT fk_Resort_Country1
FOREIGN KEY (Country_country_name)
REFERENCES Country (country_name)
);
CREATE TABLE Hotel (
hotel_name CHAR(20) NOT NULL ,
rating CHAR NOT NULL ,
address1 CHAR(50) NOT NULL ,
address2 CHAR(50) NOT NULL ,
postcode CHAR(10) NOT NULL ,
telephone CHAR(20) NOT NULL ,
Resort_resort_name VARCHAR(50) NOT NULL ,
PRIMARY KEY (hotel_name) ,
CONSTRAINT fk_Hotel_Resort1
FOREIGN KEY (Resort_resort_name)
REFERENCES Resort (resort_name)
);
CREATE TABLE Room (
room_num INT NOT NULL ,
room_type CHAR(10) NOT NULL ,
Hotel_hotel_name CHAR(20) NOT NULL ,
minibar BOOLEAN NOT NULL ,
tv BOOLEAN NOT NULL ,
smoking_permitted BOOLEAN NOT NULL ,
PRIMARY KEY (room_num) ,
CONSTRAINT fk_Room_Hotel1
FOREIGN KEY (Hotel_hotel_name)
REFERENCES Hotel (hotel_name)
);
CREATE TABLE Guest (
guest_id INT NOT NULL ,
firstname VARCHAR(20) NOT NULL ,
surname VARCHAR(20) NOT NULL ,
mobilephone CHAR(20) NOT NULL ,
PRIMARY KEY (guest_id)
);
CREATE TABLE Bookings (
Guest_guest_id INT NOT NULL ,
Hotel_hotel_name CHAR(20) NOT NULL ,
guest_count INT NOT NULL ,
PRIMARY KEY (Guest_guest_id, Hotel_hotel_name) ,
CONSTRAINT fk_Booking_Guest1
FOREIGN KEY (Guest_guest_id)
REFERENCES Guest (guest_id),
CONSTRAINT fk_Booking_Hotel1
FOREIGN KEY (Hotel_hotel_name)
REFERENCES Hotel (hotel_name)
);
CREATE TABLE HotelFacilities (
Hotel_hotel_name CHAR(20) NOT NULL ,
internet_access BOOLEAN NOT NULL ,
hotel_restaurant BOOLEAN NOT NULL ,
games_room BOOLEAN NOT NULL ,
bar BOOLEAN NOT NULL ,
evening_shows BOOLEAN NOT NULL ,
massage_parlour BOOLEAN NOT NULL ,
misc_details CHAR(100) NULL ,
PRIMARY KEY (Hotel_hotel_name) ,
CONSTRAINT fk_HotelFacilities_Hotel1
FOREIGN KEY (Hotel_hotel_name)
REFERENCES Hotel (hotel_name)
);
CREATE TABLE Attractions (
Region_region_name VARCHAR(45) NOT NULL ,
dirt_mountains BOOLEAN NOT NULL ,
beaches BOOLEAN NOT NULL ,
casinos BOOLEAN NOT NULL ,
safari BOOLEAN NOT NULL ,
snow_mountains BOOLEAN NOT NULL ,
misc_details CHAR(100) NULL ,
PRIMARY KEY (Region_region_name) ,
CONSTRAINT fk_Attractions_Region1
FOREIGN KEY (Region_region_name )
REFERENCES Region (region_name )
);
CREATE TABLE Rate (
Hotel_hotel_name CHAR(20) NOT NULL ,
Room_room_num INT NOT NULL ,
first_quarter DECIMAL(10,2) NOT NULL ,
second_quarter DECIMAL(10,2) NOT NULL ,
third_quarter DECIMAL(10,2) NOT NULL ,
fourth_quarter DECIMAL(10,2) NOT NULL ,
discount_information CHAR(100) NULL ,
PRIMARY KEY (Hotel_hotel_name, Room_room_num) ,
CONSTRAINT fk_Rate_Hotel1
FOREIGN KEY (Hotel_hotel_name )
REFERENCES Hotel (hotel_name ),
CONSTRAINT fk_Rate_Room1
FOREIGN KEY (Room_room_num)
REFERENCES Room (room_num)
);

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.