多表查询 等连接 通过两个表具有相同意义的列,建立连接条件. 查询结果只显示两个列中的值是等值条件的行数据 表中同名列被选择
多表查询
等连接
通过两个表具有相同意义的列,建立连接条件.
查询结果只显示两个列中的值是等值条件的行数据
表中同名列被选择时必须添加表名前缀进行修饰 否则无法确定这一列是属于哪个表
N张表等值连接,条件至少有N-1个,否则会产生笛卡尔积
不等连接
A表中的某列数据和B表中一列或多列的关系是非等值关系,大于,小于,不等于,等条件都属于不等连接的范畴
自连接
数据都来自一张表,所以在from字句中需要对表添加别名,添加表别名后才能合法化的引用表中的列名.
本质就是将一张表虚拟成了两张表
外连接
即是选择出满足等连接条件及其以外的行
(+)修饰符号用法:放置在选出结果只包含等连接的列后,则另一列的结果就是等值行+非等值行
全连接(Oracle 不提供连接关键字,,需要用集合模式实现)
SQL1999(SQL99) 有全连接的关键词
当数据需要从多个表中获得时
idle> select * from emp,dept;
...
...
56 rows selected.
这样得到的结果是 56 行,其实两个表产生了笛卡尔集 14 * 4
idle> select count(*) from emp;
COUNT(*)
----------
14
idle> select count(*) from dept;
COUNT(*)
----------
4
idle>
在连接中给定一个等值条件 一般是主键和外键的关系
例如deptno在dept表中是主键 在emp表中是外键
观看dept(部门表)和emp(员工表)的关系
他们是主从关系,dept是主表,emp是从表
emp中的deptno一定要隶属于dept中的deptno
所以他们两个列是等值的
具体的主键和外键关系 我们在以后建表时再详细阐述
等连接
按deptno的等值关系联合两张表的所有列
idle> select * from dept,emp where dept.deptno=emp.deptno;
DEPTNO DNAME LOC EMPNO ENAME JOB MGR HIREDATE SAL COMM DEPTNO
---------- -------------- ------------- ---------- ---------- --------- ---------- ------------------- ---------- ---------- ----------
10 ACCOUNTING NEW YORK 7782 CLARK MANAGER 7839 1981-06-09 00:00:00 2450 10
10 ACCOUNTING NEW YORK 7839 KING PRESIDENT 1981-11-17 00:00:00 5000 10
10 ACCOUNTING NEW YORK 7934 MILLER CLERK 7782 1982-01-23 00:00:00 1300 10
20 RESEARCH DALLAS 7566 JONES MANAGER 7839 1981-04-02 00:00:00 2975 20
20 RESEARCH DALLAS 7902 FORD ANALYST 7566 1981-12-03 00:00:00 3000 20
20 RESEARCH DALLAS 7876 ADAMS CLERK 7788 1987-05-23 00:00:00 1100 20
20 RESEARCH DALLAS 7369 SMITH CLERK 7902 1980-12-17 00:00:00 800 20
20 RESEARCH DALLAS 7788 SCOTT ANALYST 7566 1987-04-19 00:00:00 3000 20
30 SALES CHICAGO 7521 WARD SALESMAN 7698 1981-02-22 00:00:00 1250 500 30
30 SALES CHICAGO 7844 TURNER SALESMAN 7698 1981-09-08 00:00:00 1500 0 30
30 SALES CHICAGO 7499 ALLEN SALESMAN 7698 1981-02-20 00:00:00 1600 300 30
30 SALES CHICAGO 7900 JAMES CLERK 7698 1981-12-03 00:00:00 950 30
30 SALES CHICAGO 7698 BLAKE MANAGER 7839 1981-05-01 00:00:00 2850 30
30 SALES CHICAGO 7654 MARTIN SALESMAN 7698 1981-09-28 00:00:00 1250 1400 30
14 rows selected.
idle>
这才是我们要的结果.笛卡尔积几乎我们不会需要.多表查询时基本都带有where子句来描述多个表的关系 避免笛卡尔集
当两个表中有相同的列名时,为了区分 要在列前加上表名作前缀.
不冲突时 直接使用
idle> select deptno,empno,ename,dname,sal from emp a,dept b where b.deptno=a.deptno;
select deptno,empno,ename,dname,sal from emp a,dept b where b.deptno=a.deptno
*
ERROR at line 1:
ORA-00918: column ambiguously defined
因为deptno 没有表前缀 冲突
idle>
在联合的两个表内取数据:描述scott在哪个部门
idle> select empno,ename,dname,sal from emp,dept where emp.deptno=dept.deptno and ename='SCOTT';
EMPNO ENAME DNAME SAL
---------- ---------- -------------- ----------
7788 SCOTT RESEARCH 3000
idle>
表的别名
格式:表名 别名
给表取别名是很有必要的,因为有的表名很长 不便于引用时书写.
idle> select a.deptno,empno,ename,dname,sal from emp a,dept b where b.deptno=a.deptno and ename='SCOTT';
DEPTNO EMPNO ENAME DNAME SAL
---------- ---------- ---------- -------------- ----------
20 7788 SCOTT RESEARCH 3000
idle>
不等连接
就是排除完全相等条件以外的 >,=, between and
主要在于不同表之间显示特定范围的信息(也可以理解成包含关系)
例如: SALGRADE表把工资划分了5个等级
idle> select * from SALGRADE;
GRADE LOSAL HISAL
---------- ---------- ----------
1 700 1200
2 1201 1400
3 1401 2000
4 2001 3000
5 3001 9999
idle>
查处每个员工的工资等级
idle> select ename,sal,grade from emp a,salgrade b where sal between losal and hisal;
ENAME SAL GRADE
---------- ---------- ----------
SMITH 800 1
JAMES 950 1
ADAMS 1100 1
WARD 1250 2
MARTIN 1250 2
MILLER 1300 2
TURNER 1500 3
ALLEN 1600 3
CLARK 2450 4
BLAKE 2850 4
JONES 2975 4
SCOTT 3000 4
FORD 3000 4
KING 5000 5
14 rows selected.
idle>
练习:
1.查询名字是S开头的员工名,员工号,部门号,部门名,部位地理位置
select empno,ename,emp.deptno,dept.dname,dept.loc
from emp,dept
where ename like 'S%' and emp.deptno=dept.deptno;
2.查询员工名,部门名,工资,工资等级
select empno,ename,grade
from emp,salgrade
where sal between losal and hisal;
3.查询工作在NEW YORK的所有员工
select empno,ename,emp.deptno,dept.dname,dept.loc
from emp,dept
where emp.deptno=dept.deptno and dept.loc='NEW YORK';
自连接
同一张表内的连接查询 即把一个表映射成两个表
主要用于表的自参照关系 比如emp中的上下级或层次关系
因为自连接是同一张表之间的链接查询 所以必须定义表别名
例子:根据empno和mgr的对应关系,可以查找出KING的下属都有谁
idle> select b.ename||'''s manager is '||a.ename from emp a,emp b where a.empno = b.mgr and a.ename='KING';
B.ENAME||'''SMANAGERIS'||A.ENAME
----------------------------------
BLAKE's manager is KING
JONES's manager is KING
CLARK's manager is KING
idle>

MySQLviewshavelimitations:1)Theydon'tsupportallSQLoperations,restrictingdatamanipulationthroughviewswithjoinsorsubqueries.2)Theycanimpactperformance,especiallywithcomplexqueriesorlargedatasets.3)Viewsdon'tstoredata,potentiallyleadingtooutdatedinforma

ProperusermanagementinMySQLiscrucialforenhancingsecurityandensuringefficientdatabaseoperation.1)UseCREATEUSERtoaddusers,specifyingconnectionsourcewith@'localhost'or@'%'.2)GrantspecificprivilegeswithGRANT,usingleastprivilegeprincipletominimizerisks.3)

MySQLdoesn'timposeahardlimitontriggers,butpracticalfactorsdeterminetheireffectiveuse:1)Serverconfigurationimpactstriggermanagement;2)Complextriggersincreasesystemload;3)Largertablesslowtriggerperformance;4)Highconcurrencycancausetriggercontention;5)M

Yes,it'ssafetostoreBLOBdatainMySQL,butconsiderthesefactors:1)StorageSpace:BLOBscanconsumesignificantspace,potentiallyincreasingcostsandslowingperformance.2)Performance:LargerrowsizesduetoBLOBsmayslowdownqueries.3)BackupandRecovery:Theseprocessescanbe

Adding MySQL users through the PHP web interface can use MySQLi extensions. The steps are as follows: 1. Connect to the MySQL database and use the MySQLi extension. 2. Create a user, use the CREATEUSER statement, and use the PASSWORD() function to encrypt the password. 3. Prevent SQL injection and use the mysqli_real_escape_string() function to process user input. 4. Assign permissions to new users and use the GRANT statement.

MySQL'sBLOBissuitableforstoringbinarydatawithinarelationaldatabase,whileNoSQLoptionslikeMongoDB,Redis,andCassandraofferflexible,scalablesolutionsforunstructureddata.BLOBissimplerbutcanslowdownperformancewithlargedata;NoSQLprovidesbetterscalabilityand

ToaddauserinMySQL,use:CREATEUSER'username'@'host'IDENTIFIEDBY'password';Here'showtodoitsecurely:1)Choosethehostcarefullytocontrolaccess.2)SetresourcelimitswithoptionslikeMAX_QUERIES_PER_HOUR.3)Usestrong,uniquepasswords.4)EnforceSSL/TLSconnectionswith

ToavoidcommonmistakeswithstringdatatypesinMySQL,understandstringtypenuances,choosetherighttype,andmanageencodingandcollationsettingseffectively.1)UseCHARforfixed-lengthstrings,VARCHARforvariable-length,andTEXT/BLOBforlargerdata.2)Setcorrectcharacters


Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

Video Face Swap
Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Article

Hot Tools

SublimeText3 Linux new version
SublimeText3 Linux latest version

SecLists
SecLists is the ultimate security tester's companion. It is a collection of various types of lists that are frequently used during security assessments, all in one place. SecLists helps make security testing more efficient and productive by conveniently providing all the lists a security tester might need. List types include usernames, passwords, URLs, fuzzing payloads, sensitive data patterns, web shells, and more. The tester can simply pull this repository onto a new test machine and he will have access to every type of list he needs.

ZendStudio 13.5.1 Mac
Powerful PHP integrated development environment

DVWA
Damn Vulnerable Web App (DVWA) is a PHP/MySQL web application that is very vulnerable. Its main goals are to be an aid for security professionals to test their skills and tools in a legal environment, to help web developers better understand the process of securing web applications, and to help teachers/students teach/learn in a classroom environment Web application security. The goal of DVWA is to practice some of the most common web vulnerabilities through a simple and straightforward interface, with varying degrees of difficulty. Please note that this software

Notepad++7.3.1
Easy-to-use and free code editor
