Split SQL insert or XML to 2 different tables - mysql

I have a large sql insert like, (this is not large, it was an example)
INSERT INTO `NAMES` (`Name`, `DunkerID`, `Birthday`, `Lastname`) VALUES ('Jacob', 'Jacob18', '1965-01-05', Schulz');
But i want the insert Birthday and Lastname and their values into a different table, how can i do that?
And the XML in the title would be if i could split XML the same way, like
<Name>Jacob</Name>
<DunkerID>Jacob18</DunkerID>
<Birthday>1965-01-5</Birthday>
<Lastname>Schulz</Lastname>
The reason i need to do that is because i already codes like this with wrong tables just waiting to be inserted.

Related

MySQL/MariaDB: Is this the most efficient way to insert unique values and return nonunique values?

I had a statement in PSQL that did this just fine (based on an answer from this thread)
But I've been trying to recreate the same statement in MySQL which has been difficult. My current workaround is ugly:
CREATE TEMPORARY TABLE potential_duplicates (
id VARCHAR(64)
content TEXT,
) CHARACTER SET utf8mb4 COLLATE utf8mb4_bin;
INSERT INTO potential_duplicates(id, content) VALUES ('1', 'some content'), ('2', 'some more content');
SELECT id, content FROM potential_duplicates WHERE id IN (SELECT id FROM main_table);
INSERT INTO main_table(id, content)
SELECT id, content FROM potential_duplicates WHERE id NOT IN (SELECT id FROM main_table);
This query is going to be used very often and I feel like making a temporary table every query is inefficient. Originally I was trying to find a way to use the INSERT ON DUPLICATE feature. It would have worked fine if I was just inserting and nothing else, but i need to return the duplicate values in the end.
Assuming id is a primary key (or at least unique), you can use INSERT IGNORE to avoid duplicates and RETURNING (available as of MariaDB 10.5) to return the inserted id values:
INSERT IGNORE main_content(id, content)
VALUES ('2', 'some content'), ('5', 'some more content')
RETURNING id
Demo on dbfiddle

Single column (with spaces) name gets split into two

I am using codeigniter insert_batch($table, $data);
I have a column name as "Comment posted by the customer".
When I try to batch insert data into it. The insert query changes as:
INSERT INTO `table` (`id`, `Name`, `Status`,`Comment posted by the` `customer`, `isActive`) VALUES (),(),()...;
As seen the column splits up into 2 parts -
1.Comment posted by the
2.customer
And this throws an error. How do I overcome this? I need to insert it considering it as a single column.
Try to format the query like this
INSERT INTO table (id, Name, Status, `Comment posted by the customer`, isActive) VALUES (),(),()
UPDATE
You can omit the column names and specify the values for all columns
INSERT INTO table VALUES (), (), ()

MYSQL innerjoin insert Multiple tables

Hello i'll give you guys an example of the database tables that are being a pain so i got 3 conected tables yeah trying to make a list into a database anyway i'm having trouble writing a insert query for it
vogelsoort
id|naam|idhooftoonder|
now idhoofdtoonder references to the id a connection table to sort of translate a list to a mysql database (the logic of the table will be added underneath)
hoofdtoonder
|pkey|Id|idondersoorten
now idhoofdtoonder references to a the following table its id
ondersoort
id|naam
I'm sorry for asking this also i'm not experienced enough yet in mysql
edit: the question is since i've tried with a simple insert query it overwrote existing data that i'm looking for help with an insert without overwriting existing id's and connections since (idhootoonder references to hooftonder(id) and hooftoonder idontersoorten references to ondersoort(id) but not all data in ondersoort is connected to the same vogelsoort and i need an insert query that doesn't overide existing connections
You need 3 insert-statements, one for each table.
You may use TRANSACTION to do it safely.
You need to use LAST_INSERT_ID() function (your PK fields must been auto_increment for this), docs: http://dev.mysql.com/doc/refman/5.7/en/information-functions.html#function_last-insert-id
START TRANSACTION;
INSERT INTO ondersoort (id, naam) VALUES (NULL, 'data');
INSERT INTO hoofdtoonder (pkey, Id, idondersoorten) VALUES (NULL, NULL, LAST_INSERT_ID());
INSERT INTO vogelsoort (id, naam, idhooftoonder) VALUES (NULL, 'data', LAST_INSERT_ID());
COMMIT;

Insert specific values into mysql database

I have the following problem:
I would't insert all the values in my db.
So, i would insert only id,tessera,nome,sex. I thought i had to write something like this:
INSERT INTO registries(id,tessera,'',nome,sex) VALUES (35983,35983,'DOG', 'FABIO', 'M')
I don't want to insert the DOG value in my db cause I don't have any attribute in my schema to save it.
I have 3000 rows and I take this row as example so I can't simply delete the DOG value and the attribute field in my sql query.
I remember that I have to use something like this
INSERT INTO registries(id,tessera,'',nome,sex)
I mean, I have to write '' to not consider the value in that position.
but MySQL gives me an error..
Can you help me?
You can handle this situation in following ways.
create temporary table on same server. Insert all data in temporary table then use insert into select statement and select required fields.
create temporary column and insert data in that column and delete temporary column
INSERT INTO registries (id,tessera,new_column,nome,sex)
VALUES (35983, 35983, '', 'FABIO', 'M');
Now
DROP COLUMN new_column from registries;
Try this (assuming dog is a column name):
INSERT INTO registries (id,tessera,dog_name,nome,sex) VALUES (35983, 35983, '', 'FABIO', 'M')
If dog_name is allowed to be NULL (which it is by default) you could also just not insert any value, like this:
INSERT INTO registries (id,tessera,nome,sex) VALUES (35983, 35983, 'FABIO', 'M')

MySql Insert to multiple tables based on the ID generated in the first table

I have three tables in my database. A users table, StoreA and StoreB
StoreA and StoreB both have a unique key which is the user ID value.
What I want is; When I create a user and insert them into the database, how can I Insert a row into the other two tables without too many additional queries.
I figure I can do this by inserting the user in one query,
then in another return the newly created user ID,
then in another, using said ID, create rows in StoreA and StoreB
Can I cut out the middle query?
Can I cut out the middle query?
YES
START TRANSACTION;
INSERT INTO user (id, name, other)
VALUES (null, 'John','rest of data');
INSERT INTO storeA (id, user_id, other)
VALUES (null, #user_id:= LAST_INSERT_ID(), 'rest of data');
INSERT INTO storeB (id, user_id, other)
VALUES (null, #user_id, 'rest of data');
COMMIT;
See: http://dev.mysql.com/doc/refman/5.0/en/getting-unique-id.html
It's a good idea to do this in a transaction, you you're not stuck with just a user with no other data if something goes wrong.
It's not a DB requirement though.
Yes - there should be a function available to get the last inserted ID (assuming it's an autoincrement field) without another query. In PHP, it's mysql_insert_id(). Just call that after the first query.
YES
Q1: insert into table1 values (...);
Q2: insert into table2 values (last_insert_id(), ...);
last_insert_id is the default mysql build-in function
Most of the mysql libraries in various programming language did support return last insert id.
But You did not mention what sort of language you are using to connect to mysql.,
so cannot provide any example
I just wanted to share a php solution.
If you're using mysqli, first execute your insert query.
Then do
$db_id = $this->db->insert_id;
Why don't you use their username as the primary key instead of creating an arbitrary user_id field thats auto incremented? Their user names are unique, right?