When I try to set a Foreign key, it throws the Error number 150.
Schema Creation Failed: Can't create table 'db_2_f856e.urlnames'
This is the code:
create table images(
id int auto_increment primary key
,gender varchar(6)
,pattern varchar(50)
,item_name varchar(25)
,url_id int(250)
)//
create table urlnames(
url_id_no int(250)
,url varchar(250)
,foreign key (url_id_no) references images(url_id)
)//
Can someone explain why it is not working?
Thanks
Your data structure doesn't make sense. I think you want:
create table urlnames(
url_id_no int auto_increment primary key,
url varchar(250)
);
create table images(
image_id int auto_increment primary key,
gender varchar(6),
pattern varchar(50),
item_name varchar(25),
url_id int(250) references urlnames(url_id_no)
);
Any column referenced by a foreign key reference needs to be a primary key or unique key. And, urlnames should have its id column declared as a primary key.
Here is a SQL Fiddle example.
Related
CREATE TABLE employee(
empid int auto_increment primary key,
empfirstname varchar(200) not null,
emplastname varchar(200) not null,
email varchar(200) not null,
officenumber int not null
);
CREATE TABLE customer(
custid int auto_increment primary key,
firstname varchar(200) not null,
lastname varchar(200) not null,
address varchar(200) not null,
contact varchar(200)
);
CREATE TABLE product(
productid int auto_increment primary key,
productdesc varchar(500) not null,
weight int not null,
unit_cost int not null
);
CREATE TABLE productorder(
productid int,
orderid int,
primary key(productid,orderid),
constraint fk3 foreign key (productid) references product(productid),
constraint fk4 foreign key (orderid) references productorder(orderid)
);
CREATE TABLE salesorder(
salesorderid int auto_increment primary key,
empid int not null,
custid int not null,
orderdate date not null,
shippingmethod varchar (200) not null,
constraint a_fk1 foreign key (empid) references employee(empid),
constraint a_fk2 foreign key (custid) references customer(custid)
);
What is this meant to do?:
constraint fk4 foreign key (orderid) references productorder(orderid)
It's not uncommon for a table to have a foreign key back to its own primary key, such as for records which have a parent/child relationship. But that doesn't seem to be the case here.
More to the point of the error though, this isn't referencing the entire primary key for the target table. That key has two fields:
primary key(productid,orderid)
So the DBMS can't create the foreign key because its structure doesn't match the target primary key.
If you want to create that foreign key, it would need to match. Probably something like this:
constraint fk4 foreign key (productid,orderid) references productorder(productid,orderid)
But it doesn't appear that you need that foreign key at all, because it doesn't seem to make sense in your data model. Instead I suspect orderid might need to be autoincrement and just use the productid foreign key. Something like this:
CREATE TABLE productorder(
orderid int auto_increment primary key,
productid int,
constraint fk3 foreign key (productid) references product(productid)
);
(Note that there could be more changes you'd want to make to your data model. This answer doesn't purport to provide you with a complete production-ready data model, just to correct the error. Your data model is likely to change/evolve as you develop your system.)
Its normal,
The foreign key in table **productorder**
is refereing to the table itself:
constraint fk4 foreign key (orderid) references **productorder**(orderid)
In order to achieve the self-referencing constraint on the table productorder, you need to add another column with the same type and make typical referencing.
For instance :
Create table productorder (
productid int,
orderid int,
orderid_parent int,
primary key(productid,orderid),
constraint fk_self foreign key(orderid) references productorder(orderid_parent)
)
I`m a beginner in structured query language . I want to add multi columns with different foreign key.Like the example:
drop schema humman;
create schema humman;
CREATE TABLE humman.father (
id int not null auto_increment,
firstname varchar(200) not null,
primary key(id)
);
create table humman.mather(
id int not null auto_increment,
FirstName varchar(200),
primary key(id)
);
CREATE TABLE humman.child (
id int not null auto_increment,
firstname varchar(200) not null,
primary key(id)
);
use `humman`;
alter table humman.child
ADD `parentId` int ,
ADD `motherId` int,
ADD foreign key (`parentId`) references father(`id`),
ADD foreign key (`motherId`) references mother(`id`);
Error code: 1215 Cannot add foreign key CONSTRAINT
Your code is good except for a typo, you spelt "mother" as "mather" in your second table definition;
create table humman.mather(
id int not null auto_increment,
FirstName varchar(200),
primary key(id)
);
correct that and it should work.
i have this script running on sql, but i need to optimize it to mysql using the workbench, but it shows me an error in the foreign key and i don't know how to change it.
create table EspecialidadesMedicas(
IdEspecialidad int(4) primary key,
DescripcionEspecialidad varchar(30));
create table Doctores(
IdDoctor int(5) PRIMARY KEY,
NombreDoctor varchar(30),
Salario int(12.2),
Especialidad int(4),citascitas
Especialidad FOREIGN KEY references EspecialidadesMedicas(IdEspecialidad));
The following is working syntax:
create table EspecialidadesMedicas (
IdEspecialidad int(4) primary key,
DescripcionEspecialidad varchar(30)
);
create table Doctores(
IdDoctor int(5) PRIMARY KEY,
NombreDoctor varchar(30),
Salario int(12),
Especialidad int(4),
citascitas varchar(30),
constraint fk_Especialidad FOREIGN KEY (Especialidad) references EspecialidadesMedicas(IdEspecialidad)
);
This SQL is giving me Errno 150 when i'm trying to create the second table with the foreign key on UserID
Can anyone please advice me what am i doing wrong?
CREATE DATABASE IF NOT EXISTS OTA;
USE OTA;
CREATE TABLE IF NOT EXISTS Users
(
UserID int AUTO_INCREMENT NOT NULL PRIMARY KEY,
UserName varchar(255) NOT NULL,
Email varchar(255) UNIQUE ,
PW varchar(255),
PN varchar(255),
Admin BIT
);
CREATE TABLE IF NOT EXISTS Notes
(
UID int AUTO_INCREMENT NOT NULL PRIMARY KEY,
Note varchar (255) NOT NULL,
c_Date Date NOT NULL,
c_text varchar (255) NOT NULL,
FOREIGN KEY (UID) REFERENCES Persons(UserID)
);
Sorry for being a duplicate question but i can't find my answer between the related ones.
The problem is that you are trying to reference Persons(UserID) when your first table is called Users. Try this for your key:
FOREIGN KEY (UID) REFERENCES Users(UserID)
However, you should have a separate column for the note ID.
Try declaring the primary key after:
CREATE TABLE Orders
(O_Id int NOT NULL,
OrderNo int NOT NULL,
P_Id int,
PRIMARY KEY (O_Id),
FOREIGN KEY (P_Id) REFERENCES Persons(P_Id))
Also, you auto-incremented both UID and UserID. This is fine if they are the same length, but if you try to reference a foreign key value that doesn't exist, you will get an error.
i am trying to create a table that uses a foreign key from another table an i keep getting the error:"1215 cannot add foreign key constraint "
i checked and it says the following:
"Cannot find an index in the referenced table where the
referenced columns appear as the first columns, or column types
in the table and the referenced table do not match for constraint.
Note that the internal storage type of ENUM and SET changed in
tables created with >= InnoDB-4.1.12, and such columns in old tables
cannot be referenced by such columns in new tables.
See http://dev.mysql.com/doc/refman/5.6/en/innodb-foreign-key-constraints.html
for correct foreign key definition."
below is my sql code:
show engine innodb status;
CREATE TABLE trainee (
traineeid bigint unsigned NOT NULL,
fname VARCHAR(20),
lname VARCHAR(20),
id INT(9),age INT,
PRIMARY KEY (traineeid));
CREATE TABLE trainer(
trainerid bigint unsigned NOT NULL,
fname VARCHAR(20),lname VARCHAR(20),
id INT(9),
PRIMARY KEY (trainerid));
CREATE TABLE club(
clubid bigint unsigned NOT NULL,
clubname VARCHAR(20),
PRIMARY KEY(clubid));
CREATE TABLE team(
teamid bigint unsigned NOT NULL,
teamname VARCHAR(20),
clubid bigint unsigned,
INDEX(clubid),
FOREIGN KEY (clubid) REFERENCES club (clubid)
ON DELETE SET NULL
ON UPDATE CASCADE,
PRIMARY KEY(teamid));
CREATE TABLE competition(
competitionid bigint unsigned NOT NULL,
competitiondate DATE,
competitionname VARCHAR(40),
PRIMARY KEY(competitionid));
CREATE TABLE trainertoteam(
trainerid bigint unsigned not null,
teamid bigint unsigned not null,
PRIMARY KEY(teamid,trainerid),
INDEX(trainerid),
FOREIGN KEY (trainerid) REFERENCES trainer (trainerid)
ON DELETE set null
ON UPDATE CASCADE,
INDEX(teamid),
FOREIGN KEY (teamid) REFERENCES team (teamid)
ON DELETE CASCADE
ON UPDATE CASCADE);
could you help me to find what is the error?
i know the error is when creating trainertoteam table