将键匹配到MySQL中的表

时间:2018-09-13 11:10:19

标签: mysql

我有这样的表:

CREATE TABLE `psAdmin` (
  `id_admin` INT(10) UNSIGNED NOT NULL,
  `enable` CHAR(1) COLLATE utf8_unicode_ci NOT NULL DEFAULT '0',
  `company_id` INT(11) NOT NULL,
  `username` VARCHAR(50) COLLATE utf8_unicode_ci NOT NULL,
  `password` VARCHAR(128) COLLATE utf8_unicode_ci NOT NULL,
  `salt` VARCHAR(128) COLLATE utf8_unicode_ci NOT NULL,
  `last_logged_in_date` datetime NOT NULL,
  `valid_account_in_time` CHAR(1) COLLATE utf8_unicode_ci NOT NULL DEFAULT '0',
  `expiration_date` DATE NOT NULL
) ENGINE=InnoDB DEFAULT CHARSET=utf8 COLLATE=utf8_unicode_ci;

CREATE TABLE `psAdminDetails` (
  `id_admin_details` INT(10) UNSIGNED NOT NULL,
  `id_admin` INT(10) UNSIGNED NOT NULL,
  `name` VARCHAR(60) COLLATE utf8_unicode_ci DEFAULT NULL,
  `company_name` VARCHAR(120) COLLATE utf8_unicode_ci DEFAULT NULL,
  `nip` VARCHAR(20) COLLATE utf8_unicode_ci DEFAULT NULL,
  `street` VARCHAR(70) COLLATE utf8_unicode_ci DEFAULT NULL,
  `city` VARCHAR(50) COLLATE utf8_unicode_ci DEFAULT NULL,
  `post_code` VARCHAR(12) COLLATE utf8_unicode_ci DEFAULT NULL,
  `country_id` INT(11) NOT NULL DEFAULT '0',
  `provincial_id` INT(11) NOT NULL DEFAULT '0',
  `avatar` VARCHAR(65) COLLATE utf8_unicode_ci DEFAULT NULL,
  `level` CHAR(1) COLLATE utf8_unicode_ci NOT NULL DEFAULT '0',
  `notes` text COLLATE utf8_unicode_ci
) ENGINE=InnoDB DEFAULT CHARSET=utf8 COLLATE=utf8_unicode_ci;

CREATE TABLE `psAdminLoginAttempts` (
  `id_admin_login_attempts` INT(10) UNSIGNED NOT NULL,
  `id_admin` INT(10) UNSIGNED NOT NULL,
  `time` datetime NOT NULL
) ENGINE=InnoDB DEFAULT CHARSET=utf8 COLLATE=utf8_unicode_ci;

CREATE TABLE `psAdminLoginHistory` (
  `id_admin_history` INT(10) UNSIGNED NOT NULL,
  `id_admin` INT(10) UNSIGNED NOT NULL,
  `time` datetime NOT NULL,
  `ip` VARCHAR(25) COLLATE utf8_unicode_ci NOT NULL
) ENGINE=InnoDB DEFAULT CHARSET=utf8 COLLATE=utf8_unicode_ci;

CREATE TABLE `psCompany` (
  `id_company` INT(10) UNSIGNED NOT NULL,
  `enable` CHAR(1) COLLATE utf8_unicode_ci NOT NULL DEFAULT '0',
  `name` VARCHAR(85) COLLATE utf8_unicode_ci DEFAULT NULL,
  `nip` VARCHAR(20) COLLATE utf8_unicode_ci DEFAULT NULL,
  `notes` text COLLATE utf8_unicode_ci,
  `street` VARCHAR(70) COLLATE utf8_unicode_ci DEFAULT NULL,
  `city` VARCHAR(50) COLLATE utf8_unicode_ci DEFAULT NULL,
  `post_code` VARCHAR(12) COLLATE utf8_unicode_ci DEFAULT NULL,
  `country_id` INT(11) NOT NULL DEFAULT '0',
  `provincial_id` INT(11) NOT NULL DEFAULT '0',
  `contact_person` VARCHAR(50) COLLATE utf8_unicode_ci DEFAULT NULL,
  `email` VARCHAR(50) COLLATE utf8_unicode_ci DEFAULT NULL,
  `www` VARCHAR(50) COLLATE utf8_unicode_ci DEFAULT NULL,
  `phone_number` VARCHAR(25) COLLATE utf8_unicode_ci DEFAULT NULL,
  `date_of_registration` datetime NOT NULL
) ENGINE=InnoDB DEFAULT CHARSET=utf8 COLLATE=utf8_unicode_ci;

CREATE TABLE `psProvincial` (
  `provincial_id` INT(10) UNSIGNED NOT NULL,
  `name` VARCHAR(35) COLLATE utf8_unicode_ci NOT NULL
) ENGINE=InnoDB DEFAULT CHARSET=utf8 COLLATE=utf8_unicode_ci;

ALTER TABLE `psAdmin`
  ADD PRIMARY KEY (`id_admin`);

ALTER TABLE `psAdminDetails`
  ADD PRIMARY KEY (`id_admin_details`);

ALTER TABLE `psAdminLoginAttempts`
  ADD PRIMARY KEY (`id_admin_login_attempts`);

ALTER TABLE `psAdminLoginHistory`
  ADD PRIMARY KEY (`id_admin_history`);

ALTER TABLE `psCompany`
  ADD PRIMARY KEY (`id_company`);

ALTER TABLE `psProvincial`
  ADD PRIMARY KEY (`provincial_id`);

ALTER TABLE `psAdmin`
  MODIFY `id_admin` INT(10) UNSIGNED NOT NULL AUTO_INCREMENT;

ALTER TABLE `psAdminDetails`
  MODIFY `id_admin_details` INT(10) UNSIGNED NOT NULL AUTO_INCREMENT;

ALTER TABLE `psAdminLoginAttempts`
  MODIFY `id_admin_login_attempts` INT(10) UNSIGNED NOT NULL AUTO_INCREMENT;

ALTER TABLE `psAdminLoginHistory`
  MODIFY `id_admin_history` INT(10) UNSIGNED NOT NULL AUTO_INCREMENT;

ALTER TABLE `psCompany`
  MODIFY `id_company` INT(10) UNSIGNED NOT NULL AUTO_INCREMENT;

ALTER TABLE `psProvincial`
  MODIFY `provincial_id` INT(10) UNSIGNED NOT NULL AUTO_INCREMENT;
COMMIT;

表具有以下功能:

psAdmin-管理员登录表 psAdminDetails-详细管理员的数据表 psAdminLoginAttempts-具有管理员登录尝试的表 psAdminLoginHistory-包含管理员登录历史记录的表 psCompany-公司数据库。公司可以无限多。您可以为每个公司分配管理员 psProvincial-省份

结合起来很有意义:

psAdminLoginAttempts.id_admin = psAdmin.id_admin
psAdminLoginHistory.id_admin = psAdmin.id_admin
psAdmin.company_id = psCompany.id_company
psAdminDetails.id_admin = psAdmin.id_admin
psAdminDetails.provincial_id = psAdmin.id_admin
psAdminLoginAttempts.id_admin = psAdmin.id_admin
psAdminLoginHistory.id_admin = psAdmin.id_admin

您是否还要添加其他键(例如外键)?如果是这样,在哪里?

1 个答案:

答案 0 :(得分:0)

我会在所有主要的select命令上运行EXPLAIN SELECT,并以此添加索引 如果还没有,默认情况下,我会索引定义中为键的任何字段或任何“标签”字段的索引,例如我将分组依据的字段

在索引VARCHARS时,我也会很小心,我会使用它们的并行INT值-company_id而不是company_name。注意不要将其作为外键

我的默认索引:

ALTER TABLE psAdmin ADD KEY company_id (company_id);
ALTER TABLE psAdmin ADD KEY username (username);

ALTER TABLE psAdminDetails ADD KEY id_admin (id_admin);
ALTER TABLE psAdminDetails ADD KEY country_id (country_id);
ALTER TABLE psAdminDetails ADD KEY provincial_id (provincial_id);

ALTER TABLE psAdminLoginAttempts ADD KEY id_admin (id_admin);

ALTER TABLE psAdminLoginHistory ADD KEY id_admin (id_admin);

ALTER TABLE psAdminLoginHistory ADD KEY country_id (country_id);