Oracle数据库:自然连接natural join,using语句,注意避免写交叉连接

Oracle数据库:自然连接natural join,using语句,注意避免写交叉连接

2022找工作是学历、能力和运气的超强结合体,遇到寒冬,大厂不招人,可能很多算法学生都得去找开发,测开
测开的话,你就得学数据库,sql,oracle,尤其sql要学,当然,像很多金融企业、安全机构啥的,他们必须要用oracle数据库
这oracle比sql安全,强大多了,所以你需要学习,最重要的,你要是考网络警察公务员,这玩意你不会就别去报名了,耽误时间!
oracle系列文章:
【1】Oracle数据库:啥是oracle数据库?你为啥要学oracle?
【2】Oracle数据库:oracle 11g安装教程,已安装好的oracle各个文件夹的作用,oracle用户权限怎么样
【3】Oracle数据库:oracle启动,oracle客户端工具plsql安装教程和使用方法
【4】Oracle数据库:创建表空间,创建新用户,给用户分配对象、角色和系统权限,登录新用户建表
【5】Oracle数据库:链接配置,包括sqlnet.ora里面的transnames.ora配置数据库标识符SID,listener暂时简单了解
【6】Oracle数据库:net configureation assistant工具配置监听listener,配置本地网络访问服务器上的数据库
【7】Oracle数据库:oracle字符类型、数字类型、创建表表名的命名规则
【8】Oracle数据库:约束条件:主键约束、唯一约束、检查约束、非空约束、外键约束、默认值填写
【9】Oracle数据库:表的关系:一对多,一对一,多对多,一一继承和修改的关系,表不是重点,重点是数据和约束关系
【10】Oracle数据库:sql语言结构,数据查询语言DQL,select * from table;算术,别名,连接,去重等操作
【11】Oracle数据库:约束行限制where语句,判断条件,比较条件,字符串日期格式,in,like,escape,null语句
【12】Oracle数据库:逻辑运算and,or,not和各种运算的优先级控制
【13】Oracle数据库:排序order by语句,select from where order by的执行先后顺序,各种样例
【14】Oracle数据库:oracle函数,单行函数,多行函数,upper,lower,initcap,字符串函数
【15】Oracle数据库:数字函数,日期函数,round,trunc,mod,months_between,add_months,next_day,last_day,sysdate
【16】Oracle数据库:oracle数据类型转换to_char()日期和数字转字符,to_number()字符转数字,to_date()字符转日期函数
【17】Oracle数据库:oracle函数嵌套,nvl函数,nvl2函数,nullif函数,coalesce合并函数
【18】Oracle数据库:条件表达式case when then else end,decode函数,oracle单行函数练习示例
【19】Oracle数据库:oracle多表查询,等值连接,非等值连接,自连接的sql语句和规则
【20】Oracle数据库:oracle外连接left/right/full outer join on,oracle扩展的左右外连接展示符号(+)


oracle交叉连接:应该避免写这个东西

在这里插入图片描述
直接炸锅了卧槽
一定要避免这么写,炸了………………

select * from employees cross join departments;

2889 rows selected

切忌这么写
炸锅

oracle自然连接natural join是等值连接的另一种写法

在这里插入图片描述
列名和数据类型
必须都一样

在这里插入图片描述

多个字段相等,那就且关系

所以呢,你建立表的时候,就要考虑这个问题
natural join的本质是等值连接的简化
性能一样

懂吧

最好是等值连接
这个一查就是所有列的等

在这里插入图片描述
departments表
locations表

SQL> desc locations;
Name           Type         Nullable Default Comments                                                                                                                                  
-------------- ------------ -------- ------- ----------------------------------------------------------------------------------------------------------------------------------------- 
LOCATION_ID    NUMBER(4)                     Primary key of locations table                                                                                                            
STREET_ADDRESS VARCHAR2(40) Y                Street address of an office, warehouse, or production site of a company.
Contains building number and street name                          
POSTAL_CODE    VARCHAR2(12) Y                Postal code of the location of an office, warehouse, or production site
of a company.                                                      
CITY           VARCHAR2(30)                  A not null column that shows city where an office, warehouse, or
production site of a company is located.                                  
STATE_PROVINCE VARCHAR2(25) Y                State or Province where an office, warehouse, or production site of a
company is located.                                                  
COUNTRY_ID     CHAR(2)      Y                Country where an office, warehouse, or production site of a company is
located. Foreign key to country_id column of the countries table. 

SQL> desc departments;
Name            Type         Nullable Default Comments                                                                                                                                                                                 
--------------- ------------ -------- ------- ---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- 
DEPARTMENT_ID   NUMBER(4)                     Primary key column of departments table.                                                                                                                                                 
DEPARTMENT_NAME VARCHAR2(30)                  A not null column that shows name of a department. Administration,
Marketing, Purchasing, Human Resources, Shipping, IT, Executive, Public
Relations, Sales, Finance, and Accounting.  
MANAGER_ID      NUMBER(6)    Y                Manager_id of a department. Foreign key to employee_id column of employees table. The manager_id column of the employee table references this column.                                    
LOCATION_ID     NUMBER(4)    Y                Location id where a department is located. Foreign key to location_id column of locations table.                                                                                         

试一下

SQL> select d.department_id,d.department_name,l.city from departments d,locations l where d.location_id = l.location_id;

DEPARTMENT_ID DEPARTMENT_NAME                CITY
------------- ------------------------------ ------------------------------
           60 IT                             Southlake
           50 Shipping                       South San Francisco
           10 Administration                 Seattle
           30 Purchasing                     Seattle
           90 Executive                      Seattle
          100 Finance                        Seattle
          110 Accounting                     Seattle
          120 Treasury                       Seattle
          130 Corporate Tax                  Seattle
          140 Control And Credit             Seattle
          150 Shareholder Services           Seattle
          160 Benefits                       Seattle
          170 Manufacturing                  Seattle
          180 Construction                   Seattle
          190 Contracting                    Seattle
          200 Operations                     Seattle
          210 IT Support                     Seattle
          220 NOC                            Seattle
          230 IT Helpdesk                    Seattle
          240 Government Sales               Seattle

DEPARTMENT_ID DEPARTMENT_NAME                CITY
------------- ------------------------------ ------------------------------
          250 Retail Sales                   Seattle
          260 Recruiting                     Seattle
          270 Payroll                        Seattle
           20 Marketing                      Toronto
           40 Human Resources                London
           80 Sales                          Oxford
           70 Public Relations               Munich

27 rows selected

这是等值连接的标准格式
简化一波

SQL> select d.department_id,d.department_name,l.city from departments d natural join locations l;

DEPARTMENT_ID DEPARTMENT_NAME                CITY
------------- ------------------------------ ------------------------------
           60 IT                             Southlake
           50 Shipping                       South San Francisco
           10 Administration                 Seattle
           30 Purchasing                     Seattle
           90 Executive                      Seattle
          100 Finance                        Seattle
          110 Accounting                     Seattle
          120 Treasury                       Seattle
          130 Corporate Tax                  Seattle
          140 Control And Credit             Seattle
          150 Shareholder Services           Seattle
          160 Benefits                       Seattle
          170 Manufacturing                  Seattle
          180 Construction                   Seattle
          190 Contracting                    Seattle
          200 Operations                     Seattle
          210 IT Support                     Seattle
          220 NOC                            Seattle
          230 IT Helpdesk                    Seattle
          240 Government Sales               Seattle

DEPARTMENT_ID DEPARTMENT_NAME                CITY
------------- ------------------------------ ------------------------------
          250 Retail Sales                   Seattle
          260 Recruiting                     Seattle
          270 Payroll                        Seattle
           20 Marketing                      Toronto
           40 Human Resources                London
           80 Sales                          Oxford
           70 Public Relations               Munich

27 rows selected

natural join
的用法就如此
联合俩表相等的列
相当于直接用location_id相同
那通过部门的location_id,去查city信息
所以设计表格的时候,你要设计好
否则就没法用

oracle using子句

在这里插入图片描述
using子句
左右表都有id,x列都相同
natural join就会做判断,id和x都得相同

如果用using id
那只看id,不看x
而且你不能用表的别名玩,这个是注意的地方

natural join和using子句是相互排斥的

在这里插入图片描述

指定using
不妨设别的可能还相同
那就要限制
行选城市id是1800的

SQL> select d.department_name,l.city from departments d join locations l using(location_id) where location_id=1800;

DEPARTMENT_NAME                CITY
------------------------------ ------------------------------
Marketing                      Toronto

natural不能要了哦
另外using加括号()
然后限定行放屁股

只限定location_id链接,这就是using语句的好处
和natural冲突的

另外你要用location_id,还不表名或者别名了
不能限定了
你看

SQL> select d.department_name,l.city from departments d join locations l using(location_id) where location_id=1800;

DEPARTMENT_NAME                CITY
------------------------------ ------------------------------
Marketing                      Toronto

SQL> select d.department_name,l.city from departments d join locations l using(location_id) where locations.location_id=1800;
select d.department_name,l.city from departments d join locations l using(location_id) where locations.location_id=1800

ORA-00904: "LOCATIONS"."LOCATION_ID": 标识符无效

懂了吧
using,特定列,直接用列名玩


总结

提示:重要经验:

1)
2)学好oracle,即使经济寒冬,整个测开offer绝对不是问题!同时也是你考公网络警察的必经之路。
3)笔试求AC,可以不考虑空间复杂度,但是面试既要考虑时间复杂度最优,也要考虑空间复杂度最优。

  • 0
    点赞
  • 7
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

冰露可乐

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值