Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
431 views
in Technique[技术] by (71.8m points)

sql server - How to autoincrement a varchar

Can I make a primary key like 'c0001, c0002' and for supplier 's0001, s0002' in one table?

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)
  1. The idea in database design, is to keep each data element separate. And each element has its own datatype, constraints and rules. That c0002 is not one field, but two. Same with XXXnnn or whatever. It is incorrect , and it will severely limit your ability to use the data, and use database features and facilities.

    Break it up into two discrete data items:
    column_1 CHAR(1)
    column_2 INTEGER

    Then set AUTOINCREMENT on column_2

    And yes, your Primary Key can be (column_1, column_2), so you have not lost whatever meaning c0002 has for you.

  2. Never place suppliers and customers (whatever "c" and "s" means) in the same table. If you do that, you will not have a database table, you will have a flat file. And various problems and limitations consequent to that.

    That means, Normalise the data. You will end up with:

    • one table for Person or Organisation containing the common data (Name, Address...)
    • one table for Customer containing customer-specific data (CreditLimit...)
    • one table for Supplier containing supplier-specific data (PaymentTerms...)
    • no ambiguous or optional columns, therefore no Nulls
    • no limitations on use or SQL functions
      .

    And when you need to add columns, you do it only where it is required, without affecting all the other sues of the flat file. The scope of effect is limited to the scope of change.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...