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.
Related
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.
Sorry If my question seems too obvious but I still can't find the logic between a relationship of a database model.
ONE TO ONE:
If I have a customer that has only one address where do I have to put my foreign key ?
I first thought it would be a good idea to put it on the customer's table; one column with a foreign key referring to an id of an address.
Because if I need to create a customer, first I would need to create an address.
But I have found some example on the internet
, where they put a foreign key in the address' table, referring to an id of a customer
ONE TO MANY :
And the same question would apply if a customer can have multiple addresses, in which table, should I put my foreign key ?
The foreign key goes on the "many" side.
For example, if a sales_order is associated with at most one customer, and a customer can have zero, one or more sales_order
Then we put customer_id in the sales_order table, as a reference to the (unique) id column in customer table.
This means that we will first need to add a row to customer before we can add a sales_order for that customer. (The foreign key constraint will prevent us from adding a row in sales_order that has a value in customer_id column that doesn't exist in id column of customer.
--
For the special case of a one-to-one relationship, we can implement that the same way as a one-to-many, with an additional UNIQUE constraint on the foreign key column. It really comes down to deciding which direction of the relationship is mandatory, and which is optional. Basically, which table will we add a row to first? The table where we add rows later will have the foreign key constraint referencing the table that we previously added a row...
So I have 3 tables:
Table: Albums
Columns: Id, Name, Description, Author, Folderpath, Thumbnail, Upvotes, Downvotes
Table: AlbumsConnection
Columns: Id, AlbumId, AlbumImagesId
Table: AlbumImages
Columns: Id, InAlbum, Imagepath
So far I've been using these tables without actually using foreign keys. Am I supposed to use foreign keys here? I understand that I'd have to add 2 foreign keys to AlbumsConnection, 1 for each table and each foreign key will reference to the primary keys ( which are the ids ) of the other 2 tables. Is that correct?
Foreign keys help ensure relational integrity of the database. There is no requirement for declaring them explicitly, but it is a good idea, particularly if you are learning to use databases.
The foreign key let's the database know that a column in one table is related to a column in another table. I don't think MySQL's optimizer uses this information explicitly, although it does create an index on the foreign key column (unlike most other databases).
In addition, a declared foreign key relationship can help you deal with changes to the database. It will prevent invalid albums from being inserted into the junction table. If you delete an album, it gives you control over how the deletion and updating is handled (via cascading constraints).
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
I have 2 tables:
group_config (id_group_config, owner_id, name_group_config)
group (id_group, id_group_config, name_group) - id_group_config is FK to group_config
So basically, a user (owner_id) creates a group_config, then creates a group which references this group_config. Pretty standard, but I want a constraint that the name_group must be unique per owner_id.
Only way I found to do this would be to replicate the owner_id on the group table too, which could cause inconsistencies.
Am I missing something?
thanks
Since a group_config can't exist without owner_id You should make the PK of group_config a composite PK consisting of owner_id and group_config_id. You need to be careful about referential integrity constraints here e.g. what happens when you delete an owner?
This will automatically make the FK in group be that composite key.
To make the name_group unique per owner in group table, you create a unique index composed of the columns owner_id and the name_group.