#查看數(shù)據(jù)庫(kù)所有表

創(chuàng)新互聯(lián)公司是一家專(zhuān)業(yè)提供寧海企業(yè)網(wǎng)站建設(shè),專(zhuān)注與網(wǎng)站建設(shè)、網(wǎng)站設(shè)計(jì)、H5響應(yīng)式網(wǎng)站、小程序制作等業(yè)務(wù)。10年已為寧海眾多企業(yè)、政府機(jī)構(gòu)等服務(wù)。創(chuàng)新互聯(lián)專(zhuān)業(yè)網(wǎng)站設(shè)計(jì)公司優(yōu)惠進(jìn)行中。
SELECT tba.TABLE_NAME FROM information_schema.TABLES tba WHERE tba.TABLE_SCHEMA= '你要查的數(shù)據(jù)庫(kù)名字'
#查看某個(gè)庫(kù)中的一個(gè)表是哪些表的外鍵
SELECT TABLE_NAME FROM KEY_COLUMN_USAGE WHERE CONSTRAINT_NAME='FK_PRODUCT_ID' AND REFERENCED_TABLE_NAME ='表的名字'AND REFERENCED_TABLE_SCHEMA='表的的數(shù)據(jù)名字'
求采納良心sql啊
information_schema數(shù)據(jù)庫(kù)又稱(chēng)為信息架構(gòu),數(shù)據(jù)表保存了MySQL服務(wù)器所有數(shù)據(jù)庫(kù)的信息。如數(shù)據(jù)庫(kù)名,數(shù)據(jù)庫(kù)的表,表欄的數(shù)據(jù)類(lèi)型與訪問(wèn)權(quán)限等。
performance_schema數(shù)據(jù)庫(kù)主要用于收集數(shù)據(jù)庫(kù)服務(wù)器性能參數(shù),以便優(yōu)化mysql數(shù)據(jù)庫(kù)性能。
mysql數(shù)據(jù)庫(kù)是存儲(chǔ)著已MySQL運(yùn)行相關(guān)的基本信息等數(shù)據(jù)管理的數(shù)據(jù)庫(kù)。
sys 數(shù)據(jù)庫(kù)是mysql5.7增加的,通過(guò)這個(gè)庫(kù)可以快速的了解系統(tǒng)的元數(shù)據(jù)信息
增加外鍵
創(chuàng)建表的時(shí)候增加外鍵:在所有的表字段之后,使用foreign key(外鍵字段) references 外部表(主鍵字段)
在新增表之后增加外鍵:修改表結(jié)構(gòu),使用alter table 表名 add [constraint 外鍵名字] foreign key(外鍵字段) references 父表(主鍵字段);
修改外鍵刪除外鍵
alter table 表名 drop foreign key 外鍵名;
外鍵條件
外鍵要存在,首先必須保證表的存儲(chǔ)引擎是innodb
列類(lèi)型必須與父表的主鍵類(lèi)型一致
一張表中的外鍵名字不能重復(fù)
增加外鍵的字段數(shù)據(jù)已經(jīng)存在,必須保證數(shù)據(jù)與父表主鍵要求對(duì)應(yīng)
外鍵約束
有三種約束模式
district:嚴(yán)格模式(默認(rèn)的)
cascade:級(jí)聯(lián)模式
set null:置空模式
語(yǔ)法:foreign key(外鍵字段) references 父表(主鍵字段) on delete 模式 on update 模式;
聯(lián)合查詢(xún)
基本語(yǔ)法:
select 語(yǔ)句1
union [union 選項(xiàng)]
select 語(yǔ)句2……
union 選項(xiàng)
all:保留所有,不管重復(fù)
distinct:去重,默認(rèn)的
子查詢(xún)(sub query)
按位置分類(lèi)
from子查詢(xún)
where子查詢(xún)
exists子查詢(xún)
按結(jié)果分類(lèi)
標(biāo)量子查詢(xún)
列子查詢(xún)
行子查詢(xún)
表子查詢(xún)
子查詢(xún)
列子查詢(xún)
=any等價(jià)于in; -- 其中一個(gè)即可
any等價(jià)于some; -- 二者是一樣的
=all為全部
-- 創(chuàng)建外鍵
create table my_foreign1(
idint primary key auto_increment,
name varchar (20)not null comment
'學(xué)生姓名',
c_idint comment'班級(jí)id',
-- 增加外鍵
foreign key(c_id)references
my_class(id)
)charset utf8;
-- 創(chuàng)建表
create table my_foreign2(
idint primary key auto_increment,
name varchar (20)not null comment
'學(xué)生姓名',
c_idint comment'班級(jí)id'? -- 普通字段
)charset utf8;
-- 增加外鍵
alter table my_foreign2add
-- 指定外鍵的名字
constraint student_class_1? -- 可以指定多個(gè)外鍵 但是名字不能相同
-- 指定外鍵的字段
foreign key(c_id)
-- 引用父表主鍵
references my_class(id);
-- 刪除外鍵
alter table my_foreign1drop
foreign key my_foreign1_ibfk_1;? -- my_foreign1_ibfk_1 通過(guò)外鍵的名字來(lái)刪
-- 插入數(shù)據(jù);外鍵字段在父表不存在
insert into my_foreign2values (
null,'郭富城',4);? -- 沒(méi)有4號(hào)班級(jí)
insert? into my_foreign2values (
null,'項(xiàng)羽',1);
insert? into my_foreign2values (
null,'劉邦',2);
insert? into my_foreign2values (
null,'韓信',3);
-- 更新父表的記錄
update my_classset id=4 where id=1;? -- 失敗;id=1記錄已經(jīng)被學(xué)生引用
update my_foreign2set c_id=2 where id=4;? -- 更新
update my_classset id=4 where id=3;? -- 可以;沒(méi)有學(xué)生引用此班級(jí)
-- mysql中添加外鍵約束遇到一下情況:
-- cannot add foreign key constraint
-- 出現(xiàn)這個(gè)問(wèn)題的原因是,外鍵的使用:
-- 1. 外鍵字段不能為該表的主鍵;
-- 2. 外鍵字段參考字段必須為參考表的主鍵
-- 插入數(shù)據(jù)
insert into my_foreign1values (
null,'馬超','3'
);
-- 增加外鍵
alter table my_foreign1add
foreign key(c_id)references
my_class(id);? -- 失敗;因?yàn)闆](méi)有3號(hào)班了
-- 創(chuàng)建外鍵,指定模式;刪除置空;更新級(jí)聯(lián)
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;
-- 插入數(shù)據(jù)
insert into my_foreign3values (
null,'劉備',1),
(null,'曹操',1),
(null,'孫權(quán)',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;
-- 聯(lián)合查詢(xún)
select * from my_class
union? -- 默認(rèn)去重
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=(
-- 標(biāo)量子查詢(xún)
select idfrom my_classwhere
c_name='python1903');-- id一定只有一個(gè)值(一行一列)
insert into my_classvalues (1,
'python1907','B407');
-- 列子查詢(xún)
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);? -- 所有結(jié)果(null除外)
select * from my_studentwhere
c_id!=some(select idfrom my_class);? -- 所有結(jié)果(null除外)
select * from my_studentwhere
c_id!=all(select idfrom my_class);? -- 所有2號(hào)班級(jí)(null除外)
select * from my_studentwhere
age=(select max(age)from
my_student)
and
height=(select max(height))from
my_student);
-- 行子查詢(xún)
select * from my_student
-- (age,height)稱(chēng)之內(nèi)為行元素
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;
-- 表子查詢(xún)
select * from my_studentgroup by
c_idorder by heightdesc;? -- 每個(gè)班選出第一個(gè)學(xué)生再按身高排序
select * from (select * from
my_studentorder by heightdesc)
as studentgroup by student.c_id;
定義外鍵的方法和詳細(xì)的操作步驟如下:
1、第一步,創(chuàng)建一個(gè)主從表,如下圖所示,然后進(jìn)入下一步。
2、其次,完成上述步驟后,選擇主表,然后單擊設(shè)計(jì)表進(jìn)入表設(shè)計(jì)界面,如下圖所示,然后進(jìn)入下一步。
3、接著,完成上述步驟后,單擊外鍵進(jìn)入外鍵的設(shè)置界面,如下圖所示,然后進(jìn)入下一步。
4、然后,完成上述步驟后,設(shè)置外鍵名稱(chēng),然后選擇主表的外鍵字段,如下圖所示,然后進(jìn)入下一步。
5、隨后,完成上述步驟后,設(shè)置與數(shù)據(jù)庫(kù),表名稱(chēng)和從屬表的單詞相對(duì)應(yīng)的外鍵字段,如下圖所示,然后進(jìn)入下一步。
6、最后,完成上述步驟后,單擊保存即可,如下圖所示。這樣,問(wèn)題就解決了。
當(dāng)前標(biāo)題:mysql怎么看外鍵數(shù)據(jù),數(shù)據(jù)庫(kù)怎么查看外鍵
文章源于:http://chinadenli.net/article22/hsgccc.html
成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供App設(shè)計(jì)、虛擬主機(jī)、網(wǎng)站導(dǎo)航、網(wǎng)站建設(shè)、動(dòng)態(tài)網(wǎng)站、全網(wǎng)營(yíng)銷(xiāo)推廣
聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶(hù)投稿、用戶(hù)轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請(qǐng)盡快告知,我們將會(huì)在第一時(shí)間刪除。文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如需處理請(qǐng)聯(lián)系客服。電話(huà):028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時(shí)需注明來(lái)源: 創(chuàng)新互聯(lián)