作成中の Web サイト用に作成した、従業員のリストを含むデータベースがあります。各従業員の写真、名前、電話番号などを含むグリッドのようなリストになります。
これは私の最初のデータベース設計であり、私はそれを本当に誇りに思っています...しかし、私は初心者なので、いくつかの点で非効率的であると確信しています.
ウェブサイトが要素を作成するためにクエリを実行する最良の方法を見つけるのに苦労しています。
1) 仮想テーブルを使用する必要がありますか? それとも、1 つのクエリを使用して、従業員ごとにループする必要がありますか?
2) 一部の従業員は、複数の管轄権を持ちます。今私は持っています:
select employees.firstName, employees.lastName, employees.phone, employees.position,
employees.picture, jurisdictions.jurisdiction
from employees
inner join(jurisdictions cross join departments cross join user_jurisdictions)
on (employees.deptID = departments.deptID
AND user_jurisdictions.userID = employees.userID
AND user_jurisdictions.jurID = jurisdictions.jurID)
どちらが返されますか:
+-----------+----------+--------------+-----------------+---------+--------------+
| firstName | lastName | phone | position | picture | jurisdiction |
+-----------+----------+--------------+-----------------+---------+--------------+
| John | Smith | 210-226-3232 | Senior Manager |/pics/jpeg1|South America|
| John | Smith | 210-226-3232 | Senior Manager |/pics/jpeg1|London |
+-----------+----------+--------------+-----------------+---------+--------------+
従業員が追加の管轄権を持つため、重複行を削除するクエリを作成する方法はありますか? 何かのようなもの?
+-----------+----------+--------------+---------+---------+--------------+------------+
| firstName | lastName | phone | position| picture | jurisdiction |Jurisdiction|
+-----------+----------+--------------+---------+---------+--------------+------------+
| John | Smith | 210-226-3232 | Manager |/pics/jpeg1| South America|London |
+-----------+----------+--------------+-----------------+---------+-------------------+
これが私のmysqlスキーマです:
employees
--------------------
userID (primary key)
firstName
lastName
phone
fax
position
picture
deptID (foreign key references departments(deptID))
departments
--------------------
deptID (primary key)
department
jurisdictions
-----------------
jurID (primary key)
jurisdiction
user_jurisdictions
--------------------------
userID(foreign key references employees(userID))
jurID(foreign key references jurisdictions(jurID))
triger:
after_employees_insert | INSERT | employees | begin
insert into user_jurisdictions(userID) values (new.userID);