Creating a Database4:02 with Ben Deitch
Learn how to create a database using the SQLite command line tools!
[MUSIC] 0:00 Hi, I'm Ben. 0:05 And in this workshop, we're going to learn about SQLite from the command line. 0:06 So far, we've been learning SQL inside of SQL Playgrounds. 0:11 And while there's nothing wrong with that, 0:15 it's not super helpful when you need to start creating your own databases. 0:17 In this workshop, we're going to look at how you can create and 0:21 manage databases right on your own computer. 0:24 The first thing you'll need to do is make sure that you have SQLite 0:28 installed on your computer. 0:31 If you're on a Mac or Linux computer, then it's likely you already have SQLite. 0:33 To see if SQLite is already on your computer, open up a terminal. 0:38 And at the command prompt type, sqlite3 then hit Enter. 0:42 If your terminal looks like this, then you're good to go. 0:48 If it doesn't, then you'll need to install SQLite. 0:52 Check out the teacher's notes for links to installation instructions. 0:55 All right, now there's two commands that you 0:59 absolutely need to know to be able to use SQLite from the command line. 1:01 The first is .help. 1:06 Calling .help will show you a list of all the commands that you can run. 1:08 So if you're looking for 1:14 anything in particular, remember to look in .help first. 1:15 The second command you absolutely need to know Is .quit, 1:20 which quits SQLite and brings us back to the command prompt. 1:23 It's easy to forget this one. 1:29 So if you're ever stuck at the SQLite prompt remember that you can 1:31 always use .quit to exit. 1:36 And if you forget .quit you can always use .help to help you remember. 1:38 Okay, now that we're back at the command prompt, let's create our own database. 1:44 But first let's create a folder where we can keep our practice files. 1:50 You can do this using the file explorer if you'd like, but 1:55 I'll be using the command line. 1:58 Let's type mkdir, which stands for make directory, and 2:00 let's call it DatabasePractice. 2:03 And hit Enter to create our new folder. 2:10 Then let's change directory into that folder by typing cd and 2:13 then the name of the folder. 2:18 You can use tab to auto-complete. 2:20 And hit Enter to run the command. 2:23 Now that we're in our new folder, we can create a new database by typing sqlite3 2:26 followed by the name of the database. 2:31 Let's type sqlite3, add a space, and 2:34 then name our database mydatabase.db, then hit Enter. 2:39 And now we can start using our database. 2:47 Though before we do, let's quickly check out our DatabasePractice folder. 2:50 We just created a new database, and yet, there's nothing in here. 2:58 Turns out that SQLite won't create our database 3:04 until it's more than just an empty database. 3:07 So let's move forward with creating a table, and then we'll check back in later. 3:11 Back in the terminal, let's create a new table to store information about cars. 3:16 Let's type CREATE TABLE, we'll name it CARS. 3:21 Then for the columns, we'll give it an ID, which should be a PRIMARY KEY. 3:27 And then we'll add a couple more columns like MAKE, MODEL, and YEAR. 3:34 Then hit Enter to run it. 3:41 And that's right. 3:43 We can't forget the semicolon. 3:44 If you're ever looking at something like this, you just need to add a semicolon. 3:46 And with that, if we check back in with our database practice folder, 3:52 we can see that we've now got mydatabase.db. 3:57 Awesome. 4:01
You need to sign up for Treehouse in order to download course files.Sign up