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)
);
Related
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)
);
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)
);
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.
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 am currently working with a MySQL database table structure. I found a table structure online but i am not sure how to duplicate the exact tables structure. These tables share foreign keys and index with each other. I have attached a link that shows the tables are setup in the model I would like to duplicate. I am requesting help in creating a query that will create all tables with their correlated data. LINK- TABLE STRUCTURE
Foreign keys
semester_id
faculty_id
schedule_id
section_id
class_id
major_minor_id
CREATE TABLE event
(
ID INT,
event_description VARCHAR(30),
event_heading VARCHAR(30),
event_photo BLOB,
event_datetime VARCHAR(30),
CONSTRAINT event_pk PRIMARY KEY (ID)
);
CREATE TABLE semester
(
ID INT,
Semester_Name VARCHAR(25),
CONSTRAINT sem_pk PRIMARY KEY (ID),
CONSTRAINT sem_uq UNIQUE (semester_Name)
);
CREATE TABLE Major_Minor
(
ID INT,
Major_Minor_Name VARCHAR(30),
Major_Minor_Description VARCHAR(50),
CONSTRAINT mm_pk PRIMARY KEY (ID),
CONSTRAINT mm_uq UNIQUE (Major_Minor_Name)
);
CREATE TABLE class
(
ID INT,
class_caption VARCHAR(30),
class_name VARCHAR(30),
class_description VARCHAR(30),
class_credit_hours INT,
CONSTRAINT cc_pk PRIMARY KEY (ID),
CONSTRAINT cc_uq UNIQUE (class_caption)
);
CREATE TABLE Major_Class_br
(
ID INT,
Class_ID INT,
Major_Minor_ID INT,
CONSTRAINT mbr_pk PRIMARY KEY (ID),
CONSTRAINT cc_fk1 FOREIGN KEY (Class_ID) REFERENCES class(ID),
CONSTRAINT cc_fk2 FOREIGN KEY (Major_Minor_ID) REFERENCES Major_Minor(ID)
);
CREATE TABLE Faculty
(
ID INT,
faculty_fName VARCHAR(30),
faculty_lname VARCHAR(30),
faculty_bio VARCHAR(100),
faculty_pic_path VARCHAR(30),
CONSTRAINT f_pk PRIMARY KEY (ID),
CONSTRAINT cc_uq UNIQUE (faculty_fName, faculty_lname)
);
CREATE TABLE Faculty_Titles
(
ID INT,
faculty_ID INT,
faculty_title VARCHAR(30),
CONSTRAINT ft_pk PRIMARY KEY (ID),
CONSTRAINT ft_fk1 FOREIGN KEY (faculty_ID) REFERENCES Faculty(ID)
);
CREATE TABLE Faculty_Education
(
ID INT,
faculty_ID INT,
faculty_ed VARCHAR(30),
CONSTRAINT fe_pk PRIMARY KEY (ID),
CONSTRAINT fe_fk1 FOREIGN KEY (faculty_ID) REFERENCES faculty(ID)
);
CREATE TABLE Section
(
ID INT,
Class_ID INT,
faculty_ID INT,
section_number INT,
section_callnumber INT,
CONSTRAINT ss_pk PRIMARY KEY (ID),
CONSTRAINT ss_fk1 FOREIGN KEY (Class_ID) REFERENCES class(ID),
CONSTRAINT ss_fk2 FOREIGN KEY (faculty_ID) REFERENCES faculty(ID)
);
CREATE TABLE Schedule
(
ID INT,
Semester_ID INT,
schedule_start_time DATETIME,
schedule_end_time DATETIME,
schedule_monday TINYINT(1),
schedule_tuesday TINYINT(1),
schedule_wednesday TINYINT(1),
schedule_thursday TINYINT(1),
schedule_friday TINYINT(1),
CONSTRAINT sc_pk PRIMARY KEY (ID),
CONSTRAINT sc_fk1 FOREIGN KEY (Semester_ID) REFERENCES semester(ID)
);
CREATE TABLE Office_Hours
(
ID INT,
faculty_ID INT,
schedule_ID INT,
CONSTRAINT oh_pk PRIMARY KEY (ID),
CONSTRAINT oh_fk1 FOREIGN KEY (schedule_ID) REFERENCES Schedule(ID),
CONSTRAINT oh_fk2 FOREIGN KEY (faculty_ID) REFERENCES faculty(ID)
);
CREATE TABLE sched_sect_br
(
ID INT,
schedule_ID INT,
section_ID INT,
room_number INT,
CONSTRAINT ssb_pk PRIMARY KEY (ID),
CONSTRAINT ssb_fk1 FOREIGN KEY (schedule_ID) REFERENCES Schedule(ID),
CONSTRAINT ssb_fk2 FOREIGN KEY (section_ID) REFERENCES Section(ID)
);
SQLFiddle Demo