时间:2021-07-01 10:21:17 帮助过:31人阅读
定义SN为唯一键,系统自动为唯一键建立唯一索引,从而保证了唯一性,可以为空,但最多只可以出现一个NULL值。sn_uniq可以省略。
[constraint<约束名>]unique
定义sn+sex为唯一键,此表为约束表
- <span style="color: #0000ff">create</span> <span style="color: #0000ff">table</span><span style="color: #000000"> ss
- ( sno </span><span style="color: #0000ff">char</span>(<span style="color: #800000; font-weight: bold">6</span>) <span style="color: #0000ff">unique</span><span style="color: #000000">,
- sn </span><span style="color: #0000ff">varchar</span>(<span style="color: #800000; font-weight: bold">8</span><span style="color: #000000">),
- sex </span><span style="color: #0000ff">char</span>(<span style="color: #800000; font-weight: bold">2</span><span style="color: #000000">),
- age </span><span style="color: #0000ff">int</span><span style="color: #000000">,
- dept </span><span style="color: #0000ff">varchar</span>(<span style="color: #800000; font-weight: bold">20</span><span style="color: #000000">)
- </span><span style="color: #0000ff">constraint</span> s_uniq <span style="color: #0000ff">unique</span> (sn,sex))
前面的约束名可以省略,后面的不可以。 为sn+sex建立唯一索引,确保同一性别的学生没有重名
null/not null约束
null值不是也不是空白,更不是填入字符串NULL,而是表示不知道,不确定,或没有数据。
当某一字段的值一定要输入才有意义的时候,则可以设置not null
主键就允许出现空值,否则就失去了唯一表示一条记录的作用。
[constraint<约束名>][null|not null]
- <span style="color: #0000ff">create</span> <span style="color: #0000ff">table</span><span style="color: #000000"> ss
- ( sno </span><span style="color: #0000ff">char</span>(<span style="color: #800000; font-weight: bold">6</span>) <span style="color: #0000ff">constraint</span> s_cons <span style="color: #808080">not</span> <span style="color: #0000ff">null</span><span style="color: #000000">,
- sn </span><span style="color: #0000ff">varchar</span>(<span style="color: #800000; font-weight: bold">8</span><span style="color: #000000">),
- sex </span><span style="color: #0000ff">char</span>(<span style="color: #800000; font-weight: bold">2</span><span style="color: #000000">),
- age </span><span style="color: #0000ff">int</span><span style="color: #000000">,
- dept </span><span style="color: #0000ff">varchar</span>(<span style="color: #800000; font-weight: bold">20</span>))
当约束名省略时即 s_cons,系统会自动加字
sql
标签:没有 索引 color nbsp blog not 一键 tab 系统