Char vs Varchar
Char thiab Varchar feem ntau siv cov cim cov ntaub ntawv hauv cov txheej txheem database uas zoo sib xws txawm tias muaj qhov sib txawv ntawm lawv thaum nws los txog rau kev cia khoom. Hauv kev tsim database, muaj ntau hom ntaub ntawv siv. Tawm ntawm lawv, cov cim cov ntaub ntawv hom tau txais qhov chaw tseem ceeb dua li lawv tau siv los khaws ntau cov ntaub ntawv piv rau cov lej. Cov ntaub ntawv tus cwj pwm yog siv los khaws cov cim lossis cov ntaub ntawv alphanumeric hauv cov hlua. Hom database cim teeb yog txhais thaum tsim cov database. Ib zaug ntxiv, tawm ntawm cov cim cov ntaub ntawv hom, Char thiab Varchar yog cov uas nquag siv. Kab lus no piav qhia txog ob hom ntaub ntawv no, char thiab varchar, yog dab tsi thiab qhov txawv ntawm lawv.
Char yog dab tsi?
Lub ISO txhais ntawm char yog tus cwj pwm thiab cov ntaub ntawv char yog siv los khaws cov cim. Char (n) tuaj yeem khaws n qhov loj me ntawm cov cim. Qhov siab tshaj plaws ntawm cov cim uas char (n) tuaj yeem tuav tau yog 255 chars thiab txoj hlua ntev yuav tsum yog tus nqi ntawm 1 txog 8000. Char yog tsib caug feem pua sai dua varchar thiab, yog li ntawd, peb tuaj yeem tau txais kev ua tau zoo dua thaum peb ua haujlwm nrog. char. Char siv lub cim xeeb zoo li qub thaum khaws cov ntaub ntawv. Thaum peb xav khaws cov hlua nrog qhov paub ntev ntev, nws yog qhov zoo dua los siv cov char. Piv txwv li, thaum khaws 'Yog' thiab 'Tsis Yog' li 'Y' thiab 'N', peb tuaj yeem siv cov ntaub ntawv hom char. Thiab tseem thaum khaws ib tus neeg daim npav ID hauv tebchaws nrog kaum tus cim, peb tuaj yeem siv hom ntaub ntawv raws li char (10).
Varchar yog dab tsi?
Raws li lub npe qhia, varchar yog hu ua tus cim sib txawv. Varchar yog siv los khaws cov ntaub ntawv alphanumeric uas muaj qhov ntev sib txawv. Tus lej siab tshaj plaws uas cov ntaub ntawv no tuaj yeem tuav tau yog 4000 tus cim thiab qhov siab tshaj plaws cia yog 2 GB. Qhov loj me ntawm varchar yog qhov ntev ntawm cov ntaub ntawv ntxiv rau ob bytes. Varchar qeeb dua char thiab nws siv dynamic nco faib thaum khaws cov ntaub ntawv. Peb tuaj yeem siv varchar thaum khaws cov ntaub ntawv xws li cov npe, chaw nyob, cov lus piav qhia, thiab lwm yam. Tsis yog cov hlua nkaus xwb, tab sis kuj tsis yog cov hlua xws li hnub hom, "Lub Peb Hlis 12th 2015", "2015-03-12" kuj tuaj yeem khaws cia rau hauv tus varchar data type.
Qhov txawv ntawm Char thiab Varchar yog dab tsi?
• Txawm hais tias char thiab varchar yog cov cim cov ntaub ntawv, char yog cov ntaub ntawv ntev ntev thiab varchar yog cov ntaub ntawv loj sib txawv.
• Char tuaj yeem khaws qhov loj me uas tsis yog Unicode kab cim nkaus xwb, tab sis varchar tuaj yeem khaws ntau qhov sib txawv ntawm cov hlua.
• Char zoo dua varchar rau cov ntaub ntawv uas nquag hloov. Qhov no yog vim cov kab ntev ntev ntawm cov ntaub ntawv tsis ua rau fragmentation.
• Char yuav nyob tsuas yog qhov chaw ruaj khov uas tau txhais thaum tshaj tawm qhov sib txawv. Tab sis varchar yuav nyob qhov chaw raws li cov ntaub ntawv uas tau muab tso thiab nws tseem yuav nyob 1 lossis 2 bytes raws li qhov ntev ua ntej.
• Yog tias cov ntaub ntawv tsawg dua 255 chars, 1 byte raug faib thiab yog cov ntaub ntawv ntau dua 255 chars 2 bytes yuav tseg. Yog tias peb siv char los khaws tus chij ntawm 'Y' thiab 'N' nws yuav siv ib byte los khaws, tab sis thaum peb siv varchar nws yuav siv ob bytes khaws tus chij nrog rau ib tug ntxiv byte raws li qhov ntev prefix.
Summary:
Char vs Varchar
Char thiab varchar yog cov ntaub ntawv siv ntau tshaj plaws uas muaj nyob hauv cov ntaub ntawv. Char yog siv los khaws ib txoj hlua nrog ntev ntev thaum varchar yog siv los khaws cov hlua uas muaj qhov sib txawv ntev. Txhawm rau kom tau txais kev ua tau zoo dua los ntawm cov ntaub ntawv, nws yog qhov tseem ceeb dua los xaiv cov ntaub ntawv raug hom rau cov teb ntawm cov ntxhuav hauv koj cov ntaub ntawv. Nws yooj yim dua los siv cov ntaub ntawv tsawg tshaj plaws uas tuaj yeem khaws cov ntaub ntawv kom raug, vim tias lawv siv qhov chaw tsawg dua los ntawm lub cim xeeb.