13

Can I create an index on a CREATE TABLE statement?
I.e. can I define indexes for a table when I create the table? I mean create the index on the create stamement sql command

1 Answer 1

33

Yes, it can be defined in the CREATE TABLE, the convention is to put these definitions at the end of the list.

CREATE TABLE `example`
(
    `id` INTEGER  NOT NULL AUTO_INCREMENT,
    `index_col` VARCHAR(20),
    PRIMARY KEY (`id`),
    INDEX `index_name` (`index_col`)
)
Sign up to request clarification or add additional context in comments.

2 Comments

The only relevant answer. +1
As a caveat, in PostgreSQL it is not possible to add an index in the CREATE TABLE command. This requires a separate CREATE INDEX command.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.