您的位置:首页 > 大数据 > 人工智能

Rails Cookbook翻译(一)

2007-04-02 14:55 405 查看
Rails Cookbook翻译(一)

处方3.1:设置关系数据库以供Rails使用:
问题:
你已经安装了MySQL或者PostgeSQL,你现在想创建一个关系数据库来存储本书的章节(chapter)、每一章中的各个处方(recipes)以及可以通过处方找到相关话题的标签,这个数据库可以为你的基于Rails的web应用程序的,这个数据库包含了一对多和多对多的关系:
每一个章节包含许多处方,每一个处方只能属于一个章节,每一个处方有几个标签,每一个标签也可能被多个处方拥有。
解决方案:
首先,由于Rails定义了至少三个不同的运行环境(development,test,and production),你应各为他们分别创建一个数据库。
如果你使用MySQL,来创建这三个数据库。我们分别以cookbook_dev,cookbook_test,和cookbook_prod命名,我们首先以root身份登陆到MySQL:
$ mysql –u root

如果你没有root权限访问MySQL,你可以让系统管理员为你创建一个拥有创建数据库和用户权限的一个用户,在mysql控制台,输入:
mysql> create database cookbook_dev ;
mysql> create database cookbook_test ;
mysql> create database cookbook_prod;

现在,创建一个名叫 rails_user的用户,授他可以访问你刚才创建的每一个数据库中所有表的权限。(密码这里使用”r8!lz”,但是你应该仔细的选择你自己的密码,关于选择好的密码,你可以到http://world.std.com/~reinhold/diceware.html.看看)。

下面我们创建一个叫 create-mysql-db.sql的文件,它包括以下内容(注意:以下的各个表的创建语法需要MySQL4.1或更高的版本):
drop table if exists ‘chapters’;
create table chapters(
id int not null auto_increment,
title varchar(225) not null,
sort_order int not null default 0,
primary key (id)
)type = innodb;

drop table if exists ‘recipes’;
create table recipes(
id int not null auto_increment,
chapter_id int not null,
title varchar(225) not null,
problem text not null,
discussion text not null,
see_also text null,
sort_order int not null default 0,
  primary key(id,chapter_id,title),
  foreign key (chapter_id) references chapters(id)
)type=innodb;

drop table if exists ‘tags’;
create table tags(
id int not null auto_increment,
name varchar(80) not null,
primary key(id)
)type=innodb;

drop table if exits ‘recipes_tags’;

create table recipes_tags(
recipe_id int not null,
tag_id int not null,
primary key(recipe_id, tag_id),
foreign key(recipe_id) references recipes(id),
foreign key(tag_id) references tags(id)
)type=innodb;

现在我们使用create-mysql-db.sql里的创建表得语句来创建cookbook_dev数据库:
$ mysql cookbook_dev –u rails_user –p < create-mysql-db.sql;
$ mysql cookbook_test –u rails_user –p < create-mysql-db.sql;
$ mysql cookbook_prod –u rails_user –p < create-mysql-db.sql;

最后,我们用下面的命令来检查一下我们的数据库是否创建成功,你将会看到使用create-mysql-db.sql创建的表:
$ mysql cookbook_dev -u rails_user -p <<< "show tables;"
Enter password:
Tables_in_cookbook_dev
chapters
recipes
recipes_tags
tags

如果你是使用PostgreSQL,在这里我们演示一下怎么完成以上相同的任务。开始创建一个用户,接着使用这个用户创建每一个数据库,使用psql工具登录到PostgreSQL。你登录的用户必须拥有创建数据库的权限。
$ psql –U rob –w template1

Template1是PostgreSQL默认的数据库,我们在这里使用它作为创建新的数据库的环境。如果你没有这些权限,那你就让系统管理员授予你这些权限。在psql回显符后输入创建一个用户:

template1=# create user rails_user encrypted password 'r8!lz';
CREATE ROLE

接着创见每一个表,并制定他的拥有者:
template1=# create database cookbook_dev owner rails_user;
CREATE DATABASE
template1=# create database cookbook_test owner rails_user;
CREATE DATABASE
template1=# create database cookbook_prod owner rails_user;
CREATE DATABASE

现在创建一个名叫create-postgresql-db.sql的文件,它包含:
create table chapters (
id serial unique primary key,
title varchar(255) not null,
sort_order int not null default 0
);

create table recipes (
id serial unique primary key,
chapter_id int not null,
title varchar(255) not null,
problem text not null,
solution text not null,
discussion text not null,
see_also text null,
sort_order int not null default 0,
foreign key (chapter_id) references chapters(id)
);

create table tags (
id serial unique primary key,
name varchar(80) not null
);

create table recipes_tags (
recipe_id serial unique references recipes(id),
tag_id serial unique references tags(id)
);
接着使用create-postgresql-db.sql创建每一个表:
$ psql -U rails_user -W cookbook_dev < create-pgsql-db.sql
$ psql -U rails_user -W cookbook_test < create-pgsql-db.sql
$ psql -U rails_user -W cookbook_prod < create-pgsql-db.sql

最后,验证一下是否成功:
$ psql -U rails_user -W cookbook_dev <<< "/dt"
Password for user rails_user:
List of relations
Schema | Name | Type | Owner
--------+--------------+-------+------------
public | chapters | table | rails_user
public | recipes | table | rails_user
public | recipes_tags | table | rails_user
public | tags | table | rails_user
(4 rows)
讨论:
在解决方案部分我们创建了一个cookbook数据库,接着我们使用数据库定义语言(DDL)创建了各个表格。DDL定义名叫chaptes,recipes,tags和recipes_tags的四个表。这些表格和字段的名字都和Active Record默认的约定一致。也就是,表的名字是复数的,每一个表格(除了recipes_tags)都有名字为id的主键(primary key),参照其他表的列(columns)都以一个被参照的表格名作为开始,然后接着_id来命名.除此之外,这些数据库都是满足第三范式(3NF)的,除非你有一个更好的理由。
表格chapters与recipes是一对多的关系:一个章节可以有多个处方,这是一个非对称的关系,这些处方不能属于多个章。这个数据关系是符合直觉和被我们所熟悉的:毕竟本书就是一个具体的代表。
我们也描述了在recipes和tags表之间的多对多的关系。在这种情况下,一个recipe可以和多个tags关联,对称的,一个tag可以和多个recipes关联。这个recipes_tags保持了这种关系,被称为连接表。recipes_tags使用多个字段
来组成一个主键,他们每一个同时也是外键。Active record期望连接表的名字是他所连接的两个表名按照字母顺序使用’_’连接起来.
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: