Unable to create relationship 'FK_PATIENTS_VICTIMS' - mysql

MySQL says:
the alter table statement conflicted with the FOREIGN KEY constraint "FK_PATIENTS_VICTIMS". The conflict occurred in the database " my_database", table "dbo.victims", column "victim_id'.
I tried to alter the table by inputting;
ALTER TABLE PATIENTS WITH NOCHECK ADD CONSTRAINT [FK_PATIENTS_VICTIMS] FOREIGN KEY(VICTIM_ID) REFERENCES VICTIM(ID)
Patients table
create table patients(
victim_id int(4),
blood_type varchar(4),
vaccinations varchar(100),
assigned_practitioner int(10),
hospital_id int(10),
insurance_id int(10),
symptoms varchar(200);
Victims table
create table victims(
victim_id int(4) primary key,
first_name varchar(20),
last_name varchar(20),
sex char(1) constraint persons_sex_ck check(sex IN('F','M')),
PHONE_NUMBER varchar(20),
DOB varchar(13),
address varchar(30),
city varchar(20),
zip int(5),
state varchar(5)
);

You have:
REFERENCES VICTIM(ID)
You need:
REFERENCES VICTIMS(Victim_ID)

Related

mysql key column doesnt exist in table

CREATE TABLE DONOR
(
donor_id int,
medical_history varchar(20),
donor_name varchar(50),
blood_group char(2),
address varchar(80),
contact_number int,
PRIMARY KEY (donor_id)
);
CREATE TABLE RECEPTIONIST
(
recep_id varchar(10),
recep_name varchar(50),
phone_number int,
donor_id int,
PRIMARY KEY (recep_id),
FOREIGN KEY (donor_id) REFERENCES DONOR (donor_id)
);
CREATE TABLE HOSPITAL
(
hospital_id varchar(10),
hospital_name varchar(50),
hospital_address varchar(80),
PRIMARY KEY (hospital_id)
);
CREATE TABLE BLOOD_BANK
(
blood_bank_id varchar(10),
blood_group char(2),
stocks int,
PRIMARY KEY (blood_bank_id),
FOREIGN KEY (recep_id) REFERENCES RECEPTIONIST (recep_id),
FOREIGN KEY (hospital_id) REFERENCES HOSPITAL (hospital_id)
);
CREATE TABLE BLOOD
(
blood_code varchar(10),
blood_group char(2),
expired_date date,
PRIMARY KEY (blood_code)
);
CREATE TABLE PATIENT
(
patient_id varchar(10),
patient_name varchar(50),
contact_number int,
blood_group char(2),
address varchar(80),
PRIMARY KEY(patient_id),
FOREIGN KEY (hospital_id) REFERENCES HOSPITAL (hospital_id)
);
hi, i wrote this for my project in mysql and i dont understand why it said
Key column 'recep_id' doesn't exist
in table when i want to execute the code
it said that i need to define it in receptionist table but i already defined it
CREATE TABLE RECEPTIONIST
(
recep_id varchar(10),
the entity relationship between blood bank and receptionist is one receptionist works at a blood bank.
The problem is this line:
FOREIGN KEY (recep_id) REFERENCES RECEPTIONIST (recep_id),
while creating the table BLOOD_BANK.
The table BLOOD_BANK does not have the column recep_id which you reference in the statement. You ave to add the column to the table BLOOK_BANK
CREATE TABLE BLOOD_BANK
(
blood_bank_id varchar(10),
blood_group char(2),
stocks int,
recep_id varchar(10),
hospital_id varchar(10),
PRIMARY KEY (blood_bank_id),
FOREIGN KEY (recep_id) REFERENCES RECEPTIONIST (recep_id),
FOREIGN KEY (hospital_id) REFERENCES HOSPITAL (hospital_id)
);
Same for the hospital_id in the table BLOOD_BANK and PATIENT
CREATE TABLE PATIENT
(
patient_id varchar(10),
patient_name varchar(50),
contact_number int,
blood_group char(2),
address varchar(80),
hospital_id varchar(10),
PRIMARY KEY(patient_id),
FOREIGN KEY (hospital_id) REFERENCES HOSPITAL (hospital_id)
);

SQL Fiddle Giving "cannot add foreign key constraint" error

I am trying to figure out what is wrong with my syntax here. I feel like I have matched the examples I have found over the internet... but something must be amiss. Here is the code I am using. I keep getting the error code "cannot add Foreign Key constraint" when trying to run the build the schema.
CREATE TABLE Employe (
employee_id INT PRIMARY KEY,
first_name VARCHAR(30),
last_name VARCHAR(30),
hire_date DATE,
job_title VARCHAR(30),
shop_id INT,
FOREIGN KEY (shop_id) REFERENCES Coffee_Shop(shop_id)
);
CREATE TABLE Coffee_Shop (
shop_id INT PRIMARY KEY,
shop_name VARCHAR(50),
state CHAR(2),
);
CREATE TABLE Coffee (
coffee_id INT PRIMARY KEY,
shop_id INT,
supplier_id INT,
coffee_name VARCHAR(30),
price_per_pound NUMERIC(5,2),
FOREIGN KEY (shop_id) REFERENCES Coffee_Shop(shop_id),
FOREIGN KEY (supplier_id) REFERENCES Supplier(supplier_id)
);
CREATE TABLE Supplier (
supplier_id INT PRIMARY KEY,
company_name VARCHAR(50),
country VARCHAR(30),
sales_contact_name VARCHAR(60),
email VARCHAR(50) NOT NULL,
);
I just need to figure out what part of my foreign key is incorrectly defined. I know it normally results from an improperly defined primary key, but cannot seem to find where I messed up.
Thanks for your help
You are referencing a KEY that is not set up yet. SQL is linear, so the code gets executed line-by-line and cannot reference future lines (in most cases)
Create your tables in this order:
CREATE TABLE Coffee_Shop (
shop_id INT PRIMARY KEY,
shop_name VARCHAR(50),
state CHAR(2),
);
CREATE TABLE Supplier (
supplier_id INT PRIMARY KEY,
company_name VARCHAR(50),
country VARCHAR(30),
sales_contact_name VARCHAR(60),
email VARCHAR(50) NOT NULL,
);
CREATE TABLE Employe (
employee_id INT PRIMARY KEY,
first_name VARCHAR(30),
last_name VARCHAR(30),
hire_date DATE,
job_title VARCHAR(30),
shop_id INT,
FOREIGN KEY (shop_id) REFERENCES Coffee_Shop(shop_id)
);
CREATE TABLE Coffee (
coffee_id INT PRIMARY KEY,
shop_id INT,
supplier_id INT,
coffee_name VARCHAR(30),
price_per_pound NUMERIC(5,2),
FOREIGN KEY (shop_id) REFERENCES Coffee_Shop(shop_id),
FOREIGN KEY (supplier_id) REFERENCES Supplier(supplier_id)
);

Error 1005 (HY000): Can't create table db.Wine (errno: 150)

I have this code to create some tables:
CREATE TABLE Appelations (
No int AUTO_INCREMENT,
Appelation varchar(35),
County varchar(20),
State varchar(15),
Area varchar(25),
IsAVA varchar(3),
PRIMARY KEY (No)
);
CREATE TABLE Grapes (
ID int AUTO_INCREMENT,
Grape varchar(25),
Color varchar(10),
PRIMARY KEY (ID)
);
CREATE TABLE Wine (
No int AUTO_INCREMENT,
Grape varchar(25),
Winery varchar(40),
Appelation varchar(35),
Name varchar(40),
Year year,
Price int,
Score int,
Cases int,
PRIMARY KEY (No),
FOREIGN KEY (Grape) REFERENCES Grapes(Grape),
FOREIGN KEY (Appelation) REFERENCES Appelations(Appelation)
);
But when I run it, I get this error:
ERROR 1005 (HY000): Can't create table 'db.Wine' (errno: 150)
Does anyone know how to fix this? Thanks.
You need indexes on your foreign keys: "MySQL requires indexes on foreign keys and referenced keys so that foreign key checks can be fast and not require a table scan."
Example here: http://sqlfiddle.com/#!9/d228b8
CREATE TABLE Appelations (
No int AUTO_INCREMENT,
Appelation varchar(35),
County varchar(20),
State varchar(15),
Area varchar(25),
IsAVA varchar(3),
INDEX appelation_ind (Appelation), /*<---*/
PRIMARY KEY (No)
);
CREATE TABLE Grapes (
ID int AUTO_INCREMENT,
Grape varchar(25),
Color varchar(10),
INDEX grape_ind (Grape), /*<---*/
PRIMARY KEY (ID)
);
CREATE TABLE Wine (
No int AUTO_INCREMENT,
Grape varchar(25),
Winery varchar(40),
Appelation varchar(35),
Name varchar(40),
Year year,
Price int,
Score int,
Cases int,
PRIMARY KEY (No),
FOREIGN KEY (Grape) REFERENCES Grapes(Grape),
FOREIGN KEY (Appelation) REFERENCES Appelations(Appelation)
);
That's because the FOREIGH KEY columns in the table are referencing the columns that are neither primary keys nor have indexes on them (in parent tables). You can change the type and make them point to ID and No columns of Grapes and Appelations tables respectively, e.g.:
CREATE TABLE Wine (
No int AUTO_INCREMENT,
Grape_id int,
Winery varchar(40),
Appelation_no int,
Name varchar(40),
Year year,
Price int,
Score int,
Cases int,
PRIMARY KEY (No),
FOREIGN KEY (Grape_id) REFERENCES Grapes(id),
FOREIGN KEY (Appelation_no) REFERENCES Appelations(No)
);
Here's the SQL Fiddle.

mysql error 1064 when creating table. What can I check for?

I'm getting an ERROR 1064 (42000) at line 21, which is VIN_Number in Vehicle table. Entity Inventory gets built without a problem, VIN_Number is of type varchar(17) for all occurrences. I believe the tables are being built in the correct order. I can't find any spelling or punctuation errors. I'm out of ideas of things I should checked. What is it that I'm missing?
Note: I getting an error for Invoice as well, but I know that it can't be created until Vehicle gets created.
ALTER TABLE Vehicle DROP FOREIGN KEY fk_Veh_Vehicle_TypeID;
ALTER TABLE Inventory DROP FOREIGN KEY fk_Inv_Vehicle_TypeID;
ALTER TABLE Invoice DROP FOREIGN KEY fk_Customer_ID;
ALTER TABLE Invoice DROP FOREIGN KEY fk_Sales_Person_ID;
ALTER TABLE Invoice DROP FOREIGN KEY fk_VIN_Number;
DROP TABLE IF EXISTS Vehicle, VehicleType,
Invoice, Customer, SalesPerson, Inventory;
CREATE TABLE VehicleType (
Vehicle_TypeID int NOT NULL,
Veh_Make varchar(15),
Veh_Model varchar(15),
Veh_Year int,
PRIMARY KEY (Vehicle_TypeID)
) Engine=InnoDB;
CREATE TABLE Vehicle (
VIN_Number varchar(17) NOT NULL,
Vehicle_TypeID int NOT NULL,
Condition varchar(10),
Color varchar(8),
PRIMARY KEY (VIN_Number),
CONSTRAINT fk_Veh_Vehicle_TypeID FOREIGN KEY(Vehicle_TypeID)
REFERENCES VehicleType(Vehicle_TypeID)
) Engine=InnoDB;
CREATE TABLE Inventory (
Stock_ID int NOT NULL,
Vehicle_TypeID int NOT NULL,
Quantity int,
PRIMARY KEY (Stock_ID),
CONSTRAINT fk_Inv_Vehicle_TypeID FOREIGN KEY (Vehicle_TypeID)
REFERENCES VehicleType(Vehicle_TypeID)
) Engine=InnoDB;
CREATE TABLE Customer (
Customer_ID varchar(10) NOT NULL,
Cus_LastName varchar(15),
Cus_FirstName varchar(15),
Cus_Street varchar(20),
Cus_City varchar(15),
Cus_Zip varchar(5),
Cus_Phone varchar(10),
PRIMARY KEY (Customer_ID)
) Engine=InnoDB;
CREATE TABLE SalesPerson (
Sales_Person_ID varchar(10) NOT NULL,
Sal_LastName varchar(15),
Sal_FirstName varchar(15),
Sal_Street varchar(15),
Sal_City varchar(15),
Sal_Zip varchar(5),
Sal_Phone varchar(10),
Sal_Years_Worked int,
Sal_Commission_Rate float(4),
PRIMARY KEY (Sales_Person_ID)
) Engine=InnoDB;
CREATE TABLE Invoice (
Invoice_ID varchar(10) NOT NULL,
Customer_ID varchar(10) NOT NULL,
Sales_Person_ID varchar(10) NOT NULL,
VIN_Number varchar(17) NOT NULL,
Price float(10),
PRIMARY KEY (Invoice_ID),
CONSTRAINT fk_Customer_ID FOREIGN KEY (Customer_ID)
REFERENCES Customer(Customer_ID),
CONSTRAINT fk_Sales_Person_ID FOREIGN KEY (Sales_Person_ID)
REFERENCES SalesPerson(Sales_Person_ID),
CONSTRAINT fk_VIN_Number FOREIGN KEY (VIN_Number)
REFERENCES Vehicle(VIN_Number)
) Engine=InnoDB;
In the definition of the Vehicle table you have a column named condition which is a reserved keyword in MySQL (reference). Either use another name for the column or enclose it in backticks like this: `condition`
Using keywords (reserved or not) for object names is generally something you want to avoid.

Cannot make 2 foreign keys, "errno: 150"

I'm working on an assignment and came across an error making foreign keys on one of them.
First table:
CREATE TABLE MANUFACTURERS
(
Manufacturers_ID int(5) UNIQUE,
Name varchar(15) UNIQUE,
City varchar(30),
State char(2),
Zip char(5),
Phone char(10),
PRIMARY KEY (Manufacturers_ID)
)
ENGINE= innodb;
Second Table
CREATE TABLE OWNERS
(
Owner_ID int(10),
First_Name varchar(15),
Last_Name varchar(15),
City varchar(30),
State char(2),
Gender varchar(1),
Date_of_Birth varchar(10),
PRIMARY KEY (Owner_ID)
)
ENGINE= innodb;
Third table
CREATE TABLE CARS
(
Vehicle_Identification_Number int(17) NOT NULL UNIQUE,
Manufacturers_ID int(5) UNIQUE,
Owner_ID int(10) UNIQUE,
Model varchar(25),
Manufaturer_Year int(4),
Mileage int(10),
Price int(10),
PRIMARY KEY (Vehicle_Identification_Number),
FOREIGN KEY (Manufacturers_ID) REFERENCES MANUFACTURERS (Manufactures_ID),
FOREIGN KEY (Owner_ID) REFERENCES OWNERS (Owner_ID)
)
ENGINE= innodb;
When I try to execute the third table, I get the message:
"An error has occurred while executing your SQL command.
ERROR: Can't create table 'ds0004.CARS' (errno: 150)"
Any help would be greatly appreciated.
You misspelled the foreign reference in CARS. This is the correct version:
CREATE TABLE CARS
(
Vehicle_Identification_Number int(17) NOT NULL UNIQUE,
Manufacturers_ID int(5) ,
Owner_ID int(10) ,
Model varchar(25),
Manufaturer_Year int(4),
Mileage int(10),
Price int(10),
PRIMARY KEY (Vehicle_Identification_Number),
FOREIGN KEY (Manufacturers_ID) REFERENCES MANUFACTURERS (Manufacturers_ID),
--------------------------------------------------------------------^
FOREIGN KEY (Owner_ID) REFERENCES OWNERS (Owner_ID)
)