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)
)
Related
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)
ER Diagram -
Hi Everyone , i started learning Database few weeks ago , and had this exercise today . I have to convert E-R diagram to SQL . Can someone check my code if its ok ?
I am confused at part of weak entity DRUG .
create database Ilacet
go
use ilacet
go
--definimi i tabelave
create table Patient
(
ssn varchar(10),
phy_ssn varchar(10),
age tinyint,
name varchar(20) not null,
address varchar(25),
constraint SSN_PK primary key (ssn),
constraint PHY_FK foreign key (phy_ssn) references Doctor
)
go
create table Doctor
(
phy_ssn varchar(10),
speciality varchar(20) not null,
name varchar(20) not null,
exp_year tinyint(2),
constraint PHY_SSN_PK primary key (phy_ssn)
)
go
create table Pharmacy
(
name varchar(20),
address varchar(20) not null,
phone_number varchar(20) not null,
constraint NAME_PK primary key (name)
)
go
create table Pharm_co
(
pname varchar(20),
phone_num varchar(15) not null,
constraint PNAME_PK primary key(pname)
)
go
create table Drug
(
trade_name varchar(20),
pname varchar(20),
formula varchar(20),
constraint D_PK primary key(trade_name,pname),
constraint DP_S foreign key (pname) references Pharm_co
)
go
create table Prescription
(
ssn varchar(10),
phy_ssn varchar(20),
trade_name varchar(20),
pname varchar(10)
data date,
quantity integer,
constraint PK_PK primary key(ssn,phy_ssn,trade_name,pname),
constraint Pn_F foreign key (ssn) references Patient,
constraint DO_F foreign key(phy_ssn) references Doctor,
constraint PO_F foreign key(pname) references Pharm_co,
constraint DR_F foreign key(trade_name) references Drug
)
go
create table Contract
(
name varchar(20),
pname varchar(20),
start_date date,
end_date date,
text varchar(15),
supervisor varchar(15),
constraint PN_PK primary key(name,pname)
constraint NAME_P foreign key (name) references Pharmacy,
constraint P_N foreign key(pname)references Pharm_co
)
go
create table Sell
(
name varchar(20),
trade_name varchar(20)
pname varchar(20),
price integer,
constraint PK_KK primary key(name,trade_name,pname) ,
constraint Pharmacy_name foreign key (name) references Pharmacy,
constraint Drug_name foreign key(trade_name)references Drug,
constraint Pharm_id foreign key(pname) references Pharm_co
)
go
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.
I'm trying to create a database, i have one table in my database and want to add the other one.
The table i have:
create table department(
dept_name varchar(20),
building varchar(20) not null,
budget numeric(8,2),
primary key (dept_name)
)
and i want to add this table:
create table student (
ID varchar(5),
name varchar(20) not null,
dept_name varchar(20),
tot_cred numeric(3,0),
primary key (ID),
foreign key (dept_name) references department
)
But i get this error:
#1215 - Cannot add foreign key constraint
Can you guys help me with this problem?
Thanks.
Your 2nd table should be-
create table student (
ID varchar(5),
name varchar(20) not null,
dept_name varchar(20),
tot_cred numeric(3,0),
primary key (ID),
foreign key (dept_name) references department(dept_name)
)
Note: It will be better include dept_id as int in your parent table and create reference it in 2nd table to get better performance.
create tables as per below-
create table department(
dept_id int auto_increment,
dept_name varchar(20),
building varchar(20) not null,
budget numeric(8,2),
primary key (dept_id)
);
create table student (
ID varchar(5),
name varchar(20) not null,
dept_it int,
tot_cred numeric(3,0),
primary key (ID),
foreign key (dept_id) references department(dept_id)
)
I have two tables which i want to join with an intermediary table for a many to many relationship
the first table is booking
CREATE TABLE booking (
bookingID INT NOT NULL AUTO_INCREMENT,
customerID INT,
runID INT,
startDate DATE,
endDate DATE,
dateBookedOn TIMESTAMP DEFAULT CURRENT_TIMESTAMP,
PRIMARY KEY (bookingID),
INDEX idx_start (startDate),
INDEX idx_end (endDate),
FOREIGN KEY (runID) REFERENCES Run(RunID),
FOREIGN KEY (customerID) REFERENCES customer(CustomerID));
the second is dog
CREATE TABLE dog(
DogID int(6) NOT NULL,
DogName varchar(15),
medicalID int (6),
Gender character(1) check(gender in ('m', 'f')),
Age int(2),
Breed varchar(15),
size character (1) check(size in ('s', 'm', 'l')),
primary key (DogID));
and the intermediary table is as follows which gives an error 1005(150) there is something wrong with the foreign key for bookingID. But can't see the problem. any help is welcomed.
CREATE TABLE isBooked(
BookingID int not null,
DogID int not null,
foreign key (DogID) references Dog(dogID),
foreign key (BookingID) references Booking(BookingID));