Creating table in sql

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • mik

    Creating table in sql

    I realise this is the wrong place to post, but i can't
    find an equivalent newsgroup for sql.
    My query is simple. Please help!

    i'm trying to create a table in a database. It needs a
    primary key and the primary key should increment.

    i've written:
    CREATE TABLE animals
    (
    Id INT NOT NULL PRIMARY KEY AUTO_INCREMENT,
    Name VARCHAR(30) NOT NULL
    );


    but get this error:
    Incorrect syntax near 'AUTO_INCREMENT '.

    What am i doing wrong?
  • Willy Denoyette [MVP]

    #2
    Re: Creating table in sql


    "mik" <anonymous@disc ussions.microso ft.com> wrote in message
    news:039901c54b 0e$9568b5e0$a60 1280a@phx.gbl.. .[color=blue]
    >I realise this is the wrong place to post, but i can't
    > find an equivalent newsgroup for sql.
    > My query is simple. Please help!
    >
    > i'm trying to create a table in a database. It needs a
    > primary key and the primary key should increment.
    >
    > i've written:
    > CREATE TABLE animals
    > (
    > Id INT NOT NULL PRIMARY KEY AUTO_INCREMENT,
    > Name VARCHAR(30) NOT NULL
    > );
    >
    >
    > but get this error:
    > Incorrect syntax near 'AUTO_INCREMENT '.
    >
    > What am i doing wrong?[/color]


    If your are talking about SQL Server and T-SQL, please post to:

    microsoft.publi c.sqlserver.pro gramming

    Willy.







    Comment

    • Ignacio Machin \( .NET/ C# MVP \)

      #3
      Re: Creating table in sql

      Hi,


      You are right, this is not the group for this, anyway this is how you
      declare a PK

      create table T1
      (
      CrewID SMALLINT IDENTITY PRIMARY KEY NOT NULL
      )


      Cheers,

      --
      Ignacio Machin,
      ignacio.machin AT dot.state.fl.us
      Florida Department Of Transportation



      "mik" <anonymous@disc ussions.microso ft.com> wrote in message
      news:039901c54b 0e$9568b5e0$a60 1280a@phx.gbl.. .[color=blue]
      >I realise this is the wrong place to post, but i can't
      > find an equivalent newsgroup for sql.
      > My query is simple. Please help!
      >
      > i'm trying to create a table in a database. It needs a
      > primary key and the primary key should increment.
      >
      > i've written:
      > CREATE TABLE animals
      > (
      > Id INT NOT NULL PRIMARY KEY AUTO_INCREMENT,
      > Name VARCHAR(30) NOT NULL
      > );
      >
      >
      > but get this error:
      > Incorrect syntax near 'AUTO_INCREMENT '.
      >
      > What am i doing wrong?[/color]


      Comment

      • oj

        #4
        Re: Creating table in sql

        Microsoft.publi c.sqlserver.pro gramming is where you'd want.

        Anyway, the equivalent to auto_increment is identity. So, your ddl would
        look like this.
        CREATE TABLE animals
        (
        [Id] INT IDENTITY NOT NULL PRIMARY KEY ,
        [Name] VARCHAR(30) NOT NULL
        );

        --
        -oj


        "mik" <anonymous@disc ussions.microso ft.com> wrote in message
        news:039901c54b 0e$9568b5e0$a60 1280a@phx.gbl.. .[color=blue]
        >I realise this is the wrong place to post, but i can't
        > find an equivalent newsgroup for sql.
        > My query is simple. Please help!
        >
        > i'm trying to create a table in a database. It needs a
        > primary key and the primary key should increment.
        >
        > i've written:
        > CREATE TABLE animals
        > (
        > Id INT NOT NULL PRIMARY KEY AUTO_INCREMENT,
        > Name VARCHAR(30) NOT NULL
        > );
        >
        >
        > but get this error:
        > Incorrect syntax near 'AUTO_INCREMENT '.
        >
        > What am i doing wrong?[/color]


        Comment

        Working...