MYSQL Automatic INSERT ID in a different table - mysql

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);

Related

Inserting data into table SQL server

If I have 2 tables and Table 1 has a primary key(userID) AutoIncrement and Table 2 has a foreign Key(userID) to Table 1's (userID)
When I insert a new row into Table 1 first row will have userID = 1
Then if I insert again, userID = 2.
So how do I go about keeping Table 2's userID the same when inserting in Table 1. For instance, in Table 2, I am adding the password into another table.
My question is should I add an AUTOINCREMENT to Table 2(userID) and insert a new value into both tables when I create a user OR is there another way?
You have to manually insert data with correct id in Table2. There is no such built-in functionality in MySQL.
The algorithm is as follows:
Insert row in Table1.
Get Id of the new row.
Insert row with new id in Table2.
I think what you are looking for is this ... Assuming your ID is PK and AI
INSERT INTO yourTable (var1, var2 etc etc) VALUES ('val1', 'val2' etc etc);
SELECT LAST_INSERT_ID();
To further it ..
INSERT INTO yourTable (var1, var2 etc etc) VALUES ('val1', 'val2' etc etc);
SET #last_id = LAST_INSERT_ID();
INSERT INTO yourTable2 (id, var1, var2) VALUES (#last_id, 'blah', 'blah');

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?

mysql insert and select all at once

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.

SQL `update` if combination of keys exists in row - else create new row

First of, I've searched this topic here and elsewhere online, and found numorous articles and answers, but none of which did this...
I have a table with ratings, and you should be able to update your rating, but not create a new row.
My table contains: productId, rating, userId
If a row with productId and userId exists, then update rating. Else create new row.
How do I do this?
First add a UNIQUE constraint:
ALTER TABLE tableX
ADD CONSTRAINT productId_userId_UQ
UNIQUE (productId, userId) ;
Then you can use the INSERT ... ON DUPLICATE KEY UPDATE construction:
INSERT INTO tableX
(productId, userId, rating)
VALUES
(101, 42, 5),
(102, 42, 6),
(103, 42, 0)
ON DUPLICATE KEY UPDATE
rating = VALUES(rating) ;
See the SQL-Fiddle
You are missing something or need to provide more information. Your program has to perform a SQL query (a SELECT statement) to find out if the table contains a row with a given productId and userId, then perform a UPDATE statement to update the rating, otherwise perform a INSERT to insert the new row. These are separate steps unless you group them into a stored procedure.
Use a REPLACE INTO query.
REPLACE INTO table (productId, userId, rating) VALUES ('product id', 'user id', 'rating');
REPLACE INTO is like a normal insert, except if it finds a row already in the table with the same unique key it will delete that row before inserting.

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.