Create two columns with pairwise uniqueness - mysql

Is it possible, in MySQL, to create two unique indexes, together?
For example, there is a table that holds two IDs - customer ID and product ID.
I would like to create uniqueness for the pair only.
In simple words, combination of user and product can be present only once.
The same user with another products can be present as much as needed.
And the same product with another users can be present as much as needed.
But the pair itself to be unique. How?
This query actually has an OR relation. I am looking for an AND relation.
ALTER TABLE top ADD CONSTRAINT unique_pair UNIQUE (uid, pid)

This does what you describe:
ALTER TABLE top ADD CONSTRAINT unq_top_unique_pair UNIQUE (uid, pid);
As does:
CREATE UNIQUE INDEX unq_top_uid_pid ON TOP(uid, pid);
Both of these create a unique index, which is then used for enforcing the constraint.

Related

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.

Adding column on specific row location

Its just a quick question :
I have user table and it has fields name, address and books_bought. books_bought is foreign key and its value is some PK from other table. Now after 1st insert, I will fill out all of this fields , but after second INSERT I want only to add a additional books_bought, so that am creating array of books_bought values?
You're doing it the wrong way around - this is a one-to-many relationship i.e. many books bought to one user. You need to have the foreign key on the many side of the relationship, so instead of having a foreign key to books_bought on the users table, add a foreign key to users on the books_bought table.
If you have a books table and a users table, then this is a many-to-many relationship and you will need a link table to go between them to hold the foreign keys.
You should not have more than one book in the books_bought cell because it will violate the atomicity constraint fo the database tables. You have to have a separate entry for each book_bought. This would cause a lot of redundant information as name, address would be repeated for each book bought by a specific person.
To solve this, you have to split the table into something like this:
R1(primary_key , name , address) and R2(foreign_key , books_bought)
Here foreign_key refers to primary_key of R1

Strategies for preventing the insertion of duplicate table rows

Scenario:
User A and B executes at the same time select id from Product where id = ?, if the there are no results, both create a new product with given ID.
Problem:
This could lead to the creation of duplicate rows.
Question:
What are the possibles strategies to prevent that? I know that I can use compound/unique keys, to guarantee this, but are there any other strategies? Is there any SQL statement to lock query with same parameters?
You can use unique constraints
ALTER TABLE Persons ADD UNIQUE (P_Id)
or
ALTER TABLE Persons
ADD CONSTRAINT uc_PersonID UNIQUE (P_Id,LastName)
That way it would be impossible for a duplicate to be inserted.
Put a UNIQUE key on the field in question:
https://dev.mysql.com/doc/refman/5.0/en/constraint-primary-key.html

insert with condition in mysql

in DB tables i've: Patient Table, PatientBasicInfomation Table, PatientImageFindings Table..
i've multiple Questions depend on this design..(note that i'm beginner in DB)
1) if i have for each Patient an ID.. so according to DB concepts both PatientBasicInfomation, PatientImageFindings should have this key (ID) as a foreign key?!
2) in the Patient Table i should reference to the PatientBasicInfomation, PatientImageFindings by using their private keys so they will be in Patient Table as a foreign keys?! am i thinking correct...
3) Now my big problem: i want to insert in PatientImageFindings Table a record but under condition ID + Date (where Date is an important Field in PatientImageFindings Table, i don't know if i should put it as a private key or not..), how could i do this insertion statement in my java class..(Insertion under conditions)
What you want to do is add a UNIQUE constraint across multiple columns.
This question provides an answer to do just that:
How do I specify unique constraint for multiple columns in MySQL?
alter table votes add unique index(user, email, address);
Unless you have another reason, you should enforce this at the database level and treat exceptions as they arise after attempted INSERTs.

In a One to One relationship should i drop one of the table's id column?

I have the following 2 tables in MySQL:
Customer(Id, Firstname, Lastname...)
Bonus(Id, CustomerId, Value, ...)
The relation is One-To-One, every customer has only one bonus.(the CustomerId is unique in the Bonus Table)
Q: Should I drop the Id column of the Bonus table?
(I want to know why or why not)
I would remove the Bonus.Id coulmn and make Bonus.CustomerId the PK. Doing this will remove the need to have a unique constraint on the Bonus.CustomerId column, since it will now be a PK. Anyone looking at the table will see the one-to-one more clearly without the Bonus.Id coulmn. You won't need an index on Bonus.CustomerId, the PK index will be all you need, so less disk space and memory cache wasted. Also, if you ever need a FK to the Bonus table, you you would use the CustomerId value (the new PK), which can be used to get back to Customer or Bonus tables, not just Bonus.
I assume it isn't actually a true one-to-one because you could presumably have a Customer without a bonus row. SQL-style foreign key constraints are always optional on the referencing side of any relationship.
I agree the Bonus.Id column appears to be completely redundant.
if it's ono-to-one, why is there any extra table? you could instead put "bonusvalue" into your customer table.
(else: yes, you can drop the id of the bonus-table, the customer-id is the primary key and the "id" is completely redundant)