I cant reference to other table mysql - mysql

CREATE TABLE IF NOT EXISTS super(
id int (20) PRIMARY KEY AUTO_INCREMENT,
cod varchar(5)
);
CREATE TABLE IF NOT EXISTS super2(
id2 int (20) PRIMARY KEY AUTO_INCREMENT,
cod2 varchar(5),
FOREIGN KEY (cod2) REFERENCES super(cod)
);
I cant create share tables please help and thanks
with FOREIGN KEY errno 150...

It is because cod is not the primary key in the super table.

Related

Adding multi columns and foreign key

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.

Error adding foreign keys in many to many relationship

having two tables : "personnes"
CREATE TABLE IF NOT EXISTS personnes(
id INT UNSIGNED NOT NULL AUTO_INCREMENT,
nom VARCHAR(40) NOT NULL,
PRIMARY KEY(id)
)ENGINE=InnoDB;
and "listadresses"
CREATE TABLE IF NOT EXISTS listadresses(
id INT UNSIGNED NOT NULL AUTO_INCREMENT,
adresse VARCHAR(40) NOT NULL,
PRIMARY KEY(id)
)ENGINE=InnoDB;
i'm unable to add a foreign key for each column based on the primary key of the children tables to reproduce a many to many relation, how could i achieve this behaviour
CREATE TABLE IF NOT EXISTS liaisons(
id_nom INT UNSIGNED NOT NULL ,
id_adresse INT UNSIGNED NOT NULL ,
CONSTRAINT fk_nom FOREIGN KEY(id_nom) REFERENCES personnes(id),
CONSTRAINT fk_adresse FOREIGN KEY(id_adresse) REFERENCES listeadresses(id),
PRIMARY KEY(id_nom,id_adresse)
)ENGINE=InnoDB;
instead i got error code 1215. cannot add foreign key constraint, thanks in advance for any insight !
Here's your problem :)
Instead of:
REFERENCES listeadresses(id),
try
REFERENCES listadresses(id),
It can't add the foreign key, as obviously listeadresses doesn't exist, but listadresses does.

SQLfiddle Errno: 150 - Foreign Key Issue

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.

errno 150 mySQL foreign key

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.

Unable To Create Table, Unsure of what to do (Error: 150)

I am creating several tables at once and I keep getting the error(150):
#1005 - Can't create table 'waget.tour' (errno: 150)
and despite me knowing what the error is, I simply can't fix it. All tables references in the table "tour" are all there and exist, with keys where Foreign keys are referenced. I've checked it over plenty of times, and simply can't find anything. Can anyone see what I am doing wrong?
(Getting error when creating the table "tours")
CREATE TABLE IF NOT EXISTS tourPayment(
tourPaymentNumber int,
tourCost int(7),
PRIMARY KEY (tourPaymentNumber),
KEY (tourCost)
);
CREATE TABLE IF NOT EXISTS hotel(
hotelID int AUTO_INCREMENT,
hotelName varchar(30),
PRIMARY KEY (hotelID),
KEY (hotelName)
);
CREATE TABLE IF NOT EXISTS salutation(
salutationID int AUTO_INCREMENT,
salutation varchar(4),
KEY (salutation)
);
CREATE TABLE IF NOT EXISTS customer(
custID int AUTO_INCREMENT,
custSalutation varchar(4),
custLname varchar(30),
custAdd varchar(100),
custPcode varchar(4),
custState varChar(20),
custPhone varchar(10),
custHotel varchar(30),
PRIMARY KEY (custID),
FOREIGN KEY (custHotel) REFERENCES hotel(hotelName),
FOREIGN KEY (custSalutation) REFERENCES salutation(salutation)
);
CREATE TABLE IF NOT EXISTS bus(
busID int AUTO_INCREMENT,
busMake varchar(30),
busSeats varchar(3),
PRIMARY KEY (busID)
);
CREATE TABLE IF NOT EXISTS busDriver(
driverID int AUTO_INCREMENT,
driverName varchar(20),
driverEmail varchar(40),
busID int,
PRIMARY KEY (driverID),
FOREIGN KEY (busID) REFERENCES bus(busID)
);
CREATE TABLE IF NOT EXISTS tour(
DailyTourID int AUTO_INCREMENT,
NoOfPeople int(3),
tourDate DATE,
tourTime TIME,
tourName varchar(30),
tourCost int(7),
tourDriverID varchar(20),
PRIMARY KEY (DailyTourID),
FOREIGN KEY (tourDriverID) REFERENCES busDriver(driverID),
FOREIGN KEY (tourCost) REFERENCES tourPayment(tourCost)
);
CREATE TABLE IF NOT EXISTS TourCustLink(
TourCustLinkID int AUTO_INCREMENT,
TourID int,
custID int,
PRIMARY KEY (TourCustLinkID),
FOREIGN KEY (TourID) REFERENCES tour(DailyTourID),
FOREIGN KEY (custID) REFERENCES customer(custID)
);
Here is your problem:
FOREIGN KEY (tourDriverID) REFERENCES busDriver(driverID)
references an INT but is declared as varchar(20)
AUTO_INCREMENT fields all need to be PRIMARY KEY's, so try changing salutation to be
CREATE TABLE IF NOT EXISTS salutation(
salutationID int AUTO_INCREMENT,
salutation varchar(4),
PRIMARY KEY (salutationID),
KEY (salutation)
);
Additionally in tour you have the wrong datatype for tourDriverID, it should be INT to match the referenced key
CREATE TABLE IF NOT EXISTS tour(
DailyTourID int AUTO_INCREMENT,
NoOfPeople int(3),
tourDate DATE,
tourTime TIME,
tourName varchar(30),
tourCost int(7),
tourDriverID INT,
PRIMARY KEY (DailyTourID),
FOREIGN KEY (tourDriverID) REFERENCES busDriver(driverID),
FOREIGN KEY (tourCost) REFERENCES tourPayment(tourCost)
);
SQL fiddle example in full can be found here : http://sqlfiddle.com/#!2/840b9
Actually it's easy to try and create tables one by one and you would get a better error message..
Your Problem is the table salutation since you have to define the AUTO_INCREMENT field (in your case salutationID) as KEY and not the fieldsalutation`
CREATE TABLE IF NOT EXISTS salutation(
salutationID int AUTO_INCREMENT,
salutation varchar(4),
KEY (salutationID) -- here is your error
);
Is this the code you ran? if so, the error is about the AUTO_INCREMENT needing to be a Key.
CREATE TABLE IF NOT EXISTS salutation(
salutationID int AUTO_INCREMENT,
salutation varchar(4),
KEY (salutation)
);
Try:
CREATE TABLE IF NOT EXISTS salutation(
salutationID int AUTO_INCREMENT,
salutation varchar(4),
KEY (salutationID)
);
and make other a FOREIGN KEY if you need to.
Cheers.