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).
Related
I have two tables defined as:
dealership_inventory(vin, dealer_id, price, purchase_date)
where vin is the PK and dealer_id is the FK
transactions(transaction_id, dealer_id, customer_id, vin, cpurchase_date, price)
where transaction_id is PK and dealer_id,customer_id, and vin are FKs
Whenever I add a new transaction to the transactions table with an insert statement, I would like to remove that tuple with matching vin from the dealership_inventory table. Is this possible with some type of constraint?
You don't really need to do the removal of the VIN number from the inventory table. Instead, if you want to find out whether a vehicle be still available, just use an exists query, e.g.
SELECT di.*
FROM dealership_inventory di
WHERE NOT EXISTS (
SELECT 1
FROM transactions t
WHERE t.vin = di.vin
);
If, at some later point, the inventory table gets bogged down with items no longer available, you can run a batch job which moves these sold items to another table.
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
I have a question on foreign key settings with regards to bridging tables. I still am unsure of how the deletion process works. My foreign keys are currently all set to On Delete: No Action, so does that mean that in the case of a bridging table, in order to delete records in one or both of the parent tables, I would have to delete the records they feature in in the bridging table first or does it work differently with many-to-many relationships? Apologies if this is a simple, dumb question but it seems pretty difficult for someone new to databases to find clear, simple, easy-to-follow documentation anywhere to explain these things.
The rule is pretty straightforward:
You can't delete a row if some other row exists that references the one you want to delete.
Example: A college photography course is created as a row in the courses table.
INSERT INTO courses SET course_id = 1234, title = 'Photography';
People enroll in the course:
INSERT INTO enrollments SET course_id = 1234, student_id = 9877;
INSERT INTO enrollments SET course_id = 1234, student_id = 9876;
INSERT INTO enrollments SET course_id = 1234, student_id = 9875;
Then the instructor wants to cancel the course.
DELETE FROM courses WHERE course_id = 1234;
This is blocked, because there are rows in enrollments that reference the row in courses.
Likewise, a student may want to withdraw from school this semester. They try to remove their record:
DELETE FROM students WHERE student_id = 9877;
This is blocked, because the student is still enrolled in the photography class.
The enrollments class is a bridging table (I call these intersection tables, but there's no official terminology for these types of tables). It is basically a pair of foreign key columns, which reference the respective tables courses and students.
The foreign key constraints in enrollments require that each of the referenced rows in the other two tables exist. You can't delete either the courses row or the students row while there's an enrollment that references it.
The way to handle this is to delete the dependent row (the one that has the foreign key constraint) before you delete the referenced row.
The optional ON DELETE CASCADE syntax makes a foreign key constraint handle this automatically. That is, deleting a row in courses would automatically delete any rows that reference the course. If you don't use this option, then trying to delete the course returns an error.
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.
In mySQL if I created two tables and on table contains a foreign key referenced to the other table. I have entered data in the other table and for the defined foreign key in that as well. Is there any way for the foreign key to automatically update in the second table without having to type in the entire data?
For example I have a customer table which has 2 fields- customerID and customerName. Another table is say a invoice table which has 3 fields- invoiceID, cost and customerID, where customerID is foreign key. So if I enter data in customer table and invoice table as the number of customers are very large I do not want to keep on entering the customerID in the invoice table. As customerID is a foreign key in the other table, how do I make it automatically reference it from customer table?
You can automatically add a predefined number, like 1, but that will assign all invoices to customer#1.
On the other hand, you can add customers automatically while adding invoices. For that, you need to control the customerID and if a customer with the given ID is not present, insert a new customer to the customer table with empty name. After that, you still need to enter names for those customers, but not by one and that would get things (maybe) easier.
I think you have a misleading idea here. I strongly suggest reading a book or seeing a tutorial online or examining an example project's database tables.
If you have the customer name in invoice data and customer data already populated, you could do
INSERT INTO tbl_invoice (invoiceData, invoiceData2, customerID)
SELECT 'value1', 'value2', customer.customerID
FROM tbl_customer customer
WHERE customer.customerName = 'customer name'
So you wouldn't need to deal with customer ids directly.
There is no automatic feature in MySQL to do exactly this. A trigger could be used for it if needed.