Mysql表结构

时间:2015-10-07 10:06:57

标签: mysql ruby json

我想发送类似于此的JSON响应:

{

{"key":"Value"},
{"key":"Value"}

"title":['john','doe'],
"abcd":[
{"key":"Value"},{"key":"Value"},{"key":"Value"}]

}

我怎样才能拥有我的mysql表结构?

1 个答案:

答案 0 :(得分:0)

您最好将密钥值存储系统用于此类目的,但存在大量耗时的解决方案。

我使用DataMapper创建了下面的数据库表。它与实现一样快,如果您决定使用此方案,则应考虑添加一些约束。我试图让事情变得简单,这种灵感来自于redis

我有6张桌子。其中2个存储相关表的主键。

       Table: roots
Create Table: CREATE TABLE `roots` (
  `id` int(10) unsigned NOT NULL AUTO_INCREMENT,
  `name` varchar(50) DEFAULT NULL,
  PRIMARY KEY (`id`)
) ENGINE=InnoDB AUTO_INCREMENT=6 DEFAULT CHARSET=utf8
1 row in set (0.00 sec)

       Table: titles
Create Table: CREATE TABLE `titles` (
  `id` int(10) unsigned NOT NULL AUTO_INCREMENT,
  `name` varchar(50) DEFAULT NULL,
  PRIMARY KEY (`id`)
) ENGINE=InnoDB AUTO_INCREMENT=8 DEFAULT CHARSET=utf8
1 row in set (0.00 sec)

       Table: features
Create Table: CREATE TABLE `features` (
  `id` int(10) unsigned NOT NULL AUTO_INCREMENT,
  `name` varchar(50) DEFAULT NULL,
  PRIMARY KEY (`id`),
  UNIQUE KEY `unique_features_name` (`name`)
) ENGINE=InnoDB AUTO_INCREMENT=8 DEFAULT CHARSET=utf8
1 row in set (0.00 sec)

       Table: rootfeatures
Create Table: CREATE TABLE `rootfeatures` (
  `root_id` int(10) unsigned NOT NULL,
  `feature_id` int(10) unsigned NOT NULL,
  PRIMARY KEY (`root_id`,`feature_id`),
  KEY `index_rootfeatures_root` (`root_id`),
  KEY `index_rootfeatures_feature` (`feature_id`),
  CONSTRAINT `rootfeatures_feature_fk` FOREIGN KEY (`feature_id`) REFERENCES `features` (`id`) ON DELETE NO ACTION ON UPDATE NO ACTION,
  CONSTRAINT `rootfeatures_root_fk` FOREIGN KEY (`root_id`) REFERENCES `roots` (`id`) ON DELETE NO ACTION ON UPDATE NO ACTION
) ENGINE=InnoDB DEFAULT CHARSET=utf8
1 row in set (0.00 sec)

       Table: roottitles
Create Table: CREATE TABLE `roottitles` (
  `root_id` int(10) unsigned NOT NULL,
  `title_id` int(10) unsigned NOT NULL,
  PRIMARY KEY (`root_id`,`title_id`),
  KEY `index_roottitles_root` (`root_id`),
  KEY `index_roottitles_title` (`title_id`),
  CONSTRAINT `roottitles_title_fk` FOREIGN KEY (`title_id`) REFERENCES `titles` (`id`) ON DELETE NO ACTION ON UPDATE NO ACTION,
  CONSTRAINT `roottitles_root_fk` FOREIGN KEY (`root_id`) REFERENCES `roots` (`id`) ON DELETE NO ACTION ON UPDATE NO ACTION
) ENGINE=InnoDB DEFAULT CHARSET=utf8
1 row in set (0.00 sec)

       Table: details
Create Table: CREATE TABLE `details` (
  `id` int(10) unsigned NOT NULL AUTO_INCREMENT,
  `title` varchar(50) DEFAULT NULL,
  `feature_id` int(10) unsigned NOT NULL,
  PRIMARY KEY (`id`),
  KEY `index_details_feature` (`feature_id`)
) ENGINE=InnoDB AUTO_INCREMENT=6 DEFAULT CHARSET=utf8
1 row in set (0.00 sec)

<强> models.rb

class Root
  include DataMapper::Resource

  property :id, Serial
  property :name, String

  has n, :roottitles
  has n, :titles, :through => :roottitles

  has n, :rootfeatures
  has n, :features, :through => :rootfeatures

  def list_of_titles
    arr = []
    self.titles.each {|t| arr<< t.name}
    arr
  end

  def list_of_features
    arr = []
    self.features.each {|t| arr<< t.name }
    arr
  end

  def list_of_features_with_details
    arr = []
    self.features.each {|t| arr<< {t.name => t.detail.title} unless t.detail.nil?}
    arr
  end

  def full_description
    h = {'titles' => self.list_of_titles, 'features' => self.list_of_features_with_details}
    h
  end

  def full_json
    self.full_description.to_json
  end

end

class Title
  include DataMapper::Resource

  property :id,           Serial
  property :name,       String 

  has n, :roottitles
  has n, :roots, :through => :roottitles
end

class Feature
  include DataMapper::Resource

  property :id,         Serial
  property :name,       String, :unique => true

  has n, :rootfeatures
  has n, :roots, :through => :rootfeatures

  has 1, :detail

end

class Roottitle
  include DataMapper::Resource

  belongs_to :root, :key => true
  belongs_to :title, :key => true
end

class Rootfeature
  include DataMapper::Resource

  belongs_to :root, :key => true
  belongs_to :feature, :key => true
end

class Detail
  include DataMapper::Resource

  property :id, Serial
  property :title, String

  belongs_to :feature

end

功能数据

2.2.0 :006 > Feature.each {|t| print "#{t.id} #{t.name} \n"}
1 Shoots from distance 
2 Cuts inside 
3 Runs with ball often 
4 Runs with ball less 
5 Tries long range freekicks 
6 Argues with referee 
7 Tries killer balls

详细数据

2.2.0 :010 > Detail.each {|t| print "#{t.id}  #{t.feature_id}  #{t.title} \n"}
1  1  When no teammates around him 
2  2  When a wrong footed player on him 
3  7  When opponent team's defence is not blah blah 
4  3  He thinks he is portuguese num. 7 winger 
5  5  He is Juninho Some to titles.

标题中的一些数据

2.2.0 :005 > Title.each {|t| print "#{t.id} #{t.name} \n"}
1 Lord 
2 Marquisse 
3 Duke 
4 Duchess 
5 Baron 
6 Baronesse 
7 Chevalier 

结果。

2.2.0 :021 > john = Root.get(1) # get root instance with id 1
 => #<Root @id=1 @name="John"> 
2.2.0 :022 > john.list_of_titles # returns array
 => ["Marquisse", "Duke", "Duchess"] 
2.2.0 :023 > john.list_of_features #returns array
 => ["Shoots from distance", "Cuts inside", "Runs with ball often", "Tries long range freekicks", "Tries killer balls"] 
2.2.0 :024 > john.list_of_features_with_details # array of hashes
 => [{"Shoots from distance"=>"When no teammates around him"}, {"Cuts inside"=>"When a wrong footed player on him"}, {"Runs with ball often"=>"He thinks he is portuguese num. 7 winger"}, {"Tries long range freekicks"=>"He is Juninho"}, {"Tries killer balls"=>"When opponent team's defence is not blah blah"}] 
2.2.0 :025 > john.full_description # returns hash
 => {"titles"=>["Marquisse", "Duke", "Duchess"], "features"=>[{"Shoots from distance"=>"When no teammates around him"}, {"Cuts inside"=>"When a wrong footed player on him"}, {"Runs with ball often"=>"He thinks he is portuguese num. 7 winger"}, {"Tries long range freekicks"=>"He is Juninho"}, {"Tries killer balls"=>"When opponent team's defence is not blah blah"}]} 
2.2.0 :027 > print john.full_json # returns json (just calling .to_json on above method)
{"titles":["Marquisse","Duke","Duchess"],"features":[{"Shoots from distance":"When no teammates around him"},{"Cuts inside":"When a wrong footed player on him"},{"Runs with ball often":"He thinks he is portuguese num. 7 winger"},{"Tries long range freekicks":"He is Juninho"},{"Tries killer balls":"When opponent team's defence is not blah blah"}]} 
相关问题