Inserting new data in a table - mysql

I have created a basic table for learning purposes.
CREATE TABLE friends (
id INT,
name TEXT,
birthday DATE
);
Added some data...
INSERT INTO friends (id,name,birthday)
VALUES (1,'Jo Monro','1948-05-30');
INSERT INTO friends (id,name,birthday)
VALUES (2, 'Lara Johnson','1970-03-03');
INSERT INTO friends (id,name,birthday)
VALUES (3,'Bob Parker', '1962-09-3');
And I realised that I forgot to include the email column.
I added the column...
ALTER TABLE friends
ADD COLUMN email;
..but how can I add now data to this new column only?
I have tried WHERE statements, rewriting the INSERT INTO statements with and without the other column names but nothing worked?
What am I missing here?
Thank you!

Insert the emails into a temporary table, then update the real table with that.
CREATE TABLE friends (
id INT auto_increment primary key,
name VARCHAR(100),
birthday DATE
);
INSERT INTO friends (name, birthday) VALUES
('Jo Monro','1948-05-30')
, ('Lara Johnson','1970-03-03')
, ('Bob Parker', '1962-09-3');
ALTER TABLE friends ADD COLUMN email VARCHAR(100);
select * from friends
id | name | birthday | email
-: | :----------- | :--------- | :----
1 | Jo Monro | 1948-05-30 | null
2 | Lara Johnson | 1970-03-03 | null
3 | Bob Parker | 1962-09-03 | null
--
-- temporary table for the emails
--
CREATE TEMPORARY TABLE tmpEmails (
name varchar(100) primary key,
email varchar(100)
);
--
-- fill the temp
--
insert into tmpEmails (name, email) values
('Jo Monro','jo.monro#unmail.net')
, ('Lara Johnson','lara.johnson#unmail.net')
, ('Bob Parker', 'UltimateLordOfDarkness#chuni.byo');
--
-- update the real table
--
update friends friend
join tmpEmails tmp
on friend.name = tmp.name
set friend.email = tmp.email
where friend.email is null;
select * from friends
id | name | birthday | email
-: | :----------- | :--------- | :-------------------------------
1 | Jo Monro | 1948-05-30 | jo.monro#unmail.net
2 | Lara Johnson | 1970-03-03 | lara.johnson#unmail.net
3 | Bob Parker | 1962-09-03 | UltimateLordOfDarkness#chuni.byo
db<>fiddle here

Related

Copy id after insert to another table

I have two tables, but I would like to set a trigger. When I insert a new user to my table users, I would like to copy that id to another table: results.
Table users:
| userID | name | email | password |
+--------+----------+-------------------+----------+
| 1 | Person A | mailA#gmail.com | 12345 |
+--------+----------+-------------------+----------+
| 2 | Person B | mailB#yahoo.com | 13579 |
+--------+----------+-------------------+----------+
| 3 | Person C | mailC#outlook.com | 24681 |
+--------+----------+-------------------+----------+
Table results:
| resultID | userID | TestA | TestB |
+----------+--------+-------+-------+
| 162 | 1 | 84 | 63 |
+----------+--------+-------+-------+
| 028 | 2 | NULL | 54 |
+----------+--------+-------+-------+
| 821 | 3 | 77 | 60 |
+----------+--------+-------+-------+
I would like to copy the userID from table users to userID in table results after insert.
I tried various options with triggers, but nothing fixed my problems.
One of them is:
CREATE TRIGGER T_TableA_I
on users
after insert
as
set nocount on
insert into results (userID)
select u.UserID
from
users u
inner join
results r
on
u.UserID = r.UserID
It may be that my structure is not in accordance with the guidelines, but this is a concept.
I am still not sure if I understand the question but here's a code snippet for conversation
drop table if exists us,res;
create table us (id int);
create table res (id int);
drop trigger if exists t;
delimiter $$
create trigger t after insert on us
for each row
begin
insert into res(id) values (new.id);
end $$
delimiter ;
insert into us values (1);
select * from us;
+------+
| id |
+------+
| 1 |
+------+
1 row in set (0.00 sec)
select * from res;
+------+
| id |
+------+
| 1 |
+------+
1 row in set (0.00 sec)
I have written the following code snippet, but I could not figure out where to get the test result!
CREATE TABLE IF NOT EXISTS users (
userID INT(6) UNSIGNED PRIMARY KEY,
name VARCHAR(255),
password VARCHAR(255),
email VARCHAR(255)
);
CREATE TABLE IF NOT EXISTS results (
resultID INT(6) UNSIGNED PRIMARY KEY,
userID VARCHAR(255),
TestA VARCHAR(255),
TestB VARCHAR(255)
);
/* trigger query */
CREATE TRIGGER new_user_added
AFTER INSERT ON users
FOR EACH ROW
INSERT INTO results values('162', NEW.userID, '84', '63');
/* insert query */
replace into users values('1', 'Person A', 'mailA#gmail.com', '12345');
select * from users;
select * from results
users
| userID | name | email | password |
+--------+----------+-------------------+----------+
| 1 | Person A | mailA#gmail.com | 12345 |
+--------+----------+-------------------+----------+
results
| resultID | userID | TestA | TestB |
+----------+--------+-------+-------+
| 162 | 1 | 84 | 63 |
+----------+--------+-------+-------+
Hope this could help.

Insert a row and select or insert foreign key via a subquery

For the last two days I am stuck with the following problem I want to insert data in the logintime table with one query. I am running MySQL 5.7.21
The problem I face however: there is a possibility that the player doesn't exists in the player table, so therefor I am looking for a solution that looks up the foreign key and if it doesn't exists create a new player in the player table and use the newly created player id in the logintime table.
Player table:
+----+-------+------+
| id | name | type |
+----+-------+------+
| 1 | Steve | 2 |
| 2 | Tim | 1 |
+----+-------+------+
Logintime table:
+---------------------+------------+----------+
| logintime | logouttime | playerid |
+---------------------+------------+----------+
| 2018-02-04 12:44:11 | NULL | 1 |
| 2018-02-03 10:55:32 | NULL | 2 |
| 2018-02-03 09:22:02 | NULL | 1 |
+---------------------+------------+----------+
Thank you
EDIT
DELIMITER $$
CREATE PROCEDURE insertOnline (IN Name VARCHAR(30))
BEGIN
DECLARE playerid INT;
INSERT IGNORE INTO players (name) VALUES (Name);
SET playerid = (SELECT id FROM players p WHERE p.name = Name);
INSERT INTO playersonline (playerid) VALUES (playerid);
END$$
DELIMITER ;

split a field having comma separated values and update the same field by adding additional rows with separated values

I have a field with comma separated values.I want to split the field and add the rows with the obtained values(after splitting) in the same table.
Eg:
**Data as in db**
ID CustomerId Preferences
------------------------------
1. 4456823 AA,BB,DD
2. 4456824 BB,DD
**Data format required**
ID CustomerId Preferences
------------------------------
1. 4456823 AA
2. 4456823 BB
3. 4456823 DD
4. 4456824 BB
5. 4456824 DD
Is there a way I can do this without using a temp table because the the Customer here is a cascading entity..This Id is formed by some other table which is an auto increment key.
Basically I want to update the first field and insert the other split values to get the desired result shown above
Here's one approach using a table of integers (0-9):
DROP TABLE IF EXISTS my_table;
CREATE TABLE my_table
(id INT NOT NULL
,customerid INT NOT NULL
,preferences VARCHAR(100) NOT NULL
);
INSERT INTO my_table VALUES
(1,4456823,'AA,BB,DD'),
(2,4456824,'BB,DD');
DROP TABLE IF EXISTS my_new_table;
CREATE TABLE my_new_table (id INT NOT NULL AUTO_INCREMENT PRIMARY KEY,customerid INT NOT NULL,preferences CHAR(2) NOT NULL);
INSERT INTO my_new_table (customerid,preferences)
SELECT DISTINCT customerid, SUBSTRING_INDEX(SUBSTRING_INDEX(preferences,',',i+1),',',-1) x FROM my_table, ints ORDER BY customerid,x;
SELECT * FROM my_new_table;
+----+------------+-------------+
| id | customerid | preferences |
+----+------------+-------------+
| 1 | 4456823 | AA |
| 2 | 4456823 | BB |
| 3 | 4456823 | DD |
| 4 | 4456824 | BB |
| 5 | 4456824 | DD |
+----+------------+-------------+

Mysql query of multiple tables joined

I have the following tables:
machine_machine
id | machineid
1 | EE100034442
item_item
id | upc | name
2 | 10001 | Snickers
machine_setup
id | machine_id | selection | item_id
3 | 1 | A1 | 1
Im trying to get the following output by joining the tables.
machine_setup.machine_id=machine_machine.machineid, machine_setup.selection, item_item.upc, item_item.name
EE100034442 A1 10001 Snickers
Table machine_setup will by the main referenced table as it has multiple selection for each machine_id.
Based on the only id's I can see at the moment to join on, consider this:
create table machine_machine
( id int auto_increment primary key,
machineid varchar(50) not null
);
create table item_item
( id int auto_increment primary key,
upc varchar(30) not null,
name varchar(100) not null
);
create table machine_setup
( id int auto_increment primary key,
machine_id int not null,
selection varchar(30) not null
);
insert machine_machine(machineid) values ('EE100034442');
insert item_item(upc,name) values ('10001','Snickers');
insert machine_setup(machine_id,selection) values (1,'A1'),(1,'A2'),(1,'A(n)');
select mm.machineid,ms.selection,ii.upc,ii.name
from machine_setup ms
join machine_machine mm
on mm.id=ms.machine_id
join item_item ii
on ii.id=ms.machine_id;
+-------------+-----------+-------+----------+
| machineid | selection | upc | name |
+-------------+-----------+-------+----------+
| EE100034442 | A1 | 10001 | Snickers |
| EE100034442 | A2 | 10001 | Snickers |
| EE100034442 | A(n) | 10001 | Snickers |
+-------------+-----------+-------+----------+
I'm not quite sure I understand the question, but the sql you want is like;
Select machine1.machineid, setup.Selection, item.upc, item.name
From Machine_machine machine1 --Set alias for the table
Inner Join machine_setup setup on setup.machine_id = machine1.id --This looks like a link table to me
Inner Join item_item item on setup.item_id = item.id -- in your example this wouldn't link as item_id is 1 in the machine_setup
In your example the machine_setup item_id is set to 1, which means it wouldn't link to the item_item table. i'm assuming this is a mistake.
Let me know if you need more information.

Automating table normalization

I have a table with this structure (simplified):
artID: 1
artName: TNT
ArtBrand: ACME
...
And I want to normalize it making a separate table for the brand (it will have additional data about every brand)
So I want to end up with this
article table:
artID: 1
artName: TNT
brandID: 1
...
brand table
brandID: 1
brandName: ACME
brandInfo: xyz
....
This table have way too many brands to do this manually.
Any easy way to automate this?
I'm using MySQL
As the other answers suggested, you can use the INSERT ... SELECT syntax to do something like this:
INSERT INTO brands (brandName)
SELECT artBrand
FROM original
GROUP BY artBrand;
INSERT INTO articles (artName, brandID)
SELECT o.artName, b.brandID
FROM original o
JOIN brands b ON (b.brandName = o.artBrand);
Test case:
CREATE TABLE original (artID int, artName varchar(10), artBrand varchar(10));
CREATE TABLE articles (artID int auto_increment primary key, artName varchar(10), brandID int);
CREATE TABLE brands (brandID int auto_increment primary key, brandName varchar(10));
INSERT INTO original VALUES (1, 'TNT1', 'ACME1');
INSERT INTO original VALUES (2, 'TNT2', 'ACME1');
INSERT INTO original VALUES (3, 'TNT3', 'ACME1');
INSERT INTO original VALUES (4, 'TNT4', 'ACME2');
INSERT INTO original VALUES (5, 'TNT5', 'ACME2');
INSERT INTO original VALUES (6, 'TNT6', 'ACME3');
INSERT INTO original VALUES (7, 'TNT7', 'ACME3');
INSERT INTO original VALUES (8, 'TNT8', 'ACME3');
INSERT INTO original VALUES (9, 'TNT9', 'ACME4');
Result:
SELECT * FROM brands;
+---------+-----------+
| brandID | brandName |
+---------+-----------+
| 1 | ACME1 |
| 2 | ACME2 |
| 3 | ACME3 |
| 4 | ACME4 |
+---------+-----------+
4 rows in set (0.00 sec)
ELECT * FROM articles;
+-------+---------+---------+
| artID | artName | brandID |
+-------+---------+---------+
| 1 | TNT1 | 1 |
| 2 | TNT2 | 1 |
| 3 | TNT3 | 1 |
| 4 | TNT4 | 2 |
| 5 | TNT5 | 2 |
| 6 | TNT6 | 3 |
| 7 | TNT7 | 3 |
| 8 | TNT8 | 3 |
| 9 | TNT9 | 4 |
+-------+---------+---------+
9 rows in set (0.00 sec)
I would use create table as select
... syntax to create the brands
table with generated id-s
create the brand_id column, and fill it up with the generated id-s from the brands table, using the existing brand columns in article table.
remove the brand columns from article table except of course brand_id
create the foreign key...
Generating brands table should be fairly simple:
CREATE TABLE brands (
id INT PRIMARY KEY AUTO_INCREMENT,
brand_name VARCHAR(50),
brand_info VARCHAR(200)
);
INSERT INTO brands VALUES (brand_name)
SELECT ArtBrand FROM Table
GROUP BY ArtBrand;
Similar story with creating relations between your original table and new brands table, just that select statement in your insert will look like this:
SELECT t.artId, b.id
FROM table t JOIN brands b ON (t.ArtBrand = b.brand_name)