C
C
CallMeYourDaddy2020-11-08 20:21:39
SQL
CallMeYourDaddy, 2020-11-08 20:21:39

How to merge tables correctly?

It is necessary to create two plates: department, users. When displaying a person, information about his department should be displayed. And when outputting a department, information about the employees in that department.

CREATE TABLE Users
(
    Id INT PRIMARY KEY AUTO_INCREMENT,
    DepartmentId INT,
    Age INT, 
    FirstName VARCHAR(20) NOT NULL,
    LastName VARCHAR(20) NOT NULL,
    Phone VARCHAR(20) NOT NULL UNIQUE,
    FOREIGN KEY (DepartmentId) REFERENCES Departments (id)
);
 
CREATE TABLE Departments
(
    Id INT PRIMARY KEY AUTO_INCREMENT,
    UserId INT,
    DepartmentName VARCHAR(20) NOT NULL,	
    FOREIGN KEY (UserId)  REFERENCES Users (Id)
);


It throws an error - Failed to open the referenced table 'departments' , but I don't understand anything. Help

Answer the question

In order to leave comments, you need to log in

1 answer(s)
L
Lazy @BojackHorseman, 2020-11-08
@CallMeYourDaddy

first create the Departments table and then refer to it in the foreign key.
but since you are trying to implement cyclic foreign keys, I inform you that this is not possible. and what's more, it's not true. there are even DBMS that will write about it explicitly in the error text.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question