Loading
Current section: Database Schema 3 exercises
lesson

Intro to Database Schema

Transcript

00:00 In this exercise, we're going to create a database schema where we can insert users. And that's actually all we're going to be able to do is just do users. But this is going to get us introduced to the idea of creating a new database with Prisma. We're going to definitely be looking at some SQL too, so look forward to that. So just to give you a kind of an idea

00:19 around what database schemas are and why they're so great, this is what it would take to create a series of tables in a database that you're just creating, or maybe you already have a database, you need to make another one, another instance for staging or something like that. So you're going to execute the SQL.

00:39 So create table, you've got your ID, this is a primary key, and you've got email, username, name, all that stuff, the created at, updated at. And then you've got one for a sandwich and then order and order item. You've got these constraints on foreign keys and cascade on update and delete and stuff. Yeah, there's a lot to this.

00:56 And SQL is a really powerful language, super powerful. It runs like everything, it's amazing. But honestly, I was a business intelligence engineer. I worked in SQL a lot, and I don't totally love this myself. And so I actually really like Prisma that allows me to model things

01:14 following this type of model. So here we have our user and we can define relationships here. We have these relation attributes where we can define these relationships. We have actually a really nice editor plugin that will syntax highlight this for you. So make sure you install that if you haven't already for Prisma.

01:34 And yeah, then you just define all of your different data types and everything in the model. And then Prisma will generate the SQL necessary for creating all of these tables. Now, honestly, it's like, well, do I learn one language or do I learn another? Like probably should just learn SQL, right? And yes, you probably should learn SQL.

01:54 But one of the benefits of using the model is that, or the Prisma schema, is that it will track migrations for you. And we'll talk about migrations later, but you're gonna really like this Prisma schema. At least I really like this Prisma schema. I recommend it. It's awesome. So Prisma also has a couple of commands that you're gonna be using.

02:13 We have a Prisma DB push that will take your model and turn it or push it onto a database. So that's handy, especially during development. We have migration commands for iteratively migrating different aspects of your database, like changes to existing databases.

02:33 But if you're making a new one, you just NPX Prisma DB push and boom, there you go. Your database now looks like your schema. You can actually do the reverse. I've never actually done this. I've never had an existing database that I wanted to migrate over to Prisma. But if you did, then you could use this to really easily and quickly create a schema that matched

02:53 your existing database, which is kind of handy. And then NPX Prisma Studio allows you to have, pull up a browser tab that shows you all of the data in your database. And you can even manipulate that and stuff. So we're gonna actually be doing that in this exercise as well. This is a pretty quick exercise, actually only one step,

03:12 but it is distinct enough to deserve its own exercise. So have a good time with this one. And yeah, I think that you're gonna rock. Have fun.