MySQL "ERROR 1005 (HY000): Can't create table - mysql

Would appreciate help. Can not create the section table.
CREATE DATABASE university;
use university;
CREATE TABLE classroom(
building VARCHAR(20),
room_number INT,
capacity INT,
PRIMARY KEY (building,room_number)
) ;
CREATE TABLE Sales(
item VARCHAR(20),
color VARCHAR(20),
clothes_size VARCHAR(20),
quantity INT,
PRIMARY KEY (item,color,clothes_size,quantity)
);
CREATE TABLE Department (
dept_name VARCHAR(20),
building VARCHAR(20),
budget INT,
FOREIGN KEY (building) references classroom(building),
PRIMARY KEY (dept_name,building,budget)
);
CREATE TABLE Course (
course_id VARCHAR(20)PRIMARY KEY,
title VARCHAR(20),
dept_name VARCHAR(20),
credits INT,
FOREIGN KEY (dept_name) references Department(dept_name)
);
CREATE TABLE Instructor(
ID VARCHAR(20) PRIMARY KEY,
neme VARCHAR(20),
dept_name VARCHAR(20),
salary INT,
FOREIGN KEY (dept_name) references Department(dept_name)
);
CREATE TABLE section(
course_id VARCHAR(20),
sec_id INT,
semester VARCHAR(20),
year1 INT,
building1 VARCHAR(20),
room_number INT,
time_slot_id CHAR,
FOREIGN KEY (course_id) REFERENCES Course(course_id),
FOREIGN KEY (building1) references classroom(building),
FOREIGN KEY (room_number) references classroom(room_number),
PRIMARY KEY(course_id,sec_id,semester,year1)
);

Since you have two PK's on section table, you need to create one foreign key for both, not separate two:
CREATE TABLE section(
course_id VARCHAR(20),
sec_id INT,
semester VARCHAR(20),
year1 INT,
building1 VARCHAR(20),
room_number INT,
time_slot_id CHAR,
FOREIGN KEY (course_id) REFERENCES Course(course_id),
FOREIGN KEY (building1,room_number) references classroom(building,room_number),
PRIMARY KEY(course_id,sec_id,semester,year1)
);

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)
);

Cannot add foreign key constraint error 1215 HY000

I keep getting this error for my ENROLLS table, TAKES table, CONDUCTED_IN table, and EXAM table. when I am uploading my created tables to the database. I am not sure what the problem is can anyone help out? The COURSE, SECTION, CLASS_ROOM, and STUDENT tables were added to the database just fine the others have problems.
create table COURSE(
CourseNo varchar(10),
Course_Name varchar(30),
Department varchar(25),
primary key(CourseNo)
);
create table SECTION(
CourseNo varchar(10),
SectionNo varchar(2),
primary key(CourseNo, SectionNo),
foreign key(CourseNo) references COURSE(CourseNo)
);
create table ENROLLS(
SSN char(9),
SectionNo varchar(2),
CourseNo varchar(10),
primary key(SSN, CourseNo, SectionNo),
foreign key(SSN) references STUDENT(SSN),
foreign key(SectionNo) references SECTION(SectionNo),
foreign key(CourseNo) references SECTION(CourseNo)
);
create table STUDENT(
SSN char(9),
First_Name varchar(20),
Last_Name varchar(20),
Street varchar(15),
City varchar(15),
Zip char(5),
State varchar(20),
primary key(SSN)
);
create table TAKES(
SSN char(9),
CourseNo varchar(10),
SectionNo varchar(2),
ExamNo char(3),
Result char(1),
primary key(SSN, CourseNo, SectionNo, ExamNo),
foreign key(SSN) references STUDENT(SSN),
foreign key(CourseNo) references EXAM(CourseNo),
foreign key(SectionNo) references EXAM(SectionNo),
foreign key(ExamNo) references EXAM(ExamNo)
);
create table EXAM(
CourseNo varchar(10),
SectionNo varchar(2),
ExamNo char(3),
primary key(CourseNo, SectionNo, ExamNo),
foreign key(CourseNo) references SECTION(CourseNo),
foreign key(SectionNo) references SECTION(SectionNo)
);
create table CONDUCTED_IN(
RoomNo int,
Building varchar(30),
CourseNo varchar(10),
SectionNo varchar(2),
ExamNo char(3),
primary key(RoomNo, Building, CourseNo, SectionNo, ExamNo),
foreign key(RoomNo) references CLASS_ROOM(RoomNo),
foreign key(Building) references CLASS_Room(Building),
foreign key(CourseNo) references EXAM(CourseNo),
foreign key(SectionNo) references EXAM(SectionNo),
foreign key(ExamNo) references EXAM(ExamNo)
);
create table CLASS_ROOM(
RoomNo int,
Building varchar(30),
Capacity int,
primary key(RoomNo, Building)
);
The issues in your code are related to the management of relationships
tables must be created in sequence; when a table that refers to another table through a foreign key, it must be created after the parent table
most of your table have compound primary keys (ie primary keys that use multiple columns) - so you need compound foreign keys as well: rather than creating multiple foreign keys over each related column of the relation, you want an single, multi-column key that references the tuple of columns at once
With these base principles in mind, here is an updated version of your code that runs fine in this DB Fiddle.
create table COURSE(
CourseNo varchar(10),
Course_Name varchar(30),
Department varchar(25),
primary key(CourseNo)
);
create table CLASS_ROOM(
RoomNo int,
Building varchar(30),
Capacity int,
primary key(RoomNo, Building)
);
create table STUDENT(
SSN char(9),
First_Name varchar(20),
Last_Name varchar(20),
Street varchar(15),
City varchar(15),
Zip char(5),
State varchar(20),
primary key(SSN)
);
create table SECTION(
CourseNo varchar(10),
SectionNo varchar(2),
primary key(CourseNo, SectionNo),
foreign key(CourseNo) references COURSE(CourseNo)
);
create table ENROLLS(
SSN char(9),
SectionNo varchar(2),
CourseNo varchar(10),
primary key(SSN, CourseNo, SectionNo),
foreign key(SSN) references STUDENT(SSN),
foreign key(CourseNo, SectionNo) references SECTION(CourseNo, SectionNo)
);
create table EXAM(
CourseNo varchar(10),
SectionNo varchar(2),
ExamNo char(3),
primary key(CourseNo, SectionNo, ExamNo),
foreign key(CourseNo, SectionNo) references SECTION(CourseNo, SectionNo)
);
create table TAKES(
SSN char(9),
CourseNo varchar(10),
SectionNo varchar(2),
ExamNo char(3),
Result char(1),
primary key(SSN, CourseNo, SectionNo, ExamNo),
foreign key(SSN) references STUDENT(SSN),
foreign key(CourseNo, SectionNo, ExamNo) references EXAM(CourseNo, SectionNo, ExamNo)
);
create table CONDUCTED_IN(
RoomNo int,
Building varchar(30),
CourseNo varchar(10),
SectionNo varchar(2),
ExamNo char(3),
primary key(RoomNo, Building, CourseNo, SectionNo, ExamNo),
foreign key(RoomNo, Building) references CLASS_ROOM(RoomNo, Building),
foreign key(CourseNo, SectionNo, ExamNo) references EXAM(CourseNo, SectionNo, ExamNo)
);

Database issues errors not able to fix it

I am trying to crate a set of tables for my schema and I have used the following code. But I'm getting an error. I'm new to mysql and any help would be appreciated.
create table AutoSeller;
create table County(
CountyID INT,
CountyName varchar(25),
PRIMARY KEY (CountyID)
);
create table City (
CityID int,
CityName varchar(50),
CountyID int,
FOREIGN KEY (CountyID) REFERENCES county(CountyID),
PRIMARY KEY (CityID)
);
CREATE table Postcode(
PostcodeID int,
Postcode varchar(8),
CityID int,
FOREIGN KEY(CityID) REFERENCES city(CityID),
PRIMARY KEY (PostcodeID)
);
create table Buyer(
BuyerID int,
BuyerFN varchar(15),
BuyerLN varchar(15),
CountyID int,
CityID int,
Address varchar(30),
PostcodeID int,
ContactNr varchar(15),
FOREIGN KEY (PostcodeID) REFERENCES postcode (PostcodeID),
FOREIGN KEY (CityID) REFERENCES city (CityID),
FOREIGN KEY (CountyID) REFERENCES county (CountyID),
PRIMARY KEY (BuyerID)
);
create table Seller(
SellerID int,
SellerFN varchar(15),
SellerLN varchar(15),
CountyID int,
CityID int,
Address varchar(30),
PostcodeID int,
ContactNr varchar(15),
FOREIGN KEY (PostcodeID) REFERENCES postcode (PostcodeID),
FOREIGN KEY (CityID) REFERENCES city (CityID),
FOREIGN KEY (CountyID) REFERENCES county (CountyID),
PRIMARY KEY (SellerID)
);
create table Make(
CarMakeID int,
CarMake varchar(15),
PRIMARY KEY (CarMakeID)
);
create table Colour(
CarColourID int,
CarColour varchar(10),
PRIMARY KEY (CarColour)
);
create TABLE Fuel_type(
CarFuelID int,
CarFuel varchar(10),
PRIMARY KEY (CarFuelID)
);
create table Body(
CarBodyID int,
CarBody varchar (15),
PRIMARY KEY (CarBodyID)
);
CREATE TABLE car(
CarID int,
CarReg varchar(10),
CarMakeID int,
CarModel varchar(15),
CarColourID int,
CarBodyID int,
CarFuelID int,
CarAge int,
CarEngine int,
CarMileage int,
BuyerID int,
SellerID int,
PRIMARY KEY (CarID),
FOREIGN KEY (CarMakeID) REFERENCES make (CarMakeID),
FOREIGN KEY (CarColourID) REFERENCES colour (CarColourID),
FOREIGN KEY (CarFuelID) REFERENCES fuel_type (CarFuelID),
FOREIGN KEY (BuyerID) REFERENCES buyer (BuyerID),
FOREIGN KEY (SellerID) REFERENCES seller (SellerID)
);
I just start learning mySql and I am struggling with this error.
1005 - Can't create table autoseller.car (errno: 150 "Foreign key constraint is incorrectly formed")
Can somebody help me?
This is the problem:
create table Colour(
CarColourID int,
CarColour varchar(10),
PRIMARY KEY (CarColour)
);
The primary key should be CarColourID, not CarColour. The error you're getting is because the column referenced in a foreign key has to be indexed. Since you didn't make CarColourID the primary key, it doesn't have an index.

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.

Prescriptions-r-x chain of pharmacies E-R diagram to SQL

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