Welcome to the Treehouse Community

Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.

Start your free trial

Databases

Using INSERT INTO

Trying to copy data from one column in a table to another (placing foreign key values) using "INSERT INTO". It's successful but just creates all new rows with everything null except my foreign key values. Is there a way to tell the database to start inserting this information at a certain point?

Hope this makes sense.

2 Answers

Steven Parker
Steven Parker
231,128 Points

It's normal for unnamed columns to have null after insert.

Your insert statement must cover more than just the one column if you want them to not be null. For a complete copy, you might try something like this:

INSERT INTO new_table SELECT * FROM old_table;

On the other hand, if you already have rows with nulls and want to fix that, it's no longer an INSERT but an UPDATE operation.

Seth Kroger
Seth Kroger
56,413 Points

If I understand what you are trying to do, it is not normal to copy all the fields from one table to another. Only the foreign key is used as a reference to the row in the other table. When you make a query you will retrieve data from the first table using a JOIN between the two tables on the foreign key id.