『壹』 能否用實例說明sql語言中主鍵約束和外鍵約束
說白了主鍵就是為了不讓數據重復,你要表要是不允許重復你就得設置主鍵,主鍵是唯一的,本表外鍵是別的表中的主鍵,是為了和那個表取得聯系和約束。 確保外鍵數據一定要在別的表中主鍵出現才可以。
『貳』 SQL資料庫主鍵與外建的約束是怎麼保證數據的完整性的 外鍵為什麼可以重復空值怎麼和主鍵建立聯系
主鍵: 主鍵唯一,以此保證數據完整性
外鍵:子表數據存在的前提是主表中有相同鍵值的數據存在,刪除時,先刪除子表的相同鍵數據,再刪除主表的相同鍵數據。以此保證數據完整性。主表對子表是1對多關系,因此是外鍵可以重復。
空值不能作為主鍵。
『叄』 用sql語句進行多表連接查詢出現重復數據
1、在電腦上打開要去掉重復數據的資料庫,這里新建一張含有重復數據的user表。
『肆』 在sql中請舉例說明主鍵,和外鍵詳細舉例,快,好,採納!!!
例子:
CREATE TABLE procts (
prod_id NUMBER(3) CONSTRAINT p_ck CHECK (prod_id > 0),
prod_name CHAR(30),
prod_qty NUMBER(6),
CONSTRAINT p_name UNIQUE NOT NULL,
CONSTRAINT prod_pk PRIMARY KEY (prod_id));
CREATE TABLE warehouse (
warehouse_id NUMBER(4),
roomno NUMBER(10) CONSTRAINT r_id CHECK(roomno BETWEEN 101 AND 200),
location VARCHAR2(25),
prod_id NUMBER(3),
CONSTRAINT wr_pr_pk PRIMARY KEY (warehouse_id,prod_id),
CONSTRAINT prod_fk FOREIGN KEY (prod_id) REFERENCES procts(prod_id));
例如這兩個表格,prod_id是procts的主鍵,也是warehouse這個表格的外鍵,主外鍵之間的關聯語法就是上述兩個表格的創建語句。
這樣這兩個表格就互相關聯了,當然主鍵中不能有重復語句和null值,並且每一個表格只可以有一個主鍵,但可以有多個外鍵,希望採納
(點右下角的展開,格式會好很多)
『伍』 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;