Importing sql file data omitting columns (MySql) - mysql

I am building a new booking system in PHP at the moment and I want to take over most of the data of the old system (MySQL) however the database structure of the new system will be slightly different. However I think I could take over some of the tables. Is it possible to import rows of old tables to the new table given that the column names are equal but some column names in the new table might be missing?
an example, the old table:
tourguides
id name address email telephone_mobile telephone_home
the new table
tourguides
id name address telephone_mobile
the new table doesn't has the column telephone_home, this shouldnt stop the import but instead it should be ignored

Yes, it's possible.
You should do an INSERT SELECT with fixed fields, like this:
INSERT INTO table1 (fieldX, fieldY) SELECT fieldX, fieldY FROM table2
You can even do it between databases:
INSERT INTO db1.table1 (fieldX, fieldY) SELECT fieldX, fieldY FROM db2.table2

Related

Transfer records between database with overlap ID (Primary Key)

My current project need to transfer data between databases. The scenario is that I have two different database, they have the similar schema for tables but the information inside table is completely different.
I want to transfer one record from one database to the other, and completely removed it in the old database once transferred. I realize that the primary key for that record may overlap with the one inside the new database.
I think of many solutions: I can add a character in front of the id to distinguish data from different table, then how can I add the character (for example B1100) in front of the id when I transfer? Or how do I automatically increase the ID (meaning generating new ID with the correct order in the new database).
For example: TABLE1 and TABLE2. TABLE1 has the most recent ID is 2000, and I want to transfer a record from TABLE2 with the ID is also 2000. Is there a way that I can transfer that record from table2 with table2.ID is 2000 to a new record of TABLE1 with table1.ID is 2001?
Thank you!
I am planning to use this SQL Query for transferring:
INSERT INTO DATABASE_1.dbo.table1
SELECT *
FROM DATABASE_2.dbo.table2
WHERE DATABASE_2.dbo.table2.id = currentID;

How can I insert my data into two different tables in MySQL?

Hi I'm new in using MySQL, I would like to insert my data into two different tables, I don't know how to do it using a query. All I know from now is how to insert multiple data in one table.
My input Data in 5 textboxes were
textbox1 = Garcia (to be inserted in table1)
textbox2 = Michael (to be inserted in table1)
textbox3 = David (to be inserted in table1)
textbox4 = 24 (to be inserted in table2)
textbox5 = 8888888 (to be inserted in table2)
My table fields looks like this
Table1
Last Name
First Name
Middle NAme
Table2
Age
Contact No.
Since you've not specified a UI language I'm assuming that you simply want the SQL commands to do this.
INSERT INTO Table1 (`Last Name`, `First Name`,`Middle NAme`)
VALUES ("Garcia","Michael", "David");
INSERT INTO Table2 (`Age`, `Contact No.`)
VALUES (24, 8888888);
You've used bad table field names though. If this isn't working it is because you should remove periods and spaces from them all.
Also, it looks like you are just playing around but if you were being serious you'd want to include all of this information in one table and give that table a good name and an ID field.
I agree with with first answer you can have as many insert statement as you need if you are using SQL Server you need to add the word go between statements

SQL Derived Table

I am new to SQL and really struggle just testing it out. My question is how do I derive data from one table to a new one keeping only specific data like:
-Real name
-Screen name
and also how do I create new variables using SQL. For example the Number of tweets that the person contributed.
In MySQL, you would use create table as:
create table table2 as
select RealName, ScreenName
from table1;
However, you don't actually need to copy the data. You can just use a view instead:
create view table2 as
select RealName, ScreenName
from table1;
Or, just put the logic into your query.
SELECT ur specific column
INTO newtable
FROM oldtable;
check here for more examples

Inserting into a table from an incompatible table

I have a MySql table called Person, and one day I accidentally deleted someone from this table. I have a backup table, called PersonBak so I was going to restore my deletion from the backup. However, in the course of moving forward on my application I renamed all the fields in Person, except for the primary key, PersonID. Now Person and PersonBak have the same data, but only one matching column name.
Is there any way to restore my missing person to Person from PersonBak without doing a lot of work? I have quite a few columns. Of course I could just do the work now, but I can imagine this coming up again.
Is there some way to tell MySql that these are really the same table, with the columns in the same order, just different column names? Or any way at all to do this without writing out specifics of which columns in PersonBak match which ones in Person?
If the column datatypes are the same between the tables, the column count is the same, and they are all in the same order, then MySQL will do all of the work for you:
INSERT INTO t1 SELECT * FROM t2;
The column names are ignored. The server uses ordinal position only, to decide how to line up the from/to columns.
What about this:
insert into Person(id, col11, col12) (select id, col21, col22 from personBak where id=5)
person schema:
columns (id, col11, col12)
personBak schema:
columns (id, col21, col22)
Look at Mysql SELECT INTO and you can specify the field names & create an insert statement

Importing mysql data from one table to another

It's very simple to someone but looks like complicated to me. I have 2 tables, one is called jos_users other is called jvdb_users. Tables are exact same, both containing user informations such as user email, password, name, etc. Only table one have many users, table two is empty, i need to transfer all fields and data from table one to table two. How do i do that with phpmyadmin and mysql on linux server.
if they both have the same columns you can use this command
INSERT INTO `tabletwo` (SELECT * FROM tableone)
You can write this in the SQL section of phpmyadmin
try this :
INSERT INTO table1 VALUES (SELECT * FROM table2);