增加外鍵
公司主營業務:做網站、成都做網站、移動網站開發等業務。幫助企業客戶真正實現互聯網宣傳,提高企業的競爭能力。創新互聯是一支青春激揚、勤奮敬業、活力青春激揚、勤奮敬業、活力澎湃、和諧高效的團隊。公司秉承以“開放、自由、嚴謹、自律”為核心的企業文化,感謝他們對我們的高要求,感謝他們從不同領域給我們帶來的挑戰,讓我們激情的團隊有機會用頭腦與智慧不斷的給客戶帶來驚喜。創新互聯推出伊春免費做網站回饋大家。
創建表的時候增加外鍵:在所有的表字段之后,使用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;
select * from information_schema.TABLE_CONSTRAINTS t where t.TABLE_NAME='itcast_student' and CONSTRAINT_TYPE='FOREIGN KEY';
不加type的話,就是所有的約束了,你知道的,還有可以用圖形界面的工具啊操作很方便,不用寫sql就可以刪除,比如:navicat for mysql,mysql workbench 都是很好用的管理工具!
工具/原料
電腦??MySQL
方法/步驟
設置主鍵:
1、通過終端進入到mysql命令行工具。
2、通過use關鍵字進行到目標數據庫里。
3、如原表已有主鍵,先把原來的主鍵刪除掉,通過DROPPRIMARYKEY命令:ALTERTABLE`jingyan`DROPPRIMARYKEY;。
4、主鍵已經沒有了。
5、通過命令:ADDPRIMARYKEY來添加ALTERTABLE`jingyan`ADDPRIMARYKEY(`id`)。
6、輸入后按下回車鍵即可看到queryok執行成功的字符。
7、回到數據庫的可視化工具,即可顯示現在的表在id列上添加了主鍵了。
設置外鍵:
1、創建好主從表。
2、選擇主表,點擊設計表,進入到表設計界面。
3、點擊外鍵,進入到外鍵設置界面。
4、先設置外鍵名稱和選擇主表的外鍵字段。
5、然后在設置外鍵字段對應從表的數據庫、表名和字。
6、點擊保存就完成外鍵設置了。
SELECT CONSTRAINT_CATALOG,
CONSTRAINT_SCHEMA,
CONSTRAINT_NAME,
TABLE_SCHEMA,
TABLE_NAME,
CONSTRAINT_TYPE
FROM
information_schema.TABLE_CONSTRAINTS
WHERE
TABLE_NAME='表名'
表名替換成你要看的表
#查看數據庫所有表
SELECT tba.TABLE_NAME FROM information_schema.TABLES tba WHERE tba.TABLE_SCHEMA= '你要查的數據庫名字'
#查看某個庫中的一個表是哪些表的外鍵
SELECT TABLE_NAME FROM KEY_COLUMN_USAGE WHERE CONSTRAINT_NAME='FK_PRODUCT_ID' AND REFERENCED_TABLE_NAME ='表的名字'AND REFERENCED_TABLE_SCHEMA='表的的數據名字'
求采納良心sql啊
information_schema數據庫又稱為信息架構,數據表保存了MySQL服務器所有數據庫的信息。如數據庫名,數據庫的表,表欄的數據類型與訪問權限等。
performance_schema數據庫主要用于收集數據庫服務器性能參數,以便優化mysql數據庫性能。
mysql數據庫是存儲著已MySQL運行相關的基本信息等數據管理的數據庫。
sys 數據庫是mysql5.7增加的,通過這個庫可以快速的了解系統的元數據信息
網頁題目:mysql怎么查看外鍵名 mysql 查看外鍵
分享路徑:http://m.kartarina.com/article20/hjgdjo.html
成都網站建設公司_創新互聯,為您提供網站改版、面包屑導航、手機網站建設、網站營銷、營銷型網站建設、云服務器
聲明:本網站發布的內容(圖片、視頻和文字)以用戶投稿、用戶轉載內容為主,如果涉及侵權請盡快告知,我們將會在第一時間刪除。文章觀點不代表本網站立場,如需處理請聯系客服。電話:028-86922220;郵箱:631063699@qq.com。內容未經允許不得轉載,或轉載時需注明來源: 創新互聯