Primary key vs Unique key
Ib kem lossis ib kab ke, uas tuaj yeem siv los txheeb xyuas lossis nkag mus rau kab lossis kab hauv cov ntaub ntawv hu ua tus yuam sij. Tus yuam sij tshwj xeeb yog tus yuam sij uas tuaj yeem txheeb xyuas cov kab hauv ib lub rooj hauv cov ntsiab lus ntawm kev sib raug zoo databases. Ib qho tseem ceeb tshwj xeeb yog tsim los ntawm ib kab los yog ib pawg kab. Ib qho tseem ceeb tseem ceeb kuj yog ib qho kev sib txuas ntawm kab hauv ib lub rooj uas tshwj xeeb txheeb xyuas kab. Tab sis nws raug suav hais tias yog qhov tshwj xeeb ntawm tus yuam sij tshwj xeeb.
Tus yuam sij cim yog dab tsi?
Raws li tau hais ua ntej, tus yuam sij tshwj xeeb yog ib kab lossis kab ke uas tuaj yeem txheeb xyuas kab hauv ib lub rooj. Yog li, tus yuam sij tshwj xeeb yog txwv tsis pub muaj ob qhov txiaj ntsig ntawm nws sib npaug. Ib qho tseem ceeb ntawm cov khoom yog tias cov yuam sij tshwj xeeb tsis txwv tsis pub NULL txwv. Txij li NULL sawv cev rau qhov tsis muaj tus nqi, yog tias ob kab muaj NULL nyob rau hauv ib kem ces nws tsis txhais hais tias tus nqi sib npaug. Kem txhais tias yog tus yuam sij tshwj xeeb tso cai tsuas yog tus nqi NULL nkaus xwb hauv kab ntawv ntawd. Tom qab ntawd nws tuaj yeem siv los txheeb xyuas qhov kab ntawv tshwj xeeb. Piv txwv li, nyob rau hauv ib lub rooj uas muaj cov tub ntxhais kawm cov ntaub ntawv, tus tub kawm ntawv ID tuaj yeem txhais tau tias yog tus yuam sij tshwj xeeb. Txij li tsis muaj ob tus tub ntxhais kawm tuaj yeem muaj tus ID tib yam nws qhia tau ib tus menyuam kawm ntawv tshwj xeeb. Yog li tus tub kawm ntawv ID kem txaus siab rau txhua yam khoom ntawm tus yuam sij tshwj xeeb. Nyob ntawm tus qauv tsim ntawm cov ntaub ntawv, lub rooj yuav muaj ntau dua ib tus yuam sij tshwj xeeb.
Primary Key yog dab tsi?
Tus yuam sij tseem ceeb kuj yog ib kem lossis kev sib txuas ntawm txhua kab uas tshwj xeeb txhais cov kab hauv ib lub rooj ntawm cov ntaub ntawv sib raug zoo. Ib lub rooj tuaj yeem muaj feem ntau ntawm ib qho tseem ceeb. Lub hauv paus tseem ceeb tswj hwm qhov kev txwv tsis pub muaj NULL. Yog li, ib kem uas txhais tau tias yog tus yuam sij tseem ceeb tsis tuaj yeem muaj NULL qhov tseem ceeb hauv nws. Tus yuam sij tseem ceeb tuaj yeem yog tus cwj pwm ib txwm muaj nyob rau hauv cov lus uas tau lees paub tias nws yog tus lej social security lossis nws tuaj yeem yog tus nqi tshwj xeeb tsim los ntawm kev tswj hwm database xws li Globally Unique Identifier (GUID) hauv Microsoft SQL Server. Cov yuam sij tseem ceeb yog txhais los ntawm PRIMARY KEY txwv hauv ANSI SQL Standard. Tus yuam sij tseem ceeb tseem tuaj yeem txhais tau thaum tsim lub rooj. SQL tso cai rau tus yuam sij tseem ceeb los ntawm ib lossis ntau kab thiab txhua kab uas suav nrog hauv tus yuam sij tseem ceeb yog txhais tau hais tias tsis yog NULL. Tab sis qee qhov kev tswj hwm cov ntaub ntawv xav tau ua cov kab ntawv tseem ceeb kom meej meej TSIS NULL.
Qhov txawv ntawm tus yuam sij tseem ceeb thiab tus yuam sij tshwj xeeb
Txawm hais tias ob qho tseem ceeb thiab tus yuam sij tshwj xeeb yog ib lossis ntau kab uas tuaj yeem txheeb xyuas kab hauv ib lub rooj, lawv muaj qee qhov sib txawv tseem ceeb. Qhov tseem ceeb tshaj plaws, lub rooj tuaj yeem tsuas muaj ib tus yuam sij tseem ceeb thaum nws tuaj yeem muaj ntau dua ib tus yuam sij tshwj xeeb. Tus yuam sij tseem ceeb tuaj yeem suav tias yog qhov tshwj xeeb ntawm tus yuam sij tshwj xeeb. Lwm qhov sib txawv yog tias cov yuam sij tseem ceeb muaj qhov cuam tshuam tsis yog NULL thaum tus yuam sij tshwj xeeb tsis muaj qhov txwv. Yog li ntawd, cov kab tseem ceeb tshwj xeeb yuav lossis tsis muaj NULL qhov tseem ceeb tab sis cov kab tseem ceeb tsis tuaj yeem muaj NULL qhov tseem ceeb.