[MySql] Global unique FOREIGN KEY(guid table on serveral types)

FBIagent

Erfahrenes Mitglied
Hallo zusammen,

ich arbeite im Moment an einer Datenbankstruktur mit Benutzerkonten. Die Datenbank besteht aus
Benutzerkonten gespeichert in der Tabelle `accounts` und News gespeichert in der Tabelle `news`.

1. Jeder dieser beiden Tabellen besitzt als PRIMARY KEY eine eindeutige ID für jeden Eintrag. Die ID ist vom Typ INT(10) UNSIGNED und AUTO INCREMENT.
2. In beiden Tabellen erhält jeder Eintrag eine Spalte mit einem Namen(bei news der Titel) und ein Erstelldatum und -zeit.

Meine Frage wäre: In wie fern ist es sinnvoll eine Tabelle anzulegen, nennen wir sie einfach mal `global_uniques`, die eine eindeutige globale ID unabhängig vom Eintragstyp bekommt um Name und
Erstelldatum und -zeit zu speichern so das die Benutzerkonten und News darauf verweisen können?

Meine bisherige Struktur:
SQL:
CREATE TABLE `accounts` (
  `account_id` int(10) unsigned NOT NULL AUTO_INCREMENT,
  `account_name` varchar(20) NOT NULL COMMENT 'The account name given at registration time in lower case.',
  `account_name_exact` varchar(20) NOT NULL COMMENT 'The account name given at registration time in exact spelling.',
  `account_mail` varchar(320) NOT NULL,
  `account_pwd_hash` char(32) NOT NULL COMMENT 'Account password as MD5 hash.',
  `account_access_group` enum('user','moderator','administrator') NOT NULL DEFAULT 'user',
  `account_create_date` datetime NOT NULL,
  PRIMARY KEY (`account_id`),
  UNIQUE KEY `idx_uniq_acc_name` (`account_name`) USING BTREE
) ENGINE=InnoDB AUTO_INCREMENT=4 DEFAULT CHARSET=utf8;

CREATE TABLE `news` (
  `news_id` int(10) unsigned NOT NULL AUTO_INCREMENT,
  `news_creator_id` int(10) unsigned DEFAULT NULL COMMENT 'The id of the account who created the news.',
  `news_image` varchar(256) DEFAULT NULL,
  `news_title` varchar(100) NOT NULL,
  `news_text` varchar(6000) NOT NULL,
  `news_create_date` datetime NOT NULL,
  PRIMARY KEY (`news_id`),
  KEY `idx_norm_news_creator_id` (`news_creator_id`) USING BTREE,
  CONSTRAINT `fk_news_creator_id` FOREIGN KEY (`news_creator_id`) REFERENCES `accounts` (`account_id`) ON DELETE SET NULL ON UPDATE CASCADE
) ENGINE=InnoDB DEFAULT CHARSET=utf8;

Nette Grüße,
FBIagent
 
Zuletzt bearbeitet von einem Moderator:
Das kommt drauf an. Es macht nur dann Sinn, wenn du die beiden Datentypen wild mischen willst. Also beide etwa gleich behandeln willst.
Wenn du aber bei den zugriffen bereits weist, ob es eine News oder ein Account ist, macht es wenig Sinn.
 
Zurück