Connecting multiple MySQL tables with Foreign Keys - mysql

I have 3 different tables on MySQL:
Clients
Flights
Bookings
On Clients I have
Name
Last Name
ID
Flight Number (from Flights table)
Booking Number (from Bookings table)
On Flights I have
ID (from Clients)
Flight Number
Company
Date
On Bookings I have
ID (from Clients)
Booking Number
Hotel
Check-in date
Check-out date
I want, after creating a Client, to make what I create on Flights and Bookings tables link on the Clients.
So, each Client has an ID that is also inserted into Flights and Bookings tables. And I need to link that Clients.ID row to it's row on Flights and Bookings.
Is this possible with Foreign Keys?
I tried making Clients.ID a Primary Key and Flights.ID and Booking.ID a Foreign Key, but then when I use INSERT INTO I get :
#1452 - Cannot add or update a child row: a foreign key constraint fails
The SQL query was:
INSERT INTO clients (name, lastname, id) VALUES ('Jane', 'DOE', 123123123);
The SQL query to create the Foreign Keys was:
ALTER TABLE clients ADD CONSTRAINT fk_flightid FOREIGN KEY (id) REFERENCES flights(id);` and
ALTER TABLE clients ADD CONSTRAINT fk_bookingid FOREIGN KEY (id) REFERENCES bookings(id);`
This is my first time coding MySQL, sorry if my explanation is messy.

You have created constraints that make client a child table of flights and bookings. As a consequence, you cannot create new clients (you would need to create parent records in the two other tables first).
In your database design, client should be the parent table, with flights and bookings as children tables.
Consider:
ALTER TABLE flights
ADD CONSTRAINT fk_flights_client_id
FOREIGN KEY (id) REFERENCES client(id);
ALTER TABLE bookings
ADD CONSTRAINT fk_bookings_client_id
FOREIGN KEY (id) REFERENCES client(id);
Other remarks:
columns Flight Number (from Flights table) and Booking Number (from Bookings table) do not make sense in the Client table. These information belong to the children table, and can be accessed through JOINs
I would recommend renaming columns ID to ClientID in all 3 tables; using a name that makes sense functionaly is better than a generic name.

Related

How to Add Foreign Key with Extra Information in MySQL

I have 2 tables departments and locations and locationID is the primary key in locations table and departments table also has that field.
Now after adding a data with locationID 101 in the departments table it won't let me add foreign key referencing locations table because the locations table doesn't have any data with locationID 101 so the data with locationID 101 is extra in the departments table that doesn't belong in locations table.
So how do I fix that without deleting the information from departments table or adding the info in locations table (if possible)?
This is foreign key constraint and the behavior is as expected.
If you don't want it at all, you can make the two tables independent of each other, i.e. don't make locationID a foreign key in department table, rather let it be a different locationID having no relation to the locations table.
Otherwise, you can temporarily disable the foreign key constraint by following Disabling foreign key constraint. But this can lead to a lot of problem in maintaining the tables, in future.

Sql Foreign Key Constraint Problems

Supposed there is table called 'Bill', In the Bill Table, there are 3 columns , ['id', 'client_id', ' client_contact_id'], only 'client_contact_id' is nullable.
The client_id will foreign to the table of 'client', and in the table of 'client_contact',
there are 3 columns such that ['id','name','client_id']. How do i supposed to make a integrity of these tables to make sure the data inserted in table 'bill' with 'client_id' is same as the client_contact_id of client_id.
I want to make integrity of bill.client_contact.client_id is same as bill.client_id in mysql.
I have to mentioned that the Client : Bill are 1:n , Client :Client_contact are 1:n also. Client_contact : Bill are 1:n
Use a foreign key constraint with two columns:
foreign key (client_id, client_contact_id) references client_contacts(client_id, id)
You will need a unique index/constraint on those columns:
create unique index client_contacts_2 on client_contacts(client_id, id);
This is a bit redundant, but solves your problem without using triggers or stored procedures. It also allows NULL values. These should be ignored for foreign key relationships.
If you cannot have bills without a contract, then I suggest you remove client id from the bills table.
If you can have bills without a contract, then you need to create before insert and before update triggers on the bills table. These triggers pull the client if from the client contact table if the client_contact_id field in the inserted / updated record is not null. Alternatively, you need to handle this in your application logic.

How do you check if a table or a column has been used as a Foreign key in another table

I have a table called as customers and it has columns like id, name, address
Now I know for a fact that the id from customers is used as a Foreign key in a few tables but I dont know the whole list.
Is there a way I can find out all the tables that are dependent on records in customers table before I delete a particular record i.e. basically find out if my customers table or column from customers table has been used as a Foreign key in another table

Using foreign keys with multiple primary key combinations

I have a company table with the following fields:
CompanyID
EmailDomain1
EmailDomain2
EmailDomain3
...
I have another table t with the following fields:
CompanyID
EmailDomain
EmailDomainstat
The following are the constraints:
(CompanyID, emaildomain) should be unique in t;
If an emaildomain of a company gets deleted (email domain could be
any of emaildomain1, emaildomain2 or emaildomain3), the
corresponding companyID and EmailDomain combination should get
automatically deleted.
For example, if a company Abc Inc. has 2 email domains abcinc.in and abc.org, and they plan to remove abcinc.in tomorrow, then I should have the corresponding entry in table t deleted.
How do I get this effect with the help of primary and foreign keys ? I know that the delete on cascade has to be used, but I have no clue on how to bind foreign keys with multiple combinations of primary keys.

MySQL Foreign Keys + which table to put the key on

I just starting to use Foreign Keys to enforce the contents of certain Columns and want to be sure I'm putting the Key on the correct table.
In a situation with 2 tables that have a one to many relationship.
e.g.: customers (one) and orders (many).
Where I want to ensure a valid customer number is used in an orders column.
I'm I correct in saying the Foreign Key would placed on the orders table.
e.g.: orders (columnX) references customers (columnX)
Remember that a foreign key is a referential constraint that says the range of values in a table column(s) is being scoped by another table. Specifically, for your example, you would have a customer_id column in your order table that would be a foreign key back to the customer table, meaning that the in all cases you need a valid customer id in order to insert a record in the order table.
Yes, the primary key (typically a customer id) on the one table (customers) should be linked as a foreign key on the many table (orders) in a one-to-many relationship. Each order can belong to only one customer, but a customer can place many orders.