All right.
So let's create a table. If you remember to go to our Postgres database we simply write psql
then our database name and we'll enter the database.
And now we can communicate with it using SQL.
Now let's create a table.
How can we do that.
Again SQL comes with some language. Language syntax that we can use. And to create a table we can
use something like this.
We say 'CREATE TABLE' which is an SQL command.
We enter a table name followed by brackets and then we say what we want the column_1 to say with
the data type, column_2 with the data type, column_3 with the data type and then ending it with semicolon.
Let me just press enter and see what happens here.
I get an error 'type "datatype" does not exist' and that is because datatype is referring to what Postgres
or our relational database knows, we have to say ahead of time what type of data
column_1, column_2, and column_3 hold.
If we go back to our Chrome browser we see that for Postgres we have a few data types that we can
hold. String datatypes, numeric datatypes, and date and time datatypes.
And each one of them have different things. For a numeric data type
We have integer which is a 4 byte integer.
We have bigint which allows for a bigger number and all these different things,
we have money which is currency value and the reason we have to give this datatype is because of the
way databases work. They're optimized to make sure that as long as everything is the same in their column
they're able to work really really fast to perform actions.
In our case let's use some of the most common ones. We can use text for the first column.
So let's do that.
I'm going to press the up arrow and get create table.
I'm also going to name this table, let's call it users
and column_1 will be name, datatype can be text, column_2 we can say that is age and datatype for an
age.
Well if we look down here we look at numeric types.
We want to use something like smallint which is a 2-byte signed integer.
It means that a max for a smallint, if I go to smallint Max
we see that the max range is 32,762. Well nobody's going to be that
old, right?
So we can use smallint for now lets do that. I'm going to say age is smallint.
And finally the third column will just have 'birthday' and birthday can be a date datatype and we can
just have date let's press enter and I get create table.
Let's look at our PSequel here. And I'll minimize this.
And if I click refresh here I see that we have users. Look at that and we have columns for the table users
that is name, age, birthday.
It tells us the type and the length.
So now we have our table. Awesome.
There's also a way to do it here if we do '\d' I get list of relations and look at that we
have our users table and the owner which is me, very Cool.
There you go.
We've created our first table. Now that we have this we can start entering some information.
And by the way you might be asking how many times do we run these commands.
Well ideally once you create your database and it lives somewhere on a computer somewhere in the world
you only run this once, right.
You create a table of what you want your database to look like and the rest is just filling in and inserting
information and reading information off of it.
So this is the command that we're going to set up when we create our own database to set up our schemas.
And then after that it'll just run on its own.
And It will be able to communicate with the back end.
It's going to be very fun.
By the way before we get into the next video I want to show you one more command.
We learned the '\d' shows us our tables.
If I do '\q' We exit out of here and we're back to the terminal.
All right I'll see you in the next one.
Bye-bye.