When coding you're faced with tons of data and databases. It can be overwhelming when you're working on projects - big or small. Luckily, there is a tool that we can use to help keep that data organized, readable, and mutable. Were going to talk about what SQL is and how we can implement it in the most basic of ways.
SQL(Structured Query Language)
-- is a standard computer language used for accessing and manipulating databases. SQL creates tables for our data and allows us to be able to reference it and change it as needed. The basis for SQL is the Relational Database Management System or RDBMS for short. It's the program that manages our data and we use SQL to talk to the RDBMS so we can stay on top of our data.
Like most coding languages, CRUD (Create, Read, Update, Delete) is essential for being able to interact with the data that you are working with. SQL is no different. Let's dive into the CRUD actions step-by-step to get started using SQL.
CREATE
In SQL the "Create" is initialized with the INSERT INTO
method. It is a two-step command. In the first part you'll include the table that you're calling upon and the sections of that table you want to add to. The second part, the VALUES
, are the actual input you want added to your table. For our example were going to imagine that we are programmers for Spotify and we are in charge of editing playlists.
Hypothetically, we can do this using SQL. We want to add a new song to the playlist so we can do it like this:
INSERT INTO ThrowbackThursday (Title, Album, Date Added, Time) VALUES('Picture feat. Sheryl Crow', Cocky, 5 days ago, 4:59);
and our play list should look something like this --
READ
In order to perform a 'read' action in SQL you use the command SELECT
. SELECT statements work in a two step system just like the INSERT INTO method.
SELECT <name of column> FROM <table that contains the column>;
So, going back to our playlist from earlier we could do something like --
SELECT id, Title, Album FROM ThrowbackThursday;
And this should return your data in a nice table.
1 | Every Morning | 14:59
2 | Follow Me | Double Wide
3 | One Week | Stunt(20th Anniversary Edition)
etc.
There are ways to build on this initial statement with other conditions to return more specific data as well.
A way to get ALL the data sent back to you is to use "*" aka the 'wildcard selector'. Using the asterisk as your selector tells SQL to give back all the data from the table called on.
SELECT * FROM ThrowbackThursday;
UPDATE
To update your data using SQL you'll use a statement with the basic syntax similar to this.
UPDATE <table name>
SET <column name> = <new value>
WHERE <column name> = <old value>;
To do this in our playlist example we can do something like
UPDATE ThrowbackThursday
SET Title = "Walkin' On The Sun"
WHERE Title = "All Star";
DELETE
The final part to cover for our CRUD actions is the DELETE statements. It is a similar function to how we do the UPDATE function. A basic example would be:
DELETE FROM <table name>
WHERE <column name> = <value>;
Touching back on our playlist, we would have something like
DELETE FROM ThrowbackThursday
WHERE Title = "Tubthumping";
CONCLUSION
I hope after this post you feel ready to dip your toes into the world of SQL.
Top comments (1)
I enjoyed reading over your Video app database.
You organized it well with the CRUD operations.
Good work.