i have been trying to figure out what is wrong but could not find the problem.
MySQL said: Documentation
1064
CREATE TABLE Show
(
Show_Id int NOT NULL AUTO_INCREMENT,
FOREIGN KEY (cinema_id) REFERENCES cinema(cinema_id),
FOREIGN KEY (movie_id) REFERENCES movie(movie_id),
FOREIGN KEY (show_time_id) REFERENCES show_time(show_time_id),
FOREIGN KEY (show_day_id) REFERENCES show_day(show_day_id),
PRIMARY KEY (show_id)
);
The foreign key constraint is applied to an already declared column. It doesn't do the declaration. So, you need to declare the columns:
CREATE TABLE Shows (
Show_Id int NOT NULL AUTO_INCREMENT,
Cinema_id int,
Movie_id int,
Show_Time_id int,
Show_Day_id int,
FOREIGN KEY (cinema_id) REFERENCES cinema(cinema_id),
FOREIGN KEY (movie_id) REFERENCES movie(movie_id),
FOREIGN KEY (show_time_id) REFERENCES show_time(show_time_id),
FOREIGN KEY (show_day_id) REFERENCES show_day(show_day_id),
PRIMARY KEY (show_id)
);
Note that show is a reserved word in MySQL, so I changed the table name to shows. I typically use plurals for table names anyway, because they are collections of things.
Related
create database priceTag;
use priceTag;
CREATE TABLE `ProductNumber` (
`Sku` INT auto_increment not null,
`Model` VARCHAR(100),
PRIMARY KEY (`Sku`)
);
ALTER TABLE ProductNumber AUTO_INCREMENT=60000;
CREATE TABLE `Manufacture` (
`Manufacture` VARCHAR(100),
`Model` VARCHAR(100),
`Category` VARCHAR(100),
PRIMARY KEY (`Model`)
);
CREATE TABLE `OpenBox` (
`Condtion` VARCHAR(100),
`LP` INT auto_increment not null,
`MissingItems` VARCHAR(100),
`Model_` VARCHAR(100),
FOREIGN KEY (`Model_`) REFERENCES `Manufacture`(`Model`),
PRIMARY KEY (`LP`)
);
ALTER TABLE OpenBox AUTO_INCREMENT=200000000;
CREATE TABLE `TAG` (
`SKU*` INT,
`Model*` VARCHAR(100),
`PRICE*` DECIMAL(10,2),
`LP*` INT,
`condtion*` VARCHAR(100),
FOREIGN KEY (`SKU*`) REFERENCES `ProductNumber`(`Sku`),
FOREIGN KEY (`Model*`) REFERENCES `Manufacture`(`Model`),
FOREIGN KEY (`LP*`) REFERENCES `OpenBox`(`LP`),
FOREIGN KEY (`condtion*`) REFERENCES `OpenBox`(`condtion`)
);
CREATE TABLE `Inventory` (
`INV` int,
`Sku!` int,
`Model!` VARCHAR(100),
FOREIGN KEY (`Sku!`) REFERENCES `ProductNumber`(`Sku`),
FOREIGN KEY (`Model!`) REFERENCES `Manufacture`(`Model`)
);
The column which you refer on in FOREIGN KEY (`condtion*`) REFERENCES `OpenBox`(`condtion`) (i.e. OpenBox.condtion) is not indexed.
Add needed unique index creation then create TAG table.
DEMO
I tested your code, and then ran SHOW ENGINE INNODB STATUS to get more detailed information about the error.
LATEST FOREIGN KEY ERROR
2021-12-02 09:47:16 0x700007565000 Error in foreign key constraint of table test2/tag:
FOREIGN KEY (condtion*) REFERENCES OpenBox(condtion)
):
Cannot find an index in the referenced table where the
referenced columns appear as the first columns, or column types
in the table and the referenced table do not match for constraint.
It's complaining about this foreign key:
FOREIGN KEY (`condtion*`) REFERENCES `OpenBox`(`condtion`)
The OpenBox.condtion column is not a primary key or unique key. Foreign keys must reference a key of the parent table.
You already have another foreign key in your TAGS table referencing the OpenBox table. Are you intending that the condtion column of the respective row be copied to the TAGS table? That's not how foreign keys are intended to be used.
Hi I am creating several tables some of which have multiple keys linking to the same table. When I try to create the foreign key links I am given:
errno: 150 "Foreign key constraint is incorrectly formed"
Here are the tables I am creating:
create table EventFeed (
EventFeedID integer auto_increment not null,
EventName varchar(100),
EventTime Timestamp,
EventLocation varchar(100),
primary key (EventFeedID)
);
create table Calendar (
MyEventID integer auto_increment not null,
EventFeedID integer,
EventName varchar(100),
EventTime Timestamp,
EventLocation varchar(100),
AttendeeID varchar(100),
primary key (MyEventID),
foreign key (EventFeedID) references EventFeed (EventFeedID),
foreign key (EventName) references EventFeed (EventName),
foreign key (EventTime) references EventFeed (EventTime),
foreign key (EventLocation) references EventFeed (EventLocation),
foreign key (AttendeeID) references Users (UserID)
);
The error seems to be coming from the foreign keys in the 'Calendar' table however some of my other tables have used similar foreign keys and given no errors
can anyone advise what I am doing wrong?
Any help is much appreciated
You cannot have a foreign key on the column which is a non-primary key except the columns with unique constraint. So you have to make EventName, EventTime, etc unique if you want it to be referred by a foreign key.
You can check here at: Technet Microsoft
A FOREIGN KEY constraint does not have to be linked only to a PRIMARY KEY constraint in another table; it can also be defined to reference the columns of a UNIQUE constraint in another table.
You can see this fiddle for working example: http://sqlfiddle.com/#!9/05663
I have created the following tables:
Stops:
CREATE TABLE Stop (
routeNo DECIMAL (4,0) UNSIGNED,
stopNo DECIMAL(3,0) UNSIGNED,
latitude DECIMAL(19,16),
longitude DECIMAL(19,16),
CONSTRAINT PK_Location PRIMARY KEY (routeNo, stopNo),
CONSTRAINT FK_stop_location FOREIGN KEY (latitude,longitude) REFERENCES Location(latitude,longitude),
CONSTRAINT FK_stop_route FOREIGN KEY (routeNo) REFERENCES Route(routeNo)
);
And the table WorkDay:
CREATE TABLE Workday (
theDate DATE PRIMARY KEY,
notes VARCHAR(30)
);
I then try to create the table Order Delivery with the following code:
CREATE TABLE OrderDelivery
(
routeNo DECIMAL(4,0) UNSIGNED,
stopNO DECIMAL(3,0) UNSIGNED,
orderNo INT(9),
CONSTRAINT pk_Orders PRIMARY KEY (routeNo, stopNo, orderNo),
deliveryDateExpected DATE,
deliveryTime TIME,
customerName VARCHAR(40),
creditCardNo CHAR(16) NOT NULL,
customerRanking ENUM('platinum','gold','occasional','one-off'),
CONSTRAINT fk_Routes FOREIGN KEY (routeNo) REFERENCES Stop(routeNo),
CONSTRAINT fk_Stops FOREIGN KEY (stopNo) REFERENCES Stop(stopNo),
CONSTRAINT fk_Dates FOREIGN KEY (deliveryDateExpected) REFERENCES Workday(theDate)
);
However every time i try to create the OrderDelivery table it gives the error that the foreign key is incorrectly formed
"SQL Error (1005): Can't create table 'harry.OrderDelivery' (errno:150)Foreign key constraint is incorrectly formed"
How can i rectify this?
You can't create a foreign key to a table with a multiple primary key referencing only one field of that primary key.
So your problem lies here:
CONSTRAINT fk_Routes FOREIGN KEY (routeNo) REFERENCES Stop(routeNo),
CONSTRAINT fk_Stops FOREIGN KEY (stopNo) REFERENCES Stop(stopNo),
You should change it to
CONSTRAINT fk_Stops_Routes
FOREIGN KEY (routeNo, stopNo)
REFERENCES Stop(routeNo, stopNo),
Or depending on your requirements you have to change your model to fit your needs.
I never used MySQL before but I don't have choice this time. I've an error I don't understand. I try to create a table :
CREATE TABLE proprietaire(
num_cpt INT REFERENCES compte(num_cpt) PRIMARY KEY,
num_client INT REFERENCES client(num_client),
num_commercant INT REFERENCES commercant(num_commercant)
);
I can't see my mistake. Could you please help me with this ?
Thank You
In MySQL, to enforce foreign key references, you cannot put the references as a modifier on the column. You need a separate constraint for them:
CREATE TABLE proprietaire (
num_cpt INT PRIMARY KEY,
num_client INT,
num_commercant INT,
CONSTRAINT fk_num_cpt FOREIGN KEY REFERENCES compte(num_cpt),
CONSTRAINT fk_num_client FOREIGN KEY REFERENCES client(num_client),
CONSTRAINT fk_num_commercant FOREIGN KEY REFERENCES commercant(num_commercant)
);
However, your specific problem was the REFERENCES before the PRIMARY KEY constraint.
As a note: in most English-language databases, your use of num would be id. num sounds like a count or "number of" something, whereas id is short for identifier.
im new on mysql workbench, and i tried so many things to put my script working but i simply cant... Ive got these tables:
CREATE TABLE Utilizador (email varchar(40) not null, nome varchar(50)
not null, dataNascimento date, profissao varchar(50) not null,
reputacao double(3,2) unsigned not null, constraint pk_Utilizador
primary key(email))
This is the first table created!
CREATE TABLE POI (email varchar(40) not null, designacaoPOI
varchar(10) not null, coordenadaX int, coordenadaY int,
descricaoPOI varchar(200), constraint pk_POI primary key(email,
designacaoPOI), constraint fk_POI foreign key(email) references
Utilizador(email) on delete cascade)
This is the second table created!
CREATE TABLE Utilizador_POI (email varchar(40) not null, designacaoPOI
varchar(10) not null, constraint pk_Utilizador_POI primary key(email,
designacaoPOI), constraint fk1_Utilizador_POI foreign key(email)
references Utilizador(email) on delete cascade, constraint
fk2_Utilizador_POI foreign key(designacaoPOI) references
POI(designacaoPOI) on delete cascade)
This table gives me the error: Error Code: 1215. Cannot add foreign key constraint
I did some tests and im almost sure that the problem is in the foreign key "designacaoPOI". The other FK ("email") dont give me any error, so maybe the problem is in the Table POI?
Thanks in advanced!
The problem here is twofold:
1/ Use IDs for PRIMARY KEYs
You should be using IDs for primary keys rather than VARCHARs or anything that has any real-world "business meaning". If you want the email to be unique within the Utilizador table, the combination of email and designacaoPOI to be unique in the POI table, and the same combination (email and designacaoPOI) to be unique in Utilizador_POI, you should be using UNIQUE KEY constraints rather than PRIMARY KEY constraints.
2/ You cannot DELETE CASCADE on a FOREIGN KEY that doesn't reference the PRIMARY KEY
In your third table, Utilizador_POI, you have two FOREIGN KEYs references POI. Unfortunately, the PRIMARY KEY on POI is a composite key, so MySQL has no idea how to handle a DELETE CASCADE, as there is not a one-to-one relationship between the FOREIGN KEY in Utilizador_POI and the PRIMARY KEY of POI.
If you change your tables to all have a PRIMARY KEY of ID, as follows:
CREATE TABLE blah (
id INT(9) AUTO_INCREMENT NOT NULL
....
PRIMARY KEY (id)
);
Then you can reference each table by ID, and both your FOREIGN KEYs and DELETE CASCADEs will work.
I think the problem is that Utilizador_POI.email references POI.email, which itself references Utilizador.email. MySQL is probably upset at the double-linking.
Also, since there seems to be a many-to-many relationship between Utilizador and POI, I think the structure of Utilizador_POI isn't what you really want. Instead, Utilizador_POI should reference a primary key from Utilizador, and a matching primary key from POI.
The problem is in your second table. Your primary key is (email,designacaoPOI), when you try to reference that in your table it gives you error because of this:
InnoDB permits a foreign key to reference any index column or group of
columns. However, in the referenced table, there must be an index
where the referenced columns are listed as the first columns in the
same order.
For it to work, either change the order of your second tale PRIMARY KEY :
CREATE TABLE POI (
email VARCHAR(40) NOT NULL,
designacaoPOI VARCHAR(10) NOT NULL,
coordenadaX INT,
coordenadaY INT,
descricaoPOI VARCHAR(200),
CONSTRAINT pk_POI PRIMARY KEY (designacaoPOI,email), -- changed the order
CONSTRAINT fk_POI FOREIGN KEY (email)
REFERENCES Utilizador(email) ON DELETE CASCADE
);
sqlfiddle demo
or add an index for designacaoPOI:
CREATE TABLE POI (
email VARCHAR(40) NOT NULL,
designacaoPOI VARCHAR(10) NOT NULL,
coordenadaX INT,
coordenadaY INT,
descricaoPOI VARCHAR(200),
CONSTRAINT pk_POI PRIMARY KEY (designacaoPOI,email),
KEY key_designacaoPOI(designacaoPOI), -- added index for that column
CONSTRAINT fk_POI FOREIGN KEY (email)
REFERENCES Utilizador(email) ON DELETE CASCADE
);
sqlfiddle demo
Either of these solutions will let you create your third table without errors.