Skip to content

SQL: Using DEFAULT keyword to set value as autogenerated (number etc) #29

@tdoran

Description

@tdoran

About using INSERT in SQL without using the column names and just using VALUES:
You can use the keyword DEFAULT.

Below is an example of how to do it based on the bonus round of the SQL commands challenge

INSERT INTO post_comments VALUES (DEFAULT, (SELECT id FROM
                             blog_posts WHERE text_content LIKE '%Peculiar%'), null, 3,
                             'Interesting post')

This solution is based on the first answer found here:
https://dba.stackexchange.com/questions/60521/how-are-auto-increment-keys-handled-in-insert-select-from

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions