NULL error when adding a new line

Asked

Viewed 2,135 times

2

Good guys, I have a bench as described in the image below:

Tabelas

I have some questions:

A) Uma geladeira da marca Brastemp 220 V que custa R$ 700.
B) Três cadeiras de madeira em cores diferentes, cada uma custando R$ 53,40.

Answer: INSERT INTO Produtos (descricao,marca,preco) VALUES ('Geladeira','BrasTemp',700) INSERT INTO Eletrodomesticos(voltagem) VALUES (220)

But this error appears in question A:

Msg 515, Level 16, State 2, Line 8 Cannot Insert the value NULL into column 'Type', table 'LOJAMIX.dbo.Products'; column does not allow nulls. INSERT fails. The statement has been terminated. Msg 515, Level 16, State 2, Line 9 Cannot Insert the value NULL into column 'Codbarra', table 'LOJAMIX.dbo.Electrodes'; column does not allow nulls. INSERT fails. The statement has been terminated.

How do I fix these mistakes?

  • Take a look at [tour]. You can accept an answer if it solved your problem. You can vote on every post on the site as well. Did any help you more? You need something to be improved?

1 answer

3

As the columns described cannot be null and have no value default, you have to insert something. So:

INSERT INTO Produtos (descricao, marca, preco, Tipo) VALUES ('Geladeira', 'BrasTemp', 700, 'Eletro')
INSERT INTO Eletrodomesticos (voltagem, CodBarra) VALUES (220, '')

I put in the Github for future reference.

  • Ahhh worked. But if it cannot be NULL when I insert these simple quotes '' the bank subunderstands that I passed some value? is that it

  • Exactly. It is a text with nothing, but it is not null. It does not mean that it is the right thing to do in the application, but it is the way to work. The ideal is to put a value that means something. If it cannot be null, it has a reason,.

  • Poxaa, But for this question I have to cheat to insert ? this does not affect the safety of the bank will there is a way to solve without cheating ?

  • Security does not. There you have to see only if you can be without anything or not, it is a matter of data. You can simulate something, not to be "blank".

  • Thanks bigown, I am beginner in database, thanks for the clarification :D

  • bigown if possible wanted to take out only one more doubt, on the table Mobile and Electrodical there are foreign keys [Fk] of the table itself, because if a foreign key references another table ? that is it should be foreign in another table and not in the table itself.

  • This diagram is not very clear, but is foreign to another table. Although it is possible to have a foreign key in the same table, rare, but is.

  • Take a look at this and I think you still don’t know the site well: [tour]

Show 3 more comments

Browser other questions tagged

You are not signed in. Login or sign up in order to post.