sql - Numeric IDs vs. String IDs -


i'm using stripped down example here please ask if need more context.

i'm in process of restructuring/normalising database id fields in majority of tables have primary key fields auto-incremented numerical id's (1,2,3 etc.) , i'm thinking need change id field numerical value string value generated data in row.

my reasoning follows:

i have 5 tables; staff, members, volunteers, interns , students; of these have numeric id's.

i have table called buildingattendance logs when people visited premises , reason has following relevant fields:

id    type    premises    attended 

to differentiate between staff , members. use type field, using mem member , sta staff, etc. example:

id    type    premises      attended 1     mem     building    27/6/15 1     sta     building    27/6/15 2     stu     building b    27/6/15 

i'm thinking might better design design use id similar following:

id       premises      attended mem1     building    27/6/15 sta1     building    27/6/15 stu2     building b    27/6/15 

what best way deal this? know if primary key string query performance may take hit, easier having 2 columns?

tl;dr - how should deal table references records other tables same id system?

auto-incremented numeric ids have several advantages on strings:

  • they easier implement. in order generate strings (as want them), need implement trigger or computed column.
  • they occupy fixed amount of storage (probably 4 bytes), more efficient in data record , in indexes.
  • they allow members change between types, without affecting key.

the problem facing have subtypes of supertype. information should stored person, not in attendance record (unless person change type each visit). there several ways approach in sql, none clean simple class inheritance in programming language.

one technique put data in single table called persons. have unique id, type, , columns 5 tables. problem when columns subtables different.

in case, have table called persons unique primary key , common columns. have separate tables each 1 , use personid primary key these tables.

the advantage approach can have foreign key reference persons buildingattendance. and, can have foreign key references each of subtypes, other tables appropriate.


Comments

Popular posts from this blog

java - WARN : org.springframework.web.servlet.PageNotFound - No mapping found for HTTP request with URI [/board/] in DispatcherServlet with name 'appServlet' -

html - Outlook 2010 Anchor (url/address/link) -

android - How to create dynamically Fragment pager adapter -