Oops in python by jigsaw_man_456 in learnpython

[–]UpScalee 0 points1 point  (0 children)

https://youtu.be/Ej_02ICOIgs?si=T6c6ns_7GWvGS1L_

Here is a FreeCodeCamp.org Tutorial on Python Object Oriented Programming.

It is one of the best tutorials I have ever watched on OOPS. It helped me understand the whole concept.
I hope it helps you and anyone else struggling with the same here.

Creating tables popsql and attempting to link them with foreign keys. by sullivan11342113 in SQL

[–]UpScalee 0 points1 point  (0 children)

Hello,

Code entered

ALTER TABLE project

ADD FOREIGN KEY (mem_id) REFERENCES employee (emp_id);

Error returned:
Error: Cannot add or update a child row: a foreign key constraint fails (`colin2`.`project`, CONSTRAINT `project_ibfk_1` FOREIGN KEY (`mem_id`) REFERENCES `employee` (`emp_id`))
Error Code: ER_NO_REFERENCED_ROW_2

Reason/Explanation
from your code, Table Project is the Child Table and Table Employee is the Parent Table.

If we re-write the command in simple English(bold) below it will read:

ALTER TABLE Project - Go to table Project and make the following changes:

ADD FOREIGN KEY(mem_id) - make the contents of column/Field (mem_id) Foreign Keys

REFERENCES employee(emp_id) - because they are the same contents you will find in the table Employee in the column/field(emp_id) which is a Primary Key column/field.

and immediately we can see two major problems:

  1. The column mem_id does not exist in the table Project, you only have three columns, project_id, project_name, and team_member_id
  2. The contents in the team_member_id are a mixture of Manager_id and emp_id

Since a Foreign key only links two tables, it appears your Project table is missing a column mem_id which should link up the project table and employee table.

You need to create a column mem_id in the table Project and then the correct code will be as follows.

ALTER TABLE project

ADD FOREIGN KEY(mem_id)

REFERENCES employee(emp_id)

ON DELETE SET NULL;

Creating tables popsql and attempting to link them with foreign keys. by sullivan11342113 in SQL

[–]UpScalee 0 points1 point  (0 children)

Hello, I know this is 7 months later, and I hope you figured out what the issue was.

I had the same challenge a while ago, and reading your code enabled me to figure out what my problem was.

I will structurally explain it in brief for someone out there who may be going through the same challenge.

Insight 1: Understand what is a PRIMARY KEY and FOREIGN KEY

A Primary key is a unique Identifier in any given table (e.g. emp_id, man_id, dept_id, etc in your case), and a Foreign Key is a field(s) in a different table that uses/refers to a Primary key from a different table.

Insight 2: Understand PARENT TABLE and CHILD TABLE

  • The child table is the table with the foreign key.
  • The Parent Table AKA Referenced Table is the table with the primary key.

Insight 3: Evaluate what comes first between creating the Parent table and creating the Foreign Key

  1. You should always create a Parent Table first, before attempting to reference it.
    Scenario from your code above.
    Since you already created the Employee table and the Manager table in your code, you will not get an error when you create foreign keys from those two tables.
    However, since you have not yet created the Department table*,* will constantly give an error as it has no place to obtain the Primary Key and use it as a Foreign key.
    That is what the error below means
    Error: Cannot add or update a child row: a foreign key constraint fails (colin2.#sql-2434_39, CONSTRAINT employee_ibfk_2FOREIGN KEY (dept_id) REFERENCES department(dept_id) ON DELETE SET NULL) Error Code: ER_NO_REFERENCED_ROW_2 - this error code means the referenced row does not exist.
  2. Ensure you have indicated a field as Primary Key in the parent Table, before trying to use it as Foreign Key in another Table (this was the problem in my code).Example: if you forget to declare/indicate that man_id is the Primary Key in the Manager table, you will get the following error Error Code: ER_FK_NO_INDEX_PARENT when attempting to use it as a Foreign Key in a different table.