-2

我正在为一家新公司开发应用程序管理业务(销售、供应商、客户、产品……)。首先,我需要创建一个数据库。您能否告诉我下面的 BD 方案是否良好和优化?

CREATE TABLE IF NOT EXISTS `company` (
  id UNSIGNED INT NOT NULL auto_increment,
  `SIRET` varchar(50) NOT NULL,
  `nom` varchar(50) NOT NULL,
  `description` varchar(500) NOT NULL,
  `enable` ENUM('YES', 'NO') DEFAULT 'YES',
  `level` int(1) NOT NULL,
  PRIMARY KEY  (id),
  UNIQUE KEY SIRET (SIRET)
) ENGINE=INNODB  DEFAULT CHARSET=utf8 AUTO_INCREMENT=1 ;

// contactType can be one of the 3 values : email, phone, fax
CREATE TABLE IF NOT EXISTS `contactType` (
  id UNSIGNED INT NOT NULL auto_increment,
  `contactType` ENUM('email', 'phonenumber', 'faxnumber')
  `mobile` ENUM('YES', 'NO') default 'NO',
  PRIMARY KEY  (id),
  UNIQUE KEY type (contactType)
) ENGINE=INNODB  DEFAULT CHARSET=utf8 AUTO_INCREMENT=1 ;

CREATE TABLE IF NOT EXISTS `contacts` (
  id UNSIGNED INT NOT NULL auto_increment,
  `SIRET` varchar(50) NOT NULL,
  `contactType` varchar(50) NOT NULL, // A reference to contactType just above
  `contactref` varchar(50) NOT NULL, // Phone number, fax number or email adress
  PRIMARY KEY  (id),
  UNIQUE KEY type (SIRET)
) ENGINE=INNODB  DEFAULT CHARSET=utf8 AUTO_INCREMENT=1 ;

CREATE TABLE IF NOT EXISTS `customers` (
  id UNSIGNED INT NOT NULL auto_increment,
  `SIRET` varchar(50) NOT NULL,
  PRIMARY KEY  (id),
  UNIQUE KEY type (SIRET)
) ENGINE=INNODB  DEFAULT CHARSET=utf8 AUTO_INCREMENT=1 ;

CREATE TABLE IF NOT EXISTS `supplier` (
  id UNSIGNED INT NOT NULL auto_increment,
  `SIRET` varchar(50) NOT NULL,
  PRIMARY KEY  (id),
  UNIQUE KEY type (SIRET)
) ENGINE=INNODB  DEFAULT CHARSET=utf8 AUTO_INCREMENT=1 ;

CREATE TABLE IF NOT EXISTS `industry` (
  id UNSIGNED INT NOT NULL auto_increment,
  `industry` varchar(250) NOT NULL,
  PRIMARY KEY  (id),
  UNIQUE KEY type (activite)
) ENGINE=INNODB  DEFAULT CHARSET=utf8 AUTO_INCREMENT=1 ;

CREATE TABLE IF NOT EXISTS `entreprisePerIndustry` (
  id UNSIGNED INT NOT NULL auto_increment,
  `industry_id` varchar(250) NOT NULL, // Chemical, Computer, Consulting, ...
  FOREIGN KEY (industry_id) REFERENCES industry(id) ON DELETE CASCADE,
  `SIRET` varchar(50) NOT NULL,
  PRIMARY KEY  (id),
  UNIQUE KEY type (industry_id)
) ENGINE=INNODB  DEFAULT CHARSET=utf8 AUTO_INCREMENT=1 ;
4

1 回答 1

1

以下是一些可以帮助您创建更好的数据库的TIPS

  • 如果您想让外键约束起作用MyISAM,请将表引擎从更改为。InnoDB
  • AUTO INCREMENT DataType 应该是UNSIGNED INT。这将使范围加倍。

    `id` INT UNSIGNED NOT NULL AUTO_INCREMENT`
    
  • 如果从允许值列表中选择列值,则将 dataType 更改为 ENUM。在您的情况下enablelevel可以将其转换为ENUM dataType

    `enable` ENUM( 'y', 'n' ) NOT NULL DEFAULT 'y' COMMENT 'y:yes; n:no' `
    
  • 添加外键关系到

    contacts. contactTypecontactType.id

    entreprisePerIndustry. industryindustry.id

更新

我创建了基本和优化的表结构(AFAIK)。

--
-- Table structure for table `tbl_company`
--
CREATE TABLE IF NOT EXISTS `tbl_company` (
  `id` int(10) unsigned NOT NULL AUTO_INCREMENT,
  `siret` varchar(50) NOT NULL,
  `nom` int(11) NOT NULL,
  `description` varchar(250) NOT NULL,
  `enable` enum('y','n') NOT NULL DEFAULT 'y' COMMENT 'y:yes; n:no',
  `level` enum('1','2') NOT NULL DEFAULT '1',
  `last_updated_on` timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP,
  PRIMARY KEY (`id`)
) ENGINE=InnoDB DEFAULT CHARSET=latin1 AUTO_INCREMENT=1 ;

--
-- Table structure for table `tbl_contact`
--

CREATE TABLE IF NOT EXISTS `tbl_contact` (
  `id` int(10) unsigned NOT NULL AUTO_INCREMENT,
  `siret` varchar(50) NOT NULL,
  `contact_type` enum('email','phone','fax') NOT NULL DEFAULT 'email',
  `contact_ref` varchar(100) NOT NULL COMMENT 'Phone number, fax number or email adress',
  `last_updated_on` timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP,
  PRIMARY KEY (`id`),
  UNIQUE KEY `siret` (`siret`)
) ENGINE=InnoDB DEFAULT CHARSET=latin1 AUTO_INCREMENT=1 ;

...
...

complate structure is here

于 2012-08-27T08:50:56.167 回答