sqlfmt icon indicating copy to clipboard operation
sqlfmt copied to clipboard

CREATE TABLE — define a new table

Open tconbeer opened this issue 3 years ago • 3 comments

https://www.postgresql.org/docs/current/sql-createtable.html https://docs.snowflake.com/en/sql-reference/sql/create-table.html https://cloud.google.com/bigquery/docs/reference/standard-sql/data-definition-language#creating_a_new_table https://docs.aws.amazon.com/redshift/latest/dg/r_CREATE_TABLE_NEW.html

tconbeer avatar Nov 02 '22 19:11 tconbeer

create table my_table ( ... ) breaks a lot of "rules" elsewhere in SQL... in particular, the space between the table name and the parens, and the really the use of parens at all, mean that create table is more like a word operator (or even a plain name) than a unterminated keyword. I need to think through how we want to lex this and how specific the formatting rules should be just for this one (very common) statement.

tconbeer avatar Jan 18 '23 23:01 tconbeer

Hey @tconbeer, have you had time to think this through ? I took a look myself and I'm not sure how to implement it properly.

pauldes avatar Nov 27 '23 10:11 pauldes

@pauldes Admittedly this is not my top priority right now. Looking back at this with fresh eyes, it would be relatively easy to auto-format to something like the following:

create table if not exists
    foo
    (
        a int not null,
        b varchar,
        ...
        primary key a
    )
partition by a

Which is pretty different from the styles I usually see, but which I don't hate, tbh. The "problem" is really the initial create table... keyword, and the fact that it works together with the parens of column defs, which makes its mechanics distinct from other keywords, like select

tconbeer avatar Dec 04 '23 20:12 tconbeer