I'm new to SQL and I'm trying to create a new table however I get an error when I run my script in the SQL command line, the errors I'm getting are either ORA-00942 missing right parenthesis or ORA-00942 table or view does not exist.
Oh and yes I know I have probably written some terrible script but as mentioned earlier I'm learning so any meaningful criticism would be appreciated along with some help :).
CREATE TABLE Branch
(
Branch_ID varchar(5),
Branch_Name varchar(255),
Branch_Address varchar(255),
Branch_Town varchar(255),
Branch_Postcode varchar(10),
Branch_Phone varchar(50),
Branch_Fax varchar(50),
Branch_Email varchar(50),
Property_ID varchar(5),
Contract_ID varchar(5),
Staff_ID varchar(5),
PRIMARY KEY (Branch_ID),
FOREIGN KEY (Property_ID) REFERENCES Property(Property_Id),
FOREIGN KEY (Contract_ID) REFERENCES Contract(Contract_Id),
FOREIGN KEY (Staff_ID) REFERENCES Staff(Staff_Id)
);
CREATE TABLE Staff
(
Staff_ID varchar(5),
Staff_Forename varchar(255),
Staff_Surname varchar(255),
Staff_Address varchar(255),
Staff_Town varchar(255)
Staff_Postcode varchar(10),
Staff_Phone varchar(50),
Staff_DOB varchar(50),
Staff_NIN varchar(10),
Staff_Salary varchar(50),
Staff_Date_Joined varchar(100),
Staff_Viewing_Arranged varchar(100),
Branch_ID varchar(5),
Sales_ID varchar(5),
PRIMARY KEY (Staff_ID),
FOREIGN KEY (Branch_ID) REFERENCES Branch(Branch_ID),
FOREIGN KEY (Sales_ID) REFERENCES Sales(Sales_Id)
);
CREATE TABLE Sales
(
Sales_ID varchar(5),
Property_Address varchar(255),
Property_Town varchar(255)
Property_Postcode varchar(10),
Property_Type varchar(255),
Num_Rooms varchar(50),
Date_of_Sale varchar(10),
Sales_Bonus varchar(100),
Branch_ID varchar(5),
Property_ID varchar(5),
Staff_ID varchar(5)
Seller_ID varchar(5),
PRIMARY KEY (Sales_ID),
FOREIGN KEY (Branch_ID) REFERENCES Branch(Branch_ID),
FOREIGN KEY (Property_ID) REFERENCES Property(Property_Id),
FOREIGN KEY (Staff_ID) REFERENCES Staff(Staff_Id),
FOREIGN KEY (Seller_ID) REFERENCES Seller(Seller_Id)
);
CREATE TABLE Contract
(
Contract_ID varchar(5),
Contract_Signed_Date varchar(50),
Property_ID varchar(5),
Buyer_ID varchar(5),
Seller_ID varchar(5),
Branch_ID varchar(5),
PRIMARY KEY (Contract_ID),
FOREIGN KEY (Branch_ID) REFERENCES Branch(Branch_ID),
FOREIGN KEY (Property_ID) REFERENCES Property(Property_Id),
FOREIGN KEY (Seller_ID) REFERENCES Seller(Seller_Id),
FOREIGN KEY (Buyer_ID) REFERENCES Buyer(Buyer_Id)
);
CREATE TABLE Buyer
(
Buyer_ID varchar(5),
Viewing_Data varchar(255),
Maximum_Budject varchar(255),
Purchase_Price varchar (50),
Buyer_Forename varchar(255),
Buyer_Surname varchar(255),
Buyer_Address varchar(255),
Buyer_Town varchar(255),
Buyer_Postcode varchar(10),
Property_ID varchar(5),
Contract_ID varchar(5),
Survey_ID varchar(5),
PRIMARY KEY (Buyer_ID),
FOREIGN KEY (Property_ID) REFERENCES Property(Property_ID),
FOREIGN KEY (Contract_ID) REFERENCES Contract(Contract_Id),
FOREIGN KEY (Survey_ID) REFERENCES Survey(Survey_Id)
);
CREATE TABLE Seller
(
Seller_ID varchar(5),
Seller_Forename varchar(255),
Seller_Surname varchar(255),
Seller_Address varchar(255),
Seller_Town varchar(255),
Seller_Postcode varchar(10),
Seller_Property_ID varchar(5),
No_of_Bed varchar(5),
Contract_ID varchar(5),
Property_ID varchar(5),
Sales_ID varchar(5),
PRIMARY KEY (Seller_ID),
FOREIGN KEY (Contract_ID) REFERENCES Contract(Contract_ID),
FOREIGN KEY (Property_ID) REFERENCES Property(Property_Id),
FOREIGN KEY (Sales_ID) REFERENCES Sales(Sales_Id)
);
CREATE TABLE Property
(
Property_ID varchar(5),
Property_Address varchar(255),
Property_Town varchar(255),
Property_Postcode varchar(10),
Asking_Price varchar(20),
Date_Registered varchar(50),
Property_Fixtures varchar(255),
Size_of_Rooms varchar(100),
Buyer_ID varchar(5),
Staff_ID varchar(5),
Contract_ID varchar(5),
Seller_ID varchar(5),
PRIMARY KEY (Property_ID),
FOREIGN KEY (Buyer_ID) REFERENCES Buyer(Buyer_ID),
FOREIGN KEY (Seller_ID) REFERENCES Seller(Seller_ID),
FOREIGN KEY (Staff_ID) REFERENCES Staff(Staff_Id),
FOREIGN KEY (Contract_ID) REFERENCES Contract(Contract_Id)
);
CREATE TABLE Survey
(
Survey_ID varchar(5),
No_of_Survey varchar(10),
Survey_Type varchar(255),
Organised_By varchar(255),
Property_ID varchar(5),
Staff_ID varchar(5),
Buyer_ID varchar(5),
PRIMARY KEY (Survey_ID),
FOREIGN KEY (Property_ID) REFERENCES Property(Property_ID),
FOREIGN KEY (Staff_ID) REFERENCES Staff(Staff_Id),
FOREIGN KEY (Buyer_ID) REFERENCES Buyer(Buyer_Id)
);
CREATE TABLE Advert
(
Survey_ID Advert_ID varchar(5),
No_of_Adverts varchar(10),
Advert_Website varchar(255),
Advert_Newspaper varchar(255),
Property_ID varchar(5),
PRIMARY KEY (Advert_ID),
FOREIGN KEY (Property_ID) REFERENCES Property(Property_ID)
);
STAFF Table miss a , at the end
Staff_Town varchar(255)
Sales Table too
Property_Town varchar(255)
Staff_ID varchar(5)
Also you can't define constrains to table not created yet. I could found the error removing those constrain.
Can't define the foreign key references until the table being referred to is created. So, if the foreign key references are circular, in that A => B => C => A, they you will first have to create the tables, then use ALTER TABLE to define the foreign keys. Otherwise, create the tables in order, where first you create a table, then create the table that is referencing it.
Your script references tables that haven't been created yet. I.e., look at your first create table. by that time, STAFF and other tables don't exist yet, but you're setting them as foreign key references.
You should create your tables first, and then apply constraints after the dependencies are in place, using alter:
ALTER TABLE BRANCH
ADD CONSTRAINT fk_staff_id
FOREIGN KEY (Staff_ID) REFERENCES Staff(Staff_Id);
because you have tables that references each other you need create the references after the tables are created
--create table Buyer
CREATE TABLE Buyer
(
Buyer_ID varchar(5),
Property_ID varchar(5),
PRIMARY KEY (Buyer_ID)
);
--create table Property
CREATE TABLE Property
(
Property_ID varchar(5),
Buyer_ID varchar(5),
PRIMARY KEY (Property_ID)
);
--now you can set your reference constraints
ALTER TABLE Property Add FOREIGN KEY (Buyer_ID) REFERENCES Buyer(Buyer_ID);
ALTER TABLE Buyer Add FOREIGN KEY (Property_ID) REFERENCES Property(Property_ID);
Your syntax is not clear on your keys.
Here is an example of what a create table with a primary key and a foreign key (fk) would look like:
CREATE TABLE animals (
animal_id NUMBER(10) PRIMARY KEY,
animal_name VARCHAR2(50) NOT NULL,
animal_species_code NUMBER(50) NOT NULL
CONSTRAINT fk_animal_species REFERENCES animal_species
(species_code));
It appears that the modeling in this set of tables might benefit from some additions and changes.
As an example, the STAFF table references the BRANCH table - but BRANCH also references STAFF. This means that BRANCH can only reference one STAFF, and vice versa, which I suspect is not what's wanted. It appears that you want a many-to-many relationship between BRANCH and STAFF, and the standard solution for enabling this is a junction table - one which contains the primary keys of both tables. Such a junction table between BRANCH and STAFF might look like:
CREATE TABLE BRANCH_STAFF
(BRANCH_ID VARCHAR(5)
CONSTRAINT BRANCH_STAFF_FK1
REFERENCES BRANCH(BRANCH_ID),
STAFF_ID VARCHAR(5)
CONSTRAINT BRANCH_STAFF_FK2
REFERENCES STAFF(STAFF_ID),
CONSTRAINT PK_BRANCH_STAFF
PRIMARY KEY (BRANCH_ID, STAFF_ID)
USING INDEX);
Best of luck.
Related
I am running into the error
You cannot add or change a record because a related record is required in table
whenever I try to add a new entry to my tables. Here is my SQL code:
SQL DDL
CREATE TABLE ART_OBJECT
(
Id_no NUMBER PRIMARY KEY,
Artist VARCHAR(20),
Year INT UNIQUE,
Title VARCHAR(20),
Description VARCHAR(20),
Style VARCHAR(20) UNIQUE
);
ALTER TABLE ART_OBJECT
ADD CONSTRAINT ArtistName FOREIGN KEY (Artist) REFERENCES ARTIST(Name)
ALTER TABLE ART_OBJECT
ADD CONSTRAINT EName FOREIGN KEY (Title) REFERENCES EXHIBITIONS(Name)
CREATE TABLE ARTIST
(
Name VARCHAR(20),
DateBorn DATE,
Date_died DATE ,
Country_of_origin VARCHAR(20),
Epoch INT,
Main_style VARCHAR(20),
Description VARCHAR(20)
);
ALTER TABLE ARTIST
ADD CONSTRAINT TimeMade FOREIGN KEY (Epoch) REFERENCES ART_OBJECT(Year)
CREATE TABLE PERMANENT_COLLECTION
(
Date_acquired DATE,
Status VARCHAR(20),
Cost NUMBER,
Year INT,
ArtID NUMBER
);
ALTER TABLE PERMANENT_COLLECTION
ADD CONSTRAINT TimeMade FOREIGN KEY (Year) REFERENCES ART_OBJECT(Year)
ALTER TABLE PERMANENT_COLLECTION
ADD CONSTRAINT PCID FOREIGN KEY (ArtID) REFERENCES ART_OBJECT(Id_no)
CREATE TABLE BORROWED
(
Id_no NUMBER,
Name VARCHAR(20),
Date_borrowed DATE,
Date_returned DATE
);
ALTER TABLE BORROWED
ADD CONSTRAINT BID FOREIGN KEY (Id_no) REFERENCES ART_OBJECT(Id_no)
ALTER TABLE BORROWED
ADD CONSTRAINT BName FOREIGN KEY (Name) REFERENCES COLLECTIONS(CName)
CREATE TABLE PAINTING
(
Paint_type VARCHAR(20),
Drawn_on VARCHAR(20),
Style VARCHAR(20),
ArtID NUMBER
);
ALTER TABLE PAINTING
ADD CONSTRAINT PStyle FOREIGN KEY (Style) REFERENCES ART_OBJECT(Style)
ALTER TABLE PAINTING
ADD CONSTRAINT PID FOREIGN KEY (ArtID) REFERENCES ART_OBJECT(Id_no)
CREATE TABLE SCULPTURE
(
Material VARCHAR(20),
Height VARCHAR(20),
Weight VARCHAR(20),
Style VARCHAR(20),
ArtID NUMBER
);
ALTER TABLE SCULPTURE
ADD CONSTRAINT SCStyle FOREIGN KEY (Style) REFERENCES ART_OBJECT(Style)
ALTER TABLE PAINTING
ADD CONSTRAINT SCID FOREIGN KEY (ArtID) REFERENCES ART_OBJECT(Id_no)
CREATE TABLE STATUE
(
Material VARCHAR(20),
Height VARCHAR(20),
Weight VARCHAR(20),
Style VARCHAR(20),
ArtID NUMBER
);
ALTER TABLE STATUE
ADD CONSTRAINT STStyle FOREIGN KEY (Style) REFERENCES ART_OBJECT(Style)
ALTER TABLE STATUE
ADD CONSTRAINT STID FOREIGN KEY (ArtID) REFERENCES ART_OBJECT(Id_no)
CREATE TABLE OTHER
(
Type VARCHAR(20),
Style VARCHAR(20),
ArtID NUMBER
);
ALTER TABLE OTHER
ADD CONSTRAINT OStyle FOREIGN KEY (Style) REFERENCES ART_OBJECT(Style)
ALTER TABLE OTHER
ADD CONSTRAINT OID FOREIGN KEY (ArtID) REFERENCES ART_OBJECT(Id_no)
CREATE TABLE COLLECTIONS
(
CName VARCHAR(20) PRIMARY KEY,
Type VARCHAR(20),
Description VARCHAR(20),
Address VARCHAR(20),
Phone VARCHAR(20),
Contact_person VARCHAR(20),
);
CREATE TABLE EXHIBITIONS
(
Name VARCHAR(20) PRIMARY KEY,
Start_date DATE,
End_date DATE,
E_id NUMBER,
);
ALTER TABLE EXHIBITIONS
ADD CONSTRAINT EID FOREIGN KEY (E_id) REFERENCES ART_OBJECT(Id_no)
CREATE TABLE EXHIBITS_IN
(
Id_no NUMBER,
CName VARCHAR(20)
);
ALTER TABLE EXHIBITS_IN
ADD CONSTRAINT EIID FOREIGN KEY (Id_no) REFERENCES ART_OBJECT(Id_no)
ALTER TABLE EXHIBITS_IN
ADD CONSTRAINT EIName FOREIGN KEY (CName) REFERENCES COLLECTIONS(CName)
Database relations table for reference:
DB Relations Table
EER diagram for reference:
EER Diagram
create table ward
(
wnum int primary key,
wname varchar(30),
phoneno int,
wloc varchar(50),
chnursename varchar(20) constraint ward_fk references charge_nurse
);
create table charge_nurse
(
chnurse varchar(20) constraint charge_nurse_pk primary key,
stnum int constraint charge_nurse_fk references staff
);
create table staff
(
stname varchar(20),
stnum int constraint staff_pk primary key,
addr varchar(20),
phoneno int,
stposition varchar(30),
specality varchar(30) unique,
shift varchar(10),
noofhoursperweek int
);
create table generalsupplies
(
itnum int constraint generalsupplies_pk primary key,
itname varchar(20) unique,
quantityinstock int,
reorder varchar(10),
despt varchar(10),
costperunit int
);
create table pharmasupplies
(
dnum int constraint pharmasupplies_pk primary key,
dname varchar(30) unique,
despt varchar(20),
dosage_Mg int,
quantityinstock int,
reorder varchar(10),
costperunit int
);
While creating the below table I am facing problem:
ORA-02264: name already used by an existing constraint
create table centralsupplies
(
wardnum int constraint centralsupplies_fk references ward,
itemnum int constraint centralsupplies_fk references generalsupplies,
drugnum int constraint centralsupplies_fk references pharmasupplies,
quantity_required varchar(20),
staffname varchar(10) references staff(stname),
staffnum int constraint centralsupplies_fk references staff,
regnum int unique,
dateord date,
daterec date
);
How do I solve this problem?
You use 3 times the same constraint name centralsupplies_fk in your centralsupplies table.
3 constraints = 3 constraint names
Your create table statement has four foreign keys all called centralsupplies_fk. That is not allowed: constraint names must be unique within a schema. You must give each one a different name.
It is usual practice to include the referenced table in the key name. So
create table centralsupplies
(
wardnum int constraint centralsupplies_ward_fk references ward,
itemnum int constraint centralsupplies_generalsupplies_fk references generalsupplies,
drugnum int constraint centralsupplies_pharmasupplies_fk references pharmasupplies,
quantity_required varchar(20),
staffname varchar(10) references staff(stname),
staffnum int constraint centralsupplies_staff_fk references staff,
regnum int unique,
dateord date,
daterec date
)
Also you have another foreign key constraint on STAFFNAME which you have not named. You do not need to name constraints, the system will generate a unique one for you, but it's generally a good idea to name them, not least because it is easier to diagnose relational integrity error messages with meaningfully named constraints.
However, in this case the correct solution is to drop the STAFFNAME column. You already have a foreign on the STAFF table, and you should join to that table whenever you need to display a value for STAFFNAME. Besides you do not have a unique constraint on staff.stname column, so the foreign key statement will fail: foreign keys can only reference primary or unique keys.
I'm trying to create a database for a project that works like a school with some teachers,courses,classes etc.
I need to create a new Table involving 2 tables however I'm getting an error when creating the table Classes_Teachers.
There are no primary or candidate keys in the referenced table 'Classes' that match the referencing column list in the foreign key 'FK__Classes_T__Class__11007AA7'.
Here is my SQL Code:
CREATE TABLE Teachers(
id INT,
name varchar(40),
email varchar(30) FOREIGN KEY REFERENCES Users(email),
PRIMARY KEY(id)
);
CREATE TABLE Courses(
name varchar(20),
acr varchar(4),
teacher int FOREIGN KEY REFERENCES Teachers(id),
PRIMARY KEY(acr)
);
CREATE TABLE Classes(
id varchar(2),
courses_acronym varchar(4) FOREIGN KEY REFERENCES Courses(acr),
year_Semesters varchar(5),
PRIMARY KEY(id,courses_acronym),
);
CREATE TABLE Classes_Teacher(
Classes_id varchar(2) FOREIGN KEY REFERENCES Classes(id),
Teachers_id INT FOREIGN KEY REFERENCES Teachers(id),
courses_acronym varchar(4) FOREIGN KEY REFERENCES Classes(courses_acronym),
PRIMARY KEY(Classes_id,courses_acronym)
);
The error comes from the foreign keys declared in table Classes_Teacher. To relate to the Classes table, you want to use both primary columns of the referred tables, instead of twn separated foreign keys.
Consider:
CREATE TABLE Classes_Teacher(
Classes_id varchar(2),
Teachers_id INT FOREIGN KEY REFERENCES Teachers(id),
courses_acronym varchar(4),
FOREIGN KEY (Classes_id, courses_acronym) REFERENCES Classes(id, courses_acronym),
PRIMARY KEY(Classes_id,courses_acronym)
);
Demo on DB Fiddle.
In table Classes you define a Primary Key on two columns. This is probably not needed if courses_acronym depends on ID.
CREATE TABLE Classes(
id varchar(2),
courses_acronym varchar(4) FOREIGN KEY REFERENCES Courses(acr),
year_Semesters varchar(5),
PRIMARY KEY(id),
);
In table Classes_Teacher you refer Classes twice. Shouldn't the second Foreign Key refer Courses?
CREATE TABLE Classes_Teacher(
Classes_id varchar(2) FOREIGN KEY REFERENCES Classes(id),
Teachers_id INT FOREIGN KEY REFERENCES Teachers(id),
courses_acronym varchar(4) FOREIGN KEY REFERENCES Courses(acr),
PRIMARY KEY(Classes_id,courses_acronym)
);
I am new to SQL server,I have the table with composite primary keys as follows:
CREATE TABLE Systems
(
Layer VARCHAR(25),
System_Name VARCHAR(25) ,
Sub_System_Name VARCHAR(25),
Q1_Scope VARCHAR(25),
Q2_Scope VARCHAR(25),
Q3_Scope VARCHAR(25),
Q4_Scope VARCHAR(25),
U20 VARCHAR(25),
Extracts_Requested VARCHAR(25),
Extracts_Received VARCHAR(25),
Control VARCHAR(25),
Extracts_Reviewed VARCHAR(25),
Control_Frequency VARCHAR(25),
System_Layer_Exists VARCHAR(25),
Notes VARCHAR(25),
Alias VARCHAR(25),
Extract_Requested_Date VARCHAR(25),
PRIMARY KEY (Layer,System_Name,Sub_System_Name)
);
Now I want to refer the columns like Layer,System_Name,Sub_System_Name in to another table say:
CREATE TABLE Owns_System
(
Name VARCHAR(25) FOREIGN KEY REFERENCES Employees(Name),
Layer VARCHAR(25) FOREIGN KEY REFERENCES Systems(Layer),
System_Name VARCHAR(25) FOREIGN KEY REFERENCES Systems(System_Name),
Sub_System_Name VARCHAR(25) FOREIGN KEY REFERENCES Systems(Sub_System_Name),
Role VARCHAR(25)
);
It shows me the error that the table Systems
"There are no primary/candidates keys in the table Systems".
Can anyone please suggest me what I am doing wrong here.
You don't want to declare 3 separate foreign key constraints - you need to use a single composite foreign key:
CREATE TABLE Owns_System
(
Name VARCHAR(25) FOREIGN KEY REFERENCES Employees(Name),
Layer VARCHAR(25),
System_Name VARCHAR(25),
Sub_System_Name VARCHAR(25),
Role VARCHAR(25),
constraint FK_Blah FOREIGN KEY (Layer,System_Name,Sub_System_Name)
REFERENCES Systems(Layer,System_Name,Sub_System_Name)
);
A FOREIGN KEY is a field (or collection of fields) in one table that
refers to the PRIMARY KEY in another table
So Before referring a column (PKey) in one table (Say TableA) as Foreign key in another Table (TableB), You need to make sure that the Column PKey is the Primary Key / Unique Key in TableA.
The Table Design Might be something like this
CREATE TABLE TableA
(
PKey INT PRIMARY KEY,
FullName VARCHAR(50),
Email VARCHAR(255) NOT NULL UNIQUE
)
CREATE TABLE TableB
(
Id INT PRIMARY KEY,
FKey INT FOREIGN KEY References TableA(PKey),
Email VARCHAR(255) FOREIGN KEY References TABLEA(Email)
)
Please Refer this W3 Schools link for more in detail on Foreign key
I cant find a good anwser too my question, I want to create a table that have references too one table but i use it twice in the code..
Vårdnadshavare VARCHAR(11),
FOREIGN KEY (Vårdnadshavare) REFERENCES Person(Personnummer),
Barn VARCHAR(11),
FOREIGN KEY (Barn) REFERENCES Person(Personnummer)
But then i get the error:
ORA-02256: number of referencing columns must match referenced columns
I understand that my reference is wrong some how but can´t figure it out...
Sorry for the swedish words!!
Create table Ärende(
Ärendenr VARCHAR(50) NOT NULL,
Handläggare VARCHAR(50),
FOREIGN KEY (Handläggare) REFERENCES Handläggare(Anställningsnr),
Vårdnadshavare VARCHAR(11),
FOREIGN KEY (Vårdnadshavare) REFERENCES Person(Personnummer),
Barn VARCHAR(11),
FOREIGN KEY (Barn) REFERENCES Person(Personnummer),
In Datum VARCHAR(50),
Ömmande Skäl VARCHAR(5),
Förskola VARCHAR(50),
FOREIGN KEY (Förskola) REFERENCES Förskola(IDnr),
PRIMARY KEY (Ärendenr)
);
I have used Person(Personnumer) as a reference before just not twice in createing a table..
I created this script which does work. Probably there is an issue with your indexes:
create table handläggare
( anställningsnr varchar(50) primary key
)
create table person
( personnummer varchar(11) primary key
)
;
create table förskola
( idnr varchar(50) primary key
);
create table ärende
( ärendenr varchar(50) not null
, handläggare varchar(50)
, foreign key (handläggare) references handläggare(anställningsnr)
, vårdnadshavare varchar(11)
, foreign key (vårdnadshavare) references person(personnummer)
, barn varchar(11)
, foreign key (barn) references person(personnummer)
, indatum varchar(50)
, ömmandeskäl varchar(5)
, förskola varchar(50)
, foreign key (förskola) references förskola(idnr)
, primary key (ärendenr)
);
There are two other issues in your script above which I edited:
In Datum should be one name, not separated by a space;
Ömmande Skäl, same as above.
In you create statement you have provided 4 referenes:
Handläggare VARCHAR(50),
FOREIGN KEY (Handläggare) REFERENCES Handläggare(Anställningsnr),
Vårdnadshavare VARCHAR(11),
FOREIGN KEY (Vårdnadshavare) REFERENCES Person(Personnummer),
Barn VARCHAR(11),
FOREIGN KEY (Barn) REFERENCES Person(Personnummer),
Förskola VARCHAR(50),
FOREIGN KEY (Förskola) REFERENCES Förskola(IDnr),
Please check that all 3 distinct columns are of same data type.
That means:
Handläggare(Anställningsnr) should be VARCHAR(50)
Person(Personnummer) should be VARCHAR(11)
Förskola(IDnr) should be VARCHAR(50).
As per me i think you are making a mistake in
Förskola(IDnr) should be VARCHAR(50)
which should have been a Number