SQL: Insert into a table + Parrot memes🦜

Yeah, we’ve all been there. SQL can be hard! 🐦


Hello, there fellow reader! I see you interested in learning some basics about SQL. In this blog, I’ll be talking about how to insert data into a table using SQL. If your not really sure what a table is or how to create one. No worries, just click on the link below. I wrote a whole blog on the topic, which will provide you with further context and resources. It’s a quick read, give it a try.

Create SQL Tables + panda memes 🐼

Let’s dive in 🐳

Let’s make a new table that holds data on parrots. If you remember in the last blog, it’s the same idea. We make a template, which tells the database how we want our data to be organized.

CREATE TABLE parrots( 🦜🦜🦜  
name VARCHAR(50),
species VARCHAR(50)
feed VARCHAR(50)

Inserting Data 🤖

How do we insert our data into this table? Easy enough, will write to our database using an SQL statement that looks like this…

INSERT INTO parrots(name, age, species, feed)
VALUES('charlie',14,'african grey', 'parrot mix #7');

If you remember the words in uppercase are “keywords”, which tell the database what we want to do. the lowercase letters will tell the database where this is happening. In this case, we want to insert data into the table called parrots.

INSERT INTO <--- What we want to do!
parrots <--- Where? the parrots table of course!

Will need to include the arguments, that match this parrot table as well. Each argument should be a column, so order matters. Reference your table in case you forget. Remember databases are super-efficient ways to store information, part of that cost we pay upfront with a certain level of precision. So no typos, please! 😞 👎

Our code:INSERT INTO parrots(name, age, species, feed) ----------------------------------Format: INSERT INTO table_name (column1, column2, column3, ...)

Now let’s write in our data, will need to use the VALUES keyword. This tells our database what we are inserting and where we would like it to be inserted.

Before we move on I’d like to point out the following…

This is ok...INSERT INTO parrots(age, name, species,feed)
VALUES(14,'charlie','african grey', 'parrot mix #7');
This is ok...INSERT INTO parrots(name, age, species,feed)
VALUES('charlie',14,'african grey', 'parrot mix #7');
This is NOT ok...INSERT INTO parrots(age, name, species,feed)
VALUES('charlie',14,'african grey', 'parrot mix #7');

Your column arguments have to match your value arguments. Let’s look at the last example I have provided. If the data types are off, your statement will be rejected. However, if your statement in fact does makes it to the database. You’ll have incorrect data, even though you have the correct data type, which will lead to future headaches.

My advice is to keep the statement’s columns matching with the tables’ columns. Good habits make good developers!

Our code:INSERT INTO parrots(name, age, species,feed)
VALUES('charlie',14,'african grey', 'parrot mix #7');
--------------------------------------------------Format:INSERT INTO table_name (column1, column2, column3, ...)
VALUES (value1, value2, value3, ...);

Nice, but not really practical right. How can we manually enter each set of data? Simple, take a look below at my example…

Our code:INSERT INTO parrots(name, age, species,feed)
('Magumbae',14,'african grey', 'parrot mix #7'),
('John snow',6,'sulphur-crested cockatoo', 'apples & cashews'),
('Felix',32,'rainbow macaw', 'fresh mango');
--------------------------------------------------Format:INSERT INTO table_name (column1, column2, column3, ...)
(value1, value2, value3, ...),
(value1, value2, value3, ...),
(value1, value2, value3, ...);

Tada! a multi-insert SQL statement. Not bad right?


Now that’s just plucky

SQL is great but very particular. You make mistakes, it’s bound to happen. Take them as learning experiences, and keep at folding in new tricks. SQL wizards are always in demand, so building up your skills with SQL is well worth your time.

Super cool Links

More on SQL Inserts: https://www.w3schools.com/sql/sql_insert.asp

Career Changer, Software Engineer & Web Developer