Join egghead, unlock knowledge.

Want more egghead?

This lesson is for members. Join us? Get access to all 3,000+ tutorials + a community with expert developers around the world.

Unlock This Lesson
Become a member
to unlock all features

Level Up!

Access all courses & lessons on egghead today and lock-in your price for life.


    Create a Postgres Table

    Brett CassetteBrett Cassette

    Learn how to create a table using the most widely-used data types (serial, varchar, integer, float, boolean, and date), and the most necessary constraints (NOT NULL and primary key).



    Become a Member to view code

    You must be a Member to view code

    Access all courses and lessons, track your progress, gain confidence and expertise.

    Become a Member
    and unlock code for this lesson




    We're going to start with a Create Table statement and Create Directors table, some movie directors for us. Nice ID column here. Serial says that it's going to be auto-incrementing, so it would go one, two, three automatically. We don't have to worry about that.

    We'll get a name for our directors. This is going to be a string up to 200 characters long. We can just see here that the directors table is in fact created. There's no data in it. Name, ID -- that's what we expect to see.

    Directors don't really matter unless we have movies. Let's give it another ID column. We've seen that before. Again, these are kind of arbitrary. We'll give it a title. We probably want the title to be there, so we're going to say that it's not null. It can never be null.

    Let's give it a release date. We haven't seen the date definition before. It's going to be the number of stars they can have, maybe one, two, three, four, five. Let's give it a director ID to reference the directors table.

    We saw directors before. Let's see movies. There we go. A little empty table. That's what we expect.

    Just for good measure, I'm going to insert some data here. Don't worry about what these things say. I just want to take a look at what this means to have some data in our tables now, so we can have a sense of what these tables are about.

    Now we have our directors table up here. We see IDs were inserted for us, because we only inserted some names up here. The same thing happens for us down here in the movies table. We inserted a couple movies, and this is what those tables look like when we fill in some more data.