Error 150 : table couldn't be created - mysql

Where am I going wrong? I get the error 150 debit_card table couldn't be created. Something to do the foreign keys i know, just don't know what exactly. Help?
create table customer(
cust_id int auto_increment,
first_name varchar(25),
last_name varchar(25),
street varchar(25),
city varchar(25),
state char(2),
zip varchar(10),
CONSTRAINT customer_pk primary key(cust_id)
);
create table card_account(
acct_no char(16),
exp_date date,
card_type ENUM("Debit","Credit") not null,
CONSTRAINT card_account_pk primary key(acct_no,exp_date)
);
create table debit_card(
bank_no char(9) not null,
acct_no char(16),
exp_date date,
CONSTRAINT debit_card_pk primary key(acct_no,exp_date),
CONSTRAINT debit_card_acct_no_fk foreign key(acct_no) references card_account(acct_no) on delete cascade,
CONSTRAINT debit_card_exp_date_fk foreign key(exp_date) references card_account(exp_date) on delete cascade
);

Your card_account table has a composite primary key. You cannot use those columns separately in a foreign key constraint.
But you can add a foreign key referencing to the composite primary key:
create table debit_card(
bank_no char(9) not null,
acct_no char(16),
exp_date date,
CONSTRAINT debit_card_pk primary key(acct_no,exp_date),
CONSTRAINT debit_card_acct_no_fk foreign key(acct_no,exp_date) references card_account(acct_no,exp_date) on delete cascade
);

Related

ERROR 1005 (HY000): Can't create table (errno: 150 "Foreign key constraint is incorrectly formed')

I was creating some tables in a database, everything works fine except for when I try to create the last table 'gatt_descriptors' I get the warning
gatt_profiles (primary key 'version')
CREATE TABLE gatt_profiles(
version INT NOT NULL PRIMARY KEY,
profile_name VARCHAR(25),
value VARCHAR(25));
gatt_services (primary key 'service_name')
CREATE TABLE gatt_services(
service_name VARCHAR(25) NOT NULL,
uuid VARCHAR(36),
id VARCHAR(36),
declaration_type VARCHAR(10),
advertise BIT,
version INT NOT NULL,
CONSTRAINT ver_val FOREIGN KEY (version) REFERENCES gatt_profiles(version) ON DELETE RESTRICT ON UPDATE CASCADE,
CONSTRAINT pk_verser PRIMARY KEY (service_name, version));
gatt_characteristics (primary key 'char_name')
CREATE TABLE gatt_characteristics(
char_name VARCHAR(25) PRIMARY KEY NOT NULL,
uuid VARCHAR(36),
id VARCHAR(36),
val_const BIT,
init_val VARCHAR(25),
var_length BIT,
val_length INT,
read_val VARCHAR(7),
write_val VARCHAR(7),
write_wo_response VARCHAR(7),
reliable_write VARCHAR(7),
notify VARCHAR(7),
indicate VARCHAR(7),
version INT NOT NULL,
service_name VARCHAR(25) NOT NULL,
FOREIGN KEY (version) REFERENCES gatt_profiles (version) ON DELETE RESTRICT ON UPDATE CASCADE,
FOREIGN KEY (service_name) REFERENCES gatt_services (service_name) ON DELETE RESTRICT ON UPDATE CASCADE);
gatt_descriptors (primary key 'descriptor_name')
CREATE TABLE gatt_descriptors(
descriptor_name VARCHAR(25) NOT NULL,
uuid VARCHAR(36),
id VARCHAR(36),
val_const BIT,
init_val VARCHAR(25),
var_length BIT,
val_length INT,
read_val VARCHAR(7),
write_val VARCHAR(7),
version INT NOT NULL,
service_name VARCHAR(25) NOT NULL,
char_name VARCHAR(25),
FOREIGN KEY (char_name, service_name, version) REFERENCES gatt_characteristics (char_name, service_name, version) ON DELETE RESTRICT ON UPDATE CASCADE,
CONSTRAINT pk_verserchardescr PRIMARY KEY (descriptor_name, char_name, service_name, version));
Your help is greatly appreciated. Thank you.
gatt_characteristics has a primary key consisting of the single column char_name.
The foreign key in gatt_descriptors is three columns, the first of which is char_name.
When you define a foreign key, it should have exactly the same columns as the primary key of the table it references.

MySQL: Cannot add foreign key constraint error in school project

I'm building a really basic database for a school project and am getting a "cannot add foreign key constraint" error in MySQL. I've been scratching my head on this one for the past day, read all of the related posts and haven't been able to figure it out.
Here are the first two tables of my project:
CREATE TABLE CUSTOMER (
CUST_ID INTEGER NOT NULL AUTO_INCREMENT UNIQUE,
CUST_LNAME VARCHAR(25) NOT NULL,
CUST_FNAME VARCHAR(25) NOT NULL,
CUST_INITIAL CHAR(1),
CUST_STREET_NO VARCHAR(6),
CUST_STREET_NAME VARCHAR(25),
CUST_APT_NO VARCHAR(10),
CUST_CITY VARCHAR(25),
CUST_STATE CHAR(2),
CUST_ZIP_CODE CHAR(5),
CUST_HOME_AC CHAR(3),
CUST_HOME_PHONE CHAR(8),
PRIMARY KEY (CUST_ID))
ENGINE = InnoDB;
CREATE TABLE INVOICE (
INVOICE_ID INTEGER NOT NULL AUTO_INCREMENT UNIQUE,
CUST_ID INTEGER NOT NULL,
INV_DATE DATE NOT NULL,
SPECIAL_HANDLING VARCHAR(35),
PRIMARY KEY (INVOICE_ID),
FOREIGN KEY (CUST_ID) REFERENCES CUSTOMER ON UPDATE CASCADE)
ENGINE = InnoDB;
I'm sure it's something super easy that I'm missing. Any ideas?
You are missing the column to be referred in your foreign key constraint def.
Use:
FOREIGN KEY (CUST_ID) REFERENCES CUSTOMER(CUST_ID) ON UPDATE CASCADE)
instead of
FOREIGN KEY (CUST_ID) REFERENCES CUSTOMER ON UPDATE CASCADE)
So, create table becomes:
CREATE TABLE INVOICE (
INVOICE_ID INTEGER NOT NULL AUTO_INCREMENT UNIQUE,
CUST_ID INTEGER NOT NULL,
INV_DATE DATE NOT NULL,
SPECIAL_HANDLING VARCHAR(35),
PRIMARY KEY (INVOICE_ID),
FOREIGN KEY (CUST_ID) REFERENCES CUSTOMER(CUST_ID) ON UPDATE CASCADE)
ENGINE = InnoDB;
try to declare the name of the foreign key, and then declare the column of the current table and the table and the reference column
CONSTRAINT fk_customer FOREIGN KEY (INVOICE_ID)
REFERENCES customer(CUST_ID)

Error Code: 1215. Cannot add foreign key constraint

Can someone tell me why I am getting this error message? The error pops up when I am trying to create the RENTAL table.
CREATE TABLE CAR_CLASS
(CAR_CLASS_ID INT(3) PRIMARY KEY,
CAR_CLASS CHAR(20),
RENTAL_RATE DECIMAL(4,2) );
CREATE TABLE CAR
(CAR_ID CHAR(25) PRIMARY KEY,
CAR_CLASS_ID INT(3),
CAR_COLOR CHAR(20),
FOREIGN KEY (CAR_CLASS_ID) REFERENCES CAR_CLASS(CAR_CLASS_ID) );
CREATE TABLE CUSTOMER_INFO
(CUSTOMER_ID CHAR(30) PRIMARY KEY,
CUSTOMER_FIRST CHAR(30),
CUSTOMER_LAST CHAR(30),
CUSTOMER_CC_NUMBER CHAR(16));
CREATE TABLE RENTAL
(RENTAL_ID INT(3) PRIMARY KEY,
RENTAL_DATE_OUT DATE,
RENTAL_DATE_IN DATE,
CAR_CLASS_ID INT(3),
CAR_ID CHAR(25),
CUSTOMER_ID CHAR(30),
FOREIGN KEY (CAR_CLASS_ID) REFERENCES CAR_CLASS(CAR_CLASS_ID),
FOREIGN KEY (CAR_ID) REFERENCES CAR(CAR_ID),
FOREIGN KEY (CUSTOMER_ID) REFERENCES CUSTOMER(CUSTOMER_ID) );
Your other table is called CUSTOMER_INFO, while your foreign key references just CUSTOMER. Change your last CREATE TABLE to this:
CREATE TABLE RENTAL
(RENTAL_ID INT(3) PRIMARY KEY,
RENTAL_DATE_OUT DATE,
RENTAL_DATE_IN DATE,
CAR_CLASS_ID INT(3),
CAR_ID CHAR(25),
CUSTOMER_ID CHAR(30),
FOREIGN KEY (CAR_CLASS_ID) REFERENCES CAR_CLASS(CAR_CLASS_ID),
FOREIGN KEY (CAR_ID) REFERENCES CAR(CAR_ID),
FOREIGN KEY (CUSTOMER_ID) REFERENCES CUSTOMER_INFO(CUSTOMER_ID) );
the name of the table is wrong while creating a RENTAL table.
Look at the last line of your code.
FOREIGN KEY (CUSTOMER_ID) REFERENCES CUSTOMER(CUSTOMER_ID).
instead of customer, it should be CUSTOMER_INFO.
All the best :)

ERROR 1451 (2300) : Cannot delete or update a parent row: a foreign key constraint fails

DROP TABLE if exists employee;
CREATE TABLE employee (
fname varchar(15) not null,
minit varchar(1),
lname varchar(15) not null,
ssn char(9),
bdate date,
address varchar(50),
sex char,
salary decimal(10,2),
superssn char(9),
dno int(4),
primary key (ssn),
foreign key (superssn) references employee(ssn)
-- foreign key (dno) references department(dint)
);
DROP TABLE if exists department;
CREATE TABLE department (
dname varchar(25) not null,
dint int(4),
mgrssn char(9) not null,
mgrstartdate date,
primary key (dint),
unique (dname),
foreign key (mgrssn) references employee(ssn)
);
ALTER TABLE employee ADD (
foreign key (dno) references department(dint)
);
DROP TABLE if exists dept_locations;
CREATE TABLE dept_locations (
dint int(4),
dlocation varchar(15),
primary key (dint,dlocation),
foreign key (dint) references department(dint)
);
DROP TABLE if exists project;
CREATE TABLE project (
pname varchar(25) not null,
pint int(4),
plocation varchar(15),
dnum int(4) not null,
primary key (pint),
unique (pname),
foreign key (dnum) references department(dint)
);
DROP TABLE if exists works_on;
CREATE TABLE works_on (
essn char(9),
pno int(4),
hours decimal(4,1),
primary key (essn,pno),
foreign key (essn) references employee(ssn),
foreign key (pno) references project(pint)
);
DROP TABLE if exists dependent;
CREATE TABLE dependent (
essn char(9),
dependent_name varchar(15),
sex char,
bdate date,
relationship varchar(8),
primary key (essn,dependent_name),
foreign key (essn) references employee(ssn)
);
The error is at this line ALTER TABLE employee ADD (...
Please edit and make it look like:
ALTER TABLE employee ADD
foreign key (dno) references department(dint);
There were some unnecessary parenthesis.

Dropping tables mySql

I am having terrible trouble figuring out the order these tables should be dropped in for my script I will post the script now, If anyone could help with the order they should be dropped in it would be a great help
drop table Registered_Student;
drop table EducateYou_VideoRating;
drop table EducateYou_Video;
drop table EducateYou_Violation;
drop table EducateYou_Member;
drop table Violation_Type;
drop table EducateYou_Comment;
drop table EducateYou_FollowList;
drop table EducateYou_Group;
drop table EducateYou_Admin;
commit;
create table EducateYou_Violation
(
violationId number (9),
violationStatus char(1) DEFAULT 'X',
videoId number(9),
groupId number (9),
commentId number(9),
CONSTRAINT pk_violationId PRIMARY KEY (violationId),
CONSTRAINT fk_x_videoId FOREIGN KEY (videoId) REFERENCES EducateYou_Video,
CONSTRAINT fk_y_groupId FOREIGN KEY (groupId) REFERENCES EducateYou_Group,
CONSTRAINT fk_z_commentId FOREIGN KEY (commentId) REFERENCES EducateYou_Comment
);
create table EducateYou_Video(
videoId number(9),
videoRef varchar2(50) NOT NULL,
videoTitle varchar2(15) NOT NULL,
uploadDate DATE NOT NULL,
videoStatus char(1) DEFAULT 'X',
videoCategory varchar2(15) NOT NULL,
regNo number(6) NOT NULL,
AdminId number(9),
commentId number(9),
CONSTRAINT pk_videoId PRIMARY KEY (videoId),
CONSTRAINT fk_r_regNo FOREIGN KEY (regNo) REFERENCES EducateYou_Member,
CONSTRAINT fk_p_AdminId FOREIGN KEY (AdminId) REFERENCES EducateYou_Admin,
CONSTRAINT fk_n_commentId FOREIGN KEY (commentId) REFERENCES EducateYou_Comment
);
create table EducateYou_VideoRating(
rating number(2) NOT NULL,
regNo number(9),
videoId number(9),
CONSTRAINT pk_video_rating PRIMARY KEY (videoId, regNo),
CONSTRAINT fk_x_videoId FOREIGN KEY (videoId) REFERENCES EducateYou_Video,
CONSTRAINT fk_z_regNo FOREIGN KEY (regNo) REFERENCES EducateYou_Member
);
create table EducateYou_Admin
(
AdminId number(9),
firstName varchar2(15),
surname varchar2(15),
adminActions varchar2(40),
CONSTRAINT pk_AdminId PRIMARY KEY (AdminId)
);
create table EducateYou_Group(
groupID number(5) NOT NULL,
groupname varchar2(15) NOT NULL,
regNo number(6) NOT NULL,
CONSTRAINT pk_groupID PRIMARY KEY (groupID),
CONSTRAINT fk_i_regNo FOREIGN KEY (regNo) REFERENCES EducateYou_Member
);
create table EducateYou_Comment(
commentId number(9),
commentStatus char(1) DEFAULT 'A',
commentTime timestamp,
counter number(5) DEFAULT 5,
educateYouComment varchar2(50),
regNo number(6) NOT NULL,
CONSTRAINT pk_commentId PRIMARY KEY (commentId),
CONSTRAINT fk_b_regNo FOREIGN KEY (regNo) REFERENCES EducateYou_Member
);
create table EducateYou_FollowList(
followListID number(9),
regNo number(9),
CONSTRAINT pk_followListID PRIMARY KEY (followListID),
CONSTRAINT fk_z_regNo FOREIGN KEY (regNo) REFERENCES EducateYou_Member
);
create table Violation_Type
(
violation_TypeId varchar2(9),
violationId number(9),
CONSTRAINT pk_violation_TypeId PRIMARY KEY (violation_TypeId, violationId ),
CONSTRAINT fk_m_violationId FOREIGN KEY (violationId) REFERENCES EducateYou_Violation
);
create table EducateYou_Member
(
regNo number(6),
status char(1) DEFAULT 'A',
collegeId varchar2(9) NOT NULL,
CONSTRAINT pk_regNo PRIMARY KEY (regNo),
CONSTRAINT fk_l_collegeId FOREIGN KEY (collegeId) REFERENCES Registered_Student
);
create table Registered_Student
(
collegeId varchar2(9),
password varchar2(9) NOT NULL,
firstname varchar2(20) NOT NULL,
surname varchar2(20) NOT NULL,
dob date NOT NULL,
street varchar2(25) NOT NULL,
town varchar2(10) NOT NULL,
county varchar2(15) NOT NULL,
college varchar2(30) NOT NULL,
course varchar2(10) NOT NULL,
CONSTRAINT pk_collegeId PRIMARY KEY (collegeId)
);
Drop Table Syntax
Add SET FOREIGN_KEY_CHECKS = 0; in beginning to ignore the foreign key constraints while deleting the tables and use a single Drop command for all tables as once with CASCADE Option as below:
SET FOREIGN_KEY_CHECKS = 0;
DROP TABLE IF EXISTS Registered_Student,
IF EXISTS EducateYou_VideoRating,
IF EXISTS EducateYou_Video,
IF EXISTSEducateYou_Violation,
IF EXISTS EducateYou_Member,
IF EXISTS Violation_Type,
IF EXISTS EducateYou_Comment,
IF EXISTS EducateYou_FollowList,
IF EXISTS EducateYou_Group,
IF EXISTS EducateYou_Admin
CASCADE;
Alternatively, drop all the constraints first and then drop the tables.