Assume that a database table named “tblA” has 3 numeric fields (field1 to field3) that are NOT NULL and another text field (field4) that has a Default value of ‘Unknown’ . The Primary Key (field1) is an Identity column.
I'm looking to create one SQL statement example to populate 1 record when the information for field4 is Unknown.
Would I use the Insert Into statment:
INSERT INTO tbla(Name, EmployeeID, Position) VALUES(‘Albert Grant’, 100, ‘Salesman’);
I would recommend writing your inserts in the way you specified. Reasoning is that if table structures change, simply writing your INSERT script as:
INSERT INTO myTable VALUES ('abc', 'xyz', 123);
will require a change to the code. Without specifying a column list, your respective SQL parser will assume you will be inputting values into each of the defined columns of the table.
In your specific case, since it may not always be the case that the final value will be NULL, go ahead and specify it in your input list, and simply insert a NULL value in the VALUES part of your statement.
Of course this is just my opinion, which is only important to me...
the question was: “tblA” has 3 numeric fields (field1 to field3) and a text field (field4) with field1 being the PK
no way this is right --
INSERT INTO tbla(Name, EmployeeID, Position)
VALUES(‘Albert Grant’, 100, ‘Salesman’)
syntax error all over the floor
Can you defend your answer?
They didn't say that those were the only defaults in the row, and they also failed to give at least one of the other criteria about field 1 that was specified in class. As you might suspect, I have some insight into this one!