####1.Why do they call it a relational database?
a relationship can be established between the table and another table by creating foreign key, and can assemble related information very quickly.
####2.What is SQL?
SQL stands for Structured Query Language.
It's a standard language for accessing to databases.
####3.There are two predominant views into a relational database. What are they, and how are they different?
- The data view shows the columns and rows in the tables.
- The scheme view shows the structure of the table, like name, type, key, etc...
####4.In a table, what do we call the column that serves as the main identifier for a row of data? We're looking for the general database term, not the column name.
Primary key
####5.What is a foreign key, and how is it used?
It's a primary key of another tables. It is used for establish a relation between table to table.
####6.At a high level, describe the ActiveRecord pattern. This has nothing to do with Rails, but the actual pattern that ActiveRecord uses to perform its ORM duties.
An ActiveRecord object shows a row of the tables.
####7.If there's an ActiveRecord model called "CrazyMonkey", what should the table name be?
crazy_monkeys
####8.If I'm building a 1:M association between Project and Issue, what will the model associations and foreign key be?
Project has many issues, and Issues belongs to project.
project_id would be foreign key.
####9.Given this code
class Zoo < ActiveRecord::Base has_many :animals end
####What do you expect the other model to be and what does database schema look like?
class Animal < ActiveRecord::Base belongs_to :zoo end
create_table animals do |t| t.string "name" t.integer "zoo_id" t.timestanps end
####What are the methods that are now available to a zoo to call related to animals?
zoo = Zoo.first
then,
zoo.animals
####How do I create an animal called "jumpster" in a zoo called "San Diego Zoo"?
san_diego_zoo = Zoo.create(name: "San Diego Zoo") jumpster = Animal.create(name: "jumpster") san_diego_zoo.animals << jumpster
####10.What is mass assignment? What's the non-mass assignment way of setting values?
-The mass assignment way
User.create(params[:user])
-The non-mass sssignment way
User.create(name: params[:user][:name])
####11.What does this code do? Animal.first
It will fetch the first row of the animals table.
####12.If I have a table called "animals" with columns called "name", and a model called Animal, how do I instantiate an animal object with name set to "Joe". Which methods makes sure it saves to the database?
joe = Animals.new(name: "Joe")
Animal.create method will save to database at the same time.
####13.How does a M:M association work at the database level?
It works by joining tables.
####14.What are the two ways to support a M:M association at the ActiveRecord model level? Pros and cons of each approach?
- has_many with through option
- has_and_belongs_to_many
####15.Suppose we have a User model and a Group model, and we have a M:M association all set up. How do we associate the two?
class User < ActiveRecord::Base has_many :groups, through: assignments has_many :assignments end
class Group < ActiveRecord::Base has_many :users, through: assignments has_many :assignments end
class Assignment < ActiveRecord::Base belongs_to :user belongs_to :group end