搜尋
首頁資料庫mysql教程数据库SQL之表关系&连接查询

数据库SQL之表关系&连接查询

Jun 07, 2016 pm 03:42 PM
ampsql一對多關係資料庫查詢連接

/* 表关系:一对多:多端加外键,一端加主键 多对多:添加一个关系表,变为一对多的关系 一对一:外键--唯一键 E-R图:实体--表 属性--列 元组--行 主键列:唯一仅有的 外键列:用来约束主键列关联项的取 */ --创建 create table book( bid number(5), bname


/*
  表关系:一对多:多端加外键,一端加主键
  多对多:添加一个关系表,变为一对多的关系
  一对一:外键-->唯一键

  E-R图:实体-->表
  属性-->列
  元组-->行
  主键列:唯一仅有的
  外键列:用来约束主键列关联项的取值
 
 
*/

--创建
create table book(
  bid number(5),
  bname varchar2(20),
  slid number(5)
)
--建立主键
alter table book
add constraints PK_BOOKlei
primary key(bid)
--建立外键
alter table book
add constraints FK_BOOK_BOOKlei
foreign key(slid)
references booklei(blid)
--创建
create table booklei(
  blid number(5),
  blname varchar2(20)
)

--多对多 

create table student(
  sid number(5) primary key,
  sname varchar2(20)
)

create table tea_stu(
  tid number(5) references student(sid),
  tid number(5) references teacher(tid)
)

create table teacher(
  tid number(5) primary key,
  tname varchar2(20)
)

--一对一
create table person(
  pid number(5) primary key,
  pname varchar2(20),
  cid number(5) unique references idcard(cid)
)

create table idcard(
  cid number(5) primary key,
  cnumber varchar2(20)
)


--连接查询(笛卡尔积)
--等值连接(不一定是=号,两张表也不一定是有主外键关系)
select * from emp e,dept d where e.deptno = d.deptno
--查询部门为'ACCOUNTING'的员工(***连接查询和嵌套子查询的扫描次数不同***)
select * from emp e,dept d where e.deptno = d.deptno and d.dname = 'ACCOUNTING'
select * from emp where deptno = (select deptno from dept where dname = 'ACCOUNTING')
--查询员工的薪金等级
select * from SALGRADE s,emp e where e.sal between s.losal and s.hisal
--查询各个工资等级的人数
select s.grade,count(*) from emp e,salgrade s
where e.sal between s.losal and s.hisal
group by s.grade
--查询每个部门每种工资等级的人数
select s.grade,d.dname,count(*) from emp e, dept d,salgrade s
where e.deptno = d.deptno and e.sal between s.losal and s.hisal
group by s.grade,d.dname
order by s.grade


--外连接:左外连接 (以左边的表为标准) / 右外连接(以右边的表为标准)
select * from emp e left join dept d on d.deptno = e.deptno

select * from dept d left join emp e on d.deptno = e.deptno
where empno is null

--自连接
select e1.ename,count(*) from emp e,emp e1 where e.mgr = e1.empno
group by e1.ename




陳述
本文內容由網友自願投稿,版權歸原作者所有。本站不承擔相應的法律責任。如發現涉嫌抄襲或侵權的內容,請聯絡admin@php.cn
MySQL字符串類型:存儲,性能和最佳實踐MySQL字符串類型:存儲,性能和最佳實踐May 10, 2025 am 12:02 AM

mySqlStringTypesimpactStorageAndPerformanCeaseAsfollows:1)長度,始終使用theSamestoragespace,whatcanbefasterbutlessspace-felfficity.2)varCharisvariable varcharisvariable length,morespace-morespace-morespace-effficitybuteftife buteftife butfority butfority textifforlyslower.3)

了解MySQL字符串類型:VARCHAR,文本,char等了解MySQL字符串類型:VARCHAR,文本,char等May 10, 2025 am 12:02 AM

mysqlStringTypesIncludeVarChar,文本,char,Enum和set.1)varCharisVersAtileForvariable-lengthStringStringSuptoPuptOuptoPepePecifiedLimit.2)textisidealforlargetStortStorStoverStoverStorageWithoutAutAdefinedLength.3)charlisfixed-lenftenge,for forConsistentDatalikeCodes.4)

MySQL中的字符串數據類型是什麼?MySQL中的字符串數據類型是什麼?May 10, 2025 am 12:01 AM

MySQLoffersvariousstringdatatypes:1)CHARforfixed-lengthstrings,2)VARCHARforvariable-lengthtext,3)BINARYandVARBINARYforbinarydata,4)BLOBandTEXTforlargedata,and5)ENUMandSETforcontrolledinput.Eachtypehasspecificusesandperformancecharacteristics,sochoose

如何向新的MySQL用戶授予權限如何向新的MySQL用戶授予權限May 09, 2025 am 12:16 AM

TograntpermissionstonewMySQLusers,followthesesteps:1)AccessMySQLasauserwithsufficientprivileges,2)CreateanewuserwiththeCREATEUSERcommand,3)UsetheGRANTcommandtospecifypermissionslikeSELECT,INSERT,UPDATE,orALLPRIVILEGESonspecificdatabasesortables,and4)

如何在MySQL中添加用戶:逐步指南如何在MySQL中添加用戶:逐步指南May 09, 2025 am 12:14 AM

toadduserInmysqleffect和securly,跟隨台詞:1)USEtheCreateUserStattoDaneWuser,指定thehostandastrongpassword.2)GrantNecterAryAryaryPrivilegesSustherthing privilegesgeStatement,usifementStatement,adheringtotheprinciplelastprefilegege.3)

mysql:添加具有復雜權限的新用戶mysql:添加具有復雜權限的新用戶May 09, 2025 am 12:09 AM

toaddanewuserwithcomplexpermissionsinmysql,loldtheSesteps:1)創建eTheEserWithCreateuser'newuser'newuser'@''localhost'Indedify'pa ssword';。 2)GrantreadAccesstoalltablesin'mydatabase'withGrantSelectOnMyDatabase.to'newuser'@'localhost';。 3)GrantWriteAccessto'

mysql:字符串數據類型和coltrationsmysql:字符串數據類型和coltrationsMay 09, 2025 am 12:08 AM

MySQL中的字符串數據類型包括CHAR、VARCHAR、BINARY、VARBINARY、BLOB、TEXT,排序規則(Collations)決定了字符串的比較和排序方式。 1.CHAR適合固定長度字符串,VARCHAR適合可變長度字符串。 2.BINARY和VARBINARY用於二進制數據,BLOB和TEXT用於大對像數據。 3.排序規則如utf8mb4_unicode_ci忽略大小寫,適合用戶名;utf8mb4_bin區分大小寫,適合需要精確比較的字段。

MySQL:我應該在Varchars上使用什麼長度?MySQL:我應該在Varchars上使用什麼長度?May 09, 2025 am 12:06 AM

最佳的MySQLVARCHAR列長度選擇應基於數據分析、考慮未來增長、評估性能影響及字符集需求。 1)分析數據以確定典型長度;2)預留未來擴展空間;3)注意大長度對性能的影響;4)考慮字符集對存儲的影響。通過這些步驟,可以優化數據庫的效率和擴展性。

See all articles

熱AI工具

Undresser.AI Undress

Undresser.AI Undress

人工智慧驅動的應用程序,用於創建逼真的裸體照片

AI Clothes Remover

AI Clothes Remover

用於從照片中去除衣服的線上人工智慧工具。

Undress AI Tool

Undress AI Tool

免費脫衣圖片

Clothoff.io

Clothoff.io

AI脫衣器

Video Face Swap

Video Face Swap

使用我們完全免費的人工智慧換臉工具,輕鬆在任何影片中換臉!

熱工具

SecLists

SecLists

SecLists是最終安全測試人員的伙伴。它是一個包含各種類型清單的集合,這些清單在安全評估過程中經常使用,而且都在一個地方。 SecLists透過方便地提供安全測試人員可能需要的所有列表,幫助提高安全測試的效率和生產力。清單類型包括使用者名稱、密碼、URL、模糊測試有效載荷、敏感資料模式、Web shell等等。測試人員只需將此儲存庫拉到新的測試機上,他就可以存取所需的每種類型的清單。

PhpStorm Mac 版本

PhpStorm Mac 版本

最新(2018.2.1 )專業的PHP整合開發工具

SublimeText3 Linux新版

SublimeText3 Linux新版

SublimeText3 Linux最新版

mPDF

mPDF

mPDF是一個PHP庫,可以從UTF-8編碼的HTML產生PDF檔案。原作者Ian Back編寫mPDF以從他的網站上「即時」輸出PDF文件,並處理不同的語言。與原始腳本如HTML2FPDF相比,它的速度較慢,並且在使用Unicode字體時產生的檔案較大,但支援CSS樣式等,並進行了大量增強。支援幾乎所有語言,包括RTL(阿拉伯語和希伯來語)和CJK(中日韓)。支援嵌套的區塊級元素(如P、DIV),

SublimeText3 Mac版

SublimeText3 Mac版

神級程式碼編輯軟體(SublimeText3)