I have crate a db in MySQL which has a lot of tables. I want the value of one table to be automatically saved on another table too.
For example I write something on: table1.lastname, I want this to be also stored in table2.lastname .
How is this called and how I can do that with PHP My Admin?
CREATE TABLE new_table_name LIKE old_table_name
Create trigger after_insert on new_table
like this
CREATE TRIGGER `AFTER_INSERT` AFTER INSERT ON `new_table_name` FOR EACH ROW BEGIN
insert into new_table_name (column_names) values (column_values) ;
END
For first you must create table for data store.
Then you must create trigger on wanted table for catch event and insert data in early created table.
This will do what you want:
INSERT INTO table2 (lastname)
SELECT lastname
FROM table1
If you want to include all rows from table1. Otherwise you can add a WHERE statement to the end if you want to add only a subset of table1.
I hope this helps.
If the table doesn't exist, you can create one with the same schema like so:
CREATE TABLE table2 LIKE table1;
Then, to copy the data over:
INSERT INTO table2 SELECT * FROM table1
Or If the tables have different structures you can also:
INSERT INTO table2 (`col1`,`col2`) SELECT `col1`,`col2` FROM table1;
EDIT: to constrain this..
INSERT INTO table2 (`col1_`,`col2_`) SELECT `col1`,`col2` FROM
table1 WHERE `foo`=1
Related
I'm learning mysql and i try to pass data from one table to another.
In Details, i have table1 with columns id, firstname, lastname etc.
Then i have a table2 with columns id, firstname, lastname, bodyfat, bodyweight, matchid.
The thing that i want is to pass the data firstname and lastname from table1 to table2 and they should be matched on table1.id = table2.matchid. Also the procedure should happen every time that new records are inserted into the table1, so the specific values are also updated and displayed in table2.
Is there any way to achieve that?
Thanks, in advance!
Use an after insert trigger:
DELIMITER //
CREATE TRIGGER after_user_insert
AFTER INSERT
ON table1 FOR EACH ROW
BEGIN
INSERT INTO table2 (firstname, lastname, matchid)
VALUES
(NEW.firstname, NEW.lastname, NEW.id);
END; //
DELIMITER ;
This assumes that you only want to insert new records in table2 with the first and last name, which is all the available information in table1. You could include other columns in the insert, but you would have to specify how to do that.
Beyond this, if you needed to initially populate table2 from table1, you could do an INSERT INTO ... SELECT:
INSERT INTO table2 (firstname, lastname)
SELECT firstname, lastname
FROM table1
-- WHERE <conditions>
you can use a trigger on table 1. A trigger is a named database object that is associated with a table, and that activates when a particular event occurs for the table. Some uses for triggers are to perform checks of values to be inserted into a table or to perform calculations on values involved in an update.
You can set a trigger on insert event in table 1. Here is a link https://dev.mysql.com/doc/refman/5.7/en/triggers.html that shows examples of trigger in mysql. Hope this helps
You can Create Trigger on Table1 with the insert or update statement into the Table2 using AFTER keyword, so that whenever a data is inserted in the Table 1 the trigger will be invoked and does the respective action based on your trigger definition.
For more details about how to create and use a trigger for MySQL refer the below link.
https://dev.mysql.com/doc/refman/5.7/en/triggers.html
I am trying to populate a table with a subset of data from a select query and I want this table to be recreated at regular intervals.
I tried to create an event which inserts records but it duplicates the records.
I then tried to drop and then create the table in an event but it tells me the table already exists
This is what I am trying to achieve:
DROP TABLE IF EXISTS tablea;
CREATE TABLE tablea;
INSERT INTO tablea
SELECT * from tableb WHERE etc.
What is the proper way to achieve this?
are you in the right schema? I ask as your drop table command is fine and if you are in the right schema (use whateverdb;), it will work.
Your commands all look ok if they are summarised.
if tablea exists in your current schema:
drop table if exists tablea;
is fine
to create a table, you need to specify the fields eg.
create table tablea (column1 varchar(20), column2 int, etc etc);
or better still, if the tables are the same structure, you could ignore that part and create the table with a create table like statement:
drop table if exists tablea;
create table tablea like tableb;
insert into tablea select * from tableb where column1 = whatever;
If tablea doesn't need the same properties as tableb, you can even do:
drop table if exists tablea;
create table tableb as select * from tableb where column1 = whatever;
The only disadvantage of this method over the stament above it is that it will not carry through the defined keys and special properties (if there are any).
And finally, if you are going to be recreating tablea over and over again, then, after you have initially created tablea (create table tablea like tableb; ) you can just keep rerunning these two lines below:
truncate tablea;
insert into tablea select * from tableb where whatever;
will be best as truncate (empty the contents) is much quicker than drop and leave your keys/properties in tact.
When I want to copy all the columns and data from an existing table, I am using below query:
CREATE TABLE newtable LIKE oldtable;
INSERT newtable SELECT * FROM oldtable;
Now,I want to create new table with some specific columns only from an existing table with Column index and data.
I have One workaround is as below:
Step 1:
CREATE TABLE newtable LIKE oldtable;
Step 2:
INSERT newtable(some specific colums) SELECT (some specific colums) FROM oldtable;
Step 3:
Drop other columns that I do not want in my new table.
I want to create new table with specific columns only so I do not need to follow step 3.
Thanks,
Ronak
Try this:
CREATE TABLE newtable AS ( SELECT id FROM oldtable );
Note oldtable has more fields.
Here is the sqlfiddle link.
I need create sql script for insert in many tables - total near 2000, but tables linked (it is an xml adjancency model) - so after have inserted in table1, receive id, add some data, insert into fk of table2 - previous id, and data, after repeat for table3, for table 4,...
Length of chain of linked tables - more than 20.
The question is: exists some code generator that can read relations between tables from information schema and - after point some table as root - generate all tree of insert query?
Thanks.
If you can't do it on application layer, I suggest trying to do it with Triggers.
Essentially, you would create an "AFTER INSERT" Trigger on each of the tables. Smth like that:
CREATE TRIGGER t1
AFTER INSERT ON table1
FOR EACH ROW
BEGIN
INSERT INTO table2 (id, attrib2) VALUES(NEW.id, NEW.attrib2);
END
CREATE TRIGGER t2
AFTER INSERT ON table2
FOR EACH ROW
BEGIN
INSERT INTO table3 (id, attrib3) VALUES(NEW.id, NEW.attrib3);
END
Then, an insert into table1 would trigger an insert into table2, which would trigger an insert into table3 and so on.
Is it possible to import the attributes of one table, then I put it into another table using a query in mysql?
For example I have table1 with attributes lname, fname, mname
And I want to put those attributes into table2.
Is there any query that could do that? I'm imagining that the table2 has one attribute that could later be dropped so that it will be the same as table1.
I am not entirely sure what you are asking.
If you want to copy the structure of table1 into a new table, do something like this:
CREATE TABLE table2 LIKE table1;
If you want to copy existing values from one table to another, you can then use the INSERT...SELECT syntax as follows:
INSERT INTO table2 (lname, fname, mname)
SELECT t1.lname,
t1.fname,
t1.mname
FROM table1 t1;
Do you want TABLE2 to look just like an empty TABLE1? If so, you could do
CREATE table2 LIKE table1;
If you use
SHOW CREATE TABLE table1
It will return most of the column syntax.
Then add ADD in front of all the columns you want to append to table 2, and change CREATE TABLE table1 to ALTER TABLE table2