What is the best way to insert with SQL?

Asked

Viewed 39 times

5

I say that because there are two ways.

Has the:

INSERT INTO teste SET nome = "Lucas", sobrenome = "Alves";

And also the way:

INSERT INTO teste (nome, sobrenome) VALUES ("Lucas", "Alves");

Speed, good practice, etc., has something to do with it, or is it all the same?

  • INSERT WITH SET I didn’t know.

  • Neither, Dvdsamm, but I saw in the course of Bonieky he using, and it works the same way, then I was in doubt.

  • Bigger cool this. With SET is much better. On this site the guy even recommends using this syntax https://www.bennadel.com/blog/2913-using-the-insert-into-set-syntax-in-mysql.htm

  • I thought it better too, when it comes to maintenance, it gets easier. But from what I understand, it only works in Mysql and derivatives, so if you need to migrate to another database in the future, then you have to update everything,

  • 1

    The syntax is the same as the UPDATE. It makes it much easier.

  • Dude, I have a "damn" inheritance (I put it in quotes so it doesn’t look ungrateful, but I’m very grateful. It’s just a representation)... I learned SQL from a friend and I took his pattern and I never went too deep to know the variations. For me it was just what I learned from him. :)

  • I do courses at Upinside and with Bonieky, the two are beasts, each has a different didactic, ai, one complements the other, to learning well.

Show 2 more comments

1 answer

4


The first way works in Mysql and derivatives, but not in other databases.

The second way is the standard way that should work in all databases.

Look here at Sqlfiddle.

  • Ahhh, now I understand. Could explain derivatives? It would be for example, Mysql, ai would work in Mariadb, etc...

  • @Lucascarvalho Exactly.

  • Got it, thanks a lot Victor!

  • 2

    @Lucascarvalho and to answer the second part, the speed is probably identical, because the commands are processed but do not affect the behavior of the execution, of course only those who can affirm is who knows the core of the code, but even if there is a speed difference it should be 0.000001 seconds :P

  • 1

    Really, test yourself on PostgreSQL 9.6 it returns error: ERROR: syntax error at or near "SET"

Browser other questions tagged

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