Fork me on GitHub
#sql
<
2019-08-04
>
Ashley Smith15:08:58

Hey everyone, I need some help. I'm quite new to SQL and while I get the gist of SELECT statements and stuff I'm trying to bring things all together. I want to create a table it it hasn't already been created, but the answers I'm getting online are a little confusing. This is advised: https://stackoverflow.com/questions/5952006/how-to-check-if-table-exist-and-if-it-doesnt-exist-create-table-in-sql-server-2

IF  NOT EXISTS (SELECT * FROM sys.objects 
WHERE object_id = OBJECT_ID(N'[dbo].[YourTable]') AND type in (N'U'))

BEGIN
CREATE TABLE [dbo].[YourTable](
    ....
    ....
    ....
) 

END
Is sys.objects a part of a database by default or is that some sort of variable from the question? I'm using postgresql but I expect this will work here as well? Also, does does this basically all take place inside of jdbc/query?

carkh15:08:32

@ashley no this will not work with postgres

Ashley Smith15:08:16

ah so when I search for answers I can't just look at SQL responses it needs to be postgres responses?

carkh15:08:19

what I do is do it with pgadmin, inspect how it does things, then apply to my code

carkh15:08:20

also you don't ever want to be in a position wher eyou don't know if a table exists or not

Ashley Smith15:08:08

hmm, so how would you go about the automation of setting up a server? I want to basically say 'when you boot up, use this table. If this is your first time, create it' kind of thing

carkh15:08:45

i guess you will need this for the very first table is you're using a variables table, though postgres has database variables for this iirc

carkh15:08:02

if you create a new database, you know it's empty

carkh15:08:31

if it exists, you already have a database version somewhere in there because you added it directly

Ashley Smith15:08:56

I have docker create the database for me, but obviously when the server restarts the database still exists

Ashley Smith15:08:27

I was initially going to have docker create the tables and stuff too but I wasn't sure about how to go about that so thought about doing it on the server anyway

carkh15:08:39

in your database creation step you set it up so that you directly have a database version, so you know the state of the database

Ashley Smith16:08:14

yeah that would make sense, then the database is ready to use by my api straight away

carkh16:08:25

that's how i do it, i guess there are other means, but i never had to blindly create a table

Ashley Smith16:08:17

I need to learn how to do that I guess

carkh16:08:56

i have a startup backup of a database i created with pgadmin

carkh16:08:07

i create the database using it

carkh16:08:22

in there is a variables table with a db_version

carkh16:08:35

then i have some code to bring it to current version

Ashley Smith16:08:04

I've got a lot of research to do, thank you

Ashley Smith16:08:12

I need to try and get pgadmin working first and foremost

Ashley Smith16:08:16

I've been using shell so far

carkh16:08:42

you can use psql for command line and scripting

seancorfield18:08:22

@ashley Pretty much all databases support CREATE my_table IF NOT EXISTS ( ... ) so you can run that at startup if you want to conditionally create tables.

seancorfield18:08:38

If you need to build a database schema up over time, you'll want to look at libraries like Migratus (for SQL migrations to apply changes to the database schema).

carkh18:08:54

i'm a proponent of do it yourself, get the exact result you want

Ashley Smith18:08:58

@seancorfield I got it working 🙂 I discovered the entrypoint folder

Ashley Smith18:08:29

so I'm currently writing an sql file called init.sql that I'll use to set up my tables and initial data