MySQL: Is it okay when three proses simultaneusly (INSERT, SELECT, INSERT)? - mysql

I have table Context and Criteria with many-to-many relationship, so I created DetailsContext table that stored ID from each table.
Then, Context table is empty and Criteria is master table
I have form to insert a context, firstly I inserted Context table to get an ID (autoincrement) than I selected ID table Context and Criteria by order ID DESC to get latest ID. Then I inserted to DetailsContext table.
Is't ok I just use SELECT ID FROM CONTEXT ORDER BY ID DESC to get latest ID? I afraid it didn't work when many users is accessed together.

If I understand your question correctly, then according to MySQL documentation, you can get the last ID that was inserted into the table with SELECT LAST_INSERT_ID() as long as the id is autogenerated and you are inserting one row per INSERT statement.

Related

Is there a way to insert in MySQL a trigger of corresponding rows for each ID?

I want to create an order which then inserts multiple rows into another table. Which will work like the following:
User selects one or more items from tableOfStuff then places an order which INSERTS a new order into tableOrders. When this insert is created I need to copy the ID created for that order into a TRIGGER which then creates multiple rows in tableOrderDetails. In this table I want a row created for each of the IDs from tableOfStuff and then the one new OrderID (that gets created for the order) repeated in each of these rows.
How would I achieve this in MySQL? Any tutorials or examples would be great or topic areas I should search the net for?
TableOfStuff - PK: ToS_ID
TableOrders - PK: ToO_ID
TableOfStuffOrders - FK: ToS_ID && ToO_ID
ToO_ID would only be one ID but the ToS_ID would be one or more IDs.

Is it possible in MySql to associate inserted data to a specific user?

I just was wondering if is it possible to associate an user to the data inserted in a table.
For example:
I have a table Customers with columns customerID, name and address. Is there any method in MySql to know what user has inserted each row in the table or must I add a column userID in the table Customer to add the user in my SQL Insert statement?.
If I have only one table, it is not a proble to add the userID column. But when i have multiple tables, maybe it becomes a messy task.
Thanx in advance.
If you are trying to find out the user_id caused the insert then NO, there is no other way than you storing it explicitly likewise you already have thought of.
If there is multiple tables for which you want to store the same information; then you can probably have a separate table where you can have the user_id column and can use a AFTER INSERT TRIGGER to insert the user id in this table.
No, no such functionality is provided by MySQL. You'll have to add a column for user_id in your table(s) and insert the user id yourself.

to select and insert from one table to another

In master table I have two field named 'id-->AutpIncrement,Primary key','designation-->varchar'.In registration table 'designation-->INT,Foreign key of Id in designation table'.Now what I have to do is that I have to fire select query i.e. select id from designation where name=;something like this and than have to store it using String.getParameter.After doing all this have to pass result set query
i.e. rs.getInt(1);
and finally int id have to insert.
And one thing more in registration form designation is in drop down menu and done coding as dynamic means designation details appear on the web browser by fetching information from database.
Please help me in code for select and than passing information by insert as I have defined in first paragraph .

MySQL: Delete records whose id is not in the result set of another query

This should be a pretty simple thing, but I'm quite new to (My)SQL.
Basically, given a customers table with attributes id, client, etc., where the client field is not necessarily unique, I want to eliminate rows where the client field is a duplicate of a previous value.
The following:
SELECT MIN(id) FROM customers GROUP BY client
returns the unique id's of the rows I want. I want everything else out.
I tried
DELETE FROM customers WHERE customer.id NOT IN
(SELECT MAX(id) FROM customers GROUP BY client)
to no avail. (ERROR 1093 (HY000): You can't specify target table 'customers' for update in FROM clause).
Why doesn't it work and what do I need to do to accomplish my goal?
Thank you.
You could create a temporary table that holds the values you want to remove. Then your delete query could be based on that temporary table.

SQL: flavours of INSERT INTO

(MySQL) I am trying to migrate a "subscription" table into 3 new tables: "product", "subscription", "actual" where "actual" is the name of the actual product, say, newsletter. The subscription has a FK reference to product and the actual has FK reference into subscription.
I know the INSERT INTO SELECT statement can copy data probably from many table to one; is there a statement to do the opposite, one to many tables for my case?
I'm not aware of an SQL statement that will do what you want. Just do several INSERT INTO SELECTs one after the other. It may be faster to do them one at a time anyway.
I think you can use three seperate insert into select statements. First you convert the product table, then the subscription where you can use an embedded select to find the id in the product table:
insert into subscription (some_column, FK_id,...)
select something, (select id from product where <your where clause>),...
and finally convert the actual table using an embedded select to get the id from the subscription table.