0

I am trying to insert data into a table (table1) based on another (table2), the only problem is that table1 contains fields set to not allow null values. Do I need to create these fields in table2 where I am pulling the data from and populate them with a value?

Example not null field: password

If I do not include this in my query then I get an error and the record is not inserted however if I create the field in table2 then insert into my query it works fine. This seems a bit out of the ordinary. Example query below:

Example 1 (no password field in table 2):

$insert_new_records_query = "INSERT INTO table1 (first_name, last_name, email_address) ". 
"SELECT firstname, lastname, email FROM table2";

This generates an error saying that I must include the password field.

Example 2 (password field in table 2):

$insert_new_records_query = "INSERT INTO table1 (first_name, last_name, password, 
email_address) ". 
"SELECT firstname, lastname, password = 'password1', email FROM table2";

This allows the record to be created. The problem is that I have many more fields that are not null in table 1 and I don't think I need to create them in table 2 as blank fields and insert them into my query with values just to create a record. Is there a better way to do this?

5
  • Are you able to modify the "not null" fields in table1 to allow for nulls? Commented Jan 4, 2012 at 16:06
  • What are you trying to accomplish with this setup? Commented Jan 4, 2012 at 16:06
  • @ZackMacomber that wouldn't make sense, if the fields would allow null values, the constraints wouldn't be there in the first place. Unless the OP cannot control the first table ofcourse, but we lack such information Commented Jan 4, 2012 at 16:08
  • 2
    You can't put null values in columns specified as not null, that's kind of the point. You have to either change the constraints to allow nulls, or come up with valid values from somewhere. If you want, you can use a join on the select to pull them from somewhere else, that works fine too. Commented Jan 4, 2012 at 16:08
  • My comment on modifying the "not null" fields was along the lines of changing the constraints to allow for nulls...sorry for the lack of clarity. I have had to remove "not null" constraints on table fields in the past so thought that may be an option here... Commented Jan 4, 2012 at 16:17

1 Answer 1

1

You don't have to create fields, you can simply 'select' default values. Try this instead:

INSERT INTO table1 (first_name, last_name, password, email_address)
    SELECT firstname, lastname, 'password1', email FROM table2
Sign up to request clarification or add additional context in comments.

2 Comments

Also, you don't want to save a password as plain text, you should use "SELECT firstname, lastname, PASSWORD('password1'), email FROM table2". dev.mysql.com/doc/refman/5.0/en/…
Wow! Thanks for the prompt response guys, that was quick. Apologies if I didn't clarify but I do need to populate the not null fields in table one with a value but the way I was thinking it need doing was to create all these additionals fields in table 2 so they could be selected in my query. Thanks @a_sad_dude your response appears to work for me...and no additional table fields in table 2!!!

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.