数据库结构:好还是不好?

时间:2012-08-27 08:43:58

标签: mysql sql

我正在为新公司开发应用程序管理业务(销售,供应商,客户,产品......)。首先,我需要创建一个数据库。你能否告诉我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 ;

1 个答案:

答案 0 :(得分:1)

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

  • 如果要使外键约束起作用,请将表引擎从MyISAM更改为InnoDB
  • 自动增量 DataType应为 UNSIGNED INT 。这将使范围加倍。

    `id` INT UNSIGNED NOT NULL AUTO_INCREMENT`
    
  • 如果从允许值列表中选择的列值,则将dataType更改为ENUM。在您的案例enable中,level可以转换为 ENUM < / strong> dataType

    `enable` ENUM( 'y', 'n' ) NOT NULL DEFAULT 'y' COMMENT 'y:yes; n:no' `
    
  • 添加foreign key relation to

    contactscontactTypecontactTypeid

    entreprisePerIndustryindustryindustryid

更新

我创建了基本和优化的表结构(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