Update table value when another table is updated? - mysql

I am working with 3 tables in MySQL: students, registration, courses.
If under table courses I also have student_ID and student_Name, is it possible then if I update the the student_Name on the students table, it will also be updated on the courses table?
Or that when I insert a student_ID to the registration table, it will also insert the student_name that correspond with it to the column?
(all 3 table have the columns student_ID and student_name, I am trying to find a way to update the value on only one place..)

you can make student_id field in all tables as field with primary index. ater it try to add this feild as foreighn key to other tables

Related

How to insert into table a new row then do nothing if already exists(without UNIQUE key)

Let's say I have these two tables. Where I insert employees to employee table coming from the staging table.
staging table:
id
employee_id
name
1
12
Paul
2
13
Kyle
employee table
id
employee_id
name
5
4
Will
6
13
Kyle
Now, on the employee table let's say I'd like to copy what's on my staging table currently, using the INSERT SELECT INTO statement, Paul will be inserted but I don't want Kyle to be inserted since he's on the employee table already(employee.employee_id is the defining column).
I know this could be done by just setting a unique or primary key, on employee_id and just using the statement ON DUPLICATE KEY UPDATE then do nothing by just setting them back to their original values.
I'm new to SQL, and I'm stuck with the solution setting a UNIQUE key and ON DUPLICATE KEY UPDATE statement, but I'd like to know how to do this without that solution I mentioned?
First of all, you should keep in mind that the decision whether to create unique or primary keys or not does not depend on how to create insert statements or such. It's a matter of what your table should do and what not.
In order to achieve your goal, you can add a where to your insert statement which excludes the already existing entries, as example:
INSERT INTO employees (id, employee_id, name)
SELECT id, employee_id, name
FROM staging
WHERE employee_id NOT IN (SELECT employee_id FROM employees)
Break the problem down into its constituent parts
get a list of employees who are in the staging table but not in the target table
insert those records only
Part 1 can be achieved with a Left Join and Where condition that the target table is null. Wrap that up as a CTE and then use it in 2)

It says student_id doesn't exist in table, but it does?

It says student_id doesn't exist, but it's clearly there, I just want to connect these tables together. (Mysql 5.7)
student_id is missing on your Subject table, add student_id int unsigned not null in your query to create Subject table before you put the foreign key.
You need to have the column 'student_id' in the 'subject' table. The error denotes the 'subject' table.
You are trying to mark a column 'student_id' in 'subject' table as foreign key that references to column 'student_id' in 'students' table.
That's because you doesn't create student_id in subject table
you must create student_id after subject_id column
As other comments you Subjects Table doesnt have student_id field, and probably shouldnt have it. Otherwise you would be repeating the same data for each student attending that course.
So what you do:
Create the Tables Students, Subjects like you have it now (without the foreign key to Student)
Create a junction table student_subject {subject_id, student_id} where you store what subjects are enrolled each student.
Create Foreign keys to Students, Subjects
Then if you want get what subjects is enrolled one student.
SELECT st.*, su.*
FROM Students st
JOIN Student_Subjects ss
ON st.student_id = ss.student_id
JOIN Subjects su
ON ss.subject_id = su.subject_id
WHERE st.student_id = #student_id

Unable to figure how to manage foreign keys for 'project-customers-contacts' relation

I have a CUSTOMERS table and a CONTACTS table the relation between them is one to many obviously.
also I have PROJECTS table and PROJECT_CUSTOMERS table with relation one to many and with relation one to one between CUSTOMERS and PROJECT_CUSTOMERS.
my problem is that I have a fifth table PROJECT_CONTACTS ....I can't figure which tables shall I refer to in this tables, currently I am refering to PROJECT_CUSTOMERS and CONTACTS table, is this correct or there is something better ?
Your title refers to "foreign keys" but your question just seems to be about what columns should go in what tables.
First, decide what situations can arise and what you want/need to say about them. (This will your tables, columns, candidate keys, foreign keys and constraints.)
Every table holds rows that make some predicate (statement template parameterized by column names) true. Your design seems to have:
CUSTOMERS(id, ...) -- ID identifies a customer and ...
CONTACTS(id, ...) -- ID identifies a contact and ...
PROJECTS(id, ...) -- ID identifies a project and ...
PROJECT_CUSTOMERS(pid, cust_id, ...) -- project PID has customer CUST_ID and ...
PROJECT_CONTACTS(pid, cont_id, cust_id)...)
-- project PID has contact CONT_ID and project pid has customer CUST_ID and ...
A foreign key has a table & column list referencing a table and column list that forms a candidate key. It says that lists of values in the first table appear as lists of values in the second table. Where that is so, declare a foreign key.

Mysql Cascade on update does not work properly

I am developing an application in java. With this application, I want to store data about students and payments so I decided to construct a database with MySQL.
I created a schema with two tables(students,payments).
The students table has many columns, one of which is called student_id and is the primary key.
The payments table has many columns one of which is called payment_id(Primary key) and student_id which is a foreign key from the primary key of students table.
I set the foreign key to cascade on Update, so if i update a value in the student_id column in students table the value will be updated automatically in the column student_id in payments table. The problem is that when i change a value in a row at the student_id column in students table I can see that the changes have been applied through a select query(SELECT * FROM students) in students table. But when i execute a select query(SELECT * FROM payments) in payments table it seems that the changes have not been applied in the column students_id. Please note that autocommit is enabled. Only when I commit the select execution on payments table will show the right results(the updated row).

having trouble with foreign key queries

I'm new to SQL and I'm having a hard time figuring out how to execute queries with foreign keys on MySQL Workbench.
In my example, I have three tables: people, places, and people_places.
In people, the primary key is people_id and there's a column called name with someone's name.
In places, the primary key is places_id and there's a column called placename with the name of a place.
People_places is a junction table with three columns: idpeople_places (primary key), people_id (foreign key), and places_id (foreign key). So this table relates a person to a place using their numerical IDs from the other two tables.
Say I want the names of everyone associated with place #3. So the people_places table has those associations by number, and the people table relates those numbers back to the actual names I want.
How would I execute that query?
Try this to find all the people names who are associated with place id 3.
SELECT p.name
FROM people as p
INNER JOIN people_places as pp on pp.people_id = p.people_id
WHERE pp.places_id = 3
OK, so you need to "stitch" all three tables together, yeah?
Something like this:
select people.name
from people -- 1. I like to start with the table(s) that I want data from, and
, people_places -- 2. then the "joining" table(s), and
, places -- 3. finally the table(s) used "just" for filtering.
where people.people_id = people_places.people_id -- join table 1 to table 2
and people_places.place_id = places.place_id -- join table 2 to table 3
and places.name = "BERMUDA" -- restrict rows in table 3
I'm sure you can do the rest.
Cheers. Keith.