This question already has answers here:
MySQL: Can't create table (errno: 150)
(35 answers)
Closed 1 year ago.
DROP TABLE IF EXISTS members;
CREATE TABLE members (
Member_ID char(10) NOT NULL default '',
Provider_ID char(10) NOT NULL default '',
First_Name varchar(30) NOT NULL default '',
Middle_Initial char(1) NOT NULL default '',
Last_Name varchar(40) NOT NULL default '',
Address varchar(100) NOT NULL default '',
City varchar(100) NOT NULL default '',
State char(2) NOT NULL default '',
Zip varchar(5) NOT NULL default '',
PRIMARY KEY (Member_ID),
FOREIGN KEY (PROVIDER_ID) REFERENCES PROVIDER(PROVIDER_ID)
) ;
DROP TABLE IF EXISTS provider;
CREATE TABLE provider (
Provider_ID char(10) NOT NULL default '',
Provider_Name varchar(50) NOT NULL default '',
Address varchar(100) NOT NULL default '',
City varchar(100) NOT NULL default '',
State char(2) NOT NULL default '',
Zip char(5) NOT NULL default '',
PRIMARY KEY (Provider_ID)
) ;
DROP TABLE IF EXISTS procedures;
CREATE TABLE procedures (
Procedure_ID char(10) NOT NULL default '',
Doctor_ID char(10) NOT NULL default '',
Member_ID char(10) NOT NULL default '',
Procedure_Type_ID char(10) NOT NULL default '',
Procedure_Name varchar(50) NOT NULL default '',
Cost int(10) NOT NULL default '0',
Date_Executed date NOT NULL default '2000-01-01',
PRIMARY KEY(Procedure_ID),
FOREIGN KEY(DOCTOR_ID) REFERENCES DOCTOR(DOCTOR_ID),
FOREIGN KEY(MEMBER_ID) REFERENCES MEMBER(MEMBER_ID),
FOREIGN KEY(PROCEDURE_TYPE_ID) REFERENCES PROCEDURE_TYPE(PROCEDURE_TYPE_ID)
) ;
DROP TABLE IF EXISTS procedure_type;
CREATE TABLE procedure_type (
Procedure_Type_ID char(10) NOT NULL default '',
Procedre_Type_Name varchar(50) NOT NULL default '',
Procedure_Description varchar(255) NOT NULL default '',
Procedure_ID char(10) NOT NULL default '',
PRIMARY KEY(Procedure_Type_ID),
FOREIGN KEY(PROCEDURE_ID) REFERENCES PROCEDURES(PROCEDURE_ID)
) ;
DROP TABLE IF EXISTS doctor_type;
CREATE TABLE doctor_type (
Type_ID char(10) NOT NULL default '',
Type_Name varchar(50) NOT NULL default '',
Type_Description varchar(255) NOT NULL default '',
PRIMARY KEY(Type_ID)
) ;
DROP TABLE IF EXISTS doctor;
CREATE TABLE doctor (
Doctor_ID char(10) NOT NULL default '',
Type_ID char(10) NOT NULL default '',
Doctor_Name varchar(50) NOT NULL default '',
Address varchar(100) NOT NULL default '',
City varchar(100) NOT NULL default '',
State char(2) NOT NULL default '',
Zip char(5) NOT NULL default '',
PRIMARY KEY(Doctor_ID),
FOREIGN KEY(TYPE_ID) REFERENCES DOCTOR_TYPE(TYPE_ID)
) ;
ERROR 1005 (HY000): Can't create table 'thunderhawk.members' (errno: 150)
Any ideas?
I know it is a foreign key problem. I seem to be following the foreign key syntax...
This question is a dupe of:
MySQL: Can't create table (errno: 150)
The error message tells you exactly what the problem is: table thunderhawk.members cannot be created because of error number (errno) 150, a foreign key constraint would be violated:
FOREIGN KEY (PROVIDER_ID) REFERENCES PROVIDER(PROVIDER_ID)
You cannot create the members table before creating the provider table, because the members table has a foreign key dependency on the provider table.
Usually, If the dbms is not able to create a table that means the following things could have gone wrong:
a) The dbms process doesnt has write permission to write in the data directory, or the disk quota for the folder has exceeded.
b) file system is full and there is no space to write any data to the file.
Check your mysqld.log for more details.
first thing you need to sure that check that both table need to have same storage type like InnoDB.
see here a reference : http://verysimple.com/2006/10/22/mysql-error-number-1005-cant-create-table-mydbsql-328_45frm-errno-150/
Try this code:
DROP TABLE IF EXISTS members;
CREATE TABLE members (
Member_ID char(10) NOT NULL default 0,
Provider_ID char(10) NOT NULL default 0,
First_Name varchar(30) NOT NULL default 0,
Middle_Initial char(1) NOT NULL default 0,
Last_Name varchar(40) NOT NULL default 0,
Address varchar(100) NOT NULL default 0,
City varchar(100) NOT NULL default 0,
State char(2) NOT NULL default 0,
Zip varchar(5) NOT NULL default 0,
PRIMARY KEY (Member_ID),
FOREIGN KEY (PROVIDER_ID) REFERENCES PROVIDER(PROVIDER_ID)
) ;
Related
I am trying to create a table (nightly routine) and then insert table data from another table. Right now I get that the table already exists. Not sure the syntax I should be using here.
[Err] 1050 - Table 'testgiver.safewp_users' already exists
DROP TABLE
IF EXISTS testgiver.safewp_users;
CREATE TABLE `testgiver.safewp_users` (
`ID` bigint(20) unsigned NOT NULL AUTO_INCREMENT,
`user_login` varchar(60) NOT NULL DEFAULT '',
`user_pass` varchar(64) DEFAULT '',
`user_nicename` varchar(50) NOT NULL DEFAULT '',
`user_email` varchar(100) NOT NULL DEFAULT '',
`user_url` varchar(100) DEFAULT '',
`user_registered` datetime NOT NULL DEFAULT '2014-01-01 10:00:00',
`user_activation_key` varchar(60) NOT NULL DEFAULT '',
`user_status` int(11) NOT NULL DEFAULT '0',
`display_name` varchar(250) NOT NULL DEFAULT '',
PRIMARY KEY (`ID`),
KEY `user_login_key` (`user_login`),
KEY `user_nicename` (`user_nicename`)
) ENGINE=InnoDB AUTO_INCREMENT=8 DEFAULT CHARSET=utf8;
INSERT INTO safewp_users (`user_login`, `user_nicename`, `user_email`, `display_name`)
SELECT
ldap_full.uid,
ldap_full.sn,
ldap_full.mail,
ldap_full.cn
FROM
ldap_full
This looks like a simple typo.
CREATE TABLE `testgiver.safewp_users`
should probably read
CREATE TABLE `testgiver`.`safewp_users`
Otherwise, you are attempting to create a table called 'testgiver.safewp_users' every single time.
Get rid of the quotes in your create table statement. The quotes are causing you to attempt to create a table named testgiver.safewp_users in the default schema, instead of a table named safewp_users in the testgiver schema.
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
when i run the following command in phpmyadmin.
CREATE TABLE subscribers (
'subscriber_id' int(11) NOT NULL auto_increment,
'customers_id' int(11) default NULL,
'email_address' varchar(96) NOT NULL default '',
'email_format' varchar(4) NOT NULL default 'TEXT',
PRIMARY KEY ('subscriber_id')
) TYPE=MyISAM;
it shows me #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 ''subscriber_id' int(11) NOT NULL auto_increment, 'customers_id' int(11) defaul' at line 2
INSERT INTO query_builder (query_id, query_category, query_name, query_description, query_string, query_keys_list) VALUES (6, 'email,newsletters', 'All Newsletter Subscribers', 'Returns name and email address of all Customer Account subscribers and all Newsletter-Only subscribers.', 'select c.customers_firstname, c.customers_lastname, s.email_address as customers_email_address from TABLE_SUBSCRIBERS as s left join TABLE_CUSTOMERS as c on c.customers_id = s.customers_id ', '')
it shows :
1062 - Duplicate entry '6' for key 1
i fell the command is right. i don't know how to correct it? thank you.
You don't need the single quote around the column name. Remove that.
This should be
CREATE TABLE subscribers (
subscriber_id int(11) NOT NULL auto_increment,
customers_id int(11) default NULL,
email_address varchar(96) NOT NULL default '',
email_format varchar(4) NOT NULL default 'TEXT',
PRIMARY KEY (subscriber_id)
) TYPE=MyISAM;
Although, You can enclose the column name in backticks if that is conflicting with the mysql reserved words.
You should not use strings as column names. Also it is ENGINE not TYPE.
It should look like:
CREATE TABLE subscribers (
subscriber_id int(11) NOT NULL auto_increment,
customers_id int(11) default NULL,
email_address varchar(96) NOT NULL default '',
email_format varchar(4) NOT NULL default 'TEXT',
PRIMARY KEY (subscriber_id)
) ENGINE=MyISAM;
Just removing the quotes around the column name, and changing the last bit to Engine=MyISAM worked for me:
CREATE TABLE subscribers (
subscriber_id int(11) NOT NULL auto_increment,
customers_id int(11) default NULL,
email_address varchar(96) NOT NULL default '',
email_format varchar(4) NOT NULL default 'TEXT',
PRIMARY KEY (subscriber_id)
) Engine=MyISAM;
Thanks for all the help everyone provides.
I am trying to run my DDL statements and I am getting a weird error. If I copy and paste the ddl it works, but when I try to run it from a *.sql file, it gives an error. Here it is:
mysql> source createtable.sql
ERROR 1064 (42000): 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 'CREAT
E TABLE stars
(
id int NOT NULL AUTO_INCREMENT,
first_name varchar(50) NOT ' at line 12
I don't know if this makes a difference or not, but it is the second table def in my ddl script.
Thanks!
EDIT
Here is the starting of the DDL. It gives me an error on the second table.
CREATE TABLE movies
(
id int NOT NULL AUTO_INCREMENT,
title varchar(100) NOT NULL default '',
year int NOT NULL,
director varchar(100) NOT NULL default '',
banner_url varchar(200) default '',
trailer_url varchar(200) default '',
PRIMARY KEY (id)
)
CREATE TABLE stars
(
id int NOT NULL AUTO_INCREMENT,
first_name varchar(50) NOT NULL default '',
last_name varchar(50) NOT NULL default '',
dob date,
photo_url varchar(200) default '',
PRIMARY KEY (id)
)
You'll need to separate your statements with semicolons:
CREATE TABLE movies
(
id int NOT NULL AUTO_INCREMENT,
title varchar(100) NOT NULL default '',
year int NOT NULL,
director varchar(100) NOT NULL default '',
banner_url varchar(200) default '',
trailer_url varchar(200) default '',
PRIMARY KEY (id)
);
CREATE TABLE stars
(
id int NOT NULL AUTO_INCREMENT,
first_name varchar(50) NOT NULL default '',
last_name varchar(50) NOT NULL default '',
dob date,
photo_url varchar(200) default '',
PRIMARY KEY (id)
);
I have two tables users and lead_contacts which have similar data. When somebody buys a product they become a user. How should I modify the two create staements below so:
that the leads table receives a new
entry with first_name, last_name, company and email, when a user is created.
the first_name, last_name, company and email in users table is updated
automatically when the leads table info is changed
CREATE TABLE lead_contacts (
contact_id int(11) NOT NULL auto_increment,
user_id int(11) unsigned NOT NULL default '0',
email varchar(100) NOT NULL default '',
company varchar(50) NOT NULL default '',
first_name varchar(50) NOT NULL default '',
last_name varchar(50) NOT NULL default '',
address varchar(100) NOT NULL default '',
address_2 varchar(100) NOT NULL default '',
city varchar(50) NOT NULL default '',
state varchar(50) NOT NULL default '',
country varchar(50) NOT NULL default '',
postal_code varchar(30) NOT NULL default '',
phone varchar(30) NOT NULL default '',
fax varchar(30) NOT NULL default '',
ship_bill_same enum('Y', 'N') NOT NULL default 'Y',
notes text NOT NULL,
admin_notes text NOT NULL,
list_name varchar(50) NOT NULL default '',
lead_list int(11) unsigned NOT NULL default '0',
is_master_list enum('N', 'Y') NOT NULL default 'N',
active_work enum('Y', 'N') NOT NULL default 'Y',
parentID int(11) unsigned NOT NULL default '0',
PRIMARY KEY (contact_id),
KEY user_id (user_id),
KEY lead_list (lead_list),
KEY is_master_list (is_master_list),
KEY active_work (active_work),
KEY parentID (parentID)
) ENGINE=InnoDB DEFAULT CHARSET=latin1 PACK_KEYS=1;
CREATE TABLE users (
userID int(11) NOT NULL auto_increment,
access_level int(11) NOT NULL default '0',
username varchar(100) NOT NULL default '',
password varchar(100) NOT NULL default '',
first_name varchar(50) NOT NULL default '',
last_name varchar(50) NOT NULL default '',
company varchar(100) NOT NULL default '',
email varchar(100) NOT NULL default '',
PRIMARY KEY (userID),
UNIQUE KEY username (username)
) ENGINE=InnoDB DEFAULT CHARSET=latin1 ;
I think you misunderstand how foreign keys work.
A reference from leads to users means that a row must already exist in users before a row in leads can reference it.
There's no way in SQL to make a dependent table automatically create a row in its parent table on demand.
You could do this with a trigger, I suppose. But not a foreign key constraints. Besides, the values to fill into the parent table must come from somewhere. Either you need to specify them in an INSERT statement in your application or in a trigger, or else use the defaults defined for each column in the users table. Given that you have a unique constraint on users.username, I don't think this would be possible from a trigger.
Re: your followup question in the comment:
No, a foreign key can't do what you're describing. When you modify info in the leads table (the table with the foreign key), the only thing a foreign key can do is prevent the modification if you try to change the leads.user_id column to a value that is not found in the users table.
The foreign key in the child (leads) table can't change anything in the parent (users) table.
I'm not sure what is the source of your mistaken understanding. Did you read it somewhere or see someone do something like this?