To make first,middle,last name searchable, should I make them Primary
Keys?
No, any field in any table is "searchable".
A primary key is a unique identifier of a row in a table. Your idPeople
field satisfies this requirement, though since you stated the table will have millions of rows, you'll want to make that an INT
or BIGINT
. The maximum value of SMALLINT
is 65535. See http://dev.mysql.com/doc/refman/5.0/en/integer-types.html
How would I allow to search through ItemCode.codeSection and return
all "People" that match?
One way would be:
SELECT *
FROM PEOPLE
WHERE idPeople in
(
SELECT idPeople FROM Item WHERE idItem in
(
SELECT idItemCode from ItemCode WHERE codeSection = 'Some Section'
)
)
Another way would be to use JOIN
.
I hope I understand the foreign key concept. It's on the "Item" table
so it allows multiple items per "People". Is this the correct way?
Looks correct to me. The notation is called crows foot
notation, and on the Item
side (1 line and a "crows foot", it's saying 1 to many items. On the People
side (parallel lines), it's saying 1 and only 1 person. Using this notation, some of your other relationships appear incorrect. (People to County, in particular)
There will be separate lists for different counties, should I create a
View for this?
You could, but they could also just be separate SELECT statements or Stored Procedures.
I guess you don't want (for every Tracks.prefix
) a new row to be inserted but the existing row to be updated (otherwise you would get collisions on the primary key of Tracks
). You can this query to check first what will be changed:
SELECT
tr.prefix,
tr.label_id AS existing_label_id,
top.label,
lbl.label_id AS new_label_id
FROM
Tracks AS tr
JOIN
Top100 AS top ON top.prefix = tr.prefix
JOIN
Label AS lbl ON lbl.label_name = top.label ;
and then update:
UPDATE
Tracks AS tr
JOIN
Top100 AS top ON top.prefix = tr.prefix
JOIN
Label AS lbl ON lbl.label_name = top.label
SET
tr.label_id = lbl.label_id ;
Best Answer
1)
2)