티스토리 뷰

objective: 일단 아는대로 DB를 만들어보자 - IDE가 있으니 도움을 최대한 받아서

(학교에서 배우고 진지하게 SQL을 써본적이 없는 새럼)

#[derive(Serialize, Deserialize)]
struct User {
    id: Uuid,
    email: String,
    name: String,
    preferred_username: String,
}
create table User
(
	id uuid not null primary key,
	email varchar(128) not null,
	name varchar(128) not null,
	preferred_username varchar(128) not null
);

🔍 create table 문법?
✅ CREATE TABLE ( ... );

🔍 적당한 데이터 타입?
✅ varchar(숫자) -> 제한 있음, text: 제한 없음, pgsql 은 char와 varchar의 속도차이 없음 (이례적)
비압축: 126바이트까지 (저장은 1바이트 + 문자열(~126))
압축: 4바이트 + 압축된 문자열

https://www.postgresql.org/docs/current/sql-createtable.html

https://www.postgresql.org/docs/current/datatype.html

https://www.postgresql.org/docs/current/datatype-character.html

🔍 email maxlen?
✅ 254

https://stackoverflow.com/a/574698 - maxlen

create table users
(
	id uuid not null
		constraint users_pk
			primary key,
    -- 다른 방식: id uuid not null primary key, -> 디폴트 들어가는듯
	email varchar(254) not null,
	name varchar(126) not null,
	preferred_username varchar(126) not null
);

🔍 powershell join two command
✅ ;
sqlx migrate revert; sqlx migrate run

링크 기억 안 남 (이런 사소한걸;)


🔍 테이블 이름 대소문자?
✅ MySQL: 윈도우즈는 안 따지고 리눅스는 따짐
✅ pgsql: 쌍따옴표 안 쓴 건 lowercase 화
✅ SQL identifier는 ignore case (키워드들) (SQL-92) -> 나 소문자 쓸래

https://stackoverflow.com/a/20880247 - mysql
https://stackoverflow.com/a/20880247 - pgsql
https://stackoverflow.com/a/2029494 - sql 표준 identifier

🔍 camelCase? snake_case?
✅ 일관성 (개취인듯?)

https://stackoverflow.com/a/1881143

🔍 테이블 이름 예약어?
✅ user는 안 돼 (쌍따옴표 쓰면 가능)

https://stackoverflow.com/a/10891404 - 테이블 이름 예약어
https://www.postgresql.org/docs/current/sql-keywords-appendix.html - 예약어

🔍 테이블 이름 복수형? 단수형?
✅ 단수형 (학계 의견 & 실용적)

https://stackoverflow.com/a/5841297

create table account
(
	id uuid not null
		constraint users_pk primary key,
	email varchar(254) not null,
	name varchar(126) not null,
	preferred_username varchar(126) not null
);

🔍 Intellij 에서 constraint 에 이름 넣던데 이름 왜 넣음?
✅ (1) 에러메시지 (2) 수정 (3) 1, 2에 의한 협업 효율 향상

email varchar(254) not null unique 돌려봤는데 account_email_key 라는 키가 생김
unique 이면 어디서 key 도 같이 생긴댔는데

https://stackoverflow.com/a/1397493

create table account
(
    id uuid not null
        constraint account_pk primary key,
    email varchar(254) not null
        constraint account_email_key unique,
    preferred_username varchar(126) not null
        constraint account_preferred_username_key unique
);

🔍 딴데선 어케함?
✅ mastodon: accounts https://github.com/mastodon/mastodon/blob/main/db/schema.rb#L152
  id에 uuid 가 아니라 bigint를 씀
✅ pleroma: users https://git.pleroma.social/pleroma/pleroma/-/blob/6b3842cf50c063a63980c8d4dca93b25424059f2/lib/pleroma/user.ex#L86
➡️ phoenix ecto: https://hexdocs.pm/phoenix/ecto.html
❓ missk... 아몰랑

DB 스키마 보자고 로컬에 설치하는 무모한 짓은 하고싶지 않음

최근에 올라온 글
최근에 달린 댓글
Total
Today
Yesterday
링크
«   2024/04   »
1 2 3 4 5 6
7 8 9 10 11 12 13
14 15 16 17 18 19 20
21 22 23 24 25 26 27
28 29 30
글 보관함