ç¹ã« SQL ãªã©ã§ãå©ç¨ããã¦ãã¼ã¯ãªæ¡çªï¼åãã¼ã¿ã«çªå·ãå²ãå½ã¦ãããã»ã¹ï¼ã§ããããã ID ãå©ç¨ãããã¨ãããã¾ãã
ä¸è¬çã«ã¯ï¼åç´ãª DB ã§ã¯ï¼primary key ã«æ´æ°å㧠auto increment ã®è¨å®ãé¸æããã¨ãDB å´ã§èªåçã«æ¡çªãã¦ãããã®ã§ãã¢ããªã±ã¼ã·ã§ã³ã¯ãã¾ãæ°ã«ãããã¨ãããã¾ããã
ãã ãããã®æ¹æ³ã ã¨æ¿å ¥ã¨æ´æ°ã®æä½ã¯åªç (idempotent) ã§ã¯ãªããªãã±ã¼ã¹ãçºçãããã¨ãããã¾ãï¼ãã¹ã¦ã®ã±ã¼ã¹ã§èµ·ããããã§ã¯ãªãï¼ã
åªçï¼ã¹ãã¨ãï¼ï¼ããæä½ãï¼åè¡ã£ã¦ãè¤æ°åè¡ã£ã¦ãçµæãåãã§ãããã¨ãããæ¦å¿µ
åªçã®ä¾
å ·ä½çãªä¾ãã¨ãã¦ãDB ã«æ¡çªãä»»ããå ´åãæãããã¨ã«ãã¾ããé¢é£ããï¼ï¼ï¼ã¤ã®ãã¼ãã«ã«ãåæã«ãã¼ã¿ãå ¥ãããã¨ãã¾ãã
ãã人ç©ã®æ å ±ãè¨é²ãã DB ãããã¨ãã¾ããUser ãã¼ãã«ã«ï¼ä¸»ããï¼ãã¼ã¿ãæ¿å ¥ãã¦ãAddress ãã¼ãã«ã«ããã¼ã¿ãæ¿å ¥ããå ´åãUser ãã¼ãã«ã«ç»é²ããã¨ãã®ãã¼ã¿ã«ãã primary key ã Address ãã¼ãã«ã®ç»é²ã«ãå¿ è¦ã§ã¯ãªãã§ãããããï¼ã¾ãã¯ãåç §ãããï¼
CREATE TABLE Users ( UserID INT AUTO_INCREMENT PRIMARY KEY, UserName VARCHAR(100) NOT NULL ); CREATE TABLE Addresses ( AddressID INT AUTO_INCREMENT PRIMARY KEY, UserID INT NOT NULL, Address VARCHAR(255) NOT NULL, FOREIGN KEY (UserID) REFERENCES Users(UserID) ); -- Step 1: Users ãã¼ãã«ã«æ°ããã¦ã¼ã¶ã¼ãæ¿å ¥ INSERT INTO Users (UserName) VALUES ('John Doe'); -- Step 2: æ¡çªããã UserID ãåå¾ SET @NewUserID = LAST_INSERT_ID(); -- Step 3: Addresses ãã¼ãã«ã«é¢é£ããã¢ãã¬ã¹ãæ¿å ¥ INSERT INTO Addresses (UserID, Address) VALUES (@NewUserID, '123 Main St');
Step 2 ã®ããã«æ¡çªãããçµæãå¾ ã£ã¦ ID ãåãåããæ®ãã®ãã¼ãã«ã«å²ãæ¯ããã ID ãå©ç¨ãã¦æ¿å ¥ã®æ示ãããªãã¦ã¯ããã¾ããããã®ãããåªçã§ã¯ãªããªãã±ã¼ã¹ãããã¾ãã
ãã®ã±ã¼ã¹ã§ã¯ãï¼ã¤ç®ã®ãã¼ãã«ã§ ID ãæ¡çªãããã¼ã¿ãæ¿å
¥ããï¼ã¤ç®ã®ãã¼ãã« Addresses
ã«ãã¼ã¿ãæ¿å
¥ããã¿ã¤ãã³ã°ã§ããªãããã®ã¨ã©ã¼ãçºçããã¨ãã¾ãã
ï¼ã¤ç®ã®æ¡çªããããã¼ã¿ã¯ãã§ã«ç»é²ãã¦ããã¯ããªã®ã§ããã¼ã¿ã®çç¾ãçãã¦ãã¾ãã¾ãããã®ã¨ã©ã¼ã®å¯¾å¦æ¹æ³ã¨ãã¦ã¯ããã©ã³ã¶ã¯ã·ã§ã³å¦çã¨ãã¼ã«ããã¯ãå¿
è¦ã«ãªã£ãã INSERT ON DUPLICATE KEY UPDATE
ã®æ¸ãæ¹ãããããã¾ãããDB ã«ãã£ã¦æ¸ãæ¹ãéã£ãããåªçãªå¦çã®æ§æã«é対å¿ã ã£ãããã¢ããªã±ã¼ã·ã§ã³ã§æ¡çªããªããã¨ã§ãããã£ã¦è¤éã«ãªã£ã¦ãã¾ããã¨ãããã¾ãã
ä¸äººã§ä½ã£ã¦ããã¨çç¾ã¯ä½ãã¥ãããããããªããã©ãDB ã¨ã¢ããªã±ã¼ã·ã§ã³å´ã§æ§ç¯ãã人ãéã£ããããã¨ãããåãããããã©ããããã¨ã«ãªãï¼ãã¨ãããï¼
çµå±ãã¢ããªã±ã¼ã·ã§ã³å´ã§ primary key ãçæãã¦ãããã»ããåç´ã ã£ããããã£ã¦ããã±ã¼ã¹ã¯ããã¾ãã
ãã ããã±ã¼ã¹ãã¤ã±ã¼ã¹ã®è©±ã§ãã£ã¦ãUUID ã ã¨ããã©ã¼ãã³ã¹ãä½ä¸ããæããããã¾ããDB ã UUID ãæååã®ããã«æ±ããã¨ã«ãªãã¨
INTEGER
åã¨æ¯è¼ãã¦è¨ç®ã³ã¹ããé«ããªããã¨ãããã¾ããSQLite ã§ã¯INTEGER PRIMARY KEY
ã ã¨å é¨çã«ãROWIDãã«ãªã£ã¦ãã¾ããï¼åèãROWIDs and the INTEGER PRIMARY KEYããªã®ã§ãUUID ãå©ç¨ããã¨è¨ç®ã³ã¹ãã¯é«ããªãå¯è½æ§ãããï¼
UUIDã㨠GUID
UUID 㯠universally unique identifier ã®ãã¨ããªã®ã§ãç¨èªã®ç¤ºãæå³çã«ã ID ã¨åãããã«ãµãã¾ãããã¨ããããã¾ããåºæ¬ã¯ã¦ãã¼ã¯ãªèå¥åã§ãã
GUID ã¨ã¯ Globally Unique IDentifier ã®ãã¨ãMicrosoft Learn ã§ãNewGuidãã®èª¬æãèªãã§ã¿ã¾ãã
The method creates a Version 4 Universally Unique Identifier (UUID) as described in RFC 4122, Sec. 4.4.
ãªã®ã§ååãã UUID 㨠GUID ã¯ãããéãã¢ãã®ããã«è¦ã¾ãããå®éã¯ï¼ç¾å¨ã¯ï¼UUID ã代æ¿ããååã¨ã㦠GUID ã使ããã¦ãã¾ããï¼DB ãªã©ã§ã GUID ã¨ããç¨èªãå©ç¨ããããã¨ãããã¾ãï¼
C# ã® GUID ã®çæ
Guid uuid = Guid.NewGuid();
Console.WriteLine(uuid.ToString());
c33db100-0779-4192-91af-043516a343d1
NanoID
UUID ã§åºæ¬çã«ã¯ãããã ãã© ID ã«ã¤ãã¦èª¿ã¹ã¦ãã㨠nanoid ã¯ããã¨ç¥ã£ã¦ãã¦ãããã¨æãã¾ããã
public void Run(string[] args) { var defaultID = Nanoid.Generate(); var shortID = Nanoid.Generate(size:10); Console.WriteLine($"ID: {defaultID}, shortID: {shortID}"); var id1 = Nanoid.Generate("23456789" + "CFGHJMPQRVWX", size: 8); var id2 = Nanoid.Generate("23456789" + "CFGHJMPQRVWX", size: 8); var id3 = Nanoid.Generate("23456789" + "CFGHJMPQRVWX", size: 8); var id4 = Nanoid.Generate("23456789" + "CFGHJMPQRVWX", size: 8); var id5 = Nanoid.Generate("23456789" + "CFGHJMPQRVWX", size: 8); Console.WriteLine($"ID: {id1}"); Console.WriteLine($"ID: {id2}"); Console.WriteLine($"ID: {id3}"); Console.WriteLine($"ID: {id4}"); Console.WriteLine($"ID: {id5}"); }
nanoid ã®ããã¨ããã¯ãæååã®é·ããæå®ããããID ã«ä½¿ç¨ã§ããæåãæå®ãããã§ããã¨ããã§ãããªã®ã§ãå®éã®ã¦ãã¼ã¯ãª ID ã®ã»ãã«ã表示ç¨ã®ç°¡æçãªã¦ãã¼ã¯ ID ãç¨æããã¨ããªããã¯ä¾¿å©ã ã¨æãã¾ãã
表示ç¨ã® ID ãç¨æããçç±ã¨ãã¦ã¯ã人ã®èªã¿ã¥ããæåãåé¿ãã ID ãç¨æãããã¨ãã¾ãããããæåããã¨ãã° I 1 l
ã¿ãããªéãã¯äººã®ç®ã«ã¯å¾®å¦ã£ã¦ãã¨ã
ä¸ä¾ã¨ãã¦ãOPEN LOCATION CODE ãªã©ãããã¾ãã
TODO
ä»å¹´ã® .NET 9 㧠UUID 7 ããµãã¼ãäºå®ã«ãªã£ã¦ããããã§ãã
ããã¯ãæ°ã«ãªãã®ã§å çãããã