Results 1 to 6 of 6

Thread: Autonumbers in SQL ??

  1. #1

    Thread Starter
    New Member
    Join Date
    Feb 2004
    Posts
    12

    Arrow Autonumbers in SQL ??

    Hi guys,

    are there such things as autonumber datatypes in SQL??

    I have the following code that create a table from code, but i want the first column to be an autonumber that SQL server can take care of.

    Code:
                sqlTableConnection = New SqlConnection(strConnectionStr)
                sqlTableConnection.Open()
    
                'create table code
                sqlTableComm = New SqlCommand("CREATE TABLE Contects (Title VARCHAR(6) NOT NULL, Firstname VARCHAR(20) NOT NULL, Lastname VARCHAR(20) NOT NULL, Created DATETIME NOT NULL, Deleted BIT NOT NULL)", sqlTableConnection)
                sqlTableComm.ExecuteNonQuery()
                sqlTableConnection.Close()
    if there isnt a way to create autonumbers, is it best just to increment a number from the last record in the table??

    cheers,
    soulcode

  2. #2

    Thread Starter
    New Member
    Join Date
    Feb 2004
    Posts
    12
    Also...

    What is the best datatype to store phone numbers in?? is it best to store them in a String then validate it on the form, or store it in a number type??


    cheers again,
    soulcode

  3. #3
    Frenzied Member
    Join Date
    Feb 2003
    Location
    Argentina
    Posts
    1,950
    Not sure the exact syntax for SQL Server, but there should be a setting for autoincrement. Check the docs.
    Store phone numbers as text. You won't ever perform calculations on them, and if you store them with (), -, etc, that's text anyway.

  4. #4
    Hyperactive Member ARPRINCE's Avatar
    Join Date
    Mar 2003
    Location
    Pinoy in NJ
    Posts
    381
    Try....

    VB Code:
    1. CREATE TABLE Connects (
    2.     ConnectsID int IDENTITY (1, 1) NOT NULL , Title VARCHAR(6) NOT NULL, .....

  5. #5
    Frenzied Member Memnoch1207's Avatar
    Join Date
    Feb 2002
    Location
    DUH, Guess...Hint: It's really hot!
    Posts
    1,861
    Or use a UniqueIdentifier instead of an Identity.
    I would store phone numbers in a varchar(14) field.
    Being educated does not make you intelligent.

    Need a weekend getaway??? Come Visit

  6. #6
    New Member BlackDeath78's Avatar
    Join Date
    Feb 2004
    Location
    Lower Saxony, Germany
    Posts
    5

    SQL

    Hi folks,

    ARPRINCE has posted the correct syntax for an int column with an automatic generated number.

    If you prefer a GUID for your PKs try this:

    Code:
    CREATE TABLE Connects (MyKey uniqueidentifier NOT NULL DEFAULT newid())
    Bye
    BD
    A real hacker never dies. His TTL expires!

Posting Permissions

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



Click Here to Expand Forum to Full Width