数据库|基于T-SQL添加默认约束、外键约束、内连接查询
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">哈喽,你好啊,我是雷工!</p>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">前边学习了基于T-SQL48_47.基于T-SQL添加数据、CRUD操作、标识列详解:</p>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">《<strong style="color: blue;"><span style="color: black;"><a style="color: black;">数据库|基于T-SQL向数据库数据表中添加、修改、删除数据</a></span></strong>》</p>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">接下来接着学习基于T-SQL添加默认约束、外键约束,内连接<span style="color: black;">查找</span>,以下为学习笔记。</p>
<div style="color: black; text-align: left; margin-bottom: 10px;"><img src="https://p3-sign.toutiaoimg.com/tos-cn-i-axegupay5k/ad69d358230e49aeb98e3803938a3474~noop.image?_iz=58558&from=article.pc_detail&lk3s=953192f4&x-expires=1723346828&x-signature=Vil5fxhtp%2By5ZVIDIm3DgWl3%2FXU%3D" style="width: 50%; margin-bottom: 20px;"></div>
<h1 style="color: black; text-align: left; margin-bottom: 10px;">01 默认约束</h1>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">默认约束是用于指定列在<span style="color: black;">插进</span>数据时的默认值。</p>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">当向数据表中<span style="color: black;">插进</span>一行数据时,<span style="color: black;">倘若</span><span style="color: black;">无</span>为某个列指定<span style="color: black;">详细</span>的值,<span style="color: black;">那样</span>该列将<span style="color: black;">运用</span>其默认约束定义的默认值。</p>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">在创建表时定义默认约束:</p>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">CREATE TABLE 表名(</p>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"> 列1 数据类型 DEFAUL 默认值,</p>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"> ……</p>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">);</p>
<div style="color: black; text-align: left; margin-bottom: 10px;"><img src="https://p3-sign.toutiaoimg.com/tos-cn-i-6w9my0ksvp/9a2564a2243f4fbf9caf57f389bccc84~noop.image?_iz=58558&from=article.pc_detail&lk3s=953192f4&x-expires=1723346828&x-signature=8dmKUNhD919LVedzuNWrjw%2BvUcM%3D" style="width: 50%; margin-bottom: 20px;"></div>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">例如:在作者表中为<span style="color: black;">位置</span>添加默认值“<span style="color: black;">位置</span>不详”</p>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">代码:</p><span style="color: black;">create</span> <span style="color: black;">table</span>Author
(
AuthorId<span style="color: black;">int</span> <span style="color: black;">identity</span>(<span style="color: black;">10001</span>,<span style="color: black;">1</span>) primary <span style="color: black;">key</span>,<span style="color: black;">--作者编号,主键</span>
LoginAccount <span style="color: black;">nvarchar</span>(<span style="color: black;">50</span>)<span style="color: black;">not</span> <span style="color: black;">null</span>,<span style="color: black;">--登录账号</span>
LoginPwd <span style="color: black;">varchar</span>(<span style="color: black;">18</span>)<span style="color: black;">check</span>(<span style="color: black;">len</span>(LoginPwd)>=<span style="color: black;">8</span> <span style="color: black;">and</span> <span style="color: black;">len</span>(LoginPwd)<=<span style="color: black;">18</span>) <span style="color: black;">not</span> <span style="color: black;">null</span>,<span style="color: black;">--登录<span style="color: black;">秘码</span></span>
AuthorName <span style="color: black;">varchar</span>(<span style="color: black;">20</span>)<span style="color: black;">not</span> <span style="color: black;">null</span>,<span style="color: black;">--作者名字</span>
Phonenumber <span style="color: black;">char</span>(<span style="color: black;">11</span>)<span style="color: black;">not</span> <span style="color: black;">null</span>,<span style="color: black;">--手机</span>
NowAddress <span style="color: black;">nvarchar</span>(<span style="color: black;">100</span>)<span style="color: black;">default</span>(<span style="color: black;"><span style="color: black;">位置</span>不详</span>)<span style="color: black;">--<span style="color: black;">位置</span></span>
)
<span style="color: black;">go</span>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"><span style="color: black;">而后</span>再次向数据表中添加数据时,<span style="color: black;">倘若</span>有<span style="color: black;">位置</span>信息替换为对应<span style="color: black;">位置</span>信息,<span style="color: black;">无</span>指定<span style="color: black;">位置</span>信息则<span style="color: black;">表示</span>“<span style="color: black;">位置</span>不详”。</p>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">测试结果如下:</p>
<div style="color: black; text-align: left; margin-bottom: 10px;"><img src="https://p3-sign.toutiaoimg.com/tos-cn-i-6w9my0ksvp/3bfa7f6fb8b04864a51de348f0b0cc5a~noop.image?_iz=58558&from=article.pc_detail&lk3s=953192f4&x-expires=1723346828&x-signature=YSjLUU3kaiTElHeBh3FAxD%2FAH%2B0%3D" style="width: 50%; margin-bottom: 20px;"></div>
<h1 style="color: black; text-align: left; margin-bottom: 10px;">02 外键约束</h1>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">外键约束是数据库设计中的概念,用于<span style="color: black;">保证</span><span style="color: black;">相关</span>系的两个表数据的一致性和完整性。</p>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">在关系数据库中,外键是一个列或列集合,它指向另一个<span style="color: black;">相关</span>表中的主键或<span style="color: black;">独一</span>键。</p>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"><strong style="color: blue;"><span style="color: black;">功效</span></strong>:当<span style="color: black;">咱们</span>在外键表中,添加数据的时候,<span style="color: black;">倘若</span>和主表<span style="color: black;">创立</span>引用关系,则会自动的从主表中对应的字段中<span style="color: black;">查找</span>数据<span style="color: black;">是不是</span>一致,不一致的话会拒绝<span style="color: black;">插进</span>。</p>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"><span style="color: black;">保准</span>数据一致。</p>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"><strong style="color: blue;">示例:</strong></p>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"><strong style="color: blue;">2.1、创建账号类型表:</strong></p>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">代码如下:</p><span style="color: black;">--账号类型表</span>
if exists(<span style="color: black;">select</span>*<span style="color: black;">from</span> sysobjects <span style="color: black;">where</span> <span style="color: black;">name</span>=<span style="color: black;">AccountType</span>)
<span style="color: black;">drop</span> <span style="color: black;">table</span> AccountType
<span style="color: black;">go</span>
<span style="color: black;">create</span> <span style="color: black;">table</span> AccountType
(
TypeId <span style="color: black;">int</span> <span style="color: black;">identity</span>(<span style="color: black;">10</span>,<span style="color: black;">1</span>)primary <span style="color: black;">key</span>,<span style="color: black;">--类型编号</span>
TypeName <span style="color: black;">varchar</span>(<span style="color: black;">20</span>)<span style="color: black;">not</span> <span style="color: black;">null</span><span style="color: black;">--类型名<span style="color: black;">叫作</span></span>
)
<span style="color: black;">go</span>
<div style="color: black; text-align: left; margin-bottom: 10px;"><img src="https://p3-sign.toutiaoimg.com/tos-cn-i-6w9my0ksvp/45dfdd077355421fa3d107a7d7aa1a86~noop.image?_iz=58558&from=article.pc_detail&lk3s=953192f4&x-expires=1723346828&x-signature=vraiRr3RpsyXujHQcd9Bc7Hac8U%3D" style="width: 50%; margin-bottom: 20px;"></div>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">2.2、向账号类型表中添加【科技】、【美食】、【综合】三个类型</p>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">代码如下:</p><span style="color: black;">--</span>添加账号<span style="color: black;">归类</span>
<span style="color: black;">insert</span> <span style="color: black;">into</span> <span style="color: black;">AccountType</span>(TypeName)<span style="color: black;">values</span>(<span style="color: black;">科技</span>),(<span style="color: black;">美食</span>),(<span style="color: black;">综合</span>)<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"><strong style="color: blue;"><span style="color: black;"><span style="color: black;">查找</span>账号类型表的信息</span></strong></p>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"><span style="color: black;">代码如下:</span></p><span style="color: black;">select</span>*<span style="color: black;">from</span> AccountType<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">2.3、<span style="color: black;">查找</span>结果:</p>
<div style="color: black; text-align: left; margin-bottom: 10px;"><img src="https://p3-sign.toutiaoimg.com/tos-cn-i-6w9my0ksvp/eeef40a35c9c44c4a544a56e39e5b1d1~noop.image?_iz=58558&from=article.pc_detail&lk3s=953192f4&x-expires=1723346828&x-signature=ZYDunKGO9P3fCqlpK8Dl7GZ6wIc%3D" style="width: 50%; margin-bottom: 20px;"></div>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">2.4、创建账号表</p>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">创建账号表,其中TypeId列为外键,指向账号<span style="color: black;">归类</span>表的TypeId列</p>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">代码如下:</p><span style="color: black;">--账号表</span>if exists(<span style="color: black;">select</span>*<span style="color: black;">from</span> sysobjects <span style="color: black;">where</span> <span style="color: black;">name</span>=<span style="color: black;">Account</span>)
<span style="color: black;">drop</span> <span style="color: black;">table</span> <span style="color: black;">Account</span>
<span style="color: black;">go</span>
<span style="color: black;">create</span> <span style="color: black;">table</span> <span style="color: black;">Account</span>
(
AccountId <span style="color: black;">int</span> <span style="color: black;">identity</span>(<span style="color: black;">10001</span>,<span style="color: black;">1</span>) primary <span style="color: black;">key</span>,<span style="color: black;">--账号编号</span>
AccountName <span style="color: black;">varchar</span>(<span style="color: black;">20</span>)<span style="color: black;">not</span> <span style="color: black;">null</span>,<span style="color: black;">--账号名<span style="color: black;">叫作</span></span>
AccountContent <span style="color: black;">nvarchar</span>(<span style="color: black;">500</span>)<span style="color: black;">not</span> <span style="color: black;">null</span>,<span style="color: black;">--账号简介</span>
originality <span style="color: black;">int</span> <span style="color: black;">not</span> <span style="color: black;">null</span>,<span style="color: black;">--原创数</span>
TypeId <span style="color: black;">int</span> <span style="color: black;">references</span> AccountType(TypeId) <span style="color: black;">not</span> <span style="color: black;">null</span><span style="color: black;">--外键约束</span>
)
<span style="color: black;">go</span>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">2.5、向账号表添加信息</p>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">当添加的信息中TypeId值(9)在账号类型表中不存在时,<span style="color: black;">插进</span>报错。</p>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">代码及执行结果如下:</p>
<div style="color: black; text-align: left; margin-bottom: 10px;"><img src="https://p3-sign.toutiaoimg.com/tos-cn-i-6w9my0ksvp/f79e1798f4e340098e353d8010f6b06d~noop.image?_iz=58558&from=article.pc_detail&lk3s=953192f4&x-expires=1723346828&x-signature=cOstvMR8GlejeAW%2FyosCwIeIAco%3D" style="width: 50%; margin-bottom: 20px;"></div>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">当添加的信息中TypeId值为11,在账号类型表中存在时,则可正常<span style="color: black;">插进</span>。</p>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">代码及执行结果:</p>
<div style="color: black; text-align: left; margin-bottom: 10px;"><img src="https://p3-sign.toutiaoimg.com/tos-cn-i-6w9my0ksvp/1f2549dffac143bdbfc8c191b0249f98~noop.image?_iz=58558&from=article.pc_detail&lk3s=953192f4&x-expires=1723346828&x-signature=B0meMlbjD8LYFyS%2F4Vu8uQmwmZU%3D" style="width: 50%; margin-bottom: 20px;"></div>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"><span style="color: black;">查找</span><span style="color: black;">插进</span>结果为:</p>
<div style="color: black; text-align: left; margin-bottom: 10px;"><img src="https://p3-sign.toutiaoimg.com/tos-cn-i-6w9my0ksvp/ab2b9dd166a1424ca799188071aa4bd3~noop.image?_iz=58558&from=article.pc_detail&lk3s=953192f4&x-expires=1723346828&x-signature=i23i9msXm%2FIeOiOkyFEfNVWeHdI%3D" style="width: 50%; margin-bottom: 20px;"></div>
<h1 style="color: black; text-align: left; margin-bottom: 10px;">03 内连接<span style="color: black;">查找</span></h1>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"><span style="color: black;">经过</span>inner join连很<span style="color: black;">查找</span>两个表的信息。</p>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">代码示例:</p><span style="color: black;">select</span>AccountName,AccountContent,originality,Account.TypeId,AccountType.TypeName<span style="color: black;">from</span> <span style="color: black;">Account</span>
<span style="color: black;">inner</span> <span style="color: black;">join</span> AccountType <span style="color: black;">on</span> Account.TypeId=AccountType.TypeId<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"><span style="color: black;">查找</span>结果:</p>
<div style="color: black; text-align: left; margin-bottom: 10px;"><img src="https://p3-sign.toutiaoimg.com/tos-cn-i-6w9my0ksvp/c1bb5fd9093b43a498236570aa60cb92~noop.image?_iz=58558&from=article.pc_detail&lk3s=953192f4&x-expires=1723346828&x-signature=ClxW6p8IJ%2BbMRS5V13AfUh%2FK0%2F4%3D" style="width: 50%; margin-bottom: 20px;"></div>
<h1 style="color: black; text-align: left; margin-bottom: 10px;">04、后记</h1>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">以上为基于T-SQL添加默认约束、外键约束、内连接<span style="color: black;">查找</span><span style="color: black;">关联</span>的<span style="color: black;">基本</span>内容,</p>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">有对<span style="color: black;">关联</span>内容感兴趣的小伙伴,欢迎点个关注,<span style="color: black;">咱们</span><span style="color: black;">一块</span>学习进步。</p>
你的努力一定会被看见,相信自己,加油。
页:
[1]