SQL: Insert into a table + Parrot memesšŸ¦œ

Sam Lesser
4 min readJun 6, 2021
Yeah, weā€™ve all been there. SQL can be hard! šŸ¦

Introduction

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 šŸ¼
https://ineedmoreplates.medium.com/create-sql-tables-panda-memes-8f10973a3859

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),
age INTEGER,
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 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)
VALUES
('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, ...)
VALUES
(value1, value2, value3, ...),
(value1, value2, value3, ...),
(value1, value2, value3, ...);

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

Conclusion

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

--

--

Sam Lesser

Career Changer, Software Engineer & Web Developer