MySQL unique constraint over two column that are in different tables - mysql

First I have one table Device that have id, account_id and name .That table has unique constraint over its two column(account_id, name) , meaning we can only have same name if account_ids are different.
Now I split it into two tables Device(id, account_id, profile_id) and Profile (id, name).
My question is how I can apply unique constraint as before. I don't allow same profile name with the same account_id. How can I do that in MySql(5.5)? I think I need unique constraint over two tables.
Is it possible?

Related

MySQL items of an order without primary key?

i have two tables in my database which belongs to each other.
mp_order and mp_order_items.
mp_order has the main informations of an order of a customer like adress, date etc.
(order_id, customer_company, customer_name, customer_adress, order_date, ... [etc.])
mp_order_items has the priducts/items which was ordered
(order_id, item_id, item_qty)
Due to order_id and item_id can repeat (but not in combination) i cant set one column as primary key.
Should i implement another column as unique identifier for the single entries or is it valid to have a table without primary key?
You have two options:
Define a primary key on (order_id, item_id)
Define a synthetic primary key, such as an auto-incremented column.
I prefer the second method. It is more flexible for the future:
Perhaps an order could contain the same items, but with different pricing or shipping addresses or shipping times.
The rows are uniquely defined with a single number, which makes it easier to find them if you need to modify rows in the future.
The rows are more easily referenced in another table, for instance, if you had a returns table or if the items.
Of course, having a composite primary key also works and is a very viable method for implementing the logic as well.
Since, you requirement is that order_id and item_id can not repeat in combination meaning: (ord_134, itm_123) can't repeat itself then, I believe you need to create a COMPOSITE KEY.
PRIMARY KEY(order_id, item_id)
Basically, a combination of both Order Id and Item Id is what will uniquely identify a record in the table.
There is a caveat, if required, while defining a FOREIGN KEY, you can't link the tables using just order_id. You will need to include all the columns that are part of the COMPOSITE KEY inside the FOREIGN KEY relation.

is Unique Key Constraint creating an index behind the scenes for the table?

I have a table A that has n number of columns: amongst them id (primary key), name and org_id. Names must be unique within org_id, so I am slapping unique key constraint on that pair.
Now, in the application code, I have checks, where I verify if name for my entity is unique across organization.
The query looks like:
SELECT * FROM A WHERE name=? and org_id=?
I have org_id as a foreign key to Organizations table.
In order to speed up queries as I mentioned above, should I introduce an index against the name column? Or, maybe, there is no need, and the Unique Key index (org_id-name) will kick in and make my queries fast?
The UNIQUE constraint is already backed by an index. I assume it takes the form:
constraint uq_1 unique (org_id, name)
Your query should be fast already when using "equality searches", like the one you show, using the predicate:
WHERE name = ? and org_id = ?

Where should the foreign reside in this scenario

I want the relationship to be like a single profile only have one address associated to it. While a single address can be from many profile. Where would I place the foreign key.
Address address_id, state, town, phone, email
Profile username, profile_pic, date_added, password
well since you want one to many relation for address table you should add a foreign key ( let's say addr_id ) to profile table.
That way one address row can be used in multiple profiles
From http://www.mysqltutorial.org/mysql-foreign-key/:
"The customers table is called parent table or referenced table, and the orders table is known as child table or referencing table.
A foreign key can be a column or a set of columns. The columns in the child table often refer to the primary key columns in the parent table."
Looks like you want the foreign key on the user table.
Hope this helps!

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

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.