I am trying to create 5 tables (should not be too hard), but I am having issues assigning the foreign keys. PhpMyAdmin gives me this error:
Can't create table 'databasexx.gave' (errno: 150)
Basically all the tables with no foreign keys are created.
DROP TABLE IF EXISTS oppbygging;
CREATE TABLE oppbygging (
gnr INT,
dnr INT,
ant INT,
CONSTRAINT dnr_grn_pk PRIMARY KEY (gnr, dnr)
) ENGINE=InnoDB;
DROP TABLE IF EXISTS onske;
CREATE TABLE onske (
onr INT,
pnr INT,
gnr INT,
prioriet INT,
ferdig INT,
CONSTRAINT pnr_gnr_pk PRIMARY KEY (pnr, gnr)
) ENGINE=InnoDB;
DROP TABLE IF EXISTS person;
CREATE TABLE person (
pnr INT,
fornavn VARCHAR(64),
etternavn VARCHAR(64),
fdato DATE,
CONSTRAINT pnr_pk PRIMARY KEY (pnr),
CONSTRAINT person_pnr_fk FOREIGN KEY (pnr) REFERENCES onske(pnr)
) ENGINE=InnoDB;
DROP TABLE IF EXISTS gave;
CREATE TABLE gave
(
gnr int,
navn varchar (255) UNIQUE,
prod_tid int NOT NULL,
CONSTRAINT gnr_pk PRIMARY KEY (gnr),
CONSTRAINT gave_gnr_fk FOREIGN KEY (gnr) REFERENCES oppbygging(gnr),
CONSTRAINT gave_gnr_fk FOREIGN KEY (gnr) REFERENCES onske(gnr)
) ENGINE=InnoDB;
DROP TABLE IF EXISTS del;
CREATE TABLE del (
dnr INT,
navn VARCHAR(64),
lager_ant INT NOT NULL,
CONSTRAINT dnr_pk PRIMARY KEY (dnr),
CONSTRAINT del_dnr_fk FOREIGN KEY (dnr) REFERENCES oppbygging(dnr)
) ENGINE=InnoDB;
I am sure that I am making some sort of obvious mistake, but I just cannot figure it out. Any help would be very much appreciated.
Here is the relationship view, ignore the Eiendom table:
TO CREATE ANOTHER TABLE WITH A FOREIGN KEY, THE FOREIGN KEY
SPECIFIED MUST BE THE PRIMARY KEY OF AT LEAST ONE TABLE
THAT HAS ALREADY BEEN CREATED,E.G.
CREATE TABLE 1 WITH THE PRIMARY KEY:
CREATE TABLE PERSONS(
PERSON_ID INT(10),
FNAME VARCHAR(20)NOT NULL,
LNAME VARCHAR(20),
DOB DATETIME,
AMOUNTPAID DECIMAL(15,2),
PRIMARY KEY(PERSON_ID)
);
THEN CREATE TABLE 2 WITH THE FOREIGN KEY:
CREATE TABLE COMMENTS(
COMMENTID INT AUTO_INCREMENT,
COMMENTS VARCHAR(500),
PERSON_ID INT(10),
PRIMARY KEY(COMMENTID),
FOREIGN KEY (PERSON_ID) REFERENCES PERSONS(PERSON_ID)
);
Related
I was creating a database for a school project and I have this issue while doing it.
I'm using MySQL workbench 8.0.29
This is the database structure i'm following
Database_design
And the SQL:
CREATE DATABASE AEROPORTS;
USE AEROPORTS;
CREATE TABLE PILOTS (
IDENTIFICADOR INT,
NOM VARCHAR(15),
COGNOMS VARCHAR(30),
HORES_VOL INT,
PRIMARY KEY (IDENTIFICADOR)
)engine=innodb;
CREATE TABLE AEROPORTS (
NOM VARCHAR(20),
CIUTAT VARCHAR(20),
PRIMARY KEY (NOM)
)engine=innodb;
CREATE TABLE COMPANYIES (
IDENTIFICADOR INT,
NOM VARCHAR(20),
NACIONALITAT VARCHAR(20),
LOGO varbinary(50),
PRIMARY KEY (IDENTIFICADOR)
)engine=innodb;
CREATE TABLE VOLS (
COMPANYIA INT,
NUMERO_VOL INT,
SORTIDA DATETIME,
ARRIBADA DATETIME,
ORIGEN VARCHAR(20),
DESTI VARCHAR(20),
PRIMARY KEY (COMPANYIA, NUMERO_VOL),
FOREIGN KEY (COMPANYIA) REFERENCES COMPANYIES (IDENTIFICADOR),
FOREIGN KEY (DESTI) REFERENCES AEROPORTS (NOM)
)engine=innodb;
CREATE TABLE PASSATGERS (
COMPANYIA INT,
VOL INT,
NOM VARCHAR(15),
COGNOMS VARCHAR(30),
CLASSE VARCHAR(15),
PRIMARY KEY (COMPANYIA, VOL, NOM, COGNOMS),
FOREIGN KEY (COMPANYIA) REFERENCES VOLS (COMPANYIA),
FOREIGN KEY (VOL) REFERENCES VOLS (NUMERO_VOL)
)engine=innodb;
CREATE TABLE PILOTAR (
COMPANYIA INT,
VOL INT,
PILOT INT,
PRIMARY KEY (COMPANYIA, VOL, PILOT),
FOREIGN KEY (COMPANYIA) REFERENCES VOLS (COMPANYIA),
FOREIGN KEY (PILOT) REFERENCES PILOTS (IDENTIFICADOR)
)engine=innodb;
CREATE TABLE AVIONS (
NUMERO_AVIO INT,
HORES_VOLS DATETIME,
PLACES_PRIMERA INT,
PLACES_TURISTA INT,
COMPANYIA INT,
PRIMARY KEY (NUMERO_AVIO),
FOREIGN KEY (COMPANYIA) REFERENCES COMPANYIES (IDENTIFICADOR)
)engine=innodb;
I'm having some problems linking "COMPANYIES" from "PASSATGERS" table
This is the error that the application throws me
error_code
To reference a multi-column primary key in VOLS, make the foreign key have the same number of columns.
WRONG:
FOREIGN KEY (COMPANYIA) REFERENCES VOLS (COMPANYIA),
FOREIGN KEY (VOL) REFERENCES VOLS (NUMERO_VOL)
RIGHT:
FOREIGN KEY (COMPANYIA, VOL) REFERENCES VOLS (COMPANYIA, NUMERO_VOL)
I am having a hard time understanding why my code is giving me cannot add foreign key constraint error. I just started coding in MySQL so I am sorry for anything dumb.
drop table if exists COURSE_INSTRUCTORJG;
drop table if exists CLASSJG;
drop table if exists COURSEJG;
drop table if exists INSTRUCTORJG;
create table INSTRUCTORJG(
InstructorID int,
DepartHead int,
primary key(InstructorID)
);
create table COURSEJG(
CourseNo char(10),
ComputerNeeded smallint,
primary key(CourseNo)
);
create table CLASSJG(
Building CHAR(20),
RoomNum smallint,
Maxcap int,
primary key(Building, RoomNum)
);
create table COURSE_INSTRUCTORJG (
COURSEJG_CourseNo CHAR(10),
INSTRUCTORJG_InstructorID INT,
Semester CHAR(20),
Section INT,
CLASSJG_Building char(20),
CLASSJG_RoomNum smallint,
primary key(COURSEJG_CourseNo, INSTRUCTORJG_InstructorID, Semester, Section),
foreign key (COURSEJG_CourseNo) references COURSEJG(CourseNo),
foreign key (INSTRUCTORJG_InstructorID) references INSTRUCTORJG(InstructorID),
foreign key (CLASSJG_Building) references CLASSJG(Building),
foreign key (CLASSJG_RoomNum) references CLASSJG(RoomNum)
);
My error is coming from the line: foreign key (CLASSJG_RoomNum) references CLASSJG(RoomNum).
You can reference primary key, keys or unique comnstrqaint, but in table CLASSJG you have a double primary key, so reference both columns or define another key
create table CLASSJG(
Building CHAR(20),
RoomNum smallint,
Maxcap int,
primary key(Building, RoomNum),
KEY(RoomNum)
);
see sample fiddle
I have three tables A, B and funding:
Table A has a primary key partner_id
Table B has a primary key branch_id
When I try to create table C with the following code:
CREATE TABLE Funding (
partner_id INT,
branch_id INT,
total_fund FLOAT,
PRIMARY KEY (partners_id, branch_id),
FOREIGN KEY (partners_id) REFERENCES A(partner_id) ON delete SET NULL,
FOREIGN KEY (branch_id) REFERENCES B(branch_id) ON delete SET NULL
);
I get error message:
1830: column partner_id cannot be NOT NULL: needed in a foreign key constraint.
How can I solve this problem?
Create a separted ID for PK:
SQL DEMO
CREATE TABLE A (
partner_id INT,
PRIMARY KEY (partner_id)
);
CREATE TABLE B (
branch_id INT,
PRIMARY KEY (branch_id)
);
CREATE TABLE Funding (
id INT PRIMARY KEY AUTO_INCREMENT,
partner_id INT,
branch_id INT,
total_fund FLOAT,
FOREIGN KEY (partner_id) REFERENCES A(partner_id) ON DELETE SET NULL,
FOREIGN KEY (branch_id) REFERENCES B(branch_id) ON DELETE SET NULL
);
You can also add:
ALTER TABLE `Funding` ADD UNIQUE `unique_index`(partner_id, branch_id);
But that can cause problem when multiple partners from same branch are deleted
In CREATE TABLE Funding just add NOT NULL statement to partner_id and branch_id.
CREATE TABLE Funding ( partner_id INT NOT NULL, branch_id INT NOT NULL,...
I have to create a table how's structure is
create table reparto
(
numrep integer,
nomespec varchar(20),
nomeosp varchar(20),
cittaosp varchar(25),
primary key (numrep,nomespec,nomeosp,cittaosp),
foreign key(nomeosp,cittaosp) references ospedale(nomeosp,cittaosp),
foreign key nomespec references specializzazione(nomeospe)
);
of course I've already create the tables
create table ospedale
(
nomeosp varchar(20),
cittaosp varchar(25),
numasl integer,
idasp varchar(4),
primary key(nomeosp,cittaosp)
);
and
create table specializzazione
(
nomespe varchar (20) primary key
);
of course it doesn't work and I don't know what to do, can someone tell me how to create several differentsforeign key?
There's a few obvious things we can point out here. The column name given in the foreign key definition:
references specializzazione(nomeospe)
^
Doesn't match the column name in the table definition
... specializzazione ( nomespe ...
^^^^^^^
And the column list for the foreign key should be enclosed in parens
... foreign key (nomespec)
^ ^
You have some literal error and incorect order creating tables please check this:
create table specializzazione
(
nomespe varchar (20) primary key
);
create table ospedale
(
nomeosp varchar(20),
cittaosp varchar(25),
numasl integer,
idasp varchar(4),
primary key(nomeosp,cittaosp)
);
create table reparto
(
numrep integer,
nomespec varchar(20),
nomeosp varchar(20),
cittaosp varchar(25),
primary key (numrep,nomespec,nomeosp,cittaosp),
foreign key(nomeosp,cittaosp) references ospedale(nomeosp,cittaosp),
foreign key (nomespec) references specializzazione(nomespe)
);
I was trying to feed the following commands to MySQL CLI with
, which seems to be good, however when I added one more table, it complained there was an error in syntax.
Here are the commands
CREATE TABLE IF NOT EXISTS User(
uid INT,
name VARCHAR(64) UNIQUE,
birthday date,
PRIMARY KEY(uid)
) ENGINE = InnoDB ;
CREATE TABLE IF NOT EXISTS UserEmail(
uid INT,
email VARCHAR(64),
PRIMARY KEY(uid, email),
FOREIGN KEY(uid) REFERENCES User(uid)
);
However if I wanted to add one more table, it said there's an syntax error near the ')' at the line where PRIMARY KEY(uid) lies.
CREATE TABLE IF NOT EXISTS User(
uid INT,
name VARCHAR(64) UNIQUE,
birthday date,
PRIMARY KEY(uid)
) ENGINE = InnoDB ;
CREATE TABLE IF NOT EXISTS UserEmail(
uid INT,
email VARCHAR(64),
PRIMARY KEY(uid, email),
FOREIGN KEY(uid) REFERENCES User(uid)
);
CREATE TABLE friendship(
invite_uid INT,
accept_uid INT,
start_date DATE,
PRIMARY KEY(invite_uid, accept_uid),
FOREIGN KEY(invite_uid) REFERENCES User(uid),
FOREIGN KEY(accept_uid) REFERENCES User(uid),
);
Not sure where went wrong since the error was not complained in the newly added command.
--UPDATE--
Well that was fixed, but there's one more problem.
Adding the following table throws
cannot create table "estore.contains" (errorno: 150)
CREATE TABLE contains(
uid INT,
wid INT,
pid INT,
PRIMARY KEY(uid, wid, pid),
FOREIGN KEY(uid) REFERENCES User(uid),
FOREIGN KEY(wid) REFERENCES Wishlist(wid),
FOREIGN KEY(pid) REFERENCES Product(pid)
);
--UPDATE 2--
Full Tables that I want to add
CREATE TABLE IF NOT EXISTS User(
uid INT,
name VARCHAR(64) UNIQUE,
birthday date,
PRIMARY KEY(uid)
) ENGINE = InnoDB ;
CREATE TABLE IF NOT EXISTS UserEmail(
uid INT,
email VARCHAR(64),
PRIMARY KEY(uid, email),
FOREIGN KEY(uid) REFERENCES User(uid)
);
CREATE TABLE friendship(
invite_uid INT,
accept_uid INT,
start_date DATE,
PRIMARY KEY(invite_uid, accept_uid),
FOREIGN KEY(invite_uid) REFERENCES User(uid),
FOREIGN KEY(accept_uid) REFERENCES User(uid)
);
CREATE TABLE Seller(
sid INT,
name VARCHAR(64),
PRIMARY KEY(sid)
);
CREATE TABLE Product(
pid INT,
sid INT,
name VARCHAR(64),
description TEXT,
price DOUBLE,
PRIMARY KEY(pid),
FOREIGN KEY(sid) REFERENCES Seller(sid)
);
CREATE TABLE buy(
uid INT,
pid INT,
time DATE,
PRIMARY KEY(uid, pid),
FOREIGN KEY(uid) REFERENCES User(uid),
FOREIGN KEY(pid) REFERENCES Product(pid)
);
CREATE TABLE Wishlist(
uid INT,
wid INT,
start_time DATE,
end_time DATE,
PRIMARY KEY(uid,wid),
FOREIGN KEY(uid) REFERENCES User(uid)
);
CREATE TABLE conntains(
uid INT,
wid INT,
pid INT,
PRIMARY KEY(uid, wid, pid),
FOREIGN KEY(uid) REFERENCES User(uid),
FOREIGN KEY(wid) REFERENCES Wishlist(wid),
FOREIGN KEY(pid) REFERENCES Product(pid)
)ENGINE = InnoDB;
Anyone could help? Thx
Check the last line of your code
FOREIGN KEY(accept_uid) REFERENCES User(uid),
Remove the comma at the end.
remove , a end of line
FOREIGN KEY(accept_uid) REFERENCES User(uid),
--------------------------------------------^
There is an extra comma on the third table.
CREATE TABLE IF NOT EXISTS User(
uid INT,
name VARCHAR(64) UNIQUE,
birthday date,
PRIMARY KEY(uid)
) ENGINE = InnoDB ;
CREATE TABLE IF NOT EXISTS UserEmail(
uid INT,
email VARCHAR(64),
PRIMARY KEY(uid, email),
FOREIGN KEY(uid) REFERENCES User(uid)
);
CREATE TABLE friendship(
invite_uid INT,
accept_uid INT,
start_date DATE,
PRIMARY KEY(invite_uid, accept_uid),
FOREIGN KEY(invite_uid) REFERENCES User(uid),
FOREIGN KEY(accept_uid) REFERENCES User(uid), <---- Extra comma
);
FOREIGN KEY(accept_uid) REFERENCES User(uid),<-- Comma should be removed
Try this:
CREATE TABLE friendship(
invite_uid INT,
accept_uid INT,
start_date DATE,
PRIMARY KEY(invite_uid, accept_uid),
FOREIGN KEY(invite_uid) REFERENCES User(uid),
FOREIGN KEY(accept_uid) REFERENCES User(uid)
);
#Daniel, you cannot use the name CONTAINS for a table since its an SQL keyword. Please create the table with some other name.
Fortunately I figured the second problem out, the foreign key was not made right, I need to use
FOREIGN KEY(uid, wid) REFERENCES Wishlist(uid, wid),
to refer the the primary key combination in Wishlist