Search code examples
mysqlsqlsyntaxprimary-keycreate-table

Primary Key Syntax


Other than visually, is there a functional difference between declaring the primary in the column definition or down at the bottom?

For example:

CREATE TABLE tbl_name (
    id INT UNSIGNED NOT NULL AUTO_INCREMENT PRIMARY KEY,
    content TEXT
) ENGINE=InnoDB CHARSET=utf8;

Is the above different than:

CREATE TABLE tbl_name (
    id INT UNSIGNED NOT NULL AUTO_INCREMENT,
    content TEXT,
    PRIMARY KEY ( id )
) ENGINE=InnoDB CHARSET=utf8;

Solution

  • Both syntaxes are equivalent.

    The first one uses a column constraint.

    The second syntax uses table constraint, and is mostly useful when you want a compound primary key, that references more than one column (in which case it is not possible to use a column constraint).