user3360473 user3360473 - 1 year ago 144
SQL Question

Field doesn't have a default value

I'm trying to follow along to this for an assignment but I'm using my own entity relationship diagram in mySQL workbench.
As can be seen here

But when I try and follow what is said on the Netbeans site Delete 'select * from category' and enter the following SQL statement:

INSERT INTO `category` (`name`)
VALUES ('dairy'),('meats'),('bakery'),('fruit & veg');

But try with my own:

INSERT INTO `book` (`price`) VALUES ('20.0');
INSERT INTO `book` (`author_name`) VALUES ('author_name');

I keep getting errors saying

Error code 1364, SQL state HY000: Field 'author_name' doesn't have a default value
Line 1, column 1

Error code 1364, SQL state HY000: Field 'price' doesn't have a default value
Line 2, column 1

Execution finished after 0 s, 2 error(s) occurred.

Can someone please help me to start going in the right direction

Answer Source

Unless you want to insert two lines,

INSERT INTO `book` (`price`, `author_name`) VALUES ('20.0', 'author_name');

is likely what you want to do. The inserts trying to set just one column are failing because the other column has no default value. All columns which do not have a default value need to be set in an insert. If you intended to insert two rows here, then you'll need to make sure you specify values for both columns in each insert or ALTER your table so that the column has DEFAULT values. For example,

ALTER TABLE `book` MODIFY `author_name` varchar(200) DEFAULT '';

changing the size of the varchar to be whatever your author_name column is and replacing the empty string '' with whatever you want the default to be.

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download