Checkpoint 21.8.1.
Q-1: Deciding how to break up your application data into tables and creating the relationships between them is called __________.
Twitter
table. So we create a new table that keeps track of pairs of friends. The following is a simple way of making such a table:CREATE TABLE Pals (from_friend TEXT, to_friend TEXT)
drchuck
is following, we would insert a row of the form:INSERT INTO Pals (from_friend,to_friend) VALUES ('drchuck', 'lhawthorn')
drchuck
Twitter feed, we will insert 20 records with “drchuck” as the first parameter so we will end up duplicating the string many times in the database.People
instead of the Twitter
table used in the previous example. The People
table has an additional column to store the numeric key associated with the row for this Twitter user. SQLite has a feature that automatically adds the key value for any row we insert into a table using a special type of data column (INTEGER PRIMARY
KEY
).People
table with this additional id
column as follows:CREATE TABLE People
(id INTEGER PRIMARY KEY, name TEXT UNIQUE, retrieved INTEGER)
People
table. When we select INTEGER PRIMARY
KEY
as the type of our id
column, we are indicating that we would like SQLite to manage this column and assign a unique numeric key to each row we insert automatically. We also add the keyword UNIQUE
to indicate that we will not allow SQLite to insert two rows with the same value for name
.Pals
above, we create a table called Follows
with two integer columns from_id
and to_id
and a constraint on the table that the combination of from_id
and to_id
must be unique in this table (i.e., we cannot insert duplicate rows) in our database.CREATE TABLE Follows
(from_id INTEGER, to_id INTEGER, UNIQUE(from_id, to_id) )
UNIQUE
clauses to our tables, we are communicating a set of rules that we are asking the database to enforce when we attempt to insert records. We are creating these rules as a convenience in our programs, as we will see in a moment. The rules both keep us from making mistakes and make it simpler to write some of our code.Follows
table, we are modelling a “relationship” where one person “follows” someone else and representing it with a pair of numbers indicating that (a) the people are connected and (b) the direction of the relationship.http://en.wikipedia.org/wiki/Relational_model