Cannot add foreign key constraints, Mysql - mysql

please could you please tell me what I am doing wrong?
I have MySQl script but finally I got this
Error 1215: Cannot add foreign key constraints
Here is code:
CREATE TABLE IF NOT EXISTS profile_public_data
(
idProfile INT(11) UNSIGNED NOT NULL AUTO_INCREMENT,
login VARCHAR(64),
name VARCHAR(45),
lastName VARCHAR(45),
location VARCHAR(45),
is_active BOOLEAN DEFAULT false,
age TINYINT,
photo VARCHAR(45),
PRIMARY KEY(idProfile)
);
CREATE TABLE IF NOT EXISTS profile_private_data
(
idProfile INT(11) UNSIGNED NOT NULL AUTO_INCREMENT,
CONSTRAINT fk_idProfile
FOREIGN KEY(idProfile)
REFERENCES `profile_public_data`(idProfile)
ON DELETE SET NULL
ON UPDATE SET NULL,
email VARCHAR(45),
password VARCHAR(45),
PRIMARY KEY(idProfile)
);

You idProfile column in the dependent table is set as auto increment, this is not correct. It should be just INT(11).
Its value must be specified and exists in the source table when when inserting a new record, instead of auto incremented.

Related

MYSQL Err #1072 doesn't exist in table when specifying FK

I am new to mysql, so sorry if this is a trivial problem. Problem is when I create the second table I get the error:
key iplogger_redirect_key doesn't exist.
Here's my code:
DROP DATABASE iploggerdb;
CREATE DATABASE iploggerdb;
USE iploggerdb;
CREATE TABLE iplogger_info_table(
iplogger_redirect_key CHAR(8) PRIMARY KEY,
access_key CHAR(8) NOT NULL,
creation_timestamp TIMESTAMP DEFAULT CURRENT_TIMESTAMP,
creator_ip VARCHAR(45),
original_url VARCHAR(2000)
);
CREATE TABLE logs_table(
id INT(11) AUTO_INCREMENT,
iplogger_redirect_key FOREIGN KEY (iplogger_redirect_key) REFERENCES iplogger_info_table(iplogger_redirect_key),
logged_ip VARCHAR(45),
logged_dns_server VARCHAR(45),
logged_ip_country_city VARCHAR(200),
logged_hostname VARCHAR(200),
logged_user_agent VARCHAR(150),
logged_referrer VARCHAR(2000),
logged_ip_timestamp TIMESTAMP DEFAULT CURRENT_TIMESTAMP
);
You need to correct FOREIGN KEY part:
CREATE TABLE logs_table(
id INT(11) AUTO_INCREMENT PRIMARY KEY,
iplogger_redirect_key CHAR(8),
CONSTRAINT fk_name FOREIGN KEY (iplogger_redirect_key)
REFERENCES iplogger_info_table(iplogger_redirect_key),
...
)
DBFiddle Demo

PHP6 chapter 3 - #1215 - Cannot add foreign key constraint

I try to learn from book Professional PHP6 and in chapter 3 I need to create tables:
CREATE TABLE `entity` (
`entityid` SERIAL PRIMARY KEY NOT NULL,
`name1` varchar(100) NOT NULL,
`name2` varchar(100) NOT NULL,
`type` char(1) NOT NULL
);
and
CREATE TABLE `entityaddress` (
`addressid` SERIAL PRIMARY KEY NOT NULL,
`entityid` int,
`saddress1` varchar(255),
`saddress2` varchar(255),
`scity` varchar(255),
`cstate` char(2),
`spostalcode` varchar(10),
`stype` varchar(50),
CONSTRAINT `fk_entityaddress_entityid`
FOREIGN KEY (`entityid`) REFERENCES `entity`(`entityid`)
);
result is error: #1215 - Cannot add foreign key constraint
I check in original code for that book and there is sql file, which give me same error.
...is there anything wrong, or is something with my db in xampp?
I try to create only tables and then create relation in designers, but I got program error...
I set InnoDB engine.
Thanks for any suggestion.
The type of entityid has to be the same in both tables. SERIAL is an alias for BIGINT UNSIGNED NOT NULL AUTO_INCREMENT UNIQUE. So change
`entityid` int,
to
`entityid` BIGINT UNSIGNED NOT NULL,

Alter Table Error:150

I cant alter the table to make add a foreign key. Error: 150 comes up. Can't figure out what is wrong. Please assist
CREATE TABLE Staff (staffNo varchar(10) NOT NULL, Fname varchar(50),
Lname varchar(50), Staff_Adress varchar(100), Salary numeric(65),
JobPosition varchar(15), Sex varchar(1), DateOfBirth date, NIN varchar(25),OffceNo varchar(10),
PRIMARY KEY (staffNo),
CHECK (Sex IN ('M', 'F')));
CREATE TABLE Office (OfficeNo INT(10) unsigned NOT NULL, Address varchar(50) NOT NULL, City varchar (25) NOT NULL,
PhoneNo varchar(10) NOT NULL, ManagerNo varchar(10),
PRIMARY KEY (OfficeNo),
FOREIGN KEY (ManagerNo) references Staff(staffNo));
alter table Staff
add foreign key (OffceNo) references Office(OfficeNo) on delete set NULL;
Fix these issues:
Tables have to be using InnoDB
Columns have to have exact same data types
your problem is here :
alter table Staff
add foreign key (OffceNo) references Office(OfficeNo) on delete set NULL;
you have to change this in Staff table
OffceNo varchar(10)
to
OffceNo INT(10) unsigned
Or this in Office table.
OfficeNo INT(10) unsigned NOT NULL
to
OfficeNo varchar(10) NOT NULL
for they will be same structure and same data types to use foreign keys.

SQL create table primary key and foreign key syntax

I'm creating a MySQL database for homework, and running into syntax error #1005 in phpmyadmin. I think it has something to do with the foreign keys but if w3schools has is right my syntax should be good.
Here's the SQL statements;
create table if not exists customers
(
id int not null auto_increment,
cust_gname varchar(20) not null,
cust_fname varchar(30) not null,
cust_street varchar(30) not null,
cust_suburb varchar(30) not null,
cust_state varchar(6) not null,
cust_postcode varchar(4) not null,
cust_email varchar(50) not null,
cust_phone varchar(12),
cust_mobile varchar(12),
cust_user_id int,
foreign key (cust_user_id) references users(id),
primary key (id)
);
create table if not exists ingredients
(
id int,
name varchar(30) not null,
primary key (id)
);
create table if not exists recipes
(
id int,
name varchar(30) not null,
recipes_menu_id int,
foreign key (recipes_menu_id) references menus(id)
image varchar(30),
primary key (id)
);
create table if not exists ingredients_recipes
(
id int,
ingredients_recipes_ingredient_id int,
foreign key (ingredients_recipes_ingredient_id) references ingredients(id),
ingredients_recipes_recipe_id int,
foreign key (ingredients_recipes_recipe_id) references recipes(id),
primary key (id)
);
create table if not exists menus
(
id int,
description varchar(30) not null,
menus_restaurant_id int,
foreign key (menus_restaurant_id) references restaurants(id),
primary key (id)
);
create table if not exists restaurants
(
id int,
name varchar(30) not null,
address1 varchar(30) not null,
address 2 varchar(30),
suburb varchar(30) not null,
state varchar(10) not null,
postcode varchar(4) not null,
primary key (id)
);
create table if not exists customers_ingredients
(
id int,
customers_ingredients_customer_id int,
foreign key (customers_ingredients_customer_id) references customers(id),
customers_ingredients_ingredient_id int,
foreign key (customers_ingredients_ingredient_id) references ingredients(id),
primary key (id)
);
create table if not exists users
(
id int,
username varchar(40) not null,
password varchar(50) not null,
group_id int,
created DATETIME,
modified DATETIME,
primary key (id)
);
create table if not exists groups
(
id int,
name varchar(10) not null,
created DATETIME,
modified DATETIME,
primary key (id)
);
If you're creating a table with a foreign key reference, the table to which it refers must already exist. You're creating a customers table at the start of the script which refers to the users table which isn't created until near the end. There are other examples in the script too.
You need either to create the tables in the right order, or use set foreign_key_checks = 0; at the top to disable this requirement. Make sure you set foreign_key_checks = 1 at the end once all your tables are created.
Note: there may be other syntax errors in your script - I haven't checked it all.

Auto increment column

I want to make an AUTO_INCREMENT column in a database table,here is the syntax i write:
create table comments
(
name varchar(20),
mail varchar(30),
comment varchar(100),
com_no int auto_increment
);
and i get the following error:
ERROR 1075 (42000): Incorrect table definition; there can be only one auto column and it must be defined as a key
then i made it as a primary key:
create table comments
(
name varchar(20),
mail varchar(30),
comment varchar(100),
com_no int primary_key auto_increment
);
and i get the following error:
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 'primary_key auto_increment,name varchar(20),mail varchar(30),comment varchar(100' at line 1
what is wrong???
It is PRIMARY KEY without the underscore.
create table comments
(
name varchar(20),
mail varchar(30),
comment varchar(100),
com_no int primary key auto_increment
);
or
create table comments
(
name varchar(20),
mail varchar(30),
comment varchar(100),
com_no int auto_increment,
primary key(`com_no`)
);
create table comments(
name varchar(20),
mail varchar(30),
comment varchar(100),
com_no int auto_increment,
PRIMARY KEY (com_no)
);
(as per on-line MySQL manual).
create table comments
(
name varchar(20),
mail varchar(30),
comment varchar(100),
com_no int NOT NULL AUTO_INCREMENT,
PRIMARY KEY (com_no)
);
ref.
Use primary key in place of primary_key
The proper syntax goes like this for example:
CREATE TABLE `admin` (
`id` INT(5) UNSIGNED NOT NULL AUTO_INCREMENT,
`userid` VARCHAR(50) NULL DEFAULT '0',
`pwd` VARCHAR(50) NULL DEFAULT NULL,
PRIMARY KEY (`id`)
)
In MySQL, there can be only one auto increment column (that is generally known as identity column) and it should be also defined as a unique key. For example:
create table comments
(
com_no int NOT NULL AUTO_INCREMENT,
name varchar(20),
mail varchar(30),
comment varchar(100),
PRIMARY KEY (com_no)
);
Please see MySQL auto increment documentation for more details.