當前位置:首頁 » 編程語言 » sql查外鍵引用次數
擴展閱讀
webinf下怎麼引入js 2023-08-31 21:54:13
堡壘機怎麼打開web 2023-08-31 21:54:11

sql查外鍵引用次數

發布時間: 2023-01-23 01:40:41

sql怎樣查詢外鍵關系

看了樓主的問題補充,你可以在對象資源管理器中,選中表A,右鍵選『設計』,然後在表中選中任意鍵值,右鍵選『關系』,這個表所有的外鍵關系都會列出來了。
另外在對象資源管理器中,還有一個是資料庫關系圖,但是那個是你自己設計好的,如果之前沒有人設計這些,那麼默認是空的。

㈡ sql 通過外鍵查出每個外鍵對應數據的條數

2個表tab1,tab2,tab1的key外鍵關聯tab2的key
select a.key,b.cnt as 條數 from tab1 a
left join (select key,count(*) as cnt from tab2 group by key) b
on a.key=b.key

㈢ SQL資料庫的、外鍵和查詢

增加外鍵

創建表的時候增加外鍵:在所有的表欄位之後,使用foreign key(外鍵欄位) references 外部表(主鍵欄位)

在新增表之後增加外鍵:修改表結構,使用alter table 表名 add [constraint 外鍵名字] foreign key(外鍵欄位) references 父表(主鍵欄位);

修改外鍵&刪除外鍵

alter table 表名 drop foreign key 外鍵名;

外鍵條件

外鍵要存在,首先必須保證表的存儲引擎是innodb

列類型必須與父表的主鍵類型一致

一張表中的外鍵名字不能重復

增加外鍵的欄位數據已經存在,必須保證數據與父表主鍵要求對應

外鍵約束

有三種約束模式

district:嚴格模式(默認的)

cascade:級聯模式

set null:置空模式

語法:foreign key(外鍵欄位) references 父表(主鍵欄位) on delete 模式 on update 模式;

聯合查詢

基本語法:

select 語句1

union [union 選項]

select 語句2……

union 選項

all:保留所有,不管重復

distinct:去重,默認的

子查詢(sub query)

按位置分類

from子查詢

where子查詢

exists子查詢

按結果分類

標量子查詢

列子查詢

行子查詢

表子查詢

子查詢

列子查詢

=any等價於in; -- 其中一個即可

any等價於some; -- 二者是一樣的

=all為全部

-- 創建外鍵

create table my_foreign1(

idint primary key auto_increment,

name varchar (20)not null comment

'學生姓名',

c_idint comment'班級id',

-- 增加外鍵

foreign key(c_id)references

my_class(id)

)charset utf8;

-- 創建表

create table my_foreign2(

idint primary key auto_increment,

name varchar (20)not null comment

'學生姓名',

c_idint comment'班級id'  -- 普通欄位

)charset utf8;

-- 增加外鍵

alter table my_foreign2add

-- 指定外鍵的名字

constraint student_class_1  -- 可以指定多個外鍵 但是名字不能相同

-- 指定外鍵的欄位

foreign key(c_id)

-- 引用父表主鍵

references my_class(id);

-- 刪除外鍵

alter table my_foreign1drop

foreign key my_foreign1_ibfk_1;  -- my_foreign1_ibfk_1 通過外鍵的名字來刪

-- 插入數據;外鍵欄位在父表不存在

insert into my_foreign2values (

null,'郭富城',4);  -- 沒有4號班級

insert  into my_foreign2values (

null,'項羽',1);

insert  into my_foreign2values (

null,'劉邦',2);

insert  into my_foreign2values (

null,'韓信',3);

-- 更新父表的記錄

update my_classset id=4 where id=1;  -- 失敗;id=1記錄已經被學生引用

update my_foreign2set c_id=2 where id=4;  -- 更新

update my_classset id=4 where id=3;  -- 可以;沒有學生引用此班級

-- mysql中添加外鍵約束遇到一下情況:

-- cannot add foreign key constraint

-- 出現這個問題的原因是,外鍵的使用:

-- 1. 外鍵欄位不能為該表的主鍵;

-- 2. 外鍵欄位參考欄位必須為參考表的主鍵

-- 插入數據

insert into my_foreign1values (

null,'馬超','3'

);

-- 增加外鍵

alter table my_foreign1add

foreign key(c_id)references

my_class(id);  -- 失敗;因為沒有3號班了

-- 創建外鍵,指定模式;刪除置空;更新級聯

create table my_foreign3(

idint primary key auto_increment,

name varchar (20)not null,

c_idint,

-- 增加外鍵

foreign key (c_id)

-- 引用表

references my_class(id)

-- 指定刪除模式

on delete set null

-- 指定更新模式

on update cascade

)charset utf8;

-- 插入數據

insert into my_foreign3values (

null,'劉備',1),

(null,'曹操',1),

(null,'孫權',1),

(null,'祝賀量',2),

(null,'周瑜',2);

-- 解除My_foreign2表的外鍵

alter table my_foreign2drop

foreign key student_class_1;

-- 更新父表主鍵

update my_classset id=3 where id=1;

-- 刪除父表主鍵

delete from  my_classwhere id=2;

-- 聯合查詢

select * from my_class

union  -- 默認去重

select * from my_class;

select * from my_class

union all  -- 不去重

select * from my_class;

select id,c_name,roomfrom my_class

union all  -- 不去重

select name,number,idfrom my_student;

-- 需求;男生升序;女生降序(年齡)

(select * from my_student

where sex='男'

order by ageasc limit9999999)

union

(select * from my_student

where sex='女'

order by agedesc limit9999999);

select * from my_studentwhere

c_id=(

-- 標量子查詢

select idfrom my_classwhere

c_name='python1903');-- id一定只有一個值(一行一列)

insert into my_classvalues (1,

'python1907','B407');

-- 列子查詢

select * from my_studentwhere

c_idin(select idfrom my_class);

-- any,some,all

select * from my_studentwhere

c_id=any(select idfrom my_class);

select * from my_studentwhere

c_id=some(select idfrom my_class);

select * from my_studentwhere

c_id=all(select idfrom my_class);

select * from my_studentwhere

c_id!=any(select idfrom my_class);  -- 所有結果(null除外)

select * from my_studentwhere

c_id!=some(select idfrom my_class);  -- 所有結果(null除外)

select * from my_studentwhere

c_id!=all(select idfrom my_class);  -- 所有2號班級(null除外)

select * from my_studentwhere

age=(select max(age)from

my_student)

and

height=(select max(height))from

my_student);

-- 行子查詢

select * from my_student

-- (age,height)稱之內為行元素

where (age,height)=(select max(

age),max(height)from my_student);

update my_studentset height=188

where name='王五';

select * from my_studentorder by

agedesc,heightdesc limit1;

select * from my_studentorder by

heightdesc;

-- 表子查詢

select * from my_studentgroup by

c_idorder by heightdesc;  -- 每個班選出第一個學生再按身高排序

select * from (select * from

my_studentorder by heightdesc)

as studentgroup by student.c_id;

㈣ 關於sql主鍵外鍵查詢

selectt1.agefromt1,t2
wheret1.name=t2.name
andt2.point='xxx';

㈤ 怎樣通過sql語句查看外鍵 sql server2000

select INDEX_NAME 索引名, b.TABLE_NAME 主鍵表名, a.TABLE_NAME 外鍵表名, CONSTRAINT_TYPE, CONSTRAINT_NAME 約束名
from all_indexes a, all_constraints b
where b.TABLE_NAME='AC' AND CONSTRAINT_TYPE IN('P','R')
and R_CONSTRAINT_NAME=INDEX_NAME(+)
/
CONSTRAINT_TYPE='P'為主鍵,='R'為外鍵

㈥ mysql如何查看外鍵

查看mysql外鍵方式主要是通過第三方工具或者是sql語句,主要有以下三種方式

1、使用Navicateformysql,打開資料庫、查看資料庫表、查看設計表、選擇外鍵選項卡,就可以查看外鍵

2、使用sql語句

showcreatetable表名;這個命令可以查看錶的所有信息,包括一些欄位類型,欄位的約束,外鍵,主鍵,索引,字元編碼等等。

3、查看某個表或者某個列的外鍵信息

selectTABLE_NAME,COLUMN_NAME,CONSTRAINT_NAME,

REFERENCED_TABLE_NAME,REFERENCED_COLUMN_NAME from KEY_COLUMN_USAGE where REFERENCED_TABLE_NAME = '<table>';

如果需要查看某一列上的外鍵關系,需要添加列的條件REFERENCED_COLUMN_NAME.xx=xx

方法一比較直觀,方法三比較准確!

(6)sql查外鍵引用次數擴展閱讀:


MySQL是一種開放源代碼的關系型資料庫管理系統(RDBMS),MySQL資料庫系統使用最常用的資料庫管理語言--結構化查詢語言(SQL)進行資料庫管理。

由於MySQL是開放源代碼的,因此任何人都可以在GeneralPublicLicense的許可下下載並根據個性化的需要對其進行修改。MySQL因為其速度、可靠性和適應性而備受關注。大多數人都認為在不需要事務化處理的情況下,MySQL是管理內容最好的選擇。

㈦ Sql server2005查詢主表所有名稱,以及與主表每條記錄相關的外鍵記錄條數

SELECT
主表.所有名稱,
COUNT(子表.主鍵) AS 與主表每條記錄相關的外鍵記錄條數
FROM
主表 LEFT OUTER JOIN 子表 ON (主表.主鍵 = 子表.外鍵)
GROUP BY
主表.所有名稱

㈧ SQL找出一個外鍵所被引用過的表


select
oSub.nameAS[子表名稱],
fk.nameAS[外鍵名稱],
SubCol.nameAS[子表列名],
oMain.nameAS[主表名稱],
MainCol.nameAS[主表列名]
from
sys.foreign_keysfk
JOINsys.all_objectsoSub
ON(fk.parent_object_id=oSub.object_id)
JOINsys.all_objectsoMain
ON(fk.referenced_object_id=oMain.object_id)
JOINsys.foreign_key_columnsfkCols
ON(fk.object_id=fkCols.constraint_object_id)
JOINsys.columnsSubCol
ON(oSub.object_id=SubCol.object_id
ANDfkCols.parent_column_id=SubCol.column_id)
JOINsys.columnsMainCol
ON(oMain.object_id=MainCol.object_id
ANDfkCols.referenced_column_id=MainCol.column_id)



自己在最後 加一個 WHERE fk.name = 'Plan01_FK'


註: 上面的 SQL Server 2008 下的 sql 語句。

㈨ SQL中,一個表有多個外鍵,如何查詢外鍵對應的值,和如何進行插入

這個就是典型的插入異常..
以為你插入的這個表和其他表有外鍵聯系..插入的時候其他表中沒有這個外鍵數據所以就會報錯..

最好是在程序裡面先插入外鍵說需要的數據再插入這條數據,這樣就不會出錯..如果不想這么麻煩就去除外鍵約束,但是這樣雖然可以插入成功但是以後會出現很多孤兒數據..

查詢沒什麼問題...如果你想插入一條A表的數據,那麼就必須保證A表的j,k,i欄位在響應的B,C,D表有數據..不然就會報以上錯誤..具體程序可以先判斷你要插入的這條數據中的j,k,i在B,C,D存在不..不存在提示用戶,存在這插入..

㈩ SQL怎麼查詢外鍵引用次數

1、select A.*,B.AidNum from A,(select Aid,count(Aid) as AidNum from B group by Aid) as B
where A.Aid=B.Aid
2、select A.*,C.CountNum from A,
(select B.Aid,count(B.Aid) as CountNum from B,C where B.Bid=C.Bid group by B.Aid) as C

where A.Aid=C.Aid