Improve company productivity with a Business Account.Sign Up

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 313
  • Last Modified:

Is there a way to use 1 table to define the colums of another table

iI would like to create a mysql database table that will contain 3 fields.
Name
Description
default value

I would then like the records of this table to be used to create the fields of another table.

so that if a record contained the following

name = x
description = y
default value z

and another record contains

name a
description b
default value c

then the fields of the otehr table would be x and a.

the 2nd table would update with a new field whenever a new record was added to table 1.

furthermore when a new record is created in table 2, field x would default to z if no other value was specified at record creation.

What is the best way to go abvout this?
0
Stuartjrose
Asked:
Stuartjrose
2 Solutions
 
Plk_In_EECommented:
Hi there,
you can update one table values using the other table update.

UPDATE dest_table1
SET
filed1 = (select field1 from src_table t2 where t2.pk = t1.pk)
field2 = (select field2 from src_table t2 where t2.pk = t1.pk)
...etc.
WHERE
EXISTS (select 1 from src_table t2 where t2.pk = t1.pk)

Hope this helps for you.

thank u
0
 
johanntagleCommented:
Use triggers to automatically update table2.  From your description I don't really understand the structure of table2 but the trigger should look something like:

delimiter |
create trigger trigger_name
after insert on table1
for each row
begin
  insert into table2 (x, a) values (NEW.name, NEW.description);
end; |
delimiter ;

See http://dev.mysql.com/doc/refman/5.1/en/triggers.html for more info.

For assigning of default value, maybe you can try the ifnull() function, though I'm not sure if works within a trigger/stored procedure body as the examples in (http://dev.mysql.com/doc/refman/5.0/en/control-flow-functions.html#function_ifnull) are all for selects.  If it doesn't, then put a conditional statement in the trigger body to determine what will be assigned to table2.
0
 
Scott MadeiraCommented:
What are you trying to accomplish?  We may be able to come up with a better way to solve your problem.

To go with what you posted, you would probably have to use triggers to make this work.  You would need to create an insert trigger on table 1 so that any insertion into that table would execute an alter statement on the second table to add the new column.

Likewise, you would want to create an insert trigger on table 2 to look at each column such that any data inserted into table 2 would execute a trigger on an empty column that then does a select from table 1 to get what the default value should be.

The second trigger may be a little complicated since it needs to look for all fields in the table that have an empty or null value and then do a select from table 1 to get each default value.

0
 
StuartjroseAuthor Commented:
thanks for the help people.

To clarify the Question.

the idea is that table 2 will be used to represent an rpg character with fields for attributes, skills and skill values.

I want the fields for skills and skill values to be user customizable. i.e a user can add a skill to a modern day skillset that will not be usable in a medieval set.

So that a user can add a skill a description of the skill and set it's default value and that will then become available to that user for every character they create using that skillset (table).  I've tried to abstract that as much as possible for this explanation.

this is a self set project based on my interests in order to teach myself more advanced sql than just a query for all records with a value =x.  Even if it only teaches me that what i want is best done outside SQL.

thanks for teh help everyone.
0
 
awking00Commented:
Can you describe the original table (i.e. provide column names and datatypes) with some sample data, then indicate what the structure and values should be in the other table?
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

Join & Write a Comment

Featured Post

Get 10% Off Your First Squarespace Website

Ready to showcase your work, publish content or promote your business online? With Squarespace’s award-winning templates and 24/7 customer service, getting started is simple. Head to Squarespace.com and use offer code ‘EXPERTS’ to get 10% off your first purchase.

Tackle projects and never again get stuck behind a technical roadblock.
Join Now