mySQL: Error when attempting to create a Foreign Key [duplicate] - mysql

This question already has answers here:
ERROR 1452: Cannot add or update a child row: a foreign key constraint fails
(28 answers)
Closed 6 years ago.
I am trying to create two tables that share a common column. These tables are currently empty as I would like to add the data after all are linked appropriately. I can't seem to get to the bottom of the syntax error. I can create the table 'publisher' with no issues but can't reference it's primary key 'publishername' as a foreign key to the next table.
I get the dreaded E
RROR 1064 "near 'references publisher(publishername))' at line 1".
I'm lost because I've used a similar syntax previously to do this. Much thanks in advance!
create table publisher (
publishername varchar(30) primary key,
city varchar(15),
country varchar(10),
telephone varchar(15),
yearfounded int(4));
create table book (
booknumber int(3) primary key,
bookname varchar(50),
publicationyear int(4),
pages int(4),
publishername varchar references publisher(publishername));

It seems like you didn't give length of the foreign key. This works fine . You can check it here.
create table publisher (
publishername varchar(30) primary key,
city varchar(15),
country varchar(10),
telephone varchar(15),
yearfounded int(4));
create table book (
booknumber int(3) primary key,
bookname varchar(50),
publicationyear int(4),
pages int(4),
publishername varchar(30) references publisher(publishername));

Related

MySQL Error with CREATE TABLE

I am new to creating/testing/working with MySQL.
In this I have created a database and have used the command to USE the Database, however when trying to create a table, Error Code:1064 keeps coming up.
I have no previous tables in this database, this would be the first table.
I am unsure where the error is and would greatly appreciate it if someone could help me identiy the error and the reason why?
CREATE TABLE Customers(
customerNumber INT NOT NULL,
firstName VARCHAR(60),
lastName VARCHAR(60),
address VARCHAR(50) NOT NULL,
city VARCHAR(20) NOT NULL,
state ENUM('QLD','VIC','NSW','WA','TAS','NT','SA') NOT NULL,
postCode INT(4) NOT NULL,
region VARCHAR(60),
email VARCHAR(254),
PRIMARY KEY(customerNumber),
FOREIGN KEY(customerNumber)
);
A foreign key must reference another table.
The other table must exist before you can reference it in a foreign key.
I contributed to a checklist for foreign keys in this post:
https://stackoverflow.com/a/4673775/20860

Trying to create a joint table in MySQL but foreign key does not work

This is what I have tried:
create table books(bcode int(5) primary key, bname varchar(45));
and
create table customers(cid int(4), cname varchar(20), cadd varchar(40), bcode,
varchar(45), foreign key(bcode) references books(bcode));
After executing the second statement, the following error shows up:
ERROR 1215 (HY000): Cannot add foreign key constraint
I'm having trouble coming up with a solution. Any help is appreciated.
In the first table books you use bcode as integer
But in the second table you use bcode as varchar,
So, right one is
create table customers(cid int(4), cname varchar(20), cadd varchar(40), bcode
int(5), foreign key(bcode) references books(bcode));

MySQL - error 1215, cannot add foreign key constraint

I know there are numerous questions regarding this error on here but I've searched through many and none seem to explain it in my case!
I've created a table using the following code:
CREATE TABLE Venue (
venueID VARCHAR(20),
venueEmail VARCHAR(30) NOT NULL,
address VARCHAR(100),
phoneNo VARCHAR(20),
managerNo VARCHAR(20),
capacity INT(4),
PRIMARY KEY (venueEmail)
)ENGINE=InnoDB;
And am trying to create a table with a foreign key that refers to the first table using this code:
CREATE TABLE Concert (
referenceNo VARCHAR(6),
venueEmail VARCHAR(30),
eventDate VARCHAR(10),
startTime VARCHAR(5),
ticketsSold INT(4),
PRIMARY KEY (referenceNo),
FOREIGN KEY (venueEmail) REFERENCES Venue ON UPDATE CASCADE ON DELETE CASCADE
)ENGINE=InnoDB;
But it's giving me the 1215 error message!
Syntax wise this isn't correct.
The issue's resolved here:
CREATE TABLE Venue (
venueID VARCHAR(20),
venueEmail VARCHAR(30) NOT NULL,
address VARCHAR(100),
phoneNo VARCHAR(20),
managerNo VARCHAR(20),
capacity INT(4),
PRIMARY KEY (venueEmail)
)ENGINE=InnoDB;
CREATE TABLE Concert (
referenceNo VARCaHAR(6),
venueEmail VARCHAR(30),
eventDate VARCHAR(10),
startTime VARCHAR(5),
ticketsSold INT(4),
PRIMARY KEY (referenceNo),
FOREIGN KEY (venueEmail) REFERENCES Venue(`venueEmail`) ON UPDATE CASCADE ON DELETE CASCADE
)ENGINE=InnoDB;
Note:
The column being referenced should be stated like table_name(column_name).
You missed the column_name part.
REFERENCE
More:
#Bill Karwin added the following useful info in the comment section:
FWIW this is a MySQL idiosyncrasy. In standard SQL, if you omit the
referenced column name, it defaults to the same name as the foreign
key column. But InnoDB doesn't support this shortcut syntax—you must
specify the column in both cases.

How to create tables on putty if they all depend on each other by using mysql syntax?

I have to create a number of tables for my project and each one of them has a foreign key besides their primary key. I know how to create a table on putty without the foreign key but if they are all depends on each other, then how to create them individually ?
For example, if I want to create table A, B and C
CREATE TABLE PROFESSOR
(
SSN numeric(9) primary key,
PNAME varchar(20),
CITY varchar(20),
STREETADDRESS varchar(50),
STATE char(2),
ZIP numeric(5),
AREACODE numeric(3),
PHONENUMBER numeric(7),
SEX char(1),
TITLE char(4),
SALARY float(9),
foreign key (DNUM) references DEPARTMENT(DNUM)
);
CREATE TABLE DEPARTMENT
(
DNUM numeric(1) primary key,
DNAME varchar(20),
DPHONE numeric(10),
OFFICELOCATION varchar(20),
foreign key (CWID) references STUDENT(CWID)
);
CREATE TABLE STUDENT
(
CWID numeric(9) primary key,
FNAME varchar(20),
LNAME varchar(20),
SADDRESS varchar(50),
SPHONE numeric(10),
foreign key (DNUM) references DEPARTMENT(DNUM)
);
I'm using Putty to create table and I have to run them separately. The thing is if I run the scrip separately for each table, it's going to give me an error, because the table that has the foreign key does not create yet. How am I going to fix it ? and is there a work around way on solving this problem ? Thank you.
If I run the script for creating table separately, it will give me an error

MYSQL in PHPMYADMIN - create table relationship from one table

I want to create a table relationship with MYSQL PHPMYADMIN.
I have this Create table:
CREATE TABLE students(code_students int(8)not null AUTO_INCREMENT,
name_students varchar(25),
age_students int(3),
degree_program varchar(25),
code_advisor int(8)not null,
primary key(code_students, code_advisor)
);
and i want to make a create table named advise relationship between code_students, code_advisor.
Ok this is my tryout.
CREATE TABLE advise (
code_students int(8),
code_advisor int(8),
primary key(code_students, code_advisor),
foreign key(code_students)references students(code_students),
foreign key(code_advisor)references students(code_advisor)
);
mySQL says :
A FOREIGN KEY constraint that references a non-UNIQUE key is not standard SQL. It is an InnoDB extension to standard SQL
Try adding the UNIQUE keyword to your first table:
CREATE TABLE students(
code_students int(8)not null unique AUTO_INCREMENT,
name_students varchar(25),
age_students int(3),
degree_program varchar(25),
code_advisor int(8)not null unique,
primary key(code_students, code_advisor)
);
Check out this sqlFiddle and see how it works, then try removing the UNIQUE keywords and see that you get the same error that you mentioned. ( Hit the Build Schema button )
http://sqlfiddle.com/#!2/46b69