mysql无法与外部健形成约束,MySQL“外键约束不正确地形成”。

在创建Reservation表时遇到了外键约束错误,由于MariaDB要求外键和被参照键必须有索引。解决方案是为Showings表的Start_Time列添加索引。然而,考虑是否应将外键改为针对Showings表的主键,以确保引用完整性。
摘要由CSDN通过智能技术生成

I am having trouble with this code.

CREATE TABLE Reservation (

Movie_Title varchar(255) NOT NULL,

Theatre_No int NOT NULL,

Complex_Name varchar(255) NOT NULL,

Start_Time datetime NOT NULL,

CustomerID int NOT NULL,

Number_of_Tickets int NOT NULL,

PRIMARY KEY (Movie_Title, Theatre_No, Complex_Name, Start_Time, CustomerID),

FOREIGN KEY (Movie_Title) REFERENCES Showings(Movie_Title),

FOREIGN KEY (Theatre_No) REFERENCES Showings(Theatre_No),

FOREIGN KEY (Complex_Name) REFERENCES Showings(Complex_Name),

FOREIGN KEY (Start_Time) REFERENCES Showings(Start_Time), /* this line causes error*/

FOREIGN KEY (CustomerID) REFERENCES Customer(Account_Number)

);

if i get rid of this line, it runs fine.

FOREIGN KEY (Start_Time) REFERENCES Showings(Start_Time),

The referenced Table (Showings) is as follows;

CREATE TABLE Showings (

Movie_Title varchar(255) NOT NULL,

Theatre_No int NOT NULL,

Complex_Name varchar(255) NOT NULL,

Start_Time datetime NOT NULL,

Num_Seats int NOT NULL,

PRIMARY KEY (Movie_Title, Theatre_No, Complex_Name, Start_Time),

FOREIGN KEY (Movie_Title) REFERENCES Movie(Title),

FOREIGN KEY (Theatre_No) REFERENCES Theatre(Theatre_No),

FOREIGN KEY (Complex_Name) REFERENCES Theatre(Complex_Name)

);

I am using MariaDB 10.1.3

Sorry, if this ends up being a silly mistake, i am fairly new with SQL

Please and thank you.

解决方案MySQL requires indexes on foreign keys and referenced keys so that foreign key checks can be fast and not require a table scan.

Although this is the MySQL documentation, it likely is the same reason it fails on your version of MariaDB.

You can fix this by simply adding an index to the Start_Time column in the Showings table.

However, before you do that, consider what exactly you are trying to do with this foreign key. You are referencing ANY Start_Time in the Showings table when you create it, rather than any specific showing.

Based on your table design, making a foreign key to the Showings table's primary key is more likely what you are trying to achieve.

CREATE TABLE Reservation (

Movie_Title varchar(255) NOT NULL,

Theatre_No int NOT NULL,

Complex_Name varchar(255) NOT NULL,

Start_Time datetime NOT NULL,

CustomerID int NOT NULL,

Number_of_Tickets int NOT NULL,

PRIMARY KEY (Movie_Title, Theatre_No, Complex_Name, Start_Time, CustomerID),

FOREIGN KEY (Movie_Title) REFERENCES Showings(Movie_Title),

FOREIGN KEY (Theatre_No) REFERENCES Showings(Theatre_No),

FOREIGN KEY (Complex_Name) REFERENCES Showings(Complex_Name),

FOREIGN KEY `Showing` (Movie_Title, Theatre_No, Complex_Name, Start_Time) REFERENCES Showings(Movie_Title, Theatre_No, Complex_Name, Start_Time),

FOREIGN KEY (CustomerID) REFERENCES Customer(Account_Number)

);

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值