So im trying to create this table but im getting a #1064 eroor message. Here is my code: CREATE TABLE JagBank.AccountType ( typeID INT(4) NOT NULL , type VARCHAR(15) NULL , interestRate DOUBLE(4) NULL , PRIMARY KEY (typeID)) ENGINE = InnoDB;
I'm new to php but I was pretty sure this was correct? Whats the problem?
for table jagbank.accounttype
CREATE TABLE IF NOT EXISTS `jagbank.accounttype` (
`typeID` int(11) NOT NULL,
`type` varchar(15) COLLATE latin1_general_ci DEFAULT NULL,
`interestRate` double DEFAULT NULL,
PRIMARY KEY (`typeID`)
) ENGINE=InnoDB
just remove size for double data type,
double data type not having size try query below,
CREATE TABLE JagBank.AccountType ( typeID INT(4) NOT NULL , type VARCHAR(15) NULL , interestRate DOUBLE NULL , PRIMARY KEY (typeID)) ENGINE = InnoDB;
Related
Good day. I am trying to add a suppliers table on my database and when i try to save, it throws this error.
1064 - You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near '0) NOT NULL , PRIMARY KEY (supp_id), UNIQUE supp_name (supp_name))' at line 1
I have attached my SQL statement.
CREATE TABLE `project_inv`.`suppliers` (
`supp_id` INT(11) NOT NULL AUTO_INCREMENT ,
`supp_name` VARCHAR(255) NOT NULL ,
`supp_addr` VARCHAR(300) NOT NULL ,
`supp_phone` INT(20) NOT NULL ,
`supp_email` VARCHAR(255) NOT NULL ,
`supp_notes` VARCHAR(1000) NOT NULL ,
`status` ENUM(0) NOT NULL ,
PRIMARY KEY (`supp_id`),
UNIQUE `supp_name` (`supp_name`)
) ENGINE = InnoDB;
I think its something with my UNIQUE key but i really can't figure out what the error is. Any form of help will be appreciated. Thanks
It is not about the PRIMARY or UNIQUE declaration, but about ENUM(): it expects string literals, so you would need to surround the values in the list with single quotes:
CREATE TABLE `project_inv`.`suppliers` (
`supp_id` INT(11) NOT NULL AUTO_INCREMENT ,
`supp_name` VARCHAR(255) NOT NULL ,
`supp_addr` VARCHAR(300) NOT NULL ,
`supp_phone` INT(20) NOT NULL ,
`supp_email` VARCHAR(255) NOT NULL ,
`supp_notes` VARCHAR(1000) NOT NULL ,
`status` ENUM('0') NOT NULL ,
PRIMARY KEY (`supp_id`),
UNIQUE `supp_name` (`supp_name`)
) ENGINE = InnoDB;
However having a non-nullable ENUM() column with just one value allowed makes little sense - basically you are forcing every row to have the same value ('0'). So either add more values to the list... or just remove the column.
try like below by using string inside enum i used 'N' instated 0
CREATE TABLE `project_inv`.`suppliers` (
`supp_id` INT(11) NOT NULL AUTO_INCREMENT ,
`supp_name` VARCHAR(255) NOT NULL ,
`supp_addr` VARCHAR(300) NOT NULL ,
`supp_phone` INT(20) NOT NULL ,
`supp_email` VARCHAR(255) NOT NULL ,
`supp_notes` VARCHAR(1000) NOT NULL ,
`status` ENUM('N') NOT NULL ,
PRIMARY KEY (`supp_id`),
UNIQUE `supp_name` (`supp_name`)
) ENGINE = InnoDB;
I am trying to create a new table using the mysql GUI in phpmyadmin. This is the error I am getting #1072 - Key column 'id ' doesn't exist in table
CREATE TABLE `loginsys`.`groups`(
`id` INT NOT NULL AUTO_INCREMENT,
`name` VARCHAR(20) NOT NULL,
`permission` TEXT NOT NULL,
PRIMARY KEY(`id `)
) ENGINE = InnoDB
Please correct the extra space in when you're defining PRIMARY KEY(id )
This should be your query :-
CREATE TABLE `loginsys`.`groups`(
`id` INT NOT NULL AUTO_INCREMENT,
`name` VARCHAR(20) NOT NULL,
`permission` TEXT NOT NULL,
PRIMARY KEY(`id`)
) ENGINE = InnoDB
simply run the query without "`"
CREATE TABLE loginsys.groups(
id INT NOT NULL AUTO_INCREMENT,
name VARCHAR(20) NOT NULL,
permission TEXT NOT NULL,
PRIMARY KEY(id))
I am new to MySQL and am uploading a database for the first time. I am not uploading the data, just the columns for now. The issue is that I get an error for every line of code provided:
#1064 - You have an error in your SQL syntax;
I can understand if I was getting the message for just one or two lines of the code, but I am beginning to think I misformatted all the database file since I'm getting that error message for every line.
I previously changed the primary/foreign keys
CONSTRAINT `PurchasePK` PRIMARY KEY (`P_ORDERNO`)
CONSTRAINT `PurchaseFK` FOREIGN KEY (`SUPPLY_CODE`)
to: P_ORDERNO int(5) NOT NULL auto_increment PRIMARY KEY,
And still get errors.
I would appreciate if someone took a look at the contents of the .sql file below and let me know if there is something I am missing that is giving me consistent errors.
-- Table structure for table `Purchase`
CREATE TABLE IF NOT EXISTS `Purchase` (
`P_ORDERNO` int(5) NOT NULL auto_increment PRIMARY KEY,
`SUPPLY_CODE` int(5) NOT NULL auto_increment FOREIGN KEY,
`P_ORDER_DATE` timestamp NOT NULL,
`P_ORDER_AMT` int(5) NOT NULL,
`SUPPLY_DESC` varchar(50) NULL,
`SUPPLY QTY` int(5) NOT NULL,
);
ENGINE = InnoDB;
-- Table structure for table `Vendor`
CREATE TABLE IF NOT EXISTS `Vendor` (
`VENDORNO` int(5) NOT NULL auto_increment PRIMARY KEY,
`VENDOR_NAME` varchar(50) NULL,
`VENDOR_STREET` varchar(50) NULL,
`VENDOR_CITY` varchar(50) NULL,
`VENDOR_STATE` varchar(2) NULL,
`VENDOR_ZIP` varchar(3) NULL,
`VENDOR_AREA_CODE` varchar(5) NULL,
`VENDOR_PHONE` varchar(10) NULL,
);
ENGINE = InnoDB;
-- Table structure for table `Supply`
CREATE TABLE IF NOT EXISTS `Supply` (
`SUPPLY_CODE` int(5) NOT NULL auto_increment PRIMARY KEY,
`SUPPLY_DESC` varchar(50) NULL,
`SUPPLY QTY` int(5) NOT NULL,
);
ENGINE = InnoDB;
You have trailing commas
`SUPPLY QTY` int(5) NOT NULL,
^---here
);
in each of the table definitions. That makes the DB server expect another field definition, but instead it runs into );
I have two tables, 'po' and 'receive'
CREATE TABLE `po` (
`PO_ID` bigint(20) NOT NULL,
`SERVICE_TYPE` bit(1) DEFAULT NULL,
`ENTRY_DATE` date NOT NULL,
`RECEIPT_DATE` date DEFAULT NULL,
`TURNOVER` date DEFAULT NULL,
`MOBILIZATION` date DEFAULT NULL,
`SITE_NAME` varchar(255) NOT NULL,
`SITE_CODE` varchar(45) DEFAULT NULL,
`SITE_TIN` varchar(45) DEFAULT NULL,
`SITE_ADDRESS` varchar(255) NOT NULL,
`COST` decimal(11,2) NOT NULL,
`XML` text,
PRIMARY KEY (`PO_ID`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8$$
CREATE TABLE `receive` (
`RECEIPT_ID` varchar(100) NOT NULL,
`RECEIPT_DATE` date NOT NULL,
`PO_NUMBER` bigint(20) NOT NULL,
`SUPPLIER_ID` int(11) NOT NULL,
PRIMARY KEY (`RECEIPT_ID`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8$$
I'm trying to connect two tables by defining a foreign key 'fk_po' on the table 'receive'
ALTER TABLE `fourthwave`.`receive`
ADD CONSTRAINT `fk_po`
FOREIGN KEY (`PO_NUMBER` )
REFERENCES `fourthwave`.`po` (`PO_ID` )
ON DELETE SET NULL
ON UPDATE SET NULL
, ADD INDEX `fk_po` (`PO_NUMBER` ASC)
However, the alter query above throws an error :
Error Code: 1005. Can't create table 'fourthwave.#sql-aec_11' (errno:150)
Am i getting this error because the field's names, 'PO_ID' and 'PO_NUMBER' on both tables are different?
Execute SHOW ENGINE INNODB STATUS statement after ALTER TABLE, and you will see the error message - 'You have defined a SET NULL condition though some of the
columns are defined as NOT NULL'.
ALTER TABLE `receive`
ADD CONSTRAINT `fk_po`
FOREIGN KEY (`PO_NUMBER` )
REFERENCES `po` (`PO_ID` )
ON DELETE SET NULL
ON UPDATE SET NULL
, ADD INDEX `fk_po` (`PO_NUMBER` ASC);
SHOW ENGINE INNODB STATUS;
You need an index on PO_NUMBER in the receive table. The field you are referencing in a foreign key always should be indexed.
I've created a model with MySQL Workbench and am now attempting to install it to a mysql server.
Using File > Export > Forward Engineer SQL CREATE Script... it outputs a nice big file for me, with all the settings I ask for. I switch over to MySQL GUI Tools (the Query Browser specifically) and load up this script (note that I'm going form one official MySQL tool to another). However, when I try to actually execute this file, I get the same error over and over
SQLSTATE[HY000]: General error: 1005
Can't create table
'./srs_dev/location.frm' (errno: 150)
"OK", I say to myself, something is wrong with the location table. So I check out the definition in the output file.
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';
-- -----------------------------------------------------
-- Table `state`
-- -----------------------------------------------------
DROP TABLE IF EXISTS `state` ;
CREATE TABLE IF NOT EXISTS `state` (
`state_id` INT NOT NULL AUTO_INCREMENT ,
`iso_3166_2_code` VARCHAR(2) NOT NULL ,
`name` VARCHAR(60) NOT NULL ,
PRIMARY KEY (`state_id`) )
ENGINE = InnoDB;
-- -----------------------------------------------------
-- Table `brand`
-- -----------------------------------------------------
DROP TABLE IF EXISTS `brand` ;
CREATE TABLE IF NOT EXISTS `brand` (
`brand_id` INT UNSIGNED NOT NULL AUTO_INCREMENT ,
`name` VARCHAR(45) NOT NULL ,
`domain` VARCHAR(45) NOT NULL ,
`manager_name` VARCHAR(100) NULL ,
`manager_email` VARCHAR(255) NULL ,
PRIMARY KEY (`brand_id`) )
ENGINE = InnoDB;
-- -----------------------------------------------------
-- Table `location`
-- -----------------------------------------------------
DROP TABLE IF EXISTS `location` ;
CREATE TABLE IF NOT EXISTS `location` (
`location_id` INT UNSIGNED NOT NULL AUTO_INCREMENT ,
`name` VARCHAR(255) NOT NULL ,
`address_line_1` VARCHAR(255) NULL ,
`address_line_2` VARCHAR(255) NULL ,
`city` VARCHAR(100) NULL ,
`state_id` TINYINT UNSIGNED NULL DEFAULT NULL ,
`postal_code` VARCHAR(10) NULL ,
`phone_number` VARCHAR(20) NULL ,
`fax_number` VARCHAR(20) NULL ,
`lat` DECIMAL(9,6) NOT NULL ,
`lng` DECIMAL(9,6) NOT NULL ,
`contact_url` VARCHAR(255) NULL ,
`brand_id` TINYINT UNSIGNED NOT NULL ,
`summer_hours` VARCHAR(255) NULL ,
`winter_hours` VARCHAR(255) NULL ,
`after_hours_emergency` VARCHAR(255) NULL ,
`image_file_name` VARCHAR(100) NULL ,
`manager_name` VARCHAR(100) NULL ,
`manager_email` VARCHAR(255) NULL ,
`created_date` TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP ,
PRIMARY KEY (`location_id`) ,
CONSTRAINT `fk_location_state`
FOREIGN KEY (`state_id` )
REFERENCES `state` (`state_id` )
ON DELETE NO ACTION
ON UPDATE NO ACTION,
CONSTRAINT `fk_location_brand`
FOREIGN KEY (`brand_id` )
REFERENCES `brand` (`brand_id` )
ON DELETE NO ACTION
ON UPDATE NO ACTION)
ENGINE = InnoDB;
CREATE INDEX `fk_location_state` ON `location` (`state_id` ASC) ;
CREATE INDEX `fk_location_brand` ON `location` (`brand_id` ASC) ;
CREATE INDEX `idx_lat` ON `location` (`lat` ASC) ;
CREATE INDEX `idx_lng` ON `location` (`lng` ASC) ;
Looks ok to me. I surmise that maybe something is wrong with the Query Browser, so I put this file on the server and try to load it this way
] mysql -u admin -p -D dbname < path/to/create_file.sql
And I get the same error. So I start to Google this issue and find all kinds of accounts that talk about an error with InnoDB style tables that fail with foreign keys, and the fix is to add "SET FOREIGN_KEY_CHECKS=0;" to the SQL script. Well, as you can see, that's already part of the file that MySQL Workbench spat out.
So, my question is then, why is this not working when I'm doing what I think I'm supposed to be doing?
Version Info:
MySQL: 5.0.45
GUI Tools: 1.2.17
Workbench: 5.0.30
The type of the field in a foreign key must be the same as the type of the column they're referencing. You have the following (snipping):
CREATE TABLE IF NOT EXISTS `state` (
`state_id` INT NOT NULL AUTO_INCREMENT ,
...
CREATE TABLE IF NOT EXISTS `brand` (
`brand_id` INT UNSIGNED NOT NULL AUTO_INCREMENT ,
...
CREATE TABLE IF NOT EXISTS `location` (
...
`state_id` TINYINT UNSIGNED NULL DEFAULT NULL ,
...
`brand_id` TINYINT UNSIGNED NOT NULL ,
so you're trying to refer to INT fields (in tables state and brand) with TINYINT fields in table location. I think that's the error it's complaining about. Not sure how it came up in the first place, or why zeroing out FOREIGN_KEY_CHECKS doesn't stop MySQL from diagnosing the error, but what happens if you fix this type mismatch?
For others looking at this thread, there are a LOT of reasons that you can get this error:
See the following link for a complete list for errno 150, errno 121 and other MySQL Foreign Key Errors:
MySQL Foreign Key Errors and Errno 150
just checked #juacala 's answer. But didn't help me. Here's the message I sent #ELIACOM when I found my source of trouble.:
I was reading this great source of solutions, but my issue is not
adressed. I was trying to add an FK in a INNODB table pointing to a
PK in a MyISAM. After setting the MyIsam to INNODB it worked. I
checked your whole list before realizing that. Cheers.
I had same issue. Somehow I didn't notice to set auto_increment value to the table id.
Maybe it helps someone.