Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

sql: permit designating a primary key in CREATE TABLE AS #20940

Closed
jordanlewis opened this issue Dec 20, 2017 · 1 comment
Closed

sql: permit designating a primary key in CREATE TABLE AS #20940

jordanlewis opened this issue Dec 20, 2017 · 1 comment
Assignees
Labels
A-schema-changes A-sql-semantics C-enhancement Solution expected to add code/behavior + preserve backward-compat (pg compat issues are exception)

Comments

@jordanlewis
Copy link
Member

Currently, CREATE TABLE AS does not allow the user to specify primary key columns for the new table. This is a major limitation because CockroachDB doesn't allow users to change the primary key of a table, unlike in PostgreSQL. PostgreSQL can get away with having no syntax for this because it's no big deal for them to add a primary key later.

We should resolve this limitation by extending the grammar to allow specifying primary key columns.

Currently, the grammar is defined as:

CREATE TABLE any_name opt_column_list AS select_stmt

The opt_column_list production should be replaced with one that permits name, index_def, family_def, and table_constraint - basically exactly what's permitted in an ordinary CREATE TABLE expression, except replacing the column definition syntax (a INT NOT NULL) with column names only (a).

@jordanlewis jordanlewis added the C-enhancement Solution expected to add code/behavior + preserve backward-compat (pg compat issues are exception) label Dec 20, 2017
danhhz added a commit to danhhz/cockroach that referenced this issue Dec 20, 2017
    CREATE TABLE a (b) INTERLEAVE IN PARENT ... AS ...
    CREATE TABLE a (b) PARTITION BY ... AS ...

The interleave/partition clauses could also plausably go after the AS,
but I felt like this was more parallel to CREATE TABLE where they go
right after the column defs. (Plus there's all sorts of grammar
conflicts with putting it after the AS).

Implementation is blocked on cockroachdb#20940, but in the meantime, this will
return a friendly error message.

Also cleans up some unnecessary function parameters that I noticed were
being passed around.

For cockroachdb#20178

Release note: None
@rolandcrosby
Copy link

We may be able to pick up some of this as part of the bulk I/O CTAS reimplementation; assigning to @adityamaru27 to take a look.

craig bot pushed a commit that referenced this issue Jul 16, 2019
38589: pkg: Allow user defined primary key in CREATE TABLE ... AS r=adityamaru27 a=adityamaru27

This change allows users to specify which column in the newly created table should serve as the primary key (#20940). If no such constraint is specified, we generate a unique row_id to serve as the primary key.

Co-authored-by: Aditya Maru <[email protected]>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
A-schema-changes A-sql-semantics C-enhancement Solution expected to add code/behavior + preserve backward-compat (pg compat issues are exception)
Projects
None yet
Development

No branches or pull requests

5 participants