MYSQL error in creating foreign key constraint - mysql

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

Related

MySQL error #1089 while creating database

I am creating a database it keeps on giving me error:
CREATE TABLE `pgdavplacments`.`drive` ( `drive_id` INT(5) NOT NULL AUTO_INCREMENT ,
`drive_name` VARCHAR(200) NOT NULL ,
`drive_date` DATE NOT NULL ,
`drive_time` TIME NOT NULL ,
`drive_info` VARCHAR(8000) NOT NULL ,
`eligibility1` INT(5) NOT NULL ,
`eligibility2` INT(5) NOT NULL ,
`eligibility3` INT(5) NOT NULL ,
PRIMARY KEY (`drive_id`(1000)))
ENGINE = InnoDB;
What is the problem here?
CREATE TABLE `pgdavplacments`.`drive` (
`drive_id` INT(5) NOT NULL AUTO_INCREMENT,
`drive_name` VARCHAR(200) NOT NULL,
`drive_date` DATE NOT NULL,
`drive_time` TIME NOT NULL,
`drive_info` VARCHAR(8000) NOT NULL,
`eligibility1` INT(5) NOT NULL,
`eligibility2` INT(5) NOT NULL,
`eligibility3` INT(5) NOT NULL,
PRIMARY KEY (`drive_id` )
) ENGINE=INNODB;
You can try above query.

What does the engine and char set means after the ()?

DROP DATABASE IF EXISTS `Database` ;
CREATE DATABASE `Database` ;
DROP TABLE IF EXISTS `Registration Form` ;
CREATE TABLE `Registration Form`
(
`ID` int(11) NOT NULL AUTO_INCREMENT ,
`Username` varchar(50) NOT NULL ,
`Password` varchar(50) NOT NULL ,
`Display Name` varchar(255) NOT NULL ,
`Question` varchar(255) NOT NULL ,
`Answer` varchar(255) NOT NULL ,
`Title` varchar(10) NOT NULL ,
`Name` varchar(255) NOT NULL ,
`Date Of Birth` date NOT NULL ,
`Gender` varchar(10) NOT NULL ,
`Address` varchar(255) NOT NULL ,
`Postal Code` int(11) NOT NULL ,
`City / Town` varchar(255) NOT NULL ,
`Federal Territory / State` varchar(255) NOT NULL ,
`Citizenship` varchar(255) NOT NULL ,
`E-mail` varchar(255) NOT NULL ,
`Contact Number` int(20) NOT NULL ,
`Registration Date` datetime NOT NULL ,
PRIMARY KEY ( `ID` )
) ENGINE=MyISAM DEFAULT CHARSET=latin1 ;
I'm just starting to work arounnd mysql and php ... so bear with me as im a newbie ...
What does ALL the code after the CREATE() mean ? Why is there a ENGINE and DEFAULT what do they do ? Is CHARSET same as collation , if yes why not put it inside the row instead ? Do you need to give a length/value for date and daetime ?
CREATE TABLE is the sql clause which is required to creating a table.
After that you have to give a table name i.e "Registration Form" .but avoid spaces in between table name
Next what ever you gave those are column name along with their datatype and size.NOT NULL means while inserting data those column can't be blank.
For more details check the below link
http://www.tutorialspoint.com/mysql/mysql-create-tables.htm

SQL Error SQL DB function failed with error number 1064

I have prblem with sql when i try to check if table extist.
Am making component for joomla and when make install.sql i have sql syntax error.
check this and tell me what i do wrong:
DROP TABLE IF EXIST `#__ik_property_item`;
DROP TABLE IF EXIST `#__ik_property_categories`;
DROP TABLE IF EXIST `#__ik_property_type`;
DROP TABLE IF EXIST `#__ik_property_price_type`;
DROP TABLE IF EXIST `#__ik_property_agnts`;
DROP TABLE IF EXIST `#__ik_property_companies`;
DROP TABLE IF EXIST `#__ik_property_agnts`;
CREATE TABLE `#__ik_property_item` (
`id` INT NOT NULL AUTO_INCREMENT ,
`category_id` INT NULL ,
`property_type_id` INT NULL ,
`price_type_id` INT NULL ,
`company_id` INT NOT NULL ,
`agent_id` INT NOT NULL ,
`title` VARCHAR(45) NULL ,
`street` VARCHAR(45) NULL ,
`status` INT NULL ,
`featured` INT NULL ,
`yt_video` TEXT NULL ,
`map_lattitude` INT NULL ,
`map_longitude` VARCHAR(45) NULL ,
`beds` INT NULL ,
`baths` VARCHAR(45) NULL ,
`garage` VARCHAR(45) NULL ,
`flat_size` VARCHAR(45) NULL ,
`kitchens` VARCHAR(45) NULL ,
`year_build` VARCHAR(45) NULL ,
`floor` VARCHAR(45) NULL ,
`price` INT NULL ,
`image_1` VARCHAR(45) NULL ,
`image_2` VARCHAR(45) NULL ,
`image_3` VARCHAR(45) NULL ,
`image_4` VARCHAR(45) NULL ,
`image_5` VARCHAR(45) NULL ,
`meta_title` VARCHAR(45) NULL ,
`meta_description` TEXT NULL ,
`intro_text` TEXT NULL ,
`full_text` TEXT NULL ,
PRIMARY KEY (`id`) );
CREATE TABLE `#__ik_property_categories` (
`id` INT NOT NULL AUTO_INCREMENT ,
`name` VARCHAR(45) NULL ,
PRIMARY KEY (`id`) );
CREATE TABLE `#__ik_property_type` (
`id` INT NOT NULL AUTO_INCREMENT ,
`title` VARCHAR(45) NULL ,
PRIMARY KEY (`id`) );
CREATE TABLE `#__ik_property_price_type` (
`id` INT NOT NULL AUTO_INCREMENT ,
`title` VARCHAR(45) NULL ,
PRIMARY KEY (`id`) );
CREATE TABLE `#__ik_property_agnts` (
`id` INT NOT NULL AUTO_INCREMENT ,
`name` VARCHAR(45) NULL ,
`last_name` VARCHAR(45) NULL ,
`first_name` VARCHAR(45) NULL ,
`email` VARCHAR(45) NULL ,
`website` VARCHAR(45) NULL ,
`phone` VARCHAR(45) NULL ,
`company` VARCHAR(45) NULL ,
PRIMARY KEY (`id`) );
CREATE TABLE `#__ik_property_companies` (
`id` INT NOT NULL AUTO_INCREMENT ,
`name` VARCHAR(45) NULL ,
`logo` VARCHAR(45) NULL ,
`email` VARCHAR(45) NULL ,
`phone` VARCHAR(45) NULL ,
`website` VARCHAR(45) NULL ,
`street` VARCHAR(45) NULL ,
`fax` VARCHAR(45) NULL ,
`intro_text` VARCHAR(45) NULL ,
`full_text` VARCHAR(45) NULL ,
`image_1` VARCHAR(45) NULL ,
`image_2` VARCHAR(45) NULL ,
`image_3` VARCHAR(45) NULL ,
PRIMARY KEY (`id`) );
When i remove
DROP TABLE IF EXIST `#__ik_property_item`;
DROP TABLE IF EXIST `#__ik_property_categories`;
DROP TABLE IF EXIST `#__ik_property_type`;
DROP TABLE IF EXIST `#__ik_property_price_type`;
DROP TABLE IF EXIST `#__ik_property_agnts`;
DROP TABLE IF EXIST `#__ik_property_companies`;
DROP TABLE IF EXIST `#__ik_property_agnts`;
All is success executed and when i call uninstall.sql code inside is like above all work good. But i need to check on installation if that tables exist if exist i must delete and save new.
Thanks

mysql foreign keys

Hello I have two mysql tables one that holds username and password second that holds user information. Here are the tables:
CREATE TABLE IF NOT EXISTS `test`.`dbo.users` (
`userId` INT(11) NOT NULL AUTO_INCREMENT ,
`userUsername` VARCHAR(45) NULL ,
`userPassword` VARCHAR(45) NULL ,
`dbo.userProfiles_userProfileId` INT(11) NOT NULL ,
PRIMARY KEY (`userId`, `dbo.userProfiles_userProfileId`) ,
INDEX `fk_dbo.users_dbo.userProfiles` (`dbo.userProfiles_userProfileId` ASC) ,
CONSTRAINT `fk_dbo.users_dbo.userProfiles`
FOREIGN KEY (`dbo.userProfiles_userProfileId` )
REFERENCES `test`.`dbo.userProfiles` (`userId` )
ON DELETE CASCADE
ON UPDATE CASCADE)
ENGINE = InnoDB;
CREATE TABLE IF NOT EXISTS `test`.`dbo.userProfiles` (
`userProfileId` INT(11) NOT NULL AUTO_INCREMENT ,
`userId` INT(11) NOT NULL ,
`userFirstName` VARCHAR(45) NULL ,
`userMidleName` VARCHAR(45) NULL ,
`userLastName` VARCHAR(45) NULL ,
`userBirthDate` VARCHAR(45) NULL ,
`userCountry` VARCHAR(45) NULL ,
`userState` VARCHAR(45) NULL ,
`userCity` VARCHAR(45) NULL ,
`userZipCode` VARCHAR(45) NULL ,
`userPrimaryAddress` VARCHAR(45) NULL ,
`userSecondaryAddress` VARCHAR(45) NULL ,
`userThirdAddress` VARCHAR(45) NULL ,
`userFirstPhone` VARCHAR(45) NULL ,
`userSecondaryPhone` VARCHAR(45) NULL ,
`userThirdPhone` VARCHAR(45) NULL ,
`userFirstEmail` VARCHAR(45) NULL ,
`userSecondaryEmail` VARCHAR(45) NULL ,
`userThirdEmail` VARCHAR(45) NULL ,
`userDescription` VARCHAR(45) NULL ,
PRIMARY KEY (`userProfileId`, `userId`) )
ENGINE = InnoDB;
My problem with this tables is that the fk key doesn't work and I'm not sure why I'm missing something here.. ( I haven't used foreign keys much).
Error:
Executing SQL script in server
ERROR: Error 1005: Can't create table 'test.dbo.users' (errno: 150)
Can you please help fixing this so I can use fk on this tables and others? and if you got some time to spare to explain me how fk keys work exactly (using mysql workbench to create them on a diagram then forward engineer). thank you for your help
You have to create the root tables first (userProfiles). You can temporarily bypass this restriction by disabling foreign key checks with set foreign_key_checks=0. This must be done if you're creating multiple tables with circular references.

Create table - 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.