SQL: Insert into a table + Parrot memesš¦
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
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