Learn SQL the Hard Way Exercise 1

I’m still working on the final exercise for Learn Ruby the Hard Way Exercise. However, I thought I would start learning a bit of SQL as well. I finished Codecademy’s SQL course. As usual, Codecademy’s format made it super easy to go through many concepts in a short amount of time. However, with the handholding, it’s definitely easy to have a false sense of understanding. I decided to try Zed’s new book, Learn SQL the Hard Way.
In Exercise 1, you download sqlite 3 and create a simple table.

Creating a table with SQL
Creating a table with SQL

CREATE TABLE is a clause. A clause, also referred to as a command, performs a specific task in SQL. By convention, clauses are all upper-case.
person is the table name. This is the table that the command is applied to.
Lines 2-6 contain parameters. These are passed to the clause as arguments. Here, we have a list of column names and the associated data type.
First column in the table has a name of id, and contains integers. This is a special column because a PRIMARY KEY column: 1) does not have null values 2) has unique values (every value in this column is unique, there are no duplicates).
Second column name is first_name, and contains text values. Third column name is last_name and contains text values. The age column contains integers. And finally, the email column contains text values.
To run this, we go into Terminal and type sqlite3 ex1.db < ex1.sql, this creates a file ex1.db, we can check the existence by using the ls command. (This lists all the files in the directory.)
Note: SQL is not case-sensitive, so if we change capitals to lowercase in this file, it will still work.

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s