I'm changing my database structure and I want to do the following thing: I have one table for registered users that holds almost all the information about my site users. There is also one other table that holds information about the amount of points each user have. It has only two columns: user id and points. I want to move the points column in main users table so that the points aren't lost. I know theoretically that I have to join these two colums with user id somehow but I can't guess what would the code look like...
Hope I'm clear.
Can anyone please help?
First, you will have to add the two column names to the structure of the first table... then do a correlated updates something like
UPDATE YourTable, YourOtherTable
SET
YourTable.Points = YourOtherTable.Points,
YourTable.PointsCol2 = YourOtherTable.PointsCol2
WHERE
YourTable.id = YourOtherTable.id
Related
I have two MySQL databases. I would like to data from one database to another. Both have the same structure and entries except that one database has same IDs for different items within the same tables. I don't want to replace the data from the old to the new database. If IDs are there, I would like the new database to skip it. If it's a duplication, I would like a new ID to be generated.
I'd like to use phpmyadmin for this but have no idea if this is even possible.
0.) Make backup of both tables
PHPMYADMIN will be sufficient for your request.
First you need to ensure there is no duplicating id's or primary keys.
Assuming two tables testtable1 and testtable2 have columns testtable_id, name
1.) firstly you would make query on second table
UPDATE testtable2 SET testtable2.testtable_id = testtable2.testtable_id + (SELECT MAX( testtable1.testtable_id ) FROM testtable1);
2.) Than, again in testtable2, there is tool Copy table to (database.table): under Operations menu, set DB name and testtable1 name (db name should be already set), select Data only radio button option and click Go. 3.) Now, you have all data from both tables in testtable1.
Edit. Firstly I thought it is matter of two tables in same database. But nevertheless you can use step two for rest of the tables too. Just set correct DB and table name in step two. Also, before that, set query so expecting ID to be higher than MAX ID of table you want to extend. You can hard code parenthesis part with exact number of MAX ID first DB corresponding table.
Is it possible? If yes, how?
I am using xampp for my database and I wanted to make a database with "students" table, only one column for the list of student IDs, and every student ID has another table inside. (I was considering making tables for every student ID so that it can be done directly but it seems that it PHP/MySQL does not allow integers as table name).
No, you cannot put tables inside a row in SQL. That's not compatible with the concept of a relational database. What you can do is provide a foreign key to another table where you can collect the data you need to link to your first table.
What you need is to have one table with all you want about the students, and add a field with their ID.
So, yo don't have to get the information from student #123 as
SELECT * FROM 123
but
SELECT * FROM students_table WHERE IdStudent = 123
I have issue on INSERT INTO query,
In the registration form, when user enter username and select the department/center as bursary,
I want to insert that UserName into table bursary.
What I tried is;
$query14 = "INSERT INTO bursary (UserName) VALUES
('$UserName') WHERE DepartCent='Bursary'";
$result14=mysql_query($query14);
My table name is bursary, and it's look like ;
UserID UserName
1 ( ) <---- I want only UserName that choose department/center as bursary
Please anyone help me to solve this, appreciate that.
INSERT queries imply adding data that isn't there already. If you want to insert a value derived from a query you need to use an actual query to get it. The WHERE clause fails because there no row to examine until you insert one.
There's really not enough info to figure out what you're trying to do, but if we just go on this part of your question:
"I want only UserName that choose department/center as bursary"
Then you are probably looking for an INSERT..SELECT (assumes you have already inserted the data into some other user table 'your_user_table')
INSERT INTO bursary(UserName)
SELECT UserName from your_user_table
WHERE DepartCent='Bursary';
You can either:
insert new rows in a database table
update existing rows in a database table
Only when you are trying to change values of existing rows does it make sense to specify a WHERE clause to let the database know which values you want to change.
EDIT: Could you explain again what it is exactly that you are trying to achieve? Do you want to insert only some users filling out your form? Namely those that choose bursary as their department?
Why don't you change your table structure to something like:
Table DEPARTMENT:
Department Name, Department ID
Table USER:
User Name, Department ID (foreign key to Department)
EDIT Nr. 2: The way to do that is by normalizing your Table structure. Suppose one of the following things happened:
You want to add an additional department later on - you would not want to create a new table for that every time.
You want to change the name of a department - do you want to rename your tables? change your code,... I doubt that
So the way to go is to design your tables in a way that they separate the different "things" in your program. One type of thing you are working with are departments (bursary,...) another type of thing are Users. As a rough starting point try to make a table for each and try to connect the tables with so called foreign keys. Read it like this:
Every department has a unique department ID
Every User is associated to a department by this users Department-ID
You can then later on join these tables to find out all users of department X,...
Select u.userName,d.departmentName from User u INNER JOIN Department d ON
u.departmentId=d.departmentId
This would show you the names of all your users and their associated departmentName
I am implementing a request mechanism where the user have to approve a request. For that i have implemented a temporary table and main table. Initially when the request is added the data will be inserted to the temporary table, on approval it will be copied to the main table.
The issue is there will be more than 5k rows to be moved to the main table after approval + another 3-5 row for each row in the detail table (stores the details).
My current implementation is like this
//Get the rows from temporary table (batch_temp)
//Loop through the data
//Insert the data to the main table (batch_main) and return the id
//Get the details row from the temporary detail table (batch_temp_detail) using detail_tempid
//Loop through the data
//Insert the details to the detail table (batch_main_detail) with the main table id amount_id
//End Loop
//End Loop
But this implementation would take atleast 20k queries. Is there any better ways to implement the same.
I tried to create a sqlfiddle but was unable to create one. So i have pasted the query in pgsql.privatepaste.com
I'm sorry that I'm not familiar with PostgreSQL. My solution is in MySQL, I hope it can help since if they (MySQL & PostgreSQL) are same.
First, we should add 1 more field into your batch_main table to track the origin batch_temp record for each batch_main record.
ALTER TABLE `batch_main`
ADD COLUMN tempid bigint;
Then, on approval, we will insert 5k rows by 1 query:
INSERT INTO batch_main
(batchid, userid, amount, tempid)
SELECT batchid, userid, amount, amount_id FROM batch_temp;
So, with each new batch_main record we have its origin batch_temp record's id. Then, insert the detail records
INSERT INTO `batch_main_detail`
(detail_amount, detail_mainid)
SELECT
btd.detail_amount, bm.amount_id
FROM
batch_temp_detail `btd`
INNER JOIN batch_main `bm` ON btd.detail_tempid = bm.tempid
Done!
P/S:
I'm confuse a bit about the way you name your fields, and since I do not know about PostgreSQL and by looking into your syntax, can you use same sequence for primary key of both table batch_temp & batch_main? If you can, it's no need to add 1 more field.
Hope this help,
Simply need to update your Schema. Instead of having two tables: one main and one temporary, you should have all the data in main table, but have a flag which indicates whether a certain record is approved or no. Initially it will be set to false, and once approved it will simply be set to true and then the data can display on your website etc. That way you will not need to write the data two times, or even have to move it from one table to another
You haven't specified RDBMS you are using, but good old INSERT with SELECT in it must do the trick in one command:
insert main (field1,...,fieldN) select field1,...,fieldN from temporary
I'm creating a game in actionscript that requires the use of an external database to store user details and scores.
This database will contain multiple tables, currently there are two.
My first table contains the headers - ID, email, username, password.
My second table contains the headers - ID, lvl1Score, lvl2Score, lvl3Score.
In my game, when a new user is created it creates an entry in the first table with the ID auto-incrementing.
My question is - Is there anyway to automatically create an entry in my second table with its default values and the same ID when I add to my first table?
I've read about joins, but everything i've read just talks about looking up data over multiple tables.
Also, is my table structure correct in the sence that the ID value can be used using the JOIN keywork to look up an entry from both tables.
I would suggest you to go for triggers.
create or replace trigger trigger_name after
insert on table1
for each row
begin
insert into table2 values(new.id,"value for lvl2score","value for lvl3score");
end
Something like this.
If the tables truly have a one-to-one relation, I would recommend that you simply make one table having all the fields.
Or did you mean this should store multiple scores for each individual user? In this case, you should not insert a default record for the user. Instead, the score.ID field should instead reference user.ID and allow duplicates.
I suggest you to use triggers and for more flexibility create a many-many relationship between "user" and "level", so you will end up with 3 tables:
user
level
user_level (this will contain the foreign keys: user_id, level_id)