Last active
October 10, 2022 03:31
-
-
Save yancya/387b61924cbcd672176bfd0d58320625 to your computer and use it in GitHub Desktop.
カラム名にテーブル名を含めるべきか否か考える https://twitter.com/neko314_/status/1579000770938433536?s=61&t=TGIhzWS_A0b4j3dWhxK50A
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
WITH teams AS ( | |
SELECT 1 AS id | |
, 'yancya-club' AS name) | |
, members AS ( | |
SELECT 1 AS id | |
, 'yancya' AS name | |
, 1 AS team_id) | |
SELECT * | |
FROM members | |
JOIN teams ON teams.id = team_id | |
; | |
-- id | name | team_id | id | name | |
-- ----+--------+---------+----+------------- | |
-- 1 | yancya | 1 | 1 | yancya-club | |
-- 確かに `id` とか `name` が複数あってダルそう |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
WITH teams AS ( | |
SELECT 1 AS id | |
, 'yancya-club' AS name) | |
, members AS ( | |
SELECT 1 AS id | |
, 'yancya' AS name | |
, 1 AS team_id) | |
SELECT id, name | |
FROM members | |
JOIN teams ON teams.id = team_id | |
; | |
-- ERROR: column reference "id" is ambiguous | |
-- LINE 10: SELECT id, name | |
-- 辛い、でもまぁ、これは、それはそう、という気がする |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
WITH teams AS ( | |
SELECT 1 AS team_id | |
, 'yancya-club' AS team_name) | |
, members AS ( | |
SELECT 1 AS member_id | |
, 'yancya' AS member_name | |
, 1 AS member_team_id) -- ここがなぁ | |
SELECT member_id | |
, member_name | |
, member_team_id -- このなぁ | |
, team_id | |
, team_name | |
FROM members | |
JOIN teams ON teams.team_id = members.member_team_id | |
; | |
-- member_id | member_name | member_team_id | team_id | team_name | |
-- -----------+-------------+----------------+---------+------------- | |
-- 1 | yancya | 1 | 1 | yancya-club | |
-- SELECT の部分はスッキリしている気はしますが、外部キーの名前に違和感が出てきますね…… |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
WITH teams AS ( | |
SELECT 1 AS id | |
, 'yancya-club' AS name) | |
, members AS ( | |
SELECT 1 AS id | |
, 'yancya' AS name | |
, 1 AS team_id) | |
SELECT members.id AS member_id | |
, members.name AS member_name | |
, members.team_id AS member_team_id -- これは…… | |
, teams.id AS team_id | |
, teams.name AS team_name | |
FROM members | |
JOIN teams ON teams.id = team_id | |
; | |
-- member_id | member_name | members_team_id | team_id | team_name | |
-- -----------+-------------+-----------------+---------+------------- | |
-- 1 | yancya | 1 | 1 | yancya-club | |
-- 別名を付ければ済むわけですが、それが面倒というのも分かる | |
-- 別名も命名規約に従っていると微妙な別名を付けることになりかねない | |
-- JOIN のキーに使ったカラム名はテーブル名プレフィックスつけない方がいいのでは…… |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
WITH teams AS ( | |
SELECT 1 AS team_id | |
, 'yancya-club' AS name) | |
, members AS ( | |
SELECT 1 AS id | |
, 'yancya' AS name | |
, 1 AS team_id) | |
SELECT members.id AS member_id | |
, members.name AS member_name | |
, team_id | |
, teams.name AS team_name | |
FROM members | |
JOIN teams USING(team_id) | |
; | |
-- member_id | member_name | team_id | team_name | |
-- -----------+-------------+---------+------------- | |
-- 1 | yancya | 1 | yancya-club | |
-- USING を使うと勝手にまとまってくれて嬉しい上にテーブル名で修飾しなくて済むようになる | |
-- そのために、サロゲートキー(主キー)だけは外部キーと同じ名前(テーブル名つきの名前)にしたらどうか思う事はある |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
WITH team AS ( | |
SELECT 1 AS team_id | |
, 'yancya-club' AS name) | |
, member AS ( | |
SELECT 1 AS id | |
, 'yancya' AS name | |
, 1 AS team_id) | |
SELECT member.id AS member_id | |
, member.name AS member_name | |
, team_id | |
, team.name AS team_name | |
FROM member | |
JOIN team USING(team_id) | |
; | |
-- member_id | member_name | team_id | team_name | |
-- -----------+-------------+---------+------------- | |
-- 1 | yancya | 1 | yancya-club | |
-- オフトピですが、テーブル名は単数形の方が良いのではと思うことがあります |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment