![](/img/trans.png)
[英]MySQL 150 error on table creation form mysql workbench (foreign key options conflict with not null in columns, FIXED)
[英]Problems with MYSQL Error 150 during table creation
我查閱了關於這個問題的其他帖子(有很多),但我不得不說到目前為止對我來說沒有任何效果。 唯一沒有嘗試的是禁用外部檢查。
我的問題:
當我嘗試創建兩個簡單的表時,我收到錯誤 150,我不知道為什么。
到目前為止我的代碼:
drop table if exists SENSOR;
drop table if exists SENSOR_DATEN;
create table SENSOR (
SENSOR_ID int not null auto_increment,
SENSOR_NAME varchar(100),
SENSOR_POSX float not null,
SENSOR_POSY float not null,
SENSOR_EINHEIT varchar(100) not null,
primary key (SENSOR_ID)
);
commit;
create table SENSOR_DATEN (
SENSOR_DATEN_ID int not null auto_increment,
SENSOR_ID int not null,
SENSOR_DATEN_WERT varchar(100),
SENSOR_TYP varchar(100) not null,
primary key (SENSOR_DATEN_ID),
foreign key(SENSOR_ID) references SENSOR(SENSOR_ID),
foreign key(SENSOR_TYP) references SENSOR(SENSOR_EINHEIT)
);
commit;
我的主要問題是理解問題。 我得到了一個更大的數據庫,它幾乎相同,但沒有問題。 這是工作正常的數據庫代碼:
drop table if exists AUSGELIEHEN_FILM;
drop table if exists AUSGELIEHEN_SPIEL;
drop table if exists KUNDE;
drop table if exists FILM;
drop table if exists SPIEL;
create table KUNDE (
KUNDE_ID int not null auto_increment,
KUNDE_VNAME varchar(100),
KUNDE_NAME varchar(100),
KUNDE_ADRESSE varchar(100),
KUNDE_PLZ int not null,
primary key (KUNDE_ID)
);
create table FILM (
FILM_ID int not null auto_increment,
FILM_NAME varchar(100),
FILM_GENRE varchar(50),
FILM_FSK int not null,
FILM_BEWERTUNG int not null,
FILM_ANZAHL int not null,
FILM_MEDIUM varchar(50),
primary key (FILM_ID)
);
create table SPIEL (
SPIEL_ID int not null auto_increment,
SPIEL_NAME varchar(100),
SPIEL_GENRE varchar(50),
SPIEL_FSK int not null,
SPIEL_BEWERTUNG int not null,
SPIEL_ANZAHL int not null,
SPIEL_PLATFORM varchar(50),
primary key (SPIEL_ID)
);
create table AUSGELIEHEN_FILM (
AUSGELIEHEN_FILM_ID int not null auto_increment,
KUNDE_ID int not null,
FILM_ID int not null,
AUSGELIEHEN_ANZAHL int not null,
primary key(AUSGELIEHEN_FILM_ID),
foreign key(KUNDE_ID) references KUNDE(KUNDE_ID),
foreign key(FILM_ID) references FILM(FILM_ID)
);
create table AUSGELIEHEN_SPIEL(
AUSGELIEHEN_SPIEL_ID int not null auto_increment,
KUNDE_ID int not null,
SPIEL_ID int not null,
AUSGELIEHEN_ANZAHL int not null,
primary key(AUSGELIEHEN_SPIEL_ID),
foreign key(KUNDE_ID) references KUNDE(KUNDE_ID),
foreign key(SPIEL_ID) references SPIEL(SPIEL_ID)
);
commit;
您的表SENSOR_DATEN
聲明了一個引用SENSOR
列SENSOR_EINHEIT
的外鍵。 此列未定義為唯一的,因此不能有指向它的外鍵。 因此,你的失敗。
您可以修改SENSOR
的聲明以使該列唯一:
create table SENSOR (
SENSOR_ID int not null auto_increment,
SENSOR_NAME varchar(100),
SENSOR_POSX float not null,
SENSOR_POSY float not null,
SENSOR_EINHEIT varchar(100) not null,
primary key (SENSOR_ID),
unique (SENSOR_EINHEIT)
);
或者,將其事后應用於現有表:
alter table SENSOR add constraint SENSOR_EINHEIT_UNQ unique(SENSOR_EINHEIT);
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.