Primaire sleutels (MySQL)

Uit De Vliegende Brigade
Versie door Jeroen Strompf (overleg | bijdragen) op 13 mrt 2020 om 16:56 (→‎Samengestelde primaire sleutel)
(wijz) ← Oudere versie | Huidige versie (wijz) | Nieuwere versie → (wijz)
Naar navigatie springen Naar zoeken springen

In MySQL kun je een tabel pas behoorlijk manipuleren als-ie een primaire sleutel of primary key heeft. Daarnaast heeft MySQL Workbench een bug waardoor-ie zich soms verslikt in tabellen zonder primaire sleutel.

Daarnaast wil ik soms een kolom met 'volgnummers' van records toevoegen aan een bestaande tabel, omdat MySQL van zichzelf geen betrouwbare nummering van records kent.

PK's - Inleiding

  • Een tabel kan maar één primaire sleutel hebben. Deze kolom mag niet leeg zijn
  • Een samengestelde sleutel geldt ook als één primaire sleutel.

PK defineren bij aanmaken van tabel

Simpel:

drop table if exists access_number_tmp;

create table access_number_tmp
(
    destination_en				varchar(255),
    destination_nl				varchar(255),
    destination_es				varchar(255),
    territory_en				varchar(255),
    territory_nl				varchar(255),
    territory_es				varchar(255),
    slug_territory_nl				varchar(255),	# For constructing hyperlinks
    slug_territory_en				varchar(255),	# For constructing hyperlinks
    slug_territory_es				varchar(255),	# For constructing hyperlinks
    access_number				varchar(40),
    price_per_minute_txt_period_raw		varchar(40),	# Can have more figures behind decimal separation symbol
    price_per_minute_decimal			decimal(5,2),	# 3 Cijfers vóór decimaalscheidingsteken, 2 erna
    price_per_minute_txt_period_euro_symbol	varchar(40),    
    price_per_minute_txt_comma_euro_symbol	varchar(40),
    table_row_html_nl				text,
    table_row_html_en				text,
    table_row_html_es				text,
    
    primary key (destination_en)
);

pk-veld naderhand toevoegen

Gelukkig kun je primaire sleutels gemakkelijk later toevoegen én gelijk invullen:

ALTER TABLE tbl_tabelnaam ADD pk INT PRIMARY KEY AUTO_INCREMENT;

Als zo'n pk geen 'echt' veld, maar puur is toegevoegd om als sleutel te dienen, noem ik 'm pk.

Bestaande kolom pk maken

alter table tbl_main_tool add primary key (pk);

Samengestelde primaire sleutel

Samengestelde primaire sleutel aka. composite primary key is een fluitje van een cent. Bv.:

create table device_sku_int
(
    brand		varchar(255),	# Device brand, e.g. Bosch
    device_type		varchar(255),	# Machinereferentie, e.g. 10GM/P
    device_sku		varchar(255),	# Device SKU, assigned by brand 
    device_kind_nl	varchar(255),	# E.g. "Boormachine"
    device_voltage	varchar(40),
    device_other	varchar(255),
    device_kind		varchar(40),
    device_lang		varchar(40),
    sku			varchar(255),
    cat			varchar(255),	# Just a field from source tables (March 2020)
    note		text,		# Just a field from source tables (March 2020) 
    
    # Primary key
    ##############################################
    #
    # * Originally (bal_dwh_op.sq_device_sku_import) the primary key was (brand, device_type),
    #   which resulted in 23.918 records (March 13, 2020)
    #   That's fine if you only want to know the devices, but not fine if you want to know
    #   all combinations of devices + skus - Which is the purpose of this sproc/table.
    # * With primary key (brand, device_type, sku), there are 32.031 records (March 13,
    #   2020)
    # * There are situations without device_type, but with device_sku → Make it a
    #   PK with four fields 
    # * With primary key (brand, device_type, device_sku, sku), there are 32.233 records
    #   (March 2020) - Not much of a difference
    #
    primary key(brand, device_type, device_sku, sku),

);

of

alter table brush_tool
add primary key (tool_id, sku);

Zie ook

Bronnen