insert child mysql - mysql

I've got a newbie question...
I've got two tables:
parentTable
-----------
id_user int(11) not null auto increment primary key,
email varchar(64),
pass varchar(64)
childTable
----------
id_user int(11) not null,
name varchar(64),
address varchar(512),
foreign key (id_user) references parentTable(id_user)
on update cascade
on delete restrict
Now can I insert:
insert into childTable (id_user) select id_user from parentTable where id_user = '1'
But I just want to insert also name and address values.
Sorry for the newbie question, but I lurked for a day and found nothing.
Thank you in advance for your reply.

The interesting part about your query is that you know the id_user you're trying to select to insert - it's in your WHERE clause.
If you will always know the id_user, you can skip the extra SELECT portion of the query and directly do:
INSERT INTO childTable (id_user, name, address) VALUES (1, 'some name', '123 test street');
If you, for some other reason, need the additional SELECT, you can append the name/address values directly into the field-list, like this:
INSERT INTO childTable (id_user, name, address)
SELECT id_user, 'some name', '123 test street' FROM parentTable WHERE id_user = '1';

Related

How do you ON DUPLICATE KEY UPDATE a user info without breaking the database?

I have a simple table with students in MySQL database. Similar to:
student_id
student_name
teacher_id
1
Adam
100
2
Bob
100
3
Carl
100
4
Dan
200
Teachers can input new students or change existing student's names. Currently I have this set up to update via:
INSERT INTO student_list (name) VALUES (:name) ON DUPLICATE KEY UPDATE student name= values(name)
The problem is that this completely breaks any associated student_ids in other tables (as the ON DUPLICATE KEY UPDATE changes the student_id). I could look up each entry before updating, but it seems messy. Surely there's a better way.
CREATE TABLE `sabrep_db`.`students` ( `student_id` INT NOT NULL AUTO_INCREMENT , `name` TEXT NOT NULL , `teacher_id` INT NOT NULL , PRIMARY KEY (`student_id`)) ENGINE = InnoDB;
INSERT INTO `students` (`student_id`, `name`, `teacher_id`) VALUES (NULL, 'Adam', '100'), (NULL, 'Bob', '100');
Should also pass a key (usually the primary key) as part of the insert so that it knows the key reference to update when there is a duplicate, name is passed twice for the update portion:
INSERT INTO student_list (student_id, name)
VALUES (:student_id, :name) ON DUPLICATE KEY UPDATE student_name = :name;

How to get the user with most comments from a table

I am trying to get the user first name with the most comments. How can I do this?
Here are the tables.
The tables below are the setup for the database tables which I am trying to query.
CREATE TABLE User(
userid varchar(3),
firstname varchar(20),
lastname varchar(20),
age int,
PRIMARY KEY(userid)
)ENGINE=INNODB;
CREATE TABLE Comment(
commentid varchar(3),
userid varchar(3),
eventid varchar(3),
title varchar(20),
comment varchar(50),
PRIMARY KEY(commentid),
FOREIGN KEY(userid) REFERENCES AnonymousUser(userid),
FOREIGN KEY(eventid) REFERENCES Event(eventid)
)ENGINE=INNODB;
INSERT INTO User VALUES('U01','Charles','Darwin',99);
INSERT INTO User VALUES('U02','Keisha','Strawn',24);
INSERT INTO User VALUES('U03','Denise','Malcolm',59);
INSERT INTO User VALUES('U04','Dennis','Stewart',19);
INSERT INTO User VALUES('U05','Robert','Johns',45);
INSERT INTO User VALUES('U06','Marsha','Stewart',33);
INSERT INTO Comment VALUES ('C01','A01','E01','Boring Event','This event was boring');
INSERT INTO Comment VALUES ('C02','A02','E01','Nice Nice Event','This event was Nice');
INSERT INTO Comment VALUES ('C03','A03','E03','Wow','This event was Amazing');
INSERT INTO Comment VALUES ('C04','A01','E01','Very Sad','I missed this event');
The query I tried is
SELECT User.userid FROM User
JOIN comment ON comment.userid = user.userid
WHERE (SELECT COUNT(comment)
FROM comment = (SELECT MAX(userid) FROM comment);
SELECT
userid
FROM
comment
GROUP BY userid
ORDER BY count(userid) DESC
LIMIT 1;
Edit: oh, you need the username. Try this:
SELECT firstname
FROM user
WHERE userid = (
SELECT
userid
FROM
comment
GROUP BY userid
ORDER BY count(userid) DESC
LIMIT 1
);
Query to get firstname with most comments is
select a.firstname, max(a.comment_count) from (
select u.firstname, count(c.commentid) comment_count
from user u join comment c on u.userid = c.userid
group by u.firstname
)a;
That said, I noticed
One of the constraints on table 'comment' is pointing to a table 'AnonymousUser' FOREIGN KEY(userid) REFERENCES AnonymousUser(userid). You have not shared the create table statement for that table.
I had to remove this constraint from table definition in order to successfully create this table in my database
CREATE TABLE Comment(
commentid varchar(3),
userid varchar(3),
eventid varchar(3),
title varchar(20),
comment varchar(50),
PRIMARY KEY(commentid)
);
You dataset for table 'comment' has no userid matching 'user.userid' values
I updated the 'comment' table inserts so I could get some result when executing my query.
INSERT INTO Comment VALUES ('C01','U01','E01','Boring Event','This event was boring');
INSERT INTO Comment VALUES ('C02','U01','E01','Nice Nice Event','This event was Nice');
INSERT INTO Comment VALUES ('C03','U03','E03','Wow','This event was Amazing');
INSERT INTO Comment VALUES ('C04','U06','E01','Very Sad','I missed this event');

Is InnoDB neccessary for this case?

I have been looking for the answer to the question below:
MySQL: Insert record if not exists in table
I have an extra question to the accepted answer. However, I don't have enough reputation to comment on the page...
In the answer, Mike create the table like this:
CREATE TABLE `table_listnames` (
`id` int(11) NOT NULL auto_increment,
`name` varchar(255) NOT NULL,
`address` varchar(255) NOT NULL,
`tele` varchar(255) NOT NULL,
PRIMARY KEY (`id`)
) ENGINE=InnoDB;
Is InnoDB a must, according to the query issued below? (also quoted from the answer)
INSERT INTO table_listnames (name, address, tele)
SELECT * FROM (SELECT 'John', 'Doe', '022') AS tmp
WHERE NOT EXISTS (
SELECT name FROM table_listnames WHERE name = 'John'
) LIMIT 1;
Thanks.
No, it is not necessary.
But I prefer to write your query this way:
INSERT INTO table_listnames (name, address, tele)
SELECT 'John', 'Doe', '022' FROM dual
WHERE NOT EXISTS (
SELECT name FROM table_listnames WHERE name = 'John'
);
or even better, set an unique constraint:
ALTER TABLE table_listnames ADD UNIQUE (name)
and just insert with this:
INSERT INTO table_listnames (name, address, tele)
VALUES ('John', 'Doe', '022')
if a row with the name John is already present, the INSERT will simply fail.

sql insert query with select

Given this table where both reviewid and prankid are auto increment.
CREATE TABLE Review
(
reviewId INT NOT NULL AUTO_INCREMENT,
email VARCHAR(32) NOT NULL,
prankId INT NOT NULL,
rating INT,
comment VARCHAR(1056) NOT NULL,
PRIMARY KEY(reviewId),
FOREIGN KEY (email) REFERENCES User(email),
FOREIGN KEY (prankId) REFERENCES Prank(prankId)
);
Would this insert statement correctly insert values into all of the attributes in review table.
INSERT INTO Review (email, prankId) SELECT email, prankId from User;
INSERT INTO Review (rating, comment) VALUES(‘5’,’amazing!’);
INSERT INTO Review (rating, comment) VALUES(‘5’,’brilliant!’);
I suspect that you want this:
INSERT INTO Review (email, prankId, rating, comment)
SELECT email, prankId, 'S', 'amazing!'
from User;
INSERT INTO Review (email, prankId, rating, comment)
SELECT email, prankId, 'S', 'brilliant'
from User;
In your version, the last two inserts will fail because email and prankid are NULL. Remember, insert adds new rows into the table. It doesn't modify existing rows.

mysql Getting the same auto_increment value into another

This may have a really easy answer. I have done much database stuff for a while. I am trying to get the auto_increment value from one table inserted into the value on another table. is there an easy way of doing this. For eg i have done:
CREATE TABLE table_a (
id int NOT NULL AUTO_INCREMENT,
a_value varchar(4),
PRIMARY KEY (id)
);
CREATE TABLE table_b (
id int NOT NULL,
b_value varchar(15),
FOREIGN KEY (id) REFERENCES table_a (id)
);
Now i want to insert values into the table but I would like 'id' values for table_a and table_b to be the same. So far i have:
INSERT INTO table_a VALUES (NULL, 'foobar');
But I do not know how to go about extracting the auto_incermented 'id' number from table_a into the 'id' value of table_b. I have looked at SELECT #id = LAST_INSERT_ID() but can not get it to work.
You cannot do that at once. You'll have to first insert into the first table:
INSERT INTO table_a (a_value) VALUES ('foobar');
and then insert into the second using the generated id:
INSERT INTO table_b (id, b_value) VALUES (##IDENTITY, 'foobar');
LAST_INSERT_ID() and no need for the select statement part.