我有这段代码:
CREATE TABLE `osoby` (
`Id` int(11) NOT NULL AUTO_INCREMENT,
`nr_akt` varchar(10),
`imie1` varchar(20) NOT NULL,
`imie2` varchar(20),
`nazwisko` varchar(50) NOT NULL,
`pesel` int(11),
`Rel_Stanowisko` int(11) NOT NULL,
`IsDeleted` tinyint(1) NOT NULL DEFAULT '0',
PRIMARY KEY (`Id`),
CONSTRAINT `fk_Osoby_Stanowisko` FOREIGN KEY (`Rel_Stanowisko`) REFERENCES `stanowisko` (`id_stan`) ON DELETE NO ACTION ON UPDATE NO ACTION
);
CREATE TABLE `stanowisko` (
`id_stan` int(11) NOT NULL AUTO_INCREMENT,
`nazwa_stan` VARCHAR(40) NOT NULL,
`dzial` VARCHAR(40) NOT NULL,
`zakr_wynagr` VARCHAR(10),
`IsDeleted` tinyint(1) NOT NULL DEFAULT '0',
PRIMARY KEY (`id_stan`)
);
CREATE TABLE `dzial` (
`id_dzialu` int(4) NOT NULL AUTO_INCREMENT,
`nazwa_dzialu` VARCHAR(40) NOT NULL,
`skr_nazwa_dzialu` VARCHAR(10),
`IsDeleted` tinyint(1) NOT NULL DEFAULT '0',
PRIMARY KEY (`id_dzialu`)
);
CREATE TABLE pracownik (
`id_prac` int(6) NOT NULL AUTO_INCREMENT,
`id_osoby` int(6) NOT NULL,
`id_stan` int(4),
`id_dzialu` int(4),
`pensja` int(4),
`IsDeleted` tinyint(1) NOT NULL DEFAULT '0',
PRIMARY KEY (`id_prac`)
);
我知道问题出在外键上,但我不知道如何解决这个问题 我有这个错误:
13:24:17 CREATE TABLE `osoby` ( `Id` int(11) NOT NULL AUTO_INCREMENT, `nr_akt` varchar(10), `imie1` varchar(20) NOT NULL, `imie2` varchar(20), `nazwisko` varchar(50) NOT NULL, `pesel` int(11), `Rel_Stanowisko` int(11) NOT NULL, `IsDeleted` tinyint(1) NOT NULL DEFAULT '0', PRIMARY KEY (`Id`), CONSTRAINT `fk_Osoby_Stanowisko` FOREIGN KEY (`Rel_Stanowisko`) REFERENCES `stanowisko` (`id_stan`) ON DELETE NO ACTION ON UPDATE NO ACTION ) Error Code: 1005. Can't create table 'hurtownia.osoby' (errno: 150) 0.327 sec
我无法创建表,因为我的错误号为150。 有人知道我有问题吗?
答案 0 :(得分:0)
您的第一个表依赖于第二个表。因此,首先创建第二个表然后第一个表或更改顺序如下:
CREATE TABLE stanowisko ( id_stan int(11) NOT NULL AUTO_INCREMENT, nazwa_stan VARCHAR(40) NOT NULL, dzial VARCHAR(40) NOT NULL, zakr_wynagr VARCHAR(10), IsDeleted tinyint(1) NOT NULL DEFAULT '0', PRIMARY KEY (id_stan) );
CREATE TABLE osoby ( Id int(11) NOT NULL AUTO_INCREMENT, nr_akt varchar(10), imie1 varchar(20) NOT NULL, imie2 varchar(20), nazwisko varchar(50) NOT NULL, pesel int(11), Rel_Stanowisko int(11) NOT NULL, IsDeleted tinyint(1) NOT NULL DEFAULT '0', PRIMARY KEY (Id), CONSTRAINT fk_Osoby_Stanowisko FOREIGN KEY (Rel_Stanowisko) REFERENCES stanowisko (id_stan) ON DELETE NO ACTION ON UPDATE NO ACTION );
CREATE TABLE dzial ( id_dzialu int(4) NOT NULL AUTO_INCREMENT, nazwa_dzialu VARCHAR(40) NOT NULL, skr_nazwa_dzialu VARCHAR(10), IsDeleted tinyint(1) NOT NULL DEFAULT '0', PRIMARY KEY (id_dzialu) );
CREATE TABLE pracownik ( id_prac int(6) NOT NULL AUTO_INCREMENT, id_osoby int(6) NOT NULL, id_stan int(4), id_dzialu int(4), pensja int(4), IsDeleted tinyint(1) NOT NULL DEFAULT '0', PRIMARY KEY (id_prac) );