REPLACE INTO sql query with 2 keys? - mysql

Im trying to create a table through phpMyAdmin where I can REPLACE INTO a row where two columns match.
I have a table called channel_data that has 2 assigned ID's. One for the relevant server (guild_id) and one for the relevant data's owner (owner_id).
REPLACE INTO channel_data (guild_id, owner_id, username, userlimit)
VALUES ('123456789123456789', '123456789123456789', 'foo', '100');`
Above is an example of what I want to achieve but I want it to replace the values of username & userlimit if a row is found with the same guild_id & owner_id; but if not - it creates a new row...
I hope this makes sense...
Thanks in advance :)

Create a unique multi-column index on the two columns.
ALTER TABLE channel_data
ADD UNIQUE INDEX (guild_id, owner_id);

Related

How to sort a column based on another column to resolve can't add or update child row issue

I have a table which contains id and supervisorId. Id is the PK and supervisorId is the foreign key (Self Join).
When I try to insert the data into the table, mysql throws unable to add or update a child row error. Because of relationship between two columns, I can't populate the supervisorId column without Id column.
Is there any way to insert the data in a single query?
You need to insert the rows in two steps:
Insert the rows without supervisors (i.e. supervisor_id = NULL.
Then update the rows with the correct supervisor, when you know their ids.
It is not clear how you are processing the data. If you are trying to move rows from one table to another, you should probably ask another question, with sample data, desired results, and a clear explanation of what you want to accomplish.

How to duplicate all rows in SQL Database table and change data in one of the columns?

So I have an SQL DATABASE and I want to duplicate all rows from a table named "searches", that have the word "google" in the column named "search_url". The records in this table are more than 300,000 so I don't want to duplicate them all, just the ones that have "google" in the column named "search_url".
The new rows' ID should automatically increment, and all the rest of the columns must be the same, except for the column named "search_url" where "google" has to become "search.yahoo"
My SQL is not that good and I will appreciate all the help I can get! Thank you :)
EDIT:
Sample data:
ID SEARCH_URL SEARCHABLE_ID SEARCHABLE_TYPE
1 https://www.google.com/search?q=camping 1 App\News
2 https://www.google.com/search?q=hiking 2 App\News
So I want this to become after this is duplicate:
ID SEARCH_URL SEARCHABLE_ID SEARCHABLE_TYPE
1 https://www.google.com/search?q=camping 1 App\News
2 https://www.google.com/search?q=hiking 2 App\News
3 https://search.yahoo.com/?q=camping 1 App\News
4 https://search.yahoo.com/?q=hiking 2 App\News
Here's a step by step. Follow this process so you don't end up inserting a load of bad data.
1) Select the rows you want, except the autoincrement primary key id - mysql will manage that itself:
SELECT SEARCH_URL, SEARCHABLE_ID, SEARCHABLE_TYPE FROM TABLE --where .. ?
Use a where clause if you don't want all rows
2) You modify the data according to your wish. Here we use the string replace function to change google into yahoo:
SELECT REPLACE(SEARCH_URL, 'google', 'search.yahoo'), SEARCHABLE_ID, SEARCHABLE_TYPE FROM TABLE
Check it looks good.. (You said you wanted to change google -> search.yahoo but strictly speaking your example data demands www.google -> search.yahoo) - CHECK YOUR DATA BEFORE YOU INSERT IT
3) Then add in an insert above it, to insert into your table
INSERT INTO TABLE(SEARCH_URL, SEARCHABLE_ID, SEARCHABLE_TYPE)
SELECT REPLACE(SEARCH_URL, 'google', 'search.yahoo'), SEARCHABLE_ID, SEARCHABLE_TYPE FROM TABLE
You need to specify the full set of columns you're inserting, and you have to specify the same count of columns for insert as you have selected.
If you have an autoincrementing primary key, you can't do an insert into table select * from table because that will select the ID column and then try to insert what will technically be a bunch of already-existing values
Please use below query to get the desired output:
INSERT INTO searches(search_url, searchable_id,searchable_type)
SELECT REPLACE(search_url, 'google', 'search.yahoo'), searchable_id,searchable_type FROM searches;
Hope it works for you.

Multiple rows inside each table row (but only on certain columns)

I'm not quite sure how to word this so I've not managed to find an answer for it!
I want to create a table, where in certain columns there is multiple rows. As shown in the picture
How do I structure this?
An example of what I'm trying to achieve is imagine a table that listed all users in an application. Each row is a user, but I want to also have a sub row for each of the photos that a user may have.
You cannot have multi-valued columns. As they do not satisfy first normal form (For detailed information about 1st Normal form: https://en.wikipedia.org/wiki/First_normal_form)
Let your table have the following columns:
(A,B,C,D)
As per the picture provided by you, I am making the following assumptions:
1) A is the primary key
2) C and D are both are multi-valued and the rows have values like (A1,B1,C1,D1),(A1,B1,C2,D2). That is for single A value we have multiple pairs of C and D.
Do comment if any of the assumption is wrong.
What you can do is make two tables.
TABLE1 (A,B)
TABLE2 (A,C,D)
Where A is the primary key in TABLE1 and foreign key in TABLE2.
As asked for a snippet,you can have tables like these:
Tables

Inserting values in Mysql table if all the columns have different values

I am working on a blogging site but problem which I am facing is that, I want to allow user to create a blog if and only if his/her combination of email address, Blog Name and Blog Title is different from any other rows in the table. For example let us consider this table:-
Email BlogName BlogTitle
abc#gmail.com Coder's world What is coding?
abc#gmail.com Sql world What is sql?
Now if a user tries to enter a values('abc#gmail.com','Coder's Worlds' ,'What is coding?') in the table again then he/she will not be allowed to do so. But if the user tries to insert different combination of email, BlogName and BlogTitle then he/she will be allowed to do so.
As I am pretty new to Mysql, I don't know how to do this, so please help me to do this.
My Table structure :-
You can add a unique constraint on the combination of the three columns:
ALTER TABLE `blog_table` ADD UNIQUE `unique_index`(`Email`(100), `BlogName`(100), `BlogTitle`(100));
This will also remove any duplicates which might already exist in your blog table.
Note that this constraint uses only the first 100 characters from each of the three columns to avoid the #1071-Specified key was too long error.
Make those 3 Columns unique.
Here is a example of Making multiple fields composite
ALTER TABLE table ADD UNIQUE ('field_1','field_2', 'field_3');
IF (SELECT 1 = 1 FROM Table WHERE Email=#email and BlogName=#blogname and BlogTitle=#blogtitle) THEN
BEGIN
#return error message do not insert
END;
ELSE
BEGIN
#insert logic here
END;
END IF;
Check if matching record exists in table before inserting using above code

Assign a unique id to 3 tables which have the same column name in mysql

I want to know how to give unique id to 3 tables which have the same column name in mysql.
when inserting a new value the value should be compared with all three table column values and assign a new unique id to the inserted table. Is this possible in mysql.
Thanks
You'll need to create a temporary table that has all the values aggregated together so you can check.
What do you mean by "unique id"? A suitable hash function like SHA1() usually gives you something reasonably unique.