Update MySQL table with values from another table - mysql

I have a MySQL database and I need to update table rows with values from another table. For example in one table there is a list of addresses which are stored for a specific customer. There is one address entry per customer and this address ID is stored in the Customers table in each customer entry under "address_id" column. I would like to know if it's possible to run a query which will update a list of addresses with the id's of the customers. So for each address row which id is found in customer row should have customer id in the specific column. Thanks in advance!

Related

How to store multiple records in a single column in trigger table

I have created a new trigger with table name as advance_audit, and I have a employee table, whenever user updates more than one column in employee form, I want to store those values in advance_audit table in old_value column. If user edits two records then two records should save in old_Value column,
At present only firstname is saving, i want to store other fields also like lastname. How to do it
Below is my code.
Insert into advance_audit
set action='update'
old_value =old.firstname
Thanks in advance

Is there a possible way to have a row from a meta data table with a certain meta key automatically inserted into a new row?

I currently have a MYSQL query that searches a usermeta table and matches emails up with customer emails from another table, then creates a row in the usermeta table with the corresponding customer id. However, is there a way to have this done automatically every time these types of rows are inserted into the customer table?
Or at the very least, is there a way to send ALL rows from one table to the other automatically with the emails being what the two tables share in common, then after it matches the email it assigns the correct user id based on the userdata table?

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.

How to retrieve the columns based on the UUID

In my application database the table is called Customer which has fields:
CUSTOMER TABLE
cust_id
cust_name
cust_age
In above table cust_id data type is binary and UUID has been inserted into the table :
Insert statement looks like this:
INSERT INTO Customer VALUES ('444d264e-9024-11e4-968c-b82a72ad8ef8','swify',41);
The data has been inserted successfully . Similarly in this fashion i have inserted multiple records into the DB
Now i want to retrieve a particular record from the DB Usin select statement.
I tried this approach but it's giving no rows
SELECT * FROM Customer WHERE cust_id='444d264e-9024-11e4-968c-b82a72ad8ef8';
Hence, My question is how can i retrieve a record based on the UUID values;
Try this:
SELECT * FROM Customer WHERE cust_id LIKE '444d264e-9024-11e4-968c-b82a72ad8ef8';

Adding a database record with foreign key

Let's say there is a database with two tables: one customer table and one country table. Each customer row contains (among other things) a countryId foreign key. Let's also assume that we are populating the database from a data file (i.e., it is not an operator that is selecting a country from a UI).
What is the best practice for this?
Should one query the database first and get all ID's for all countries, and then just supply the (now known) country id's in the insert query? This is not a problem for my 'country' example, but what if there is a large number of records in the table that is being referred?
Or should the insert query use a sub query to get the country id based on the country name? If so, what if the record for the country does not exist yet and has to be added?
Or another approach? Or does it depend? :)
I would suggest using a join in your insert query to get the country id based on the country name. However, I don't know if that's something possible with every SGBD and you don't give more precision on the one you're using.