0

So I have created a database which looks like that.

CREATE TABLE Budova
(
BudovaID int primary key not null,
BytyPocet int not null,
);
CREATE TABLE Skupina
(
SkupinaID int primary key not null,
NajemniciPocet int not null,
BytID int not null,
FOREIGN KEY (BytID) REFERENCES Byt(BytID) ON DELETE CASCADE,
);
CREATE TABLE Najemnici
(
NajemnikID int primary key not null,
Jmeno varchar(255) null,
Prijmeni varchar(255) null, 
Vek int null,
SkupinaID int not null,
BytID int not null,
CenaEnergii int not null,
FOREIGN KEY (BytID) REFERENCES Byt(BytID),
FOREIGN KEY (SkupinaID) REFERENCES Skupina(SkupinaID) 
);
CREATE TABLE Byt
(
BytID int primary key not null,
BudovaID int not null,
OpravaID int not null,
FOREIGN KEY (BudovaID) REFERENCES Budova(BudovaID) ON DELETE CASCADE,
FOREIGN KEY (OpravaID) REFERENCES Opravy(OpravaID) ON DELETE CASCADE

);
CREATE TABLE Vydaje
(
BytID int not null,
Voda int not null,
Elektrina int not null,
Plyn int not null,
Zaloha int not null,
Celkem int not null,
CelkemEura int not null
FOREIGN KEY (BytID) REFERENCES Byt(BytID)
);
CREATE TABLE Opravy 
(
OpravaID int primary key not null,
OpravaTyp varchar(255) not null,
OpravaCena int not null,
);

I have basically 6 tables in the database but when I've tried to create database diagram, it haven't shown every single table. As you can see, it shows only 5 of them.

At the end, it looks like that.

I've tried to change the references, but It went literally to hell. Do you know, what I should do to make it works?

1 Answers1

0

You have a lot some errors on your statementm regardkless which dbms you use.

Try this create query in that order it is posted, or else the foreign keys won't work

CREATE TABLE Budova
(
BudovaID int primary key not null,
BytyPocet int not null
);
CREATE TABLE Opravy 
(
OpravaID int primary key not null,
OpravaTyp varchar(255) not null,
OpravaCena int not null
);
CREATE TABLE Byt
(
BytID int primary key not null,
BudovaID int not null,
OpravaID int not null,
FOREIGN KEY (BudovaID) REFERENCES Budova(BudovaID) ON DELETE CASCADE,
FOREIGN KEY (OpravaID) REFERENCES Opravy(OpravaID) ON DELETE CASCADE

);
CREATE TABLE Skupina
(
SkupinaID int primary key not null,
NajemniciPocet int not null,
BytID int not null,
FOREIGN KEY (BytID) REFERENCES Byt(BytID) ON DELETE CASCADE
);

CREATE TABLE Najemnici
(
NajemnikID int primary key not null,
Jmeno varchar(255) null,
Prijmeni varchar(255) null, 
Vek int null,
SkupinaID int not null,
BytID int not null,
CenaEnergii int not null,
FOREIGN KEY (BytID) REFERENCES Byt(BytID),
FOREIGN KEY (SkupinaID) REFERENCES Skupina(SkupinaID) 
);



CREATE TABLE Vydaje
(
BytID int not null,
Voda int not null,
Elektrina int not null,
Plyn int not null,
Zaloha int not null,
Celkem int not null,
CelkemEura int not null,
FOREIGN KEY (BytID) REFERENCES Byt(BytID)
);
nbk
  • 45,398
  • 8
  • 30
  • 47
  • So you are saying that it depends on what table is before another? – Tomáš Jahoda Jan 26 '20 at 14:37
  • you can only set a foreign key to an existing table/column. also your post gas too many errors – nbk Jan 26 '20 at 14:47
  • Yea there will be probably errors, because I've had it just for 3 months, so I don't understand it that much. But I have to have it complete tomorrow. Can you specify some errors that I can work on ? Edit: I mean as a subject in school and this is my project.* – Tomáš Jahoda Jan 26 '20 at 14:52
  • simply compre my post to your and you see, commas that are to much and of course the order in which ti create and please maek the question as answered – nbk Jan 26 '20 at 15:00