create table bankdb.customer(
customer_name varchar(45) not null,
social_security int not null,
customer_street varchar(45),
customer_city varchar(45),
primary key(`social_security`)
)engine=InnoDB;
create table bankdb.branch(
branch_name varchar(45) not null,
branch_city varchar(45),
assets int,
primary key (`branch_name`)
)engine=InnoDB;
create table bankdb.account(
branch_name varchar(45),
account_number varchar(45) not null,
balance int,
primary key (`account_number`),
constraint fk_acount_branch
foreign key (`branch_name`)
references bankdb.branch(`branch_name`)
)engine=InnoDB;
create table bankdb.depositor(
customer_name varchar(45) not null,
account_number varchar(45) not null,
primary key (`customer_name`, `account_number`),
constraint fk_depositor_customer
foreign key(`customer_name`)
references bankdb.customer(`customer_name`),
constraint fk_depositor_account
foreign key(`account_number`)
references bankdb.account(`account_number`)
)engine=InnoDB;
That's my sql code... i get the error can't create table for the table bankdb.depositor... Is there anything wrong with my foreign keys? Any clues?
Edit, based on your new script the following appears to be working in SQL Fiddle. I had to add indexes:
create table bankdb.customer(
customer_name varchar(45) not null,
social_security int not null,
customer_street varchar(45),
customer_city varchar(45),
primary key(`social_security`),
INDEX (customer_name)
)engine=InnoDB;
create table bankdb.branch(
branch_name varchar(45) not null,
branch_city varchar(45),
assets int,
primary key (`branch_name`)
)engine=InnoDB;
create table bankdb.account(
branch_name varchar(45),
account_number varchar(45) not null,
balance int,
primary key (`account_number`),
INDEX (account_number),
constraint fk_acount_branch
foreign key (`branch_name`)
references bankdb.branch(`branch_name`)
)engine=InnoDB;
create table bankdb.depositor(
customer_name varchar(45) not null,
account_number varchar(45) not null,
primary key (`customer_name`, `account_number`),
INDEX (customer_name),
INDEX (account_number),
constraint fk_depositor_customer
foreign key(`customer_name`)
references bankdb.customer(`customer_name`),
constraint fk_depositor_account
foreign key(`account_number`)
references bankdb.account(`account_number`)
)engine=InnoDB;
See SQL Fiddle with Demo
If this is your full script your issue is with the create table for account.
You are trying to create a foreign key on the branch table which doesn't appear to exist:
create table bankdb.account(
branch_name varchar(45),
account_number varchar(45) not null,
balance int,
primary key (`account_number`),
constraint fk_acount_branch
foreign key (`branch_name`)
references bankdb.branch(`branch_name`) -- does this exist
)engine=InnoDB;
Related
I am trying to create a database for a school project. I am getting the error specified in the title, and cannot figure out how to resolve it. I am not very experienced in mySQL but apparently the error is vague and can be a number of thing. It happens when I try to input the showing table.
`CREATE TABLE Complex(
name varchar(50) not null,
streetNum integer,
streetName varchar(50),
city varchar(50),
province varchar(50),
postalCode char(6),
numTheatres integer,
primary key(name)
);
CREATE TABLE Theatre(
complexName varchar(50) not null,
theatreNum integer not null,
maxSeats integer,
screenSize varchar(6),
foreign key (complexName) references Complex(name),
primary key (complexName, theatreNum)
);
CREATE TABLE Supplier(
compName varchar(50) not null,
streetName varchar(50),
streetNum varchar(50),
city varchar(50),
province varchar(50),
postalCode char(6),
phone char(10),
contactFName varchar(10),
contactLName varchar(10),
primary key (compName)
);
CREATE TABLE Movie(
title varchar(100) not null,
runningTime integer,
rating varchar(4),
synopsis varchar(500),
director varchar(100),
prodComp varchar(100),
supplierName varchar(100),
startDate date,
endDate date,
foreign key (supplierName) references Supplier(compName),
primary key (title)
);
Create Table Actor(
fName varchar(50) not null,
lName varchar(50) not null,
primary key (fName,lName)
);
create table Stars
(
fName varchar(100) not null references Actor(fName),
lName varchar(100) not null references Actor(lName),
title varchar(100) not null references Movie(title),
primary key (fName, lName, title)
);
CREATE TABLE Account(
accountNum integer not null AUTO_INCREMENT,
password varchar(50) not null,
fName varchar(50),
lName varchar(50),
phone char(10),
email varchar(50),
creditCard varchar(15),
cardExpiry char(4),
primary key (accountNum)
);
CREATE TABLE Showing(
complexName varchar(50) not null,
title varchar(100) not null,
theatreNum integer not null,
startTime time not null,
seatsAvailable integer,
foreign key (complexName) references Theatre(complexName),
foreign key (title) references Movie(title),
foreign key (theatreNum) references Theatre(theatreNum),
primary key (startTime)
);
CREATE TABLE Review(
title varchar(100) not null,
ID integer not null,
score integer,
primary key (ID),
foreign key (title) references Movie(title)
);
CREATE TABLE Reserved(
accountNum integer not null,
complexName varchar(50) not null,
theatreNum integer not null,
movieTitle varchar(50) not null,
startTime time not null,
ticketsNum integer,
foreign key (accountNum) references Account(accountNum),
foreign key (complexName) references Showing(compName),
foreign key (theatreNum) references Showing(theatreNum),
foreign key (movieTitle) references Showing(movieTitle),
foreign key (startTime) references Showing(startTime),
primary key (accountNum, complexName, theatreNum, movieTitle, startTime)
)
;`
This is one of the most common misunderstandings about foreign keys for beginners.
CREATE TABLE Theatre(
complexName varchar(50) not null,
theatreNum integer not null,
primary key (complexName, theatreNum)
CREATE TABLE Showing(
complexName varchar(50) not null,
theatreNum integer not null,
foreign key (complexName) references Theatre(complexName),
foreign key (theatreNum) references Theatre(theatreNum),
...
When you reference a table with a multi-column primary key, your foreign key must be multi-column as well. Don't declare two foreign key constraints. Declare one foreign key constraint with the same columns as the primary key it references.
CREATE TABLE Showing(
complexName varchar(50) not null,
theatreNum integer not null,
foreign key (complexName, theatreNum) references Theatre(complexName, theatreNum),
...
For more tips on foreign key, like a checklist of what needs to be true, see the top answer on this question: MySQL Creating tables with Foreign Keys giving errno: 150
I get this following error for some reason, and I know it has to do with the foreignkey I just added "UsesTruck" However I don't know WHAT it is I did wrong.. I have included only the tables I think are important for this issue.
CREATE TABLE Employee (
EmployeeID INT AUTO_INCREMENT,
Name VARCHAR(90) NOT NULL,
UsesTruck CHAR(20) NULL,
FOREIGN KEY (UsesTruck) REFERENCES Truck(LicensePlate),
PRIMARY KEY (EmployeeID));
CREATE TABLE Truck (
LicensePlate CHAR(20) NULL,
color VARCHAR(45) NULL,
capacity VARCHAR(45) NULL,
PRIMARY KEY (LicensePlate));
CREATE TABLE Shifts (
ShiftTime VARCHAR(15) NOT NULL,
PRIMARY KEY (ShiftTime));
CREATE TABLE Reservation (
ReservNum INT NOT NULL,
ReserveDate VARCHAR(45) NULL,
PickupTime VARCHAR(45) NOT NULL,
NumOfPassengers INT NULL,
sheduledTime VARCHAR(45) NULL,
ActualPickupTime VARCHAR(45),
ActualTime VARCHAR(45),
PricePaid VARCHAR(45),
DriverHourlyRate DECIMAL(7,2) NULL,
PassEmployeeHourlyRate DECIMAL (7,2) NULL,
DriverSalary VARCHAR(10),
PassEmployeeSalary VARCHAR(10),
Customer_CustomerID INT,
Truck_LicensePlate char(20) NULL,
Employee_EmployeeID_Driver INT,
Location_Address_Pickup VARCHAR(100),
Employee_EmployeeID_Passenger INT,
Location_Address_Drop VARCHAR(100),
PRIMARY KEY (ReservNum),
FOREIGN KEY (Customer_CustomerID) REFERENCES Customer (CustomerID),
FOREIGN KEY (Truck_LicensePlate) REFERENCES Truck (LicensePlate),
FOREIGN KEY (Employee_EmployeeID_Driver) REFERENCES Employee (EmployeeID),
FOREIGN KEY (Location_Address_Pickup) REFERENCES Location (Address),
FOREIGN KEY (Employee_EmployeeID_Passenger) REFERENCES Employee (EmployeeID),
FOREIGN KEY (Location_Address_Drop) REFERENCES Location (Address));
Try to run create Truck table firstly.
CREATE TABLE tblTransaction (
strTransCode VARCHAR(50) NOT NULL,
dtmTransDate datetime,
strTransDesc VARCHAR(50) NOT NULL,
dblTransAmt double,
intVoucRefCodeTrans INT,
FOREIGN KEY (intVoucRefCodeTrans) REFERENCES tblVoucher (intVoucRefCode) ON DELETE RESTRICT ON UPDATE CASCADE,
PRIMARY KEY (strTransCode)
)ENGINE=InnoDB;
CREATE TABLE tblVoucher (
intVoucRefCode INT,
strVoucRefDesc VARCHAR(50) NOT NULL,
dtmVoucDate datetime,
PRIMARY KEY (intVoucRefCode)
)ENGINE=InnoDB;
these are my tables I don't know why it displays "Cannot add foreign key constraint" please help
First create tblVoucher table then reference it in tblTransaction table
CREATE TABLE tblVoucher (
intVoucRefCode INT,
strVoucRefDesc VARCHAR(50) NOT NULL,
dtmVoucDate datetime,
PRIMARY KEY (intVoucRefCode)
)ENGINE=InnoDB;
CREATE TABLE tblTransaction (
strTransCode VARCHAR(50) NOT NULL,
dtmTransDate datetime,
strTransDesc VARCHAR(50) NOT NULL,
dblTransAmt double,
intVoucRefCodeTrans INT,
FOREIGN KEY (intVoucRefCodeTrans) REFERENCES tblVoucher (intVoucRefCode) ON DELETE RESTRICT ON UPDATE CASCADE,
PRIMARY KEY (strTransCode)
)ENGINE=InnoDB;
DEMO
can't create database
--------------------------------------------------------
create table DurableArticle(
durable_code varchar(30) not null,
durable_name varchar(100),
durable_number varchar(20),
durable_brandname varchar(125),
durable_modelordetail varchar(125),
durable_price varchar(20),
durable_entrance varchar(20),
constraint durable_PK primary key (durable_code)
)ENGINE=InnoDB DEFAULT CHARSET=utf8;
---------------------------------------------
create table DurableProfile(
profile_year integer not null,
profile_status varchar(100),
profile_note varchar(100),
staff_id integer not null,
owner_id integer not null,
room_id integer not null,
durable_code varchar(30) not null,
constraint profile_PK primary key (profile_year,durable_code),
constraint profile_FK1 foreign key (staff_id) references staff(staff_id),
constraint profile_FK2 foreign key (owner_id) references owner(owner_id),
constraint profile_FK3 foreign key (room_id) references room(room_id),
constraint profile_Fk4 foreign key (durable_code) references durablearticle (durable_code)
)ENGINE=InnoDB;
you reference 3 tables in the second statement, which obviously do not exist (yet).
Remove the foreign keys or create those tables, then it works.
create table DurableProfile(
profile_year integer not null,
profile_status varchar(100),
profile_note varchar(100),
staff_id integer not null,
owner_id integer not null,
room_id integer not null,
durable_code varchar(30) not null,
constraint profile_PK primary key (profile_year,durable_code),
constraint profile_Fk4 foreign key (durable_code) references durablearticle (durable_code)
)ENGINE=InnoDB;
I am trying to create a database that holds staff information such as their names, timesheets, holidays booked etc and also information about the projects the are carrying out and what companies the projects are for. My code is below:
CREATE TABLE IF NOT EXISTS tblcompany (
companyid INT(11) UNSIGNED NOT NULL,
custfirst VARCHAR(50),
custlast VARCHAR(50),
company VARCHAR(50),
custphone VARCHAR(50),
custemail VARCHAR(50),
PRIMARY KEY (companyid),
INDEX (companyid),
CONSTRAINT FOREIGN KEY (companyid)
REFERENCES tblproject (companyid)
) ENGINE=InnoDB;
CREATE TABLE IF NOT EXISTS tblemployee (
employeeid INT(11) UNSIGNED NOT NULL,
employeefirst VARCHAR(50),
employeelast VARCHAR(50),
employeephone VARCHAR(50),
employeeemail VARCHAR(50),
PRIMARY KEY (employeeid),
INDEX (employeeid),
CONSTRAINT FOREIGN KEY (employeeid)
REFERENCES tbltimesheet (employeeid),
CONSTRAINT FOREIGN KEY (employeeid)
REFERENCES tblholiday (employeeid),
CONSTRAINT FOREIGN KEY (employeeid)
REFERENCES tblannualleave (employeeid)
) ENGINE=InnoDB;
CREATE TABLE IF NOT EXISTS tblholiday (
holidayid INT(11) UNSIGNED NOT NULL,
employeeid INT(11) UNSIGNED NOT NULL,
holidayfrom DATE,
holidayto DATE,
holidayhalfday BOOLEAN,
holidayreason VARCHAR(50),
INDEX (employeeid),
PRIMARY KEY (holidayid)
) ENGINE=InnoDB;
CREATE TABLE IF NOT EXISTS tblannualleave (
annualleaveid INT(11) UNSIGNED NOT NULL,
employeeid INT(11) UNSIGNED NOT NULL,
annualleavetaken INT(11),
annualleaveremain INT(11),
anuualleavetotal INT(11),
INDEX (employeeid),
PRIMARY KEY (annualleaveid)
) ENGINE=InnoDB;
CREATE TABLE IF NOT EXISTS tblproject (
projectid INT(11) UNSIGNED NOT NULL,
projectname VARCHAR(50),
projecttype VARCHAR(50),
companyid INT(11) UNSIGNED NOT NULL,
projectnotes VARCHAR(50),
PRIMARY KEY (projectid),
INDEX (projectid),
CONSTRAINT FOREIGN KEY (projectid)
REFERENCES tbltimesheet (projectid)
) ENGINE=InnoDB;
CREATE TABLE IF NOT EXISTS tbltimesheet (
timesheetid INT(11) UNSIGNED NOT NULL,
employeeid INT(11) UNSIGNED NOT NULL,
projectid INT(11) UNSIGNED NOT NULL,
timesheetdate DATE,
timesheethours INT(11),
timesheetnotes VARCHAR(50),
INDEX (employeeid),
PRIMARY KEY (timesheetid)
) ENGINE=InnoDB;
I have been looking around and have tried everything, it is probably something so simple. I have changed all the datatypes to similar ones to see if this would solve the problem butno luck. The error code I get is:
Error Code: 1215. Cannot add foreign key constraint
0.063 sec
CREATE TABLE IF NOT EXISTS tblcompany ( companyid INT(11) UNSIGNED
NOT NULL, custfirst VARCHAR(50), custlast VARCHAR(50),
company VARCHAR(50), custphone VARCHAR(50), custemail
VARCHAR(50), PRIMARY KEY (companyid), INDEX (companyid),
CONSTRAINT FOREIGN KEY (companyid) REFERENCES tblproject
(companyid) ) ENGINE=InnoDB
11:15:57 CREATE TABLE IF NOT EXISTS tblcompany ( companyid INT(11)
UNSIGNED NOT NULL, custfirst VARCHAR(50), custlast
VARCHAR(50), company VARCHAR(50), custphone VARCHAR(50),
custemail VARCHAR(50), PRIMARY KEY (companyid), INDEX
(companyid), CONSTRAINT FOREIGN KEY (companyid) REFERENCES
tblproject (companyid) ) ENGINE=InnoDB Error Code: 1215. Cannot add
foreign key constraint 0.063 sec
Thank you for looking..
Create the table tblproject first before you reference it.
Besides the wrong table order,you need either a primary or unique key on referenced columns.
SQL fiddle
I think you will solve your problem creating your tables in the opposite ordersm, otherwise you will have the same issue with tblemployee.
A Foreigh Key needs that the reference table exists already.
You cannot create a foreign key when the table it references does not yet exist. You can always create the foreign key later.
You must first create the tables containing the referenced fields, after you create the table with the foreign key.
You could also create first all the tables, and create all foreign keys in a second step.