Theoretical solution about implementing foreign key related data - mysql

How would you implement the following?
I would like to insert data to mysql tables. Let's imagine there are two tables where a foreign key relation exists. First, i insert a row that has a primary key that should be inserted as a foreign key to one of the rows to the other table. So when i would like to insert the foreign key and it's related data, i have to know the primary key of the related row in the other table. As i am a beginner, my solution would be the following: I would insert a field value with a particular data to the original table so that the inserted value could be used to retrieve the primary key with a SELECT, and then insert the retrieved primary key as the foreign key to the related rows of the other table.
Although I don't know a better solution, I think this would be a very clumsy way to implement this logic. There must be a better way of doing this.

Your solution won't work because if you are inserting not unique data, you may not retrieve the appropriate primary key. MySQL offers LAST_INSERT_ID() function for this. Just insert row into your primary key table and then use SELECT LAST_INSERT_ID(). It returns last primary key value inserted into your original table (last insert query) and now you can use it as foreign key in related table.

Related

How to prevent duplicate row insert in MemSQL?

I have AUTO_INCREMENT PRIMARY KEY and another column that I can't set UNIQUE because unlike standard RDBMS like MySQL or PostgreSQL, MemSQL only allow only one of them, not both.
Is there workaround to prevent duplicate rows without sacrificing the auto_increment column?
I can use unique as primary key and use atomic counter in other product/service like Redis/atomic variable, but when I need to update the unique column I have to delete it first then reinsert, which is bad/unpreferred way for me..
MemSQL does support multiple unique keys together with a primary key. However, MemSQL requires that the columns in each unique or primary key must be a superset of the columns in the shard key - i.e. that all values that would be considered duplicate under each unique key have the same shard key, so that they get mapped to the same partition. This further implies that all the unique/primary keys must share at least one column in common.
For your case, it is not possible to have both a unique/primary key on the autoincrement column and the other column. But you can have a unique/primary key on the other column, without a unique key on the auto_increment column - just define it as a non-unique key. The automatically generated values will still be unique. Do note that then the table won't be able to enforce uniqueness if you manually insert values that are duplicate with other auto_increment values.

is there `on duplicate key insert` in MYSQL?

I am learning MySQL and used the ON DUPLICATE KEY UPDATE, what this does, if I am not mistaken, when it found a duplicate; it will update the row. So, to my question, is there something like ON DUPLICATE KEY INSERT? like when it found a duplicate, it will still insert the data into the table?
No, there is no way to insert a row that has duplicate values in columns that are constrained against duplicate values. If you could, the result would be that the database would be in a state that violates its own constraints.
You would have to drop any unique key or primary key constraints on the table to allow duplicates in the respective columns.

mysql - multiple foreign keys with different columns of different table

Would be great if someone provides me a hint to achieve this below scenario.
two foreign keys for table3
FK1 referring to table1 column1
FK2 referring to table 2 column1
Now when i insert a row to table 3 i want only FK1 to be active .
In other words right now mysql will not allow me insert a row until all foreign key constraints are satisfied.
For me- my row should get inserted either if FK1 or FK2 is satisfied. Is there a way I can achieve this ?
There is no real simple way to have an either-or reference constraint; but you can make both FK constraints optional by making the referencing fields nullable.
This would not prevent a row from being inserted with both references null though. To prevent that possibility, you could use a BEFORE INSERT trigger to "throw an error" if an attempt to insert such a row was ever made. Then, you might want to consider a BEFORE UPDATE trigger to prevent rows from being changed in a similar manner.
This is an atypical request, and might indicate that your design has a problem. That being said, one idea which might work would be to create a new table containing just a single column. This column is a primary key and will be populated via an after insert trigger on both table1 and table2. The trigger will only insert if the value is not already in the table. Now, in table3 you may put a foreign key constraint referencing this new column. For the constraint to pass, there only need be one of the two tables having this primary key value.

Upsert without using on duplicate key MYSQL

I'm trying to write a mysql statement that upserts into a table without having to use the primary key.
I know of the on duplicate key command but I can't use it here since I'm checking for the uniqueness of two columns that aren't primary keys. I know it would be better to just make these two primary keys, but I can't since this was the schema that was given.
The schema looks like this:
tbl_order_detail
key_order_detail
key_order
key_product
some_other_keys
If the key_order,key_product pair is unique then I do a regular insert.
If they aren't unique then I update the row.
Any suggestions?
ON DUPLICATE KEY UPDATE works not only with primary keys, but with any unique constraints.
So just create a composite unique index for (key_order, key_product) columns and use it.

problem setting up primary keys in mysql database... help?

I have a table set up on a mysql database called "access" with three columns called:
rights_id, (PRIMARY KEY)
username,
name,
In the rights_id column the user can only input 3 different values ("1","2", or "3") 1 means resource, 2 means manager, and 3 means administrator. my problem occurs when there are more than one row with the same rights_id (ie: more than one administrator).It displays an error that tells me i can't have a duplicate entry for the PRIMARY KEY... i was wondering if there was a way to supress this error and allow me to do this? im using vb.net to interact with my MYSQL database running on a Windows 7 OS. Thanks!
rights_id is primary key. You can have only distinct values of primary keys in a table. So consider another primary key or do not use rights_id column this way. You should learn more about relational databases if you would like to use them.
In my opinion the best solution there is to add anothe column id which could be a primary key (you could also set multi-column primary key but this wouldn't fit your data in my opinion).
I'm not sure what "name" means in that table. If it's safe for me to ignore it . . .
If each username can have only one "rights_id", then the primary key should be username. If each username can have more than one "rights_id"--if user Catcall can have rights_id 1 and 2 at the same time--then your primary key should be the pair of columns (rights_id, username).
Since MySQL doesn't enforce CHECK constraints, you should have a separate table of rights id numbers, containing three rows.
create table rights_ids (
rights_id integer primary key
);
insert into rights_ids values (1);
insert into rights_ids values (2);
insert into rights_ids values (3);
Then you can set a foreign key constraint that will prevent any numbers besides those three from appearing in the table named "access". Something like
alter table access
add constraint foreign key (rights_id) references rights_ids (rights_id);
Create a compound PRIMARY KEY of rights_id and username (if usernames are unique that is).
No, you can't suppress that error. The issue is that rights_id is NOT your primary key.
The primary key must be able to uniquely identify a row in your table. If you can have more than 1 rights_id entry, then that is NOT able to fulfill the role of a primary key.
Read this wiki article about unique keys (a primary key is a specific type of unique key).
As Shef pointed out, you'll likely want to use a compound primary key of rights_id and username if that combination actually uniquely identifies a single row in the table.