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
Related
I created hotel, room, guest tables. After I created booking table I can't add Foreign key. It makes error.
Can't create table hotelbooking.booking (errno: 150 "Foreign key constraint is incorrectly formed")
CREATE TABLE Booking (
gId int,
hId int,
RoomNo int,
FromDate Date,
ToDate Date,
PaymentMethod varchar(100),
FOREIGN KEY (hid) REFERENCES Hotel(hid),
FOREIGN KEY (gid) REFERENCES Guest(gid),
FOREIGN KEY (RoomNo) REFERENCES Room(RoomNo),
CONSTRAINT PK_bookingRoom PRIMARY KEY (gId, hId, RoomNo, FromDate)
);
CREATE DATABASE hotelBooking;
CREATE TABLE Hotel (
hId int,
Name varchar(100),
Address varchar(255),
City varchar(200),
ContactNumber int(10),
PRIMARY KEY (hid)
);
CREATE TABLE Guest (
gId int,
firstName varchar(100),
lastName varchar(100),
Age int,
Gender varchar(2),
Address varchar(255),
City varchar(50),
ContactNumber int(10),
PRIMARY KEY (gid)
);
CREATE TABLE Room (
hid int,
RoomNo int,
RoomType varchar(100),
Price varchar(50),
FOREIGN KEY (hid) REFERENCES Hotel(hid),
CONSTRAINT PK_roomId PRIMARY KEY (hid, RoomNo)
);
You can define a combined foreign key that fits with teh primary key from Room
CREATE TABLE Room (
hid int,
RoomNo int,
RoomType varchar(100),
Price varchar(50),
FOREIGN KEY (hid) REFERENCES Hotel(hid),
CONSTRAINT PK_roomId PRIMARY KEY (hid, RoomNo)
);
CREATE TABLE Booking (
gId int,
hId int,
rhId int,
RoomNo int,
FromDate Date,
ToDate Date,
PaymentMethod varchar(100),
FOREIGN KEY (hid) REFERENCES Hotel(hid),
FOREIGN KEY (gid) REFERENCES Guest(gid),
FOREIGN KEY (rhid,RoomNo) REFERENCES Room(hid,RoomNo),
CONSTRAINT PK_bookingRoom PRIMARY KEY (gId, hId, RoomNo, FromDate)
);
Or you define an idex on roomNo
CREATE TABLE Room (
hid int,
RoomNo int,
RoomType varchar(100),
Price varchar(50),
FOREIGN KEY (hid) REFERENCES Hotel(hid),
KEY(RoomNo),
CONSTRAINT PK_roomId PRIMARY KEY (hid, RoomNo)
);
CREATE TABLE Booking (
gId int,
hId int,
RoomNo int,
FromDate Date,
ToDate Date,
PaymentMethod varchar(100),
FOREIGN KEY (hid) REFERENCES Hotel(hid),
FOREIGN KEY (gid) REFERENCES Guest(gid),
FOREIGN KEY (RoomNo) REFERENCES Room(RoomNo),
CONSTRAINT PK_bookingRoom PRIMARY KEY (gId, hId, RoomNo, FromDate)
);
You seem to be trying to create the Booking table with foreign key references to the Hotel, Guest, and Room tables, but if you create Booking first, then those tables don't exist yet.
You need to create the other tables first, then create the Booking table that references them.
Or as an alternative, create Booking, but without declaring its foreign keys. Then create the other tables. Then use ALTER TABLE Booking to add foreign key constraints to that table.
However you do it, foreign keys can't reference a table before you create those tables.
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'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)
)
I'm not sure why the SQL isn't working for the Class table. I know the problem has to do with the foreign key line, if I remove that the table will be created. Is there something wrong with my syntax?
The error I receive is: Error Code: 1005. Can't create table 'university.class' (errno: 150)
CREATE TABLE Faculty (
facId VARCHAR(6),
name VARCHAR(20) NOT NULL,
department VARCHAR(20),
rank VARCHAR(10),
CONSTRAINT Faculty_facId_pk PRIMARY KEY (facId));
DROP TABLE Class;
CREATE TABLE Class (
classNumber VARCHAR(8),
facId VARCHAR(6) NOT NULL,
schedule VARCHAR(8),
room VARCHAR(6),
CONSTRAINT Class_classNumber_pk PRIMARY KEY (classNumber),
CONSTRAINT Class_facId_fk FOREIGN KEY (facId) REFERENCES Faculty (facId) ON DELETE SET NULL,
CONSTRAINT Class_schedule_room_uk UNIQUE (schedule, room));
You have ON DELETE SET NULL but Class.facID does not allow nulls so it would not be possible.
This works and deletes the parent record leaving the column NULL in the orphan.
CREATE TABLE Faculty (
facId VARCHAR(6),
name VARCHAR(20) NOT NULL,
department VARCHAR(20),
rank VARCHAR(10),
CONSTRAINT Faculty_facId_pk PRIMARY KEY (facId));
CREATE TABLE Class (
classNumber VARCHAR(8),
facId VARCHAR(6) NULL,
schedule VARCHAR(8),
room VARCHAR(6),
CONSTRAINT Class_classNumber_pk PRIMARY KEY (classNumber),
CONSTRAINT Class_facId_fk FOREIGN KEY (facId) REFERENCES Faculty (facId) ON DELETE set null,
CONSTRAINT Class_schedule_room_uk UNIQUE (schedule, room));
INSERT INTO Faculty
VALUES('1','Foo','foo','foo');
INSERT INTO Class
VALUES('1','1','foo','foo');
DELETE FROM Faculty;
Foreign key should not be NULL
CREATE TABLE Class (
classNumber VARCHAR(8),
facId VARCHAR(6) NOT NULL,
schedule VARCHAR(8),
room VARCHAR(6),
CONSTRAINT Class_classNumber_pk PRIMARY KEY (classNumber),
CONSTRAINT Class_facId_fk FOREIGN KEY (facId) REFERENCES Faculty (facId) ,
CONSTRAINT Class_schedule_room_uk UNIQUE (schedule, room))