b1gc8v 发表于 2024-8-4 12:21:19

数据库进阶:实用命令,约束,链接查询


    <p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"><span style="color: black;">在这个数字化转型的浪潮中,信息技术正以前所未有的速度重塑着<span style="color: black;">咱们</span>的世界。数据库已<span style="color: black;">作为</span>存储和管理数据不可或缺的工具。<span style="color: black;">把握</span><span style="color: black;">基本</span>的SQL命令仅仅是<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 style="color: black;">繁杂</span></span>
      </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>

    <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>将介绍一系列高级SQL命令,这些命令能够让你更<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><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;"><strong style="color: blue;"><span style="color: black;">数据库进阶<span style="color: black;">重点</span>内容:</span></strong></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;">:use,set,show,rename,truncate和万能的 help 命令。</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;">主键自增,主键约束,外键约束,<span style="color: black;">独一</span>约束,默认约束,check约束。</p>

    <p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"><span style="color: black;">- 链接<span style="color: black;">查找</span>:</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;"><span style="color: black;">说明:本文部分关键字采用了大写,为了让SQL语句结构更加清晰,更利于区分SQL之间差异。</span></p>
    <p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"><strong style="color: blue;">1. USE</strong></p><img src="https://mmbiz.qpic.cn/sz_mmbiz_png/fLF2peu3TJqSJ5rH4hy5DB5EYq63GQyZV3z2Ou0SpXicSwuEnKOBUAsbQne53LBZZMNCzFOP6Ir4MfQI4wz6cVw/640?wx_fmt=png&amp;from=appmsg&amp;tp=webp&amp;wxfrom=5&amp;wx_lazy=1&amp;wx_co=1" style="width: 50%; margin-bottom: 20px;">
    <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><span style="color: black;">use database_name;</span>
    <p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"><strong style="color: blue;">2. SET</strong></p><img src="https://mmbiz.qpic.cn/sz_mmbiz_png/fLF2peu3TJqSJ5rH4hy5DB5EYq63GQyZV3z2Ou0SpXicSwuEnKOBUAsbQne53LBZZMNCzFOP6Ir4MfQI4wz6cVw/640?wx_fmt=png&amp;from=appmsg&amp;tp=webp&amp;wxfrom=5&amp;wx_lazy=1&amp;wx_co=1" style="width: 50%; margin-bottom: 20px;">
    <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></p><span style="color: black;"><span style="color: black;">set</span> names utf8;</span>
    <p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"><strong style="color: blue;">3.&nbsp; SHOW</strong></p><img src="https://mmbiz.qpic.cn/sz_mmbiz_png/fLF2peu3TJqSJ5rH4hy5DB5EYq63GQyZV3z2Ou0SpXicSwuEnKOBUAsbQne53LBZZMNCzFOP6Ir4MfQI4wz6cVw/640?wx_fmt=png&amp;from=appmsg&amp;tp=webp&amp;wxfrom=5&amp;wx_lazy=1&amp;wx_co=1" style="width: 50%; margin-bottom: 20px;">
    <p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"><strong style="color: blue;">1. 查看数据库</strong></p><span style="color: black;"><span style="color: black;">show</span> databases <span style="color: black;">|</span> schemas;</span>
    <p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"><strong style="color: blue;">2. 查看表</strong></p><span style="color: black;"><span style="color: black;">show</span> tables;</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;">3. 查看表结构</span></strong></p><span style="color: black;"><span style="color: black;">show</span> columns <span style="color: black;">from</span> table_name &nbsp;<span style="color: black;">|</span> <span style="color: black;">desc</span> table_name;</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;">4. 查看服务器状态</span></strong></p><span style="color: black;"><span style="color: black;">show</span> status;</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;">5. 查看MySQL进程</span></strong></p><span style="color: black;"><span style="color: black;">show</span> processlist;</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;">6. <span style="color: black;">表示</span>引擎</span></strong></p><span style="color: black;"><span style="color: black;">show</span> engines;</span><img src="https://mmbiz.qpic.cn/sz_mmbiz_png/fLF2peu3TJqSJ5rH4hy5DB5EYq63GQyZLeOXGGSvDSGNksmia6ViaNMOseQ5rMghl2KticHycCb2uw5ugLJJ6NQaA/640?wx_fmt=png&amp;from=appmsg&amp;tp=webp&amp;wxfrom=5&amp;wx_lazy=1&amp;wx_co=1" style="width: 50%; margin-bottom: 20px;">
    <p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"><strong style="color: blue;"><span style="color: black;">7. 列出所有字符集</span></strong></p><span style="color: black;"><span style="color: black;">show</span> charset;</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;">8. 显示数据库的编码</span></strong></p><span style="color: black;"><span style="color: black;">show</span> variables <span style="color: black;">like</span> <span style="color: black;">character_set_database</span>;</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;">9. <span style="color: black;">表示</span>校验规则</span></strong></p><span style="color: black;"><span style="color: black;">show</span> variables <span style="color: black;">like</span> <span style="color: black;">collation_database</span>;</span><img src="https://mmbiz.qpic.cn/sz_mmbiz_jpg/fLF2peu3TJqSJ5rH4hy5DB5EYq63GQyZYTWHUVwGicekPVQkictd6MsdhXJVzAttnVibBd4A9obGuNpq9H8kcnD1g/640?wx_fmt=jpeg&amp;from=appmsg&amp;tp=webp&amp;wxfrom=5&amp;wx_lazy=1&amp;wx_co=1" style="width: 50%; margin-bottom: 20px;">
    <p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"><span style="color: black;">提示:在SQL编写过程中<span style="color: black;">检测</span>关键字和语句无误,<span style="color: black;">显现</span>红色<span style="color: black;">浪花</span>线<span style="color: black;">能够</span>忽略。</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;">10. <span style="color: black;">表示</span>数据库引擎</span></strong></p><span style="color: black;"><span style="color: black;">show</span>variables<span style="color: black;">like</span> "have_%";</span><img src="https://mmbiz.qpic.cn/sz_mmbiz_jpg/fLF2peu3TJqSJ5rH4hy5DB5EYq63GQyZqRA6axLbVS1GvC4tXWcic6NAhcks8sdqxWffa6CNGeLeeY4z3KyJKKg/640?wx_fmt=jpeg&amp;from=appmsg&amp;tp=webp&amp;wxfrom=5&amp;wx_lazy=1&amp;wx_co=1" style="width: 50%; margin-bottom: 20px;">
    <p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"><strong style="color: blue;">4. Rename</strong></p><img src="https://mmbiz.qpic.cn/sz_mmbiz_png/fLF2peu3TJqSJ5rH4hy5DB5EYq63GQyZV3z2Ou0SpXicSwuEnKOBUAsbQne53LBZZMNCzFOP6Ir4MfQI4wz6cVw/640?wx_fmt=png&amp;from=appmsg&amp;tp=webp&amp;wxfrom=5&amp;wx_lazy=1&amp;wx_co=1" style="width: 50%; margin-bottom: 20px;">
    <p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"><strong style="color: blue;"><span style="color: black;">1. 修改表名,方式一</span></strong></p><span style="color: black;">Rename <span style="color: black;">table</span> oldname <span style="color: black;">to</span> newname;</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;">2. 修改表名,方式二</span></strong></p><span style="color: black;"><span style="color: black;">alter</span> <span style="color: black;">table</span> table_name &nbsp;RENAME &nbsp;[<span style="color: black;">TO</span><span style="color: black;">|</span><span style="color: black;">AS</span>] new_table_name</span><span style="color: black;"><span style="color: black;">-- 将 employees(雇员表)修改为 user(用户表) --</span></span><span style="color: black;"><span style="color: black;">ALTER</span> <span style="color: black;">TABLE</span> employees RENAME <span style="color: black;">TO</span> <span style="color: black;">user</span>;</span><span style="color: black;"><span style="color: black;">-- <span style="color: black;">或</span> --</span></span><span style="color: black;"><span style="color: black;">ALTER</span> <span style="color: black;">TABLE</span> employees RENAME <span style="color: black;">AS</span> <span style="color: black;">user</span>;</span>
    <p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"><strong style="color: blue;">5. Truncate</strong></p><img src="https://mmbiz.qpic.cn/sz_mmbiz_png/fLF2peu3TJqSJ5rH4hy5DB5EYq63GQyZV3z2Ou0SpXicSwuEnKOBUAsbQne53LBZZMNCzFOP6Ir4MfQI4wz6cVw/640?wx_fmt=png&amp;from=appmsg&amp;tp=webp&amp;wxfrom=5&amp;wx_lazy=1&amp;wx_co=1" style="width: 50%; margin-bottom: 20px;">
    <p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"><strong style="color: blue;"><span style="color: black;">1. 清空表数据</span></strong></p><span style="color: black;"><span style="color: black;">truncate</span> table_name; &nbsp;</span>
    <p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"><span style="color: black;">提示:</span>truncate table_name 清空表<span style="color: black;">不可</span>和where<span style="color: black;">要求</span><span style="color: black;">一块</span><span style="color: black;">运用</span>。和delete区别是,delete删除表中的数据,是从删除所有层面来操作的,操作后自增列还是存在的。而truncate 相当于删除表数据,再重新建一张相同结构的表,操作后得到一张新表,速度快。</p>
    <p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"><strong style="color: blue;">6. HELP</strong></p><img src="https://mmbiz.qpic.cn/sz_mmbiz_png/fLF2peu3TJqSJ5rH4hy5DB5EYq63GQyZV3z2Ou0SpXicSwuEnKOBUAsbQne53LBZZMNCzFOP6Ir4MfQI4wz6cVw/640?wx_fmt=png&amp;from=appmsg&amp;tp=webp&amp;wxfrom=5&amp;wx_lazy=1&amp;wx_co=1" style="width: 50%; margin-bottom: 20px;"> <span style="color: black;"><span style="color: black;">-- <span style="color: black;">帮忙</span>查看 show <span style="color: black;">关联</span>命令 --</span></span><span style="color: black;">help <span style="color: black;">show</span>;</span><span style="color: black;"><span style="color: black;">-- <span style="color: black;">帮忙</span>查看 create <span style="color: black;">关联</span>命令 --</span></span><span style="color: black;">help <span style="color: black;">create</span>;</span><span style="color: black;"><span style="color: black;">-- <span style="color: black;">帮忙</span>查看 alter <span style="color: black;">关联</span>命令 --</span></span><span style="color: black;">help <span style="color: black;">alter</span>;</span>
    <p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"><strong style="color: blue;">7. 约束</strong></p><img src="https://mmbiz.qpic.cn/sz_mmbiz_png/fLF2peu3TJqSJ5rH4hy5DB5EYq63GQyZV3z2Ou0SpXicSwuEnKOBUAsbQne53LBZZMNCzFOP6Ir4MfQI4wz6cVw/640?wx_fmt=png&amp;from=appmsg&amp;tp=webp&amp;wxfrom=5&amp;wx_lazy=1&amp;wx_co=1" style="width: 50%; margin-bottom: 20px;">
    <p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"><span style="color: black;">约束<span style="color: black;">重点</span><span style="color: black;">包括</span>6种:主键自增,主键约束,外键约束,<span style="color: black;">独一</span>约束,默认约束,check约束。</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;">1. 主键自动增长</span></strong></p><span style="color: black;"><span style="color: black;">alter</span>&nbsp;<span style="color: black;">table</span>&nbsp;table_name&nbsp;<span style="color: black;">column</span>&nbsp;<span style="color: black;">id</span>&nbsp;<span style="color: black;">int</span>primary<span style="color: black;">key</span>&nbsp;auto_&nbsp;<span style="color: black;">increment</span></span>
    <p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"><span style="color: black;">提示:自动增长<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>是自动增长的。auto_increment:自动增长关键字,primary key:主键关键字。</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;">2. 添加主键约束</span></strong></p><span style="color: black;"><span style="color: black;">alter</span>&nbsp;<span style="color: black;">table</span>&nbsp;table_name&nbsp;<span style="color: black;">add</span>&nbsp;<span style="color: black;">constraint</span>&nbsp;PK_ID&nbsp;primary&nbsp;<span style="color: black;">key</span>&nbsp;(&nbsp;<span style="color: black;">ID</span>&nbsp;)</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></strong></p><span style="color: black;"><span style="color: black;">-- 将用户表的 id 设置为主键 --</span></span><span style="color: black;"><span style="color: black;">ALTER</span> <span style="color: black;">TABLE</span> users </span><span style="color: black;"><span style="color: black;">ADD</span> <span style="color: black;">CONSTRAINT</span> PK_users_id <span style="color: black;">PRIMARY</span> <span style="color: black;">KEY</span> (id);</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;">3. 添加外键约束</span></strong></p><span style="color: black;"><span style="color: black;">alter</span>&nbsp;<span style="color: black;">table</span>&nbsp;table_name&nbsp;<span style="color: black;">add</span>&nbsp;<span style="color: black;">constraint</span>&nbsp;FK_ID&nbsp;<span style="color: black;">foreign</span>&nbsp;<span style="color: black;">key</span>&nbsp;(<span style="color: black;">ID</span>)&nbsp;<span style="color: black;">references</span>&nbsp;table_name(&nbsp;<span style="color: black;">id</span>&nbsp;)</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></strong></p><span style="color: black;"><span style="color: black;">-- orders 订单表,customers 客户表,orders 添加一个外键 customer_id 指向客户表中的主键 id --</span></span><span style="color: black;"><span style="color: black;">ALTER</span> <span style="color: black;">TABLE</span> orders</span><span style="color: black;"><span style="color: black;">ADD</span> <span style="color: black;">CONSTRAINT</span> FK_customers_id <span style="color: black;">FOREIGN</span> <span style="color: black;">KEY</span>(customer_id)<span style="color: black;">REFERENCES</span> customers(id);</span>
    <p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"><span style="color: black;"><span style="color: black;">重视</span>:<span style="color: black;"><span style="color: black;">针对</span>MySQL不支持外键<span style="color: black;">,之前是有<span style="color: black;">有些</span>误解。</span></span><span style="color: black;">为何</span>之前会有MySQL不支持外键的误解呢?还一度<span style="color: black;">作为</span><span style="color: black;">机构</span>的面试题。</span></p>
    <p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"><span style="color: black;"><span style="color: black;">不是不支持外键,而是大厂不<span style="color: black;">举荐</span>MySQL<span style="color: black;">运用</span>外键。</span><span style="color: black;">在阿里手册中<span style="color: black;">说到</span>:</span><span style="color: black;">不得<span style="color: black;">运用</span>外键与级联,一切外键概念<span style="color: black;">必要</span>在应用层<span style="color: black;">处理</span>。</span></span></p>
    <p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"><span style="color: black;">定义外键约束之后,数据库的每次操作都<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>,级联更新是强阻塞,存在数据库更新风暴 Database Update Storm 的<span style="color: black;">危害</span>。外链约束<span style="color: black;">亦</span>不是完全<span style="color: black;">无</span>优点,外链的存在就<span style="color: black;">保准</span>了表关系的完整性。但鱼和熊掌不可兼得。</span></p>
    <p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"><span style="color: black;">我是最<span style="color: black;">起始</span>是在<span style="color: black;">运用</span></span>Microsoft SQL Server数据库,Microsoft SQL Server 在数据库设计时候,表之间的关系是<span style="color: black;">必须</span><span style="color: black;">知道</span>主外键约束。<span style="color: black;">运用</span>MySQL在设计表结构时候<span style="color: black;">发掘</span>项目组小伙伴都不<span style="color: black;">运用</span>外键。<span style="color: black;">起始</span><span style="color: black;">还挺意外的,有些不适,后面一看,它不影响我的多表连查,我<span style="color: black;">亦</span>就没管它了。</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;">在 MySQL5.5 及之前的版本中,默认的存储引擎是 MyISAM,它不支持外键。从MySQL5.5<span style="color: black;">起始</span>,默认存储引擎变为 InnoDB,它支持外键。从下表<span style="color: black;">能够</span>看出MySQL<span style="color: black;">供给</span>了多种引擎。<span style="color: black;">截止</span><span style="color: black;">日前</span>跟新到 MySQL Community Server 9.0.1版本。</span></p><img src="https://mmbiz.qpic.cn/sz_mmbiz_png/fLF2peu3TJqSJ5rH4hy5DB5EYq63GQyZLeOXGGSvDSGNksmia6ViaNMOseQ5rMghl2KticHycCb2uw5ugLJJ6NQaA/640?wx_fmt=png&amp;from=appmsg&amp;tp=webp&amp;wxfrom=5&amp;wx_lazy=1&amp;wx_co=1" style="width: 50%; margin-bottom: 20px;">
    <p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"><strong style="color: blue;"><span style="color: black;">4. <span style="color: black;">独一</span>约束</span></strong></p><span style="color: black;"><span style="color: black;">alter</span>&nbsp;<span style="color: black;">table</span>&nbsp;table_name&nbsp;<span style="color: black;">add</span>&nbsp;<span style="color: black;">constraint</span>&nbsp;QU_id&nbsp;<span style="color: black;">unique</span> (identityID)</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></strong></p><span style="color: black;"><span style="color: black;">ALTER</span> <span style="color: black;">TABLE</span> users</span><span style="color: black;"><span style="color: black;">ADD</span> <span style="color: black;">CONSTRAINT</span> QU_id <span style="color: black;">UNIQUE</span> (identityID);</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;">5. 默认约束</span></strong></p><span style="color: black;"><span style="color: black;">alter</span>&nbsp;<span style="color: black;">table</span>&nbsp;table_name&nbsp;<span style="color: black;">add</span>&nbsp;<span style="color: black;">constraint</span>DF_loginpwd<span style="color: black;">default</span>&nbsp;(<span style="color: black;">123</span>)&nbsp;<span style="color: black;">for</span>&nbsp;loginpwd</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></strong></p><span style="color: black;"><span style="color: black;">ALTER</span> <span style="color: black;">TABLE</span> users</span><span style="color: black;"><span style="color: black;">ADD</span> <span style="color: black;">CONSTRAINT</span> DF_loginpwd <span style="color: black;">DEFAULT</span> <span style="color: black;">123</span> <span style="color: black;">FOR</span> loginpwd;</span><span style="color: black;"><span style="color: black;">-- <span style="color: black;">倘若</span>您<span style="color: black;">运用</span>的数据库系统不支持 FOR 关键字,<span style="color: black;">能够</span>省略它。--</span></span><span style="color: black;"><span style="color: black;">ALTER</span> <span style="color: black;">TABLE</span> users</span><span style="color: black;"><span style="color: black;">ADD</span> <span style="color: black;">CONSTRAINT</span> DF_loginpwd <span style="color: black;">DEFAULT</span> <span style="color: black;">123</span>;</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;">6. check 约束</span></strong></p><span style="color: black;"><span style="color: black;">alter</span> <span style="color: black;">table</span> table_name <span style="color: black;">add</span> <span style="color: black;">constraint</span> CK_Date <span style="color: black;">check</span> ( <span style="color: black;">Date</span> <span style="color: black;">&gt;=</span><span style="color: black;">1990-1-1</span>)</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;">7. 约束<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>:数据库中的表,是<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;">二是:一张表中只能有一个主键,<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><span style="color: black;">拥有</span><span style="color: black;">类似</span>的数据类型。数据类型,长度和精度,<span style="color: black;">有没有</span>符号<span style="color: black;">不可</span>混用,字符集,NULL处理:外键列和参照列对 NULL 值的处理<span style="color: black;">必要</span>一致。<span style="color: black;">倘若</span>参照列不<span style="color: black;">准许</span> NULL 值,外键列<span style="color: black;">亦</span><span style="color: black;">必要</span>设置为 NOT NULL,存储引擎<span style="color: black;">必要</span>支持外键,<span style="color: black;">一般</span><span style="color: black;">运用</span>InnoDB。</p>
    <p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"><strong style="color: blue;">8. 链接<span style="color: black;">查找</span></strong></p><img src="https://mmbiz.qpic.cn/sz_mmbiz_png/fLF2peu3TJqSJ5rH4hy5DB5EYq63GQyZV3z2Ou0SpXicSwuEnKOBUAsbQne53LBZZMNCzFOP6Ir4MfQI4wz6cVw/640?wx_fmt=png&amp;from=appmsg&amp;tp=webp&amp;wxfrom=5&amp;wx_lazy=1&amp;wx_co=1" style="width: 50%; margin-bottom: 20px;">
    <p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"><strong style="color: blue;"><span style="color: black;">1. 笛卡尔积</span></strong></p>
    <p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"><span style="color: black;">提示:表联查<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></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></p><span style="color: black;">mysql<span style="color: black;">&gt;</span> <span style="color: black;">select</span> <span style="color: black;">*</span> <span style="color: black;">from</span> fruits;</span><span style="color: black;"><span style="color: black;">+</span><span style="color: black;">------+--------+</span></span><span style="color: black;"><span style="color: black;">|</span> f_id <span style="color: black;">|</span> f_name <span style="color: black;">|</span></span><span style="color: black;"><span style="color: black;">+</span><span style="color: black;">------+--------+</span></span><span style="color: black;"><span style="color: black;">|</span> &nbsp; &nbsp;<span style="color: black;">1</span> <span style="color: black;">|</span> 苹果 &nbsp; <span style="color: black;">|</span></span><span style="color: black;"><span style="color: black;">|</span> &nbsp; &nbsp;<span style="color: black;">2</span> <span style="color: black;">|</span> 梨子 &nbsp; <span style="color: black;">|</span></span><span style="color: black;"><span style="color: black;">+</span><span style="color: black;">------+--------+</span></span><span style="color: black;"><span style="color: black;">2</span> <span style="color: black;">rows</span> <span style="color: black;">in</span> <span style="color: black;">set</span> (<span style="color: black;">0.00</span> sec)</span><span style="color: black;">mysql<span style="color: black;">&gt;</span> <span style="color: black;">select</span> <span style="color: black;">*</span> <span style="color: black;">from</span> animal;</span><span style="color: black;"><span style="color: black;">+</span><span style="color: black;">----+--------+</span></span><span style="color: black;"><span style="color: black;">|</span> id <span style="color: black;">|</span> name &nbsp; <span style="color: black;">|</span></span><span style="color: black;"><span style="color: black;">+</span><span style="color: black;">----+--------+</span></span><span style="color: black;"><span style="color: black;">|</span> &nbsp;<span style="color: black;">1</span> <span style="color: black;">|</span> 老虎 &nbsp; <span style="color: black;">|</span></span><span style="color: black;"><span style="color: black;">|</span> &nbsp;<span style="color: black;">2</span> <span style="color: black;">|</span> 狮子 &nbsp; <span style="color: black;">|</span></span><span style="color: black;"><span style="color: black;">|</span> &nbsp;<span style="color: black;">3</span> <span style="color: black;">|</span> 熊猫 &nbsp; <span style="color: black;">|</span></span><span style="color: black;"><span style="color: black;">+</span><span style="color: black;">----+--------+</span></span><span style="color: black;"><span style="color: black;">3</span> <span style="color: black;">rows</span> <span style="color: black;">in</span> <span style="color: black;">set</span> (<span style="color: black;">0.00</span> sec)</span><span style="color: black;"><span style="color: black;">查找</span>结果:产生笛卡尔积,<span style="color: black;">SQL</span>语句<span style="color: black;">查找</span><span style="color: black;">无</span>指定<span style="color: black;">where</span>条件</span><span style="color: black;">mysql<span style="color: black;">&gt;</span> <span style="color: black;">select</span> id,name,f_id,f_name <span style="color: black;">from</span> fruits,animal;</span><span style="color: black;"><span style="color: black;">+</span><span style="color: black;">----+--------+------+--------+</span></span><span style="color: black;"><span style="color: black;">|</span> id <span style="color: black;">|</span> name &nbsp; <span style="color: black;">|</span> f_id <span style="color: black;">|</span> f_name <span style="color: black;">|</span></span><span style="color: black;"><span style="color: black;">+</span><span style="color: black;">----+--------+------+--------+</span></span><span style="color: black;"><span style="color: black;">|</span> &nbsp;<span style="color: black;">1</span> <span style="color: black;">|</span> 老虎 &nbsp; <span style="color: black;">|</span> &nbsp; &nbsp;<span style="color: black;">1</span> <span style="color: black;">|</span> 苹果 &nbsp; <span style="color: black;">|</span></span><span style="color: black;"><span style="color: black;">|</span> &nbsp;<span style="color: black;">1</span> <span style="color: black;">|</span> 老虎 &nbsp; <span style="color: black;">|</span> &nbsp; &nbsp;<span style="color: black;">2</span> <span style="color: black;">|</span> 梨子 &nbsp; <span style="color: black;">|</span></span><span style="color: black;"><span style="color: black;">|</span> &nbsp;<span style="color: black;">2</span> <span style="color: black;">|</span> 狮子 &nbsp; <span style="color: black;">|</span> &nbsp; &nbsp;<span style="color: black;">1</span> <span style="color: black;">|</span> 苹果 &nbsp; <span style="color: black;">|</span></span><span style="color: black;"><span style="color: black;">|</span> &nbsp;<span style="color: black;">2</span> <span style="color: black;">|</span> 狮子 &nbsp; <span style="color: black;">|</span> &nbsp; &nbsp;<span style="color: black;">2</span> <span style="color: black;">|</span> 梨子 &nbsp; <span style="color: black;">|</span></span><span style="color: black;"><span style="color: black;">|</span> &nbsp;<span style="color: black;">3</span> <span style="color: black;">|</span> 熊猫 &nbsp; <span style="color: black;">|</span> &nbsp; &nbsp;<span style="color: black;">1</span> <span style="color: black;">|</span> 苹果 &nbsp; <span style="color: black;">|</span></span><span style="color: black;"><span style="color: black;">|</span> &nbsp;<span style="color: black;">3</span> <span style="color: black;">|</span> 熊猫 &nbsp; <span style="color: black;">|</span> &nbsp; &nbsp;<span style="color: black;">2</span> <span style="color: black;">|</span> 梨子 &nbsp; <span style="color: black;">|</span></span><span style="color: black;"><span style="color: black;">+</span><span style="color: black;">----+--------+------+--------+</span></span><span style="color: black;"><span style="color: black;">6</span> <span style="color: black;">rows</span> <span style="color: black;">in</span> <span style="color: black;">set</span> (<span style="color: black;">0.01</span> sec)</span>
    <p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"><span style="color: black;"><span style="color: black;">两表做全乘:</span>从行看,<span style="color: black;">便是</span>两表每一行,两两组合。从列看,结果集中列,两表列相加。</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;">2. 左连接 LEFT JOIN ON</span></strong></p><span style="color: black;"><span style="color: black;">-- LEFT JOIN <span style="color: black;">或</span> LEFT OUTER JOIN --</span></span><span style="color: black;"><span style="color: black;">SELECT</span> columns</span><span style="color: black;"><span style="color: black;">FROM</span> table1</span><span style="color: black;"><span style="color: black;">LEFT</span> <span style="color: black;">JOIN</span> table2</span><span style="color: black;"><span style="color: black;">ON</span> table1.common_field <span style="color: black;">=</span> table2.common_field;</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;">3. 右连接 RIGHT JOIN ON</span></strong></p><span style="color: black;"><span style="color: black;">-- RIGHT JOIN <span style="color: black;">或</span> RIGHT OUTER JOIN --</span></span><span style="color: black;"><span style="color: black;">SELECT</span> columns</span><span style="color: black;"><span style="color: black;">FROM</span> table1</span><span style="color: black;"><span style="color: black;">RIGHT</span> <span style="color: black;">JOIN</span> table2</span><span style="color: black;"><span style="color: black;">ON</span> table1.common_field <span style="color: black;">=</span> table2.common_field;</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;">4. 内连接 INNER JOIN ON</span></strong></p><span style="color: black;"><span style="color: black;">SELECT</span> columns</span><span style="color: black;"><span style="color: black;">FROM</span> table1</span><span style="color: black;"><span style="color: black;">INNER</span> <span style="color: black;">JOIN</span> table2</span><span style="color: black;"><span style="color: black;">ON</span> table1.common_field <span style="color: black;">=</span> table2.common_field;</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;">5. 全连接 FULL JOIN ON</span></strong></p><span style="color: black;"><span style="color: black;">-- FULL JOIN <span style="color: black;">或</span> FULL OUTER JOIN --</span></span><span style="color: black;"><span style="color: black;">SELECT</span> columns</span><span style="color: black;"><span style="color: black;">FROM</span> table1</span><span style="color: black;"><span style="color: black;">FULL</span> <span style="color: black;">JOIN</span> table2</span><span style="color: black;"><span style="color: black;">ON</span>table1.common_field<span style="color: black;">=</span> table2.common_field;</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;">6.&nbsp; 交叉连接 CROSS JOIN</span></strong></p><span style="color: black;"><span style="color: black;">SELECT</span> <span style="color: black;">columns</span></span><span style="color: black;"><span style="color: black;">FROM</span> table1</span><span style="color: black;"><span style="color: black;">CROSS</span> <span style="color: black;">JOIN</span> table2;</span>
    <p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"><span style="color: black;"><span style="color: black;">重视</span>:返回两张表的笛卡尔积,不<span style="color: black;">思虑</span>连接<span style="color: black;">要求</span>。</span><span style="color: black;">两表相乘<span style="color: black;">查找</span>方式,内存消耗大。</span></p>
    <p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"><strong style="color: blue;">7. 自连接 SELF JOIN</strong></p>
    <p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"><span style="color: black;">自连接(Self Join)是一种特殊的SQL连接操作,它<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></p><span style="color: black;"><span style="color: black;">SELECT</span> columns</span><span style="color: black;"><span style="color: black;">FROM</span> table1 <span style="color: black;">AS</span> t1, table1 <span style="color: black;">AS</span> t2</span><span style="color: black;"><span style="color: black;">WHERE</span> t1.common_field <span style="color: black;">=</span>t2.another_field;</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></strong></p><span style="color: black;"><span style="color: black;">SELECT</span> </span><span style="color: black;"> &nbsp; &nbsp;e1.EmployeeID, </span><span style="color: black;"> &nbsp; &nbsp;e1.Name <span style="color: black;">AS</span> <span style="color: black;">Employee Name</span>, </span><span style="color: black;"> &nbsp; &nbsp;e2.Name <span style="color: black;">AS</span> <span style="color: black;">Manager Name</span></span><span style="color: black;"><span style="color: black;">FROM</span> </span><span style="color: black;"> &nbsp; &nbsp;Employees e1</span><span style="color: black;"><span style="color: black;">JOIN</span> </span><span style="color: black;"> &nbsp; &nbsp;Employees e2 <span style="color: black;">ON</span> e1.ManagerID <span style="color: black;">=</span> e2.EmployeeID;</span>
    <p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"><span style="color: black;"><span style="color: black;">写在最后:这篇<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 style="color: black;"><span style="color: black;">文案</span>详情</span>:</span><a style="color: black;">《数据库的<span style="color: black;">基本</span>:增删改查操作指南》</a>⬅ 点看&nbsp;<img src="https://res.wx.qq.com/t/wx_fed/we-emoji/res/v1.3.10/assets/newemoji/Addoil.png?tp=webp&amp;wxfrom=5&amp;wx_lazy=1&amp;wx_co=1" style="width: 50%; margin-bottom: 20px;"> ~</span></span></p>
    <p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"><span style="color: black;"><span style="color: black;"><strong style="color: blue;">公众号|</strong></span>杨小样随笔</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;">关注&nbsp; 点赞&nbsp; 在看</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>





qzmjef 发表于 2024-10-1 10:15:44

太棒了、厉害、为你打call、点赞、非常精彩等。

j8typz 发表于 2024-10-15 18:39:22

感谢你的精彩评论,为我的思绪打开了新的窗口。

4lqedz 发表于 2024-11-8 20:10:01

论坛外链网http://www.fok120.com/

b1gc8v 发表于 2024-11-9 16:33:39

你的见解独到,让我受益匪浅,期待更多交流。
页: [1]
查看完整版本: 数据库进阶:实用命令,约束,链接查询