Results 1 to 4 of 4

Thread: how do I change the properties of a column in mysql

  1. #1
    Join Date
    Dec 2011
    Location
    Itegem
    Beans
    40
    Distro
    Kubuntu 14.04 Trusty Tahr

    how do I change the properties of a column in mysql

    I have a table named "boek" and I try to make the id column auto increment.
    what is the exact syntax, please?

    mysql> select * from boek;
    +---------------------------------------+-----------------+-------+----+
    | titel | auteur | aard | id |
    +---------------------------------------+-----------------+-------+----+
    | Sony-San S&W | W.Vandersteen | strip | 0 |
    | De 7 schaken S&w | W.Vandersteen | strip | 0 |
    | Bibber contra Tutter PP & BB | Pom | strip | 0 |
    | Het straalgas mysterie PP & BB | Pom | strip | 0 |
    Last edited by johnnybgoode; January 21st, 2015 at 07:22 PM.

  2. #2
    Join Date
    Dec 2014
    Beans
    2,590

    Re: how do I change the properties of a column in mysql

    See http://dev.mysql.com/doc/refman/5.7/en/alter-table.html and for the column definitions http://dev.mysql.com/doc/refman/5.7/...ate-table.html.

    So it should be something like "alter table modify column boeken integer auto_increment", replace integer with the actual datatype of the column.

  3. #3
    Join Date
    Dec 2011
    Location
    Itegem
    Beans
    40
    Distro
    Kubuntu 14.04 Trusty Tahr

    Re: how do I change the properties of a column in mysql

    Sorry, it doesn't work. I entered

    mysql> alter table modify column id (smallint auto_increment,PRIMARY_KEY (id));
    and get the message:

    ERROR 1064 (42000): You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'column id (smallint auto_increment,PRIMARY_KEY (id))' at line 1

    Am I overlooking something?

  4. #4
    Join Date
    Dec 2014
    Beans
    2,590

    Re: how do I change the properties of a column in mysql

    If you read through the manual page for alter table i linked to, you will find an example which fits with what you want to do about a third of the way down in the page.
    It should be:
    ALTER TABLE boek MODIFY COLUMN id SMALLINT AUTO_INCREMENT;
    Indices and keys are not changed with MODIFY COLUMN. You could also use CHANGE COLUMN instead of MODIFY COLUMN, but the you have to give the name of the column twice (change is for renaming and changing attributes, so you'd be renaming the column to the same name it had before). Take care to give all the attributes you want the column to have, it will be set to have exactly the attributes you set. For example, if you originally defined the column with NOT NULL and executed the statement above, the NOT NULL would be removed from the column definition.

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •