mysql insert and select all at once - mysql

I am new to MySQL so bear with me please. I have set up a member table and a session table. When insert a new record in member table, session table will grab the assigned new record ID from member table and automatically insert into session table. Is this possible?
so the order will be something like:
insert new record(member table) -> fetch id(member table) -> insert new record(session table)
-I have already set up a foreign key in session table, but I don't know what to do
-Im using php, if this matter..

easily you can use LAST_INSERT_ID();
your query can be like this:
INSERT INTO members (id, name)
VALUES('', 'test');
INSERT INTO sessions (member_id, description)
VALUES(LAST_INSERT_ID(),'some description');
in this case id is set auto increment.
at the moment you insert your data into member table by using (LAST_INSERT_ID) the last inserted id will store in session table.

Related

MYSQL Automatic INSERT ID in a different table

I am trying to learn some new things in MYSQL with PHPMyAdmin and I am wondering if the following thing is possible and how to get that to work. I'll try to explain it plain.
I have two tables
Table 1 = users & has
userID
username
etc.
Table 2 = users_permission & has
id
userID
is_admin
Now my idea was when I create record in table 1 by using an relation to table 2. It will automatically insert userID in Table 2 and create record based on default values. Is there someone who can help to get on track?
you need to make insertion for both tables seperately.
You first need to insert user into users table after that you need to insert into user permissions.
insert into users (userId, username) values( 1, 'JohnDoe');
insert into user_permission (id, userId, isAdmin) values (10, 1, 1);
For auto incremented ID column
insert into users (username) values('JohnDoe');
insert into user_permission (userId, isAdmin) values (LAST_INSERT_ID(), 1);

Mysql insert to main table with auto increment and link data in other table to the inserted main

I am not sure if the title explains it well, But I am sure that my explanation will explain it better:
I have a table called Tracks and a tble called Flocks.
each Track has many Flocks in it.
So when I insert a new track, a new ID is created with the AUTO_INCREMENT function, and in the same query, I want to insert the track's flocks aswell, but in order to make these flocks belong to the track I just inserted, I have to set their track_id to the auto incremented value.
I can do this in 3 queries, insert the Track, fetch the incremented ID, and then insert all flocks with the ID.
But I want to do this in one query, is that possible?
You need at least two queries unless you go for triggers or stored procedures:
Insert the track
use last_insert_id() as foreign key value to insert into flocks
example:
insert into track (name) values ('Trackname');
insert info flocks (trackid) select last_insert_id();
I normally group such tasks together in a stored procedure:
create procedure createTrack ( p_trackname varchar(20) ) as
begin
insert into track (name) values (p_trackname);
insert info flocks (trackid) select last_insert_id();
end;
And then call it this way:
call createTrack("Trackname");

MySQL DB Trigger Insert after cost linked to id

first Trigger I created.
I have two tables, student and cost.
If I insert a new student I want to automatically insert a cost row for this student in the table cost and insert the corresponding student id to the cost.
I donĀ“t know how I can link the student id to the cost...
CREATE TRIGGER `add_cost` AFTER INSERT ON `student` FOR EACH ROW INSERT INTO cost (amount) VALUES (2000)
Thanks everybody!!
In your insert statement please use just:
NEW.{primary_key}
from related table(in your case student). If your primary is id this should looks like:
NEW.id
NEW is created after row is inserted and contain all inserted values + PK after insert.
Finally your query should looks like below:
INSERT INTO cost (student_id, amount) VALUES (NEW.id, 2000)
You can find more info on http://dev.mysql.com/doc/refman/5.7/en/trigger-syntax.html

Update Record If Exists (Without Key) If Not Insert

Suppose I have a table user_keys with id, username, key. I need a query which will receive two parameters: username, key. I want to update record (only column key) if it exists (means that record with the incoming username already exists) if not then just insert new row with both parameters.
Well, I have tried a few things, here is one of them
if not exists (select * from user_keys where username='uname') then
insert into user_keys
(username, token)
values( 'uname','token')
else
update...
But it does not work. Any idea how to achieve?

I need a logic to implement Mysql query

I have a 3 tables:
1. Users -- here "userd id" is primary key
2. UserPermision --here "userd id" is a foreign key and there is "pageid" coloumn
3. Page -- here page id is a primary key
Now i need to write a query when i inser a new user say user id = "1" then this user id 1 should be inserted into Userpermision table and for this user it shouls have all the pages from page table .
I'm assuming you are using InnoDB.
In that case you have foreign key constraints and transactions.
If not you'll have to use triggers.
START TRANSACTION;
INSERT INTO users (name, etc) VALUES ('test', 'remainder');
SELECT last_insert_id() INTO #my_user_id;
INSERT INTO userpermission (userid, permission, pageid)
SELECT
#my_user_id as user_id
, 'all' as permission
, pageid
FROM pages
WHERE pages.userid = #my_user_id;
COMMIT;
This is assuming you've already made pages for that user.
link
http://dev.mysql.com/doc/refman/5.0/en/getting-unique-id.html
You need two INSERT queries, one for each table.
INSERT INTO Users (user_id,...) VALUES (:user_id,...)
INSERT INTO UserPermission (user_id,page_id)
SELECT :user_id, page_id FROM Page
You can either execute them within the same transaction, or execute the second one from the AFTER INSERT trigger on the Users table.